Merge pull request #231 from medusajs/fix/segment-track-swaps

fix(segment): track swaps
This commit is contained in:
Sebastian Rindom
2021-04-09 10:09:25 +02:00
committed by GitHub
3 changed files with 2302 additions and 20 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -18,6 +18,17 @@ class OrderSubscriber {
this.fulfillmentService_ = fulfillmentService
// Swaps
// order.swap_received <--- Will be deprecated
// swap.created
// swap.received
// swap.shipment_created
// swap.payment_completed
// swap.payment_captured
// swap.refund_processed
eventBusService.subscribe(
"order.shipment_created",
async ({ id, fulfillment_id }) => {

View File

@@ -0,0 +1,149 @@
import { humanizeAmount } from "medusa-core-utils"
class OrderSubscriber {
constructor({
segmentService,
eventBusService,
swapService,
lineItemService,
fulfillmentService,
}) {
this.fulfillmentService_ = fulfillmentService
this.lineItemService_ = lineItemService
this.swapService_ = swapService
this.segmentService_ = segmentService
eventBusService.subscribe(
"swap.shipment_created",
async ({ id, fulfillment_id }) => {
const [swap, swapReport] = await this.gatherSwapReport(id)
const fulfillment = await this.fulfillmentService_.retrieve(
fulfillment_id
)
const currency = swapReport.currency
const total = humanizeAmount(swap.difference_due, currency)
const reporting_total = await this.segmentService_.getReportingValue(
currency,
total
)
return await segmentService.track({
event: "Swap Shipped",
userId: swap.order.customer_id,
timestamp: fulfillment.shipped_at,
properties: {
reporting_total,
total,
...swapReport,
},
})
}
)
eventBusService.subscribe("swap.payment_completed", async ({ id }) => {
const [swap, swapReport] = await this.gatherSwapReport(id)
const currency = swapReport.currency
const total = humanizeAmount(swap.difference_due, currency)
const reporting_total = await this.segmentService_.getReportingValue(
currency,
total
)
return await segmentService.track({
event: "Swap Confirmed",
userId: swap.order.customer_id,
timestamp: swap.confirmed_at,
properties: {
reporting_total,
total,
...swapReport,
},
})
})
eventBusService.subscribe("swap.created", async ({ id }) => {
const [swap, swapReport] = await this.gatherSwapReport(id)
return await segmentService.track({
event: "Swap Created",
userId: swap.order.customer_id,
timestamp: swap.created_at,
properties: swapReport,
})
})
}
async gatherSwapReport(id) {
const swap = await this.swapService_.retrieve(id, {
relations: [
"order",
"additional_items",
"additional_items.variant",
"return_order",
"return_order.items",
"return_order.shipping_method",
],
})
const currency = swap.order.currency_code
const newItems = await Promise.all(
swap.additional_items.map(async (i) => {
const price = humanizeAmount(i.unit_price, currency)
const reporting_price = await this.segmentService_.getReportingValue(
currency,
price
)
return {
name: i.title,
product_id: i.variant.product_id,
variant: i.variant.sku,
quantity: i.quantity,
price,
reporting_price,
}
})
)
const returnItems = await Promise.all(
swap.return_order.items.map(async (ri) => {
const i = await this.lineItemService_.retrieve(ri.item_id, {
relations: ["variant"],
})
const price = humanizeAmount(i.unit_price, currency)
const reporting_price = await this.segmentService_.getReportingValue(
currency,
price
)
return {
name: i.title,
product_id: i.variant.product_id,
variant: i.variant.sku,
quantity: ri.quantity,
price,
reporting_price,
}
})
)
return [
swap,
{
swap_id: swap.id,
order_id: swap.order_id,
new_items: newItems,
return_items: returnItems,
currency,
},
]
}
}
export default OrderSubscriber