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

This commit is contained in:
Sebastian Rindom committed 2020-09-13 11:52:55 +02:00
1 parent 099304f335
commit 6a90e94c62
1 file changed
+29 -9
@@ -40,7 +40,9 @@ class SegmentService extends BaseService {
if (fromCurrency === toCurrency) return value.toFixed(2) if (fromCurrency === toCurrency) return value.toFixed(2)
const exchangeRate = await axios 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 }) => { .then(({ data }) => {
return data.rates[fromCurrency] return data.rates[fromCurrency]
}) })
@@ -48,7 +50,6 @@ class SegmentService extends BaseService {
return (value / exchangeRate).toFixed(2) return (value / exchangeRate).toFixed(2)
} }
async buildOrder(order) { async buildOrder(order) {
const subtotal = await this.totalsService_.getSubtotal(order) const subtotal = await this.totalsService_.getSubtotal(order)
const total = await this.totalsService_.getTotal(order) const total = await this.totalsService_.getTotal(order)
@@ -66,12 +67,29 @@ class SegmentService extends BaseService {
checkout_id: order.cart_id, checkout_id: order.cart_id,
order_id: order._id, order_id: order._id,
email: order.email, 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_total: await this.getReportingValue(order.currency_code, total),
reporting_subtotal: await this.getReportingValue(order.currency_code, subtotal), reporting_subtotal: await this.getReportingValue(
reporting_revenue: await this.getReportingValue(order.currency_code, revenue), order.currency_code,
reporting_shipping: await this.getReportingValue(order.currency_code, shipping), 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_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, total,
subtotal, subtotal,
revenue, revenue,
@@ -81,13 +99,16 @@ class SegmentService extends BaseService {
coupon, coupon,
currency: order.currency_code, currency: order.currency_code,
products: await Promise.all( products: await Promise.all(
order.items.map(async item => { order.items.map(async (item) => {
let name = item.title let name = item.title
let variant = item.description let variant = item.description
const unit_price = item.content.unit_price const unit_price = item.content.unit_price
const line_total = unit_price * item.content.quantity * item.quantity 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 { return {
name, name,
variant, variant,
@@ -101,7 +122,6 @@ class SegmentService extends BaseService {
), ),
} }
return orderData return orderData
} }
} }