Files
medusa-store/packages/medusa/src/subscribers/order.js
Sebastian Rindom f1baca3cbd Replaces MongoDB support with PostgreSQL (#151)
- All schemas have been rewritten to a relational model
- All services have been rewritten to accommodate the new data model
- Adds idempotency keys to core endpoints allowing you to retry requests with no additional side effects
- Adds staged jobs to avoid putting jobs in the queue when transactions abort
- Adds atomic transactions to all methods with access to the data layer

Co-authored-by: Oliver Windall Juhl <oliver@mrbltech.com>
2021-01-26 10:26:14 +01:00

60 lines
1.3 KiB
JavaScript

class OrderSubscriber {
constructor({
manager,
eventBusService,
discountService,
giftCardService,
totalsService,
orderService,
regionService,
}) {
this.manager_ = manager
this.totalsService_ = totalsService
this.discountService_ = discountService
this.giftCardService_ = giftCardService
this.orderService_ = orderService
this.regionService_ = regionService
this.eventBus_ = eventBusService
this.eventBus_.subscribe("order.placed", this.handleOrderPlaced)
}
handleOrderPlaced = async data => {
const order = await this.orderService_.retrieve(data.id, {
select: ["subtotal"],
relations: ["discounts", "items", "gift_cards"],
})
await Promise.all(
order.items.map(async i => {
if (i.is_giftcard) {
for (let qty = 0; qty < i.quantity; qty++) {
await this.giftCardService_.create({
region_id: order.region_id,
order_id: order.id,
value: i.unit_price,
balance: i.unit_price,
metadata: i.metadata,
})
}
}
})
)
await Promise.all(
order.discounts.map(async d => {
return this.discountService_.update(d.id, {
usage_count: d.usage_count + 1,
})
})
)
}
}
export default OrderSubscriber