From 61e1a7d7f1c95061f895c1a190d99a5de8e65552 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Wed, 5 May 2021 17:23:26 +0200 Subject: [PATCH] hotfix(webshipper): adds option to specify which countries have CoO attached --- packages/medusa-cli/.gitignore | 2 ++ .../medusa-fulfillment-webshipper/README.md | 1 + .../src/services/webshipper-fulfillment.js | 22 +++++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/medusa-cli/.gitignore b/packages/medusa-cli/.gitignore index 1eae0cf670..41bdfcc44f 100644 --- a/packages/medusa-cli/.gitignore +++ b/packages/medusa-cli/.gitignore @@ -1,2 +1,4 @@ dist/ node_modules/ + +.env diff --git a/packages/medusa-fulfillment-webshipper/README.md b/packages/medusa-fulfillment-webshipper/README.md index 8055be0b42..5ec1d6ec1e 100644 --- a/packages/medusa-fulfillment-webshipper/README.md +++ b/packages/medusa-fulfillment-webshipper/README.md @@ -12,4 +12,5 @@ A webhook listener is exposed at `/webshipper/shipments` to listen for shipment api_token: [a webshipper api token] (required) 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") ``` diff --git a/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js b/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js index 7348c422c5..308c1d4edb 100644 --- a/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js +++ b/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js @@ -9,6 +9,16 @@ class WebshipperFulfillmentService extends FulfillmentService { this.options_ = options + if (!options.coo_countries) { + this.options_.coo_countries = ["all"] + } else if (Array.isArray(options.coo_countries)) { + this.options_.coo_countries = options.coo_countries.map((c) => + c.toLowerCase() + ) + } else if (typeof options.coo_countries === "string") { + this.options_.coo_countries = [options.coo_countries] + } + /** @private @const {logger} */ this.logger_ = logger @@ -242,6 +252,8 @@ class WebshipperFulfillmentService extends FulfillmentService { webshipperOrder = await this.client_.orders.retrieve(existing) } + const { shipping_address } = fromOrder + if (!webshipperOrder) { let invoice let certificateOfOrigin @@ -266,7 +278,14 @@ class WebshipperFulfillmentService extends FulfillmentService { throw err }) - if (this.invoiceGenerator_.createCertificateOfOrigin) { + const cooCountries = this.options_.coo_countries + if ( + (cooCountries.includes("all") || + cooCountries.includes( + shipping_address.country_code.toLowerCase() + )) && + this.invoiceGenerator_.createCertificateOfOrigin + ) { const base64Coo = await this.invoiceGenerator_.createCertificateOfOrigin( fromOrder, fulfillmentItems @@ -297,7 +316,6 @@ class WebshipperFulfillmentService extends FulfillmentService { visible_ref = `S-${fromOrder.display_id}` } - const { shipping_address } = fromOrder const newOrder = { type: "orders", attributes: {