fix(medusa-plugin-segment): Track additional order details

This commit is contained in:
Sebastian Rindom
2020-09-13 11:52:36 +02:00
parent 099304f335
commit 6a90e94c62

View File

@@ -10,7 +10,7 @@ class SegmentService extends BaseService {
* write_key: Segment write key given in Segment dashboard
* }
*/
constructor({totalsService}, options) {
constructor({ totalsService }, options) {
super()
this.options_ = options
@@ -40,7 +40,9 @@ class SegmentService extends BaseService {
if (fromCurrency === toCurrency) return value.toFixed(2)
const exchangeRate = await axios
.get(`https://api.exchangeratesapi.io/${date}?symbols=${fromCurrency}&base=${toCurrency}`)
.get(
`https://api.exchangeratesapi.io/${date}?symbols=${fromCurrency}&base=${toCurrency}`
)
.then(({ data }) => {
return data.rates[fromCurrency]
})
@@ -48,7 +50,6 @@ class SegmentService extends BaseService {
return (value / exchangeRate).toFixed(2)
}
async buildOrder(order) {
const subtotal = await this.totalsService_.getSubtotal(order)
const total = await this.totalsService_.getTotal(order)
@@ -66,12 +67,29 @@ class SegmentService extends BaseService {
checkout_id: order.cart_id,
order_id: order._id,
email: order.email,
region_id: order.region_id,
payment_provider: order.payment_method.provider_id,
shipping_methods: order.shipping_methods,
shipping_country: order.shipping_address.country_code,
shipping_city: order.shipping_address.city,
reporting_total: await this.getReportingValue(order.currency_code, total),
reporting_subtotal: await this.getReportingValue(order.currency_code, subtotal),
reporting_revenue: await this.getReportingValue(order.currency_code, revenue),
reporting_shipping: await this.getReportingValue(order.currency_code, shipping),
reporting_subtotal: await this.getReportingValue(
order.currency_code,
subtotal
),
reporting_revenue: await this.getReportingValue(
order.currency_code,
revenue
),
reporting_shipping: await this.getReportingValue(
order.currency_code,
shipping
),
reporting_tax: await this.getReportingValue(order.currency_code, tax),
reporting_discount: await this.getReportingValue(order.currency_code, discount),
reporting_discount: await this.getReportingValue(
order.currency_code,
discount
),
total,
subtotal,
revenue,
@@ -81,13 +99,16 @@ class SegmentService extends BaseService {
coupon,
currency: order.currency_code,
products: await Promise.all(
order.items.map(async item => {
order.items.map(async (item) => {
let name = item.title
let variant = item.description
const unit_price = item.content.unit_price
const line_total = unit_price * item.content.quantity * item.quantity
const revenue = await this.getReportingValue(order.currency_code, line_total)
const revenue = await this.getReportingValue(
order.currency_code,
line_total
)
return {
name,
variant,
@@ -101,7 +122,6 @@ class SegmentService extends BaseService {
),
}
return orderData
}
}