Adds gift card support

This commit is contained in:
Sebastian Rindom committed 2020-07-29 11:51:59 +02:00
1 parent 4c9b876407
commit 630bf3abc6
33 files changed
+722 -128

No files matched your search

+36
View File
@@ -4,9 +4,17 @@ class OrderSubscriber {
cartService,
customerService,
eventBusService,
discountService,
totalsService,
}) {
this.totalsService_ = totalsService
this.paymentProviderService_ = paymentProviderService
this.customerService_ = customerService
this.discountService_ = discountService
this.cartService_ = cartService
this.eventBus_ = eventBusService
@@ -33,6 +41,34 @@ class OrderSubscriber {
this.eventBus_.subscribe("order.placed", async order => {
await this.cartService_.delete(order.cart_id)
})
this.eventBus_.subscribe("order.placed", this.handleDiscounts)
}
handleDiscounts = async order => {
await Promise.all(
order.discounts.map(async d => {
const subtotal = await this.totalsService_.getSubtotal(order)
if (d.is_giftcard) {
const discountRule = {
...d.discount_rule,
value: Math.max(0, d.discount_rule.value - subtotal),
}
delete discountRule._id
return this.discountService_.update(d._id, {
discount_rule: discountRule,
usage_count: d.usage_count + 1,
disabled: discountRule.value === 0,
})
} else {
return this.discountService_.update(d._id, {
usage_count: d.usage_count + 1,
})
}
})
)
}
}