feat(medusa): adds support for gift cards (#132)
* fix(medusa-plugin-brightpearl): adds gift cards to gift card nominal code * fix: allow orders with 0 total to be created * fix: gift card ready * tests passing * docs: brightpearl comment * docs: clearer REMEMBER note
This commit is contained in:
1 parent
b33fcfa182
commit
f2c62cd232
17 files changed
+316
-113
No files matched your search
@@ -1,11 +1,12 @@
|
||||
class OrderSubscriber {
|
||||
constructor({
|
||||
paymentProviderService,
|
||||
cartService,
|
||||
customerService,
|
||||
eventBusService,
|
||||
discountService,
|
||||
totalsService,
|
||||
orderService,
|
||||
regionService,
|
||||
}) {
|
||||
this.totalsService_ = totalsService
|
||||
|
||||
@@ -15,7 +16,9 @@ class OrderSubscriber {
|
||||
|
||||
this.discountService_ = discountService
|
||||
|
||||
this.cartService_ = cartService
|
||||
this.orderService_ = orderService
|
||||
|
||||
this.regionService_ = regionService
|
||||
|
||||
this.eventBus_ = eventBusService
|
||||
|
||||
@@ -31,6 +34,8 @@ class OrderSubscriber {
|
||||
})
|
||||
|
||||
this.eventBus_.subscribe("order.placed", this.handleDiscounts)
|
||||
|
||||
this.eventBus_.subscribe("order.placed", this.handleGiftCards)
|
||||
}
|
||||
|
||||
handleDiscounts = async order => {
|
||||
@@ -58,6 +63,40 @@ class OrderSubscriber {
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
handleGiftCards = async order => {
|
||||
const region = await this.regionService_.retrieve(order.region_id)
|
||||
const items = await Promise.all(
|
||||
order.items.map(async i => {
|
||||
if (i.is_giftcard) {
|
||||
const giftcard = await this.discountService_
|
||||
.generateGiftCard(i.content.unit_price, region._id)
|
||||
.then(result => {
|
||||
this.eventBus_.emit("order.gift_card_created", {
|
||||
line_item: i,
|
||||
currency_code: region.currency_code,
|
||||
tax_rate: region.tax_rate,
|
||||
giftcard: result,
|
||||
email: order.email,
|
||||
})
|
||||
return result
|
||||
})
|
||||
return {
|
||||
...i,
|
||||
metadata: {
|
||||
...i.metadata,
|
||||
giftcard: giftcard._id,
|
||||
},
|
||||
}
|
||||
}
|
||||
return i
|
||||
})
|
||||
)
|
||||
|
||||
return this.orderService_.update(order._id, {
|
||||
items,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default OrderSubscriber
|
||||
Reference in new issue
Block a user