Skip to content

Commit

Permalink
fix(dev-server): Fix multivendor plugin setting aggregate order state
Browse files Browse the repository at this point in the history
Fixes #2227
  • Loading branch information
michaelbromley committed Jun 19, 2023
1 parent 23b3f05 commit adca2dd
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OrderType } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';
import {
ChannelService,
CustomOrderProcess,
idsAreEqual,
Order,
Expand All @@ -10,16 +11,22 @@ import {
orderItemsAreShipped,
OrderService,
RequestContext,
RequestContextService,
TransactionalConnection,
User,
} from '@vendure/core';

let connection: TransactionalConnection;
let orderService: OrderService;
let channelService: ChannelService;
let requestContextService: RequestContextService;

export const multivendorOrderProcess: CustomOrderProcess<any> = {
init(injector) {
connection = injector.get(TransactionalConnection);
orderService = injector.get(OrderService);
channelService = injector.get(ChannelService);
requestContextService = injector.get(RequestContextService);
},

async onTransitionStart(fromState, toState, data) {
Expand Down Expand Up @@ -67,20 +74,41 @@ export const multivendorOrderProcess: CustomOrderProcess<any> = {
if (order.type === OrderType.Seller) {
const aggregateOrder = await orderService.getAggregateOrder(ctx, order);
if (aggregateOrder) {
// Create a new RequestContext on the default Channel, since the current
// RequestContext may be scoped to the Seller channel, and will not be able to
// update the AggregateOrder.
const defaultChannel = await channelService.getDefaultChannel();
const defaultChannelCtx = await requestContextService.create({
apiType: 'admin',
channelOrToken: defaultChannel,
req: ctx.req,
languageCode: ctx.languageCode,
user: ctx.activeUserId ? new User({ id: ctx.activeUserId }) : undefined,
});

// This part is responsible for automatically updating the state of the aggregate Order
// based on the fulfillment state of all the associated seller Orders.
const otherSellerOrders = (await orderService.getSellerOrders(ctx, aggregateOrder)).filter(
so => !idsAreEqual(so.id, order.id),
);

const sellerOrderStates = [...otherSellerOrders.map(so => so.state), toState];
if (sellerOrderStates.every(state => state === 'Shipped')) {
await orderService.transitionToState(data.ctx, aggregateOrder.id, 'Shipped');
await orderService.transitionToState(defaultChannelCtx, aggregateOrder.id, 'Shipped');
} else if (sellerOrderStates.every(state => state === 'Delivered')) {
await orderService.transitionToState(data.ctx, aggregateOrder.id, 'Delivered');
await orderService.transitionToState(defaultChannelCtx, aggregateOrder.id, 'Delivered');
} else if (sellerOrderStates.some(state => state === 'Delivered')) {
await orderService.transitionToState(data.ctx, aggregateOrder.id, 'PartiallyDelivered');
await orderService.transitionToState(
defaultChannelCtx,
aggregateOrder.id,
'PartiallyDelivered',
);
} else if (sellerOrderStates.some(state => state === 'Shipped')) {
await orderService.transitionToState(data.ctx, aggregateOrder.id, 'PartiallyShipped');
await orderService.transitionToState(
defaultChannelCtx,
aggregateOrder.id,
'PartiallyShipped',
);
}
}
}
Expand Down

0 comments on commit adca2dd

Please sign in to comment.