From c1bec4f843bd054add0b15835632f1c30e730d56 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Thu, 24 Jun 2021 17:56:02 +0200 Subject: [PATCH] cleanup: Eventbus, BP, Contentful, NotificationService (#292) --- .../src/api/index.js | 4 +- .../src/services/brightpearl.js | 98 ++++++++++--------- .../src/subscribers/order.js | 9 ++ .../src/services/contentful.js | 15 ++- .../src/subscribers/index.js | 2 + packages/medusa/src/services/notification.js | 2 +- packages/medusa/src/services/order.js | 3 + 7 files changed, 76 insertions(+), 57 deletions(-) diff --git a/packages/medusa-plugin-brightpearl/src/api/index.js b/packages/medusa-plugin-brightpearl/src/api/index.js index c32337a134..b20726fac3 100644 --- a/packages/medusa-plugin-brightpearl/src/api/index.js +++ b/packages/medusa-plugin-brightpearl/src/api/index.js @@ -22,10 +22,10 @@ export default (container) => { app.post("/brightpearl/goods-out", bodyParser.json(), async (req, res) => { const { id, lifecycle_event } = req.body - const brightpearlService = req.scope.resolve("brightpearlService") + const eventBusService = req.scope.resolve("eventBusService") if (lifecycle_event === "created") { - await brightpearlService.createFulfillmentFromGoodsOut(id) + eventBusService.emit("brightpearl.goods_out_note", { id }) } res.sendStatus(200) diff --git a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js index 8d4c197bcb..c458d24b34 100644 --- a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js +++ b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js @@ -1110,55 +1110,63 @@ class BrightpearlService extends BaseService { } async createFulfillmentFromGoodsOut(id) { - const client = await this.getClient() + await this.manager_.transaction(async (m) => { + const client = await this.getClient() - // Get goods out and associated order - const goodsOut = await client.warehouses.retrieveGoodsOutNote(id) - const order = await client.orders.retrieve(goodsOut.orderId) + // Get goods out and associated order + const goodsOut = await client.warehouses.retrieveGoodsOutNote(id) + const order = await client.orders.retrieve(goodsOut.orderId) - // Only relevant for medusa orders check channel id - if (order.channelId !== parseInt(this.options.channel_id)) { - return - } - - // Combine the line items that we are going to create a fulfillment for - const lines = Object.keys(goodsOut.orderRows) - .map((key) => { - const row = order.rows.find((r) => r.id == key) - if (row) { - return { - item_id: row.externalRef, - - // Brightpearl sometimes gives multiple order row entries - quantity: goodsOut.orderRows[key].reduce( - (sum, next) => sum + next.quantity, - 0 - ), - } - } - - return null - }) - .filter((i) => !!i) - - // Orders with a concatenated externalReference are swap orders - const [_, partId] = order.externalRef.split(".") - - if (partId) { - if (partId.startsWith("claim")) { - return this.claimService_.createFulfillment(partId, { - goods_out_note: id, - }) - } else { - return this.swapService_.createFulfillment(partId, { - goods_out_note: id, - }) + // Only relevant for medusa orders check channel id + if (order.channelId !== parseInt(this.options.channel_id)) { + return } - } - return this.orderService_.createFulfillment(order.externalRef, lines, { - goods_out_note: id, - }) + // Combine the line items that we are going to create a fulfillment for + const lines = Object.keys(goodsOut.orderRows) + .map((key) => { + const row = order.rows.find((r) => r.id == key) + if (row) { + return { + item_id: row.externalRef, + + // Brightpearl sometimes gives multiple order row entries + quantity: goodsOut.orderRows[key].reduce( + (sum, next) => sum + next.quantity, + 0 + ), + } + } + + return null + }) + .filter((i) => !!i) + + // Orders with a concatenated externalReference are swap orders + const [_, partId] = order.externalRef.split(".") + + if (partId) { + if (partId.startsWith("claim")) { + return this.claimService_ + .withTransaction(m) + .createFulfillment(partId, { + goods_out_note: id, + }) + } else { + return this.swapService_ + .withTransaction(m) + .createFulfillment(partId, { + goods_out_note: id, + }) + } + } + + return this.orderService_ + .withTransaction(m) + .createFulfillment(order.externalRef, lines, { + goods_out_note: id, + }) + }, "SERIALIZABLE") } async createCustomer(fromOrder) { diff --git a/packages/medusa-plugin-brightpearl/src/subscribers/order.js b/packages/medusa-plugin-brightpearl/src/subscribers/order.js index d2b89bb247..61caed781e 100644 --- a/packages/medusa-plugin-brightpearl/src/subscribers/order.js +++ b/packages/medusa-plugin-brightpearl/src/subscribers/order.js @@ -19,6 +19,11 @@ class OrderSubscriber { eventBusService.subscribe("order.placed", this.sendToBrightpearl) + eventBusService.subscribe( + "brightpearl.goods_out_note", + this.createFulfillmentFromGoodsOut + ) + eventBusService.subscribe("claim.created", this.registerClaim) eventBusService.subscribe("order.refund_created", this.registerRefund) @@ -50,6 +55,10 @@ class OrderSubscriber { return this.brightpearlService_.createPayment(id) } + createFulfillmentFromGoodsOut = ({ id }) => { + return this.brightpearlService_.createFulfillmentFromGoodsOut(id) + } + registerSwapPayment = async (data) => { return this.registerSwap({ id: data.id, swap_id: data.id }) } diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index ab28b058db..edd046c8a5 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -297,9 +297,6 @@ class ContentfulService extends BaseService { } ) - // const ignoreIds = (await this.getIgnoreIds_("product_variant")) || [] - // ignoreIds.push(v.id) - // this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds)) return result } catch (error) { throw error @@ -311,7 +308,7 @@ class ContentfulService extends BaseService { .then(() => true) .catch(() => false) if (!hasType) { - return + return Promise.resolve() } try { const r = await this.regionService_.retrieve(region.id, { @@ -353,7 +350,7 @@ class ContentfulService extends BaseService { .then(() => true) .catch(() => false) if (!hasType) { - return + return Promise.resolve() } const updateFields = [ @@ -435,13 +432,13 @@ class ContentfulService extends BaseService { const found = data.fields.find((f) => updateFields.includes(f)) if (!found) { - return + return Promise.resolve() } try { const ignore = await this.shouldIgnore_(data.id, "contentful") if (ignore) { - return + return Promise.resolve() } const p = await this.productService_.retrieve(data.id, { @@ -594,14 +591,14 @@ class ContentfulService extends BaseService { if (variant.fields) { const found = variant.fields.find((f) => updateFields.includes(f)) if (!found) { - return + return Promise.resolve() } } try { const ignore = await this.shouldIgnore_(variant.id, "contentful") if (ignore) { - return + return Promise.resolve() } const environment = await this.getContentfulEnvironment_() diff --git a/packages/medusa-plugin-restock-notification/src/subscribers/index.js b/packages/medusa-plugin-restock-notification/src/subscribers/index.js index 53b3e1dbd7..e97e5293c6 100644 --- a/packages/medusa-plugin-restock-notification/src/subscribers/index.js +++ b/packages/medusa-plugin-restock-notification/src/subscribers/index.js @@ -19,6 +19,8 @@ class VariantSubscriber { .triggerRestock(id) ) } + + return Promise.resolve() } } diff --git a/packages/medusa/src/services/notification.js b/packages/medusa/src/services/notification.js index df75b7fdc0..0c4a5eb747 100644 --- a/packages/medusa/src/services/notification.js +++ b/packages/medusa/src/services/notification.js @@ -162,7 +162,7 @@ class NotificationService extends BaseService { handleEvent(eventName, data) { const subs = this.subscribers_[eventName] if (!subs) { - return + return Promise.resolve() } return Promise.all( diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index 8b16ee17fe..f6e91bd37d 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -110,10 +110,13 @@ class OrderService extends BaseService { shippingOptionService: this.shippingOptionService_, shippingProfileService: this.shippingProfileService_, fulfillmentProviderService: this.fulfillmentProviderService_, + fulfillmentService: this.fulfillmentService_, + customerService: this.customerService_, discountService: this.discountService_, totalsService: this.totalsService_, cartService: this.cartService_, giftCardService: this.giftCardService_, + addressRepository: this.addressRepository_, draftOrderService: this.draftOrderService_, })