diff --git a/.changeset/itchy-flowers-hug.md b/.changeset/itchy-flowers-hug.md new file mode 100644 index 0000000000..bd111d85a0 --- /dev/null +++ b/.changeset/itchy-flowers-hug.md @@ -0,0 +1,5 @@ +--- +"medusa-plugin-brightpearl": patch +--- + +fix(medusa-plugin-bright-pearl): update order creation to include sales channel brightpearl id where possible diff --git a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js index eaa0fb7227..e1007cdc51 100644 --- a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js +++ b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js @@ -553,7 +553,10 @@ class BrightpearlService extends BaseService { currency: parentSo.currency, ref: parentSo.ref, externalRef: `${parentSo.externalRef}.${fromRefund.id}`, - channelId: this.options.channel_id || `1`, + channelId: + fromOrder.sales_channel?.metadata?.bp_id || + this.options.channel_id || + `1`, installedIntegrationInstanceId: authData.installation_instance_id, customer: parentSo.customer, delivery: parentSo.delivery, @@ -889,7 +892,10 @@ class BrightpearlService extends BaseService { currency: parentSo.currency, ref: parentSo.ref, externalRef: `${parentSo.externalRef}.${fromReturn.id}`, - channelId: this.options.channel_id || `1`, + channelId: + fromOrder.sales_channel?.metadata?.bp_id || + this.options.channel_id || + `1`, installedIntegrationInstanceId: authData.installation_instance_id, customer: parentSo.customer, delivery: parentSo.delivery, @@ -1129,7 +1135,10 @@ class BrightpearlService extends BaseService { }, ref: `${fromOrder.display_id}-S${sIndex + 1}`, externalRef: `${fromOrder.id}.${fromSwap.id}`, - channelId: this.options.channel_id || `1`, + channelId: + fromOrder.sales_channel?.metadata?.bp_id || + this.options.channel_id || + `1`, installedIntegrationInstanceId: authData.installation_instance_id, statusId: this.options.swap_status_id || this.options.default_status_id || `3`, @@ -1216,7 +1225,10 @@ class BrightpearlService extends BaseService { currency: parentSo.currency, ref: `${parentSo.ref}-C${cIndex + 1}`, externalRef: `${parentSo.externalRef}.${fromClaim.id}`, - channelId: this.options.channel_id || `1`, + channelId: + fromOrder.sales_channel?.metadata?.bp_id || + this.options.channel_id || + `1`, installedIntegrationInstanceId: authData.installation_instance_id, customer: parentSo.customer, delivery: parentSo.delivery, @@ -1310,7 +1322,10 @@ class BrightpearlService extends BaseService { currency: parentSo.currency, ref: `${parentSo.ref}-S${sIndex + 1}`, externalRef: `${parentSo.externalRef}.${fromSwap.id}`, - channelId: this.options.channel_id || `1`, + channelId: + fromOrder.sales_channel?.metadata?.bp_id || + this.options.channel_id || + `1`, installedIntegrationInstanceId: authData.installation_instance_id, customer: parentSo.customer, delivery: parentSo.delivery, @@ -1554,7 +1569,10 @@ class BrightpearlService extends BaseService { priceListId: this.options.cost_price_list || `1`, ref: `${fromOrder.display_id}-C${cIndex + 1}`, externalRef: `${fromOrder.id}.${fromClaim.id}`, - channelId: this.options.channel_id || `1`, + channelId: + fromOrder.sales_channel?.metadata?.bp_id || + this.options.channel_id || + `1`, installedIntegrationInstanceId: authData.installation_instance_id, statusId: this.options.claim_status_id || this.options.default_status_id || `3`, diff --git a/packages/medusa-plugin-brightpearl/src/subscribers/order.js b/packages/medusa-plugin-brightpearl/src/subscribers/order.js index 95e12649a0..732905a96d 100644 --- a/packages/medusa-plugin-brightpearl/src/subscribers/order.js +++ b/packages/medusa-plugin-brightpearl/src/subscribers/order.js @@ -8,6 +8,7 @@ class OrderSubscriber { brightpearlService, claimService, fulfillmentService, + featureFlagRouter, }) { this.orderService_ = orderService this.brightpearlService_ = brightpearlService @@ -16,6 +17,7 @@ class OrderSubscriber { this.paymentProviderService_ = paymentProviderService this.fulfillmentService_ = fulfillmentService this.claimService_ = claimService + this.featureFlagRouter = featureFlagRouter eventBusService.subscribe("order.placed", this.sendToBrightpearl) @@ -95,14 +97,28 @@ class OrderSubscriber { "return_order.shipping_method", "return_order.shipping_method.tax_lines", "additional_items", + "additional_items.variant", + "additional_items.variant.product", "additional_items.tax_lines", "shipping_address", "shipping_methods", "shipping_methods.tax_lines", ], }) + + const relations = [ + "payments", + "region", + "swaps", + "discounts", + "discounts.rule", + ] + if (this.featureFlagRouter.isFeatureEnabled("sales_channels")) { + relations.push("sales_channel") + } + const fromOrder = await this.orderService_.retrieve(fromSwap.order_id, { - relations: ["payments", "region", "swaps", "discounts", "discounts.rule"], + relations, }) if ( @@ -128,20 +144,27 @@ class OrderSubscriber { "return_order.shipping_method", "return_order.shipping_method.tax_lines", "additional_items", + "additional_items.variant", + "additional_items.variant.product", "additional_items.tax_lines", "shipping_address", "shipping_methods", ], }) + const relations = [ + "payments", + "region", + "claims", + "discounts", + "discounts.rule", + ] + if (this.featureFlagRouter.isFeatureEnabled("sales_channels")) { + relations.push("sales_channel") + } + const fromOrder = await this.orderService_.retrieve(fromClaim.order_id, { - relations: [ - "payments", - "region", - "claims", - "discounts", - "discounts.rule", - ], + relations, }) if (fromClaim.type === "replace") { @@ -167,8 +190,13 @@ class OrderSubscriber { registerReturn = async (data) => { const { id, return_id } = data + const relations = ["discounts", "region", "swaps", "payments"] + if (this.featureFlagRouter.isFeatureEnabled("sales_channels")) { + relations.push("sales_channel") + } + const order = await this.orderService_.retrieve(id, { - relations: ["discounts", "region", "swaps", "payments"], + relations, }) const fromReturn = await this.returnService_.retrieve(return_id, { @@ -182,8 +210,15 @@ class OrderSubscriber { registerRefund = async (data) => { const { id, refund_id } = data + + const relations = ["region", "payments"] + + if (this.featureFlagRouter.isFeatureEnabled("sales_channels")) { + relations.push("sales_channel") + } + const order = await this.orderService_.retrieve(id, { - relations: ["region", "payments"], + relations, }) const refund = await this.paymentProviderService_.retrieveRefund(refund_id) return this.brightpearlService_