fix(webshipper): allow cancelling WS orders with error status (#1755)

This commit is contained in:
Sebastian Rindom
2022-07-04 07:57:35 +00:00
committed by GitHub
parent c76e23e84d
commit 1d3032dc67
3 changed files with 21 additions and 12 deletions
@@ -1,6 +1,6 @@
# medusa-fulfillment-webshipper # 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. 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. 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) order_channel_id: [the channel id to register orders on] (required)
webhook_secret: [the webhook secret used to listen for shipments] (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") 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)
``` ```
@@ -623,19 +623,17 @@ class WebshipperFulfillmentService extends FulfillmentService {
return Promise.resolve() return Promise.resolve()
} }
if (order) { if (this.options_.delete_on_cancel) {
if ( return await this.client_.orders.delete(data.id)
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")
}
} }
return this.client_.orders.delete(data.id) return await this.client_.orders.update(data.id, {
id: data.id,
type: "orders",
attributes: {
status: "cancelled",
},
})
} }
} }
@@ -83,6 +83,16 @@ class Webshipper {
}, },
}).then(({ data }) => data) }).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) => { delete: async (id) => {
const path = `/v2/orders/${id}` const path = `/v2/orders/${id}`
return this.client_({ return this.client_({