cleanup: Eventbus, BP, Contentful, NotificationService (#292)
This commit is contained in:
1 parent
db9d6c0cf5
commit
c1bec4f843
7 files changed
+76
-57
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user