From 8232ddce0b03f8f1b30e8d02563d5fe2d5f5ebe8 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 15 Sep 2020 16:39:30 +0200 Subject: [PATCH] feat(core): Use transaction to update Fulfillment state --- .../src/api/resolvers/admin/order.resolver.ts | 1 + .../service/services/fulfillment.service.ts | 2 +- .../src/service/services/order.service.ts | 23 ++++++++++++------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/core/src/api/resolvers/admin/order.resolver.ts b/packages/core/src/api/resolvers/admin/order.resolver.ts index 91fa0af77f..379b87fd79 100644 --- a/packages/core/src/api/resolvers/admin/order.resolver.ts +++ b/packages/core/src/api/resolvers/admin/order.resolver.ts @@ -116,6 +116,7 @@ export class OrderResolver { return this.orderService.transitionToState(ctx, args.id, args.state as OrderState); } + @Transaction() @Mutation() @Allow(Permission.UpdateOrder) async transitionFulfillmentToState( diff --git a/packages/core/src/service/services/fulfillment.service.ts b/packages/core/src/service/services/fulfillment.service.ts index 4ca352c408..22af991807 100644 --- a/packages/core/src/service/services/fulfillment.service.ts +++ b/packages/core/src/service/services/fulfillment.service.ts @@ -62,7 +62,7 @@ export class FulfillmentService { const orders = ordersInOrderItems.filter((v, i, a) => a.findIndex(t => t.id === v.id) === i); const fromState = fulfillment.state; await this.fulfillmentStateMachine.transition(ctx, fulfillment, orders, state); - await this.connection.getRepository(Fulfillment).save(fulfillment, { reload: false }); + await this.connection.getRepository(ctx, Fulfillment).save(fulfillment, { reload: false }); this.eventBus.publish(new FulfillmentStateTransitionEvent(fromState, state, ctx, fulfillment)); return { fulfillment, orders, fromState, toState: state }; diff --git a/packages/core/src/service/services/order.service.ts b/packages/core/src/service/services/order.service.ts index 19ea68d994..2b07a51a89 100644 --- a/packages/core/src/service/services/order.service.ts +++ b/packages/core/src/service/services/order.service.ts @@ -465,6 +465,7 @@ export class OrderService { this.eventBus.publish(new OrderStateTransitionEvent(fromState, state, ctx, order)); return order; } + async transitionFulfillmentToState( ctx: RequestContext, fulfillmentId: ID, @@ -476,30 +477,36 @@ export class OrderService { state, ); await Promise.all( - orders.map(order => this.handleFulfillmentStateTransitByOrder(ctx, order.id, fromState, toState)), + orders.map(order => this.handleFulfillmentStateTransitByOrder(ctx, order, fromState, toState)), ); return fulfillment; } + private async handleFulfillmentStateTransitByOrder( ctx: RequestContext, - orderId: ID, + order: Order, fromState: FulfillmentState, toState: FulfillmentState, ): Promise { + const nextOrderStates = this.getNextOrderStates(order); + + const transitionOrderIfStateAvailable = (state: OrderState) => + nextOrderStates.includes(state) && this.transitionToState(ctx, order.id, state); + if (fromState === 'Pending' && toState === 'Shipped') { - const orderWithFulfillment = await this.getOrderWithFulfillments(ctx, orderId); + const orderWithFulfillment = await this.getOrderWithFulfillments(ctx, order.id); if (orderItemsAreShipped(orderWithFulfillment)) { - await this.transitionToState(ctx, orderId, 'Shipped'); + await transitionOrderIfStateAvailable('Shipped'); } else { - await this.transitionToState(ctx, orderId, 'PartiallyShipped'); + await transitionOrderIfStateAvailable('PartiallyShipped'); } } if (fromState === 'Shipped' && toState === 'Delivered') { - const orderWithFulfillment = await this.getOrderWithFulfillments(ctx, orderId); + const orderWithFulfillment = await this.getOrderWithFulfillments(ctx, order.id); if (orderItemsAreDelivered(orderWithFulfillment)) { - await this.transitionToState(ctx, orderId, 'Delivered'); + await transitionOrderIfStateAvailable('Delivered'); } else { - await this.transitionToState(ctx, orderId, 'PartiallyDelivered'); + await transitionOrderIfStateAvailable('PartiallyDelivered'); } } }