Skip to content

Commit

Permalink
feat(core): Use transaction to update Fulfillment state
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Sep 15, 2020
1 parent 657949e commit 8232ddc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/core/src/api/resolvers/admin/order.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/services/fulfillment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
23 changes: 15 additions & 8 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ export class OrderService {
this.eventBus.publish(new OrderStateTransitionEvent(fromState, state, ctx, order));
return order;
}

async transitionFulfillmentToState(
ctx: RequestContext,
fulfillmentId: ID,
Expand All @@ -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<void> {
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');
}
}
}
Expand Down

0 comments on commit 8232ddc

Please sign in to comment.