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 Korsholmandolivermrbl authored and GitHub committed 2023-06-05 16:01:01 +02:00
1 parent 2637b82933
commit 572280e1d1
3 files changed
+70 -12

No files matched your search

+5
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
@@ -553,7 +553,10 @@ class BrightpearlService extends BaseService {
currency: parentSo.currency, currency: parentSo.currency,
ref: parentSo.ref, ref: parentSo.ref,
externalRef: `${parentSo.externalRef}.${fromRefund.id}`, 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, installedIntegrationInstanceId: authData.installation_instance_id,
customer: parentSo.customer, customer: parentSo.customer,
delivery: parentSo.delivery, delivery: parentSo.delivery,
@@ -889,7 +892,10 @@ class BrightpearlService extends BaseService {
currency: parentSo.currency, currency: parentSo.currency,
ref: parentSo.ref, ref: parentSo.ref,
externalRef: `${parentSo.externalRef}.${fromReturn.id}`, 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, installedIntegrationInstanceId: authData.installation_instance_id,
customer: parentSo.customer, customer: parentSo.customer,
delivery: parentSo.delivery, delivery: parentSo.delivery,
@@ -1129,7 +1135,10 @@ class BrightpearlService extends BaseService {
}, },
ref: `${fromOrder.display_id}-S${sIndex + 1}`, ref: `${fromOrder.display_id}-S${sIndex + 1}`,
externalRef: `${fromOrder.id}.${fromSwap.id}`, 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, installedIntegrationInstanceId: authData.installation_instance_id,
statusId: statusId:
this.options.swap_status_id || this.options.default_status_id || `3`, this.options.swap_status_id || this.options.default_status_id || `3`,
@@ -1216,7 +1225,10 @@ class BrightpearlService extends BaseService {
currency: parentSo.currency, currency: parentSo.currency,
ref: `${parentSo.ref}-C${cIndex + 1}`, ref: `${parentSo.ref}-C${cIndex + 1}`,
externalRef: `${parentSo.externalRef}.${fromClaim.id}`, 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, installedIntegrationInstanceId: authData.installation_instance_id,
customer: parentSo.customer, customer: parentSo.customer,
delivery: parentSo.delivery, delivery: parentSo.delivery,
@@ -1310,7 +1322,10 @@ class BrightpearlService extends BaseService {
currency: parentSo.currency, currency: parentSo.currency,
ref: `${parentSo.ref}-S${sIndex + 1}`, ref: `${parentSo.ref}-S${sIndex + 1}`,
externalRef: `${parentSo.externalRef}.${fromSwap.id}`, 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, installedIntegrationInstanceId: authData.installation_instance_id,
customer: parentSo.customer, customer: parentSo.customer,
delivery: parentSo.delivery, delivery: parentSo.delivery,
@@ -1554,7 +1569,10 @@ class BrightpearlService extends BaseService {
priceListId: this.options.cost_price_list || `1`, priceListId: this.options.cost_price_list || `1`,
ref: `${fromOrder.display_id}-C${cIndex + 1}`, ref: `${fromOrder.display_id}-C${cIndex + 1}`,
externalRef: `${fromOrder.id}.${fromClaim.id}`, 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, installedIntegrationInstanceId: authData.installation_instance_id,
statusId: statusId:
this.options.claim_status_id || this.options.default_status_id || `3`, this.options.claim_status_id || this.options.default_status_id || `3`,
@@ -8,6 +8,7 @@ class OrderSubscriber {
brightpearlService, brightpearlService,
claimService, claimService,
fulfillmentService, fulfillmentService,
featureFlagRouter,
}) { }) {
this.orderService_ = orderService this.orderService_ = orderService
this.brightpearlService_ = brightpearlService this.brightpearlService_ = brightpearlService
@@ -16,6 +17,7 @@ class OrderSubscriber {
this.paymentProviderService_ = paymentProviderService this.paymentProviderService_ = paymentProviderService
this.fulfillmentService_ = fulfillmentService this.fulfillmentService_ = fulfillmentService
this.claimService_ = claimService this.claimService_ = claimService
this.featureFlagRouter = featureFlagRouter
eventBusService.subscribe("order.placed", this.sendToBrightpearl) eventBusService.subscribe("order.placed", this.sendToBrightpearl)
@@ -95,14 +97,28 @@ class OrderSubscriber {
"return_order.shipping_method", "return_order.shipping_method",
"return_order.shipping_method.tax_lines", "return_order.shipping_method.tax_lines",
"additional_items", "additional_items",
"additional_items.variant",
"additional_items.variant.product",
"additional_items.tax_lines", "additional_items.tax_lines",
"shipping_address", "shipping_address",
"shipping_methods", "shipping_methods",
"shipping_methods.tax_lines", "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, { const fromOrder = await this.orderService_.retrieve(fromSwap.order_id, {
relations: ["payments", "region", "swaps", "discounts", "discounts.rule"], relations,
}) })
if ( if (
@@ -128,20 +144,27 @@ class OrderSubscriber {
"return_order.shipping_method", "return_order.shipping_method",
"return_order.shipping_method.tax_lines", "return_order.shipping_method.tax_lines",
"additional_items", "additional_items",
"additional_items.variant",
"additional_items.variant.product",
"additional_items.tax_lines", "additional_items.tax_lines",
"shipping_address", "shipping_address",
"shipping_methods", "shipping_methods",
], ],
}) })
const fromOrder = await this.orderService_.retrieve(fromClaim.order_id, { const relations = [
relations: [
"payments", "payments",
"region", "region",
"claims", "claims",
"discounts", "discounts",
"discounts.rule", "discounts.rule",
], ]
if (this.featureFlagRouter.isFeatureEnabled("sales_channels")) {
relations.push("sales_channel")
}
const fromOrder = await this.orderService_.retrieve(fromClaim.order_id, {
relations,
}) })
if (fromClaim.type === "replace") { if (fromClaim.type === "replace") {
@@ -167,8 +190,13 @@ class OrderSubscriber {
registerReturn = async (data) => { registerReturn = async (data) => {
const { id, return_id } = 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, { const order = await this.orderService_.retrieve(id, {
relations: ["discounts", "region", "swaps", "payments"], relations,
}) })
const fromReturn = await this.returnService_.retrieve(return_id, { const fromReturn = await this.returnService_.retrieve(return_id, {
@@ -182,8 +210,15 @@ class OrderSubscriber {
registerRefund = async (data) => { registerRefund = async (data) => {
const { id, refund_id } = 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, { const order = await this.orderService_.retrieve(id, {
relations: ["region", "payments"], relations,
}) })
const refund = await this.paymentProviderService_.retrieveRefund(refund_id) const refund = await this.paymentProviderService_.retrieveRefund(refund_id)
return this.brightpearlService_ return this.brightpearlService_