fix(medusa-plugin-brightpearl): Add bp from sales channel of order where possible (#4160)

* add bp from sales channel of order where possible

* add changeset

* correct typo

* Add missing relations

---------

Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
Philip Korsholm
2023-06-05 16:01:01 +02:00
committed by GitHub
parent 2637b82933
commit 572280e1d1
3 changed files with 74 additions and 16 deletions

View File

@@ -0,0 +1,5 @@
---
"medusa-plugin-brightpearl": patch
---
fix(medusa-plugin-bright-pearl): update order creation to include sales channel brightpearl id where possible

View File

@@ -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`,

View File

@@ -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_