Skip to content

Commit

Permalink
Merge pull request #282 from Pinelab-studio/fix/picqer-fulfilment
Browse files Browse the repository at this point in the history
Fix/picqer fulfilment
  • Loading branch information
martijnvdbrug authored Nov 1, 2023
2 parents 2216aee + 5a1645c commit da443e1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
16 changes: 7 additions & 9 deletions packages/util/src/order-state-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export async function transitionToDelivered(
}

/**
* Throw an error if the transition failed
* Throws the error result if the transition failed
*/
export function throwIfTransitionFailed(
result:
Expand All @@ -93,17 +93,15 @@ export function throwIfTransitionFailed(
| AddFulfillmentToOrderResult
): void {
const stateError = result as FulfillmentStateTransitionError;
if (stateError.transitionError) {
if (stateError.fromState === stateError.toState) {
return; // If already 'Shipped', don't count this as an error
}
throw Error(
`${stateError.message} - from ${stateError.fromState} to ${stateError.toState} - ${stateError.transitionError}`
);
if (
stateError.transitionError &&
stateError.fromState === stateError.toState
) {
return; // If already 'Shipped', don't count this as an error
}
// It's not a stateTransition error
const error = result as ErrorResult;
if (error.errorCode) {
throw Error(`${error.errorCode}: ${error.message}`);
throw error;
}
}
6 changes: 5 additions & 1 deletion packages/vendure-plugin-picqer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 2.0.0
# 2.0.2

- Try to fulfill again on order status changed webhooks, to support fulfilling of back orders.

# 2.0.1

- Push to Picqer even when fulfilling failed. Log order codes and manual steps needed when fulfillment fails.

Expand Down
2 changes: 1 addition & 1 deletion packages/vendure-plugin-picqer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-plugin-picqer",
"version": "2.0.1",
"version": "2.0.2",
"description": "Vendure plugin syncing to orders and stock with Picqer",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
46 changes: 29 additions & 17 deletions packages/vendure-plugin-picqer/src/api/picqer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,9 @@ export class PicqerService implements OnApplicationBootstrap {
if (data.status === 'completed' && order.state !== 'Delivered') {
// Order should be transitioned to Shipped, then to Delivered
if (order.state !== 'Shipped') {
// Try to fulfill order first. This should have been done already, except for back orders
await this.safeFulfill(ctx, order, 'order.status_changed webhook');
// If order isn't Shipped yet, mark all it's fulfillments as Shipped
// The order should already have been fulfilled when pushing to Picqer
for (const fulfillment of order.fulfillments) {
const result = await this.orderService.transitionFulfillmentToState(
ctx,
Expand Down Expand Up @@ -728,22 +729,7 @@ export class PicqerService implements OnApplicationBootstrap {
return;
}
// Fulfill order first
try {
const fulfillment = await fulfillAll(ctx, this.orderService, order, {
code: picqerHandler.code,
arguments: [],
});
Logger.info(
`Created fulfillment (${fulfillment.id}) for order ${order.code}`,
loggerCtx
);
} catch (e: any) {
Logger.error(
`Failed to fulfill order ${order.code}: ${e?.message}. Transition this order manually to 'Delivered' if it has been sent to Picqer, to prevent future errors related to status changes for this order.`,
loggerCtx,
util.inspect(e)
);
}
await this.safeFulfill(ctx, order, 'order placement');
// Push the order to Picqer
await this.pushOrderToPicqer(ctx, order, client);
}
Expand Down Expand Up @@ -1177,6 +1163,32 @@ export class PicqerService implements OnApplicationBootstrap {
};
}

/**
* Fulfill without throwing errors. Logs an error if fulfilment fails
*/
private async safeFulfill(
ctx: RequestContext,
order: Order,
logAction: string
): Promise<void> {
try {
const fulfillment = await fulfillAll(ctx, this.orderService, order, {
code: picqerHandler.code,
arguments: [],
});
Logger.info(
`Created fulfillment (${fulfillment.id}) for order ${order.code} on '${logAction}'`,
loggerCtx
);
} catch (e: any) {
Logger.error(
`Failed to fulfill order ${order.code} on '${logAction}': ${e?.message}. Transition this order manually to 'Delivered' after checking that it exists in Picqer.`,
loggerCtx,
util.inspect(e)
);
}
}

/**
* Combine street and housenumber to get a full readable address.
* Returns undefined if address undefined
Expand Down

0 comments on commit da443e1

Please sign in to comment.