Skip to content

Commit

Permalink
feat(core): Pass RequestContext to OrderMergeStrategy functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The OrderMergeStrategy `merge()`
function signature has changed: the first argument is now the
RequestContext of the current request.
  • Loading branch information
michaelbromley committed Oct 27, 2020
1 parent a4d4311 commit eae71f0
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 39 deletions.
14 changes: 8 additions & 6 deletions packages/core/src/config/order/merge-orders-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { RequestContext } from '../../api/common/request-context';
import { Order } from '../../entity/order/order.entity';
import { createOrderFromLines, parseLines } from '../../testing/order-test-utils';

import { MergeOrdersStrategy } from './merge-orders-strategy';

describe('MergeOrdersStrategy', () => {
const strategy = new MergeOrdersStrategy();
const ctx = RequestContext.empty();

it('both orders empty', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).toEqual([]);
});
Expand All @@ -20,7 +22,7 @@ describe('MergeOrdersStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(guestLines);
});
Expand All @@ -30,7 +32,7 @@ describe('MergeOrdersStrategy', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -48,7 +50,7 @@ describe('MergeOrdersStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual([
{ lineId: 21, quantity: 2, productVariantId: 201 },
Expand All @@ -72,7 +74,7 @@ describe('MergeOrdersStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual([
{ lineId: 22, quantity: 1, productVariantId: 202 },
Expand All @@ -88,7 +90,7 @@ describe('MergeOrdersStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).not.toBe(guestOrder.lines);
expect(result).not.toBe(existingOrder.lines);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/config/order/merge-orders-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { OrderLine } from '../../entity/order-line/order-line.entity';
import { Order } from '../../entity/order/order.entity';

Expand All @@ -12,7 +13,7 @@ import { OrderMergeStrategy } from './order-merge-strategy';
* @docsPage Merge Strategies
*/
export class MergeOrdersStrategy implements OrderMergeStrategy {
merge(guestOrder: Order, existingOrder: Order): OrderLine[] {
merge(ctx: RequestContext, guestOrder: Order, existingOrder: Order): OrderLine[] {
const mergedLines = existingOrder.lines.slice();
const guestLines = guestOrder.lines.slice();
for (const guestLine of guestLines.reverse()) {
Expand All @@ -25,6 +26,6 @@ export class MergeOrdersStrategy implements OrderMergeStrategy {
}

private findCorrespondingLine(existingOrder: Order, guestLine: OrderLine): OrderLine | undefined {
return existingOrder.lines.find((line) => line.productVariant.id === guestLine.productVariant.id);
return existingOrder.lines.find(line => line.productVariant.id === guestLine.productVariant.id);
}
}
3 changes: 2 additions & 1 deletion packages/core/src/config/order/order-merge-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { InjectableStrategy } from '../../common/types/injectable-strategy';
import { OrderLine } from '../../entity/order-line/order-line.entity';
import { Order } from '../../entity/order/order.entity';
Expand All @@ -18,5 +19,5 @@ export interface OrderMergeStrategy extends InjectableStrategy {
* Merges the lines of the guest Order with those of the existing Order which is associated
* with the active customer.
*/
merge(guestOrder: Order, existingOrder: Order): OrderLine[];
merge(ctx: RequestContext, guestOrder: Order, existingOrder: Order): OrderLine[];
}
14 changes: 8 additions & 6 deletions packages/core/src/config/order/use-existing-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { RequestContext } from '../../api/common/request-context';
import { Order } from '../../entity/order/order.entity';
import { createOrderFromLines, parseLines } from '../../testing/order-test-utils';

import { UseExistingStrategy } from './use-existing-strategy';

describe('UseExistingStrategy', () => {
const strategy = new UseExistingStrategy();
const ctx = RequestContext.empty();

it('both orders empty', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).toEqual([]);
});
Expand All @@ -20,7 +22,7 @@ describe('UseExistingStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual([]);
});
Expand All @@ -30,7 +32,7 @@ describe('UseExistingStrategy', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -48,7 +50,7 @@ describe('UseExistingStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -66,7 +68,7 @@ describe('UseExistingStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -77,7 +79,7 @@ describe('UseExistingStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).not.toBe(guestOrder.lines);
expect(result).not.toBe(existingOrder.lines);
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/config/order/use-existing-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { OrderLine } from '../../entity/order-line/order-line.entity';
import { Order } from '../../entity/order/order.entity';

Expand All @@ -11,7 +12,7 @@ import { OrderMergeStrategy } from './order-merge-strategy';
* @docsPage Merge Strategies
*/
export class UseExistingStrategy implements OrderMergeStrategy {
merge(guestOrder: Order, existingOrder: Order): OrderLine[] {
merge(ctx: RequestContext, guestOrder: Order, existingOrder: Order): OrderLine[] {
return existingOrder.lines.slice();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { RequestContext } from '../../api/common/request-context';
import { Order } from '../../entity/order/order.entity';
import { createOrderFromLines, parseLines } from '../../testing/order-test-utils';

import { UseGuestIfExistingEmptyStrategy } from './use-guest-if-existing-empty-strategy';

describe('UseGuestIfExistingEmptyStrategy', () => {
const strategy = new UseGuestIfExistingEmptyStrategy();
const ctx = RequestContext.empty();

it('both orders empty', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).toEqual([]);
});
Expand All @@ -20,7 +22,7 @@ describe('UseGuestIfExistingEmptyStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(guestLines);
});
Expand All @@ -30,7 +32,7 @@ describe('UseGuestIfExistingEmptyStrategy', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -48,7 +50,7 @@ describe('UseGuestIfExistingEmptyStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -66,7 +68,7 @@ describe('UseGuestIfExistingEmptyStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(existingLines);
});
Expand All @@ -77,7 +79,7 @@ describe('UseGuestIfExistingEmptyStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).not.toBe(guestOrder.lines);
expect(result).not.toBe(existingOrder.lines);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { OrderLine } from '../../entity/order-line/order-line.entity';
import { Order } from '../../entity/order/order.entity';

Expand All @@ -11,7 +12,7 @@ import { OrderMergeStrategy } from './order-merge-strategy';
* @docsPage Merge Strategies
*/
export class UseGuestIfExistingEmptyStrategy implements OrderMergeStrategy {
merge(guestOrder: Order, existingOrder: Order): OrderLine[] {
merge(ctx: RequestContext, guestOrder: Order, existingOrder: Order): OrderLine[] {
return existingOrder.lines.length ? existingOrder.lines.slice() : guestOrder.lines.slice();
}
}
14 changes: 8 additions & 6 deletions packages/core/src/config/order/use-guest-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { RequestContext } from '../../api/common/request-context';
import { Order } from '../../entity/order/order.entity';
import { createOrderFromLines, parseLines } from '../../testing/order-test-utils';

import { UseGuestStrategy } from './use-guest-strategy';

describe('UseGuestStrategy', () => {
const strategy = new UseGuestStrategy();
const ctx = RequestContext.empty();

it('both orders empty', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).toEqual([]);
});
Expand All @@ -20,7 +22,7 @@ describe('UseGuestStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = new Order({ lines: [] });

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(guestLines);
});
Expand All @@ -30,7 +32,7 @@ describe('UseGuestStrategy', () => {
const guestOrder = new Order({ lines: [] });
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual([]);
});
Expand All @@ -48,7 +50,7 @@ describe('UseGuestStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(guestLines);
});
Expand All @@ -66,7 +68,7 @@ describe('UseGuestStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(parseLines(result)).toEqual(guestLines);
});
Expand All @@ -77,7 +79,7 @@ describe('UseGuestStrategy', () => {
const guestOrder = createOrderFromLines(guestLines);
const existingOrder = createOrderFromLines(existingLines);

const result = strategy.merge(guestOrder, existingOrder);
const result = strategy.merge(ctx, guestOrder, existingOrder);

expect(result).not.toBe(guestOrder.lines);
expect(result).not.toBe(existingOrder.lines);
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/config/order/use-guest-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { OrderLine } from '../../entity/order-line/order-line.entity';
import { Order } from '../../entity/order/order.entity';

Expand All @@ -11,7 +12,7 @@ import { OrderMergeStrategy } from './order-merge-strategy';
* @docsPage Merge Strategies
*/
export class UseGuestStrategy implements OrderMergeStrategy {
merge(guestOrder: Order, existingOrder: Order): OrderLine[] {
merge(ctx: RequestContext, guestOrder: Order, existingOrder: Order): OrderLine[] {
return guestOrder.lines.slice();
}
}
Loading

0 comments on commit eae71f0

Please sign in to comment.