|
| 1 | +--- |
| 2 | +title: "RescaleOrderLinePromotionAdjustments" |
| 3 | +generated: true |
| 4 | +--- |
| 5 | +<GenerationInfo sourceFile="packages/core/src/migration-utils/v3_8_orderline_promotion_rescale.ts" sourceLine="156" packageName="@vendure/core" since="3.8.0" /> |
| 6 | + |
| 7 | +Rescales `PROMOTION`-type `Adjustment`s on `order_line` rows that were partially |
| 8 | +cancelled by a pre-#5127 `cancelOrderByOrderLines()`, which reduced `quantity` |
| 9 | +without rescaling `adjustments`. Reading those rows with the fixed `OrderLine` |
| 10 | +getters (which divide a `PROMOTION` adjustment by the *current* `quantity`, |
| 11 | +instead of `orderPlacedQuantity`) would otherwise inflate their discount. |
| 12 | + |
| 13 | +`OrderModifier.modifyOrder()` ends in `applyPriceAdjustments()`, which recomputes the |
| 14 | +`PROMOTION` amounts for every line in the order, including lines not referenced by the |
| 15 | +`OrderModification`. For a subsequently-cancelled line, the stored basis is its current |
| 16 | +quantity plus the quantities in order-cancellation history after the order's latest |
| 17 | +`OrderModification`. |
| 18 | + |
| 19 | +Some databases can store a modification and cancellation with the same timestamp. Their |
| 20 | +order is then unknowable from persisted data, so the helper throws before writing anything. |
| 21 | +Inspect those lines and pass `ambiguousOrderLineQuantityBases`: use the current quantity if |
| 22 | +the modification happened last, or the quantity before cancellation if cancellation happened |
| 23 | +last. |
| 24 | + |
| 25 | +Call this from your migration's `up()` method - it needs no schema change, so it |
| 26 | +can run at any point in the migration. |
| 27 | + |
| 28 | +```ts |
| 29 | +import { MigrationInterface, QueryRunner } from 'typeorm'; |
| 30 | +import { rescaleOrderLinePromotionAdjustments } from '@vendure/core'; |
| 31 | + |
| 32 | +export class RescaleOrderLinePromotionAdjustments1234567890 implements MigrationInterface { |
| 33 | + public async up(queryRunner: QueryRunner): Promise<any> { |
| 34 | + await rescaleOrderLinePromotionAdjustments(queryRunner); |
| 35 | + } |
| 36 | + |
| 37 | + public async down(queryRunner: QueryRunner): Promise<any> { |
| 38 | + // This is a one-way data migration - the pre-migration amounts are not recoverable. |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```ts title="Signature" |
| 44 | +function rescaleOrderLinePromotionAdjustments(queryRunner: QueryRunner, options: RescaleOrderLinePromotionAdjustmentsOptions = {}): Promise<void> |
| 45 | +``` |
| 46 | +Parameters |
| 47 | + |
| 48 | +### queryRunner |
| 49 | + |
| 50 | +<MemberInfo kind="parameter" type={`QueryRunner`} /> |
| 51 | + |
| 52 | +### options |
| 53 | + |
| 54 | +<MemberInfo kind="parameter" type={`RescaleOrderLinePromotionAdjustmentsOptions`} /> |
| 55 | + |
0 commit comments