From 1d3032dc671b477654afd13a218caa772454bde0 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 4 Jul 2022 09:57:35 +0200 Subject: [PATCH] fix(webshipper): allow cancelling WS orders with error status (#1755) --- .../medusa-fulfillment-webshipper/README.md | 3 ++- .../src/services/webshipper-fulfillment.js | 20 +++++++++---------- .../src/utils/webshipper.js | 10 ++++++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/medusa-fulfillment-webshipper/README.md b/packages/medusa-fulfillment-webshipper/README.md index 5ec1d6ec1e..130bdfe202 100644 --- a/packages/medusa-fulfillment-webshipper/README.md +++ b/packages/medusa-fulfillment-webshipper/README.md @@ -1,6 +1,6 @@ # medusa-fulfillment-webshipper -Adds Webshipper as a fulfilment provider in Medusa Commerce. +Adds Webshipper as a fulfilment provider in Medusa Commerce. On each new fulfillment an order is created in Webshipper. The plugin listens for shipment events and updated the shipment accordingly. A webhook listener is exposed at `/webshipper/shipments` to listen for shipment creations. You must create this webhook in Webshipper to have Medusa listen for shipment events. @@ -13,4 +13,5 @@ A webhook listener is exposed at `/webshipper/shipments` to listen for shipment order_channel_id: [the channel id to register orders on] (required) webhook_secret: [the webhook secret used to listen for shipments] (required) coo_countries: [an array of countries in which a Certificate of Origin will be attached] (default: "all") + delete_on_cancel [determines whether Webshipper orders are deleted when a Medusa fulfillment is canceled] (default: false) ``` diff --git a/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js b/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js index 92a991d9a0..7f77ef20c0 100644 --- a/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js +++ b/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js @@ -623,19 +623,17 @@ class WebshipperFulfillmentService extends FulfillmentService { return Promise.resolve() } - if (order) { - if ( - order.data.attributes.status !== "pending" && - order.data.attributes.status !== "missing_rate" - ) { - if (order.data.attributes.status === "cancelled") { - return Promise.resolve(order) - } - throw new Error("Cannot cancel order") - } + if (this.options_.delete_on_cancel) { + return await this.client_.orders.delete(data.id) } - return this.client_.orders.delete(data.id) + return await this.client_.orders.update(data.id, { + id: data.id, + type: "orders", + attributes: { + status: "cancelled", + }, + }) } } diff --git a/packages/medusa-fulfillment-webshipper/src/utils/webshipper.js b/packages/medusa-fulfillment-webshipper/src/utils/webshipper.js index af4b0e5212..e93b2d2204 100644 --- a/packages/medusa-fulfillment-webshipper/src/utils/webshipper.js +++ b/packages/medusa-fulfillment-webshipper/src/utils/webshipper.js @@ -83,6 +83,16 @@ class Webshipper { }, }).then(({ data }) => data) }, + update: async (id, data) => { + const path = `/v2/orders/${id}` + return this.client_({ + method: "PATCH", + url: path, + data: { + data, + }, + }).then(({ data }) => data) + }, delete: async (id) => { const path = `/v2/orders/${id}` return this.client_({