UNPKG

@pinelab/vendure-plugin-metrics

Version:

Vendure plugin measuring and visualizing e-commerce metrics

50 lines (49 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AverageOrderValueMetric = void 0; const graphql_1 = require("../ui/generated/graphql"); /** * Calculates the average order value per month */ class AverageOrderValueMetric { constructor() { this.metricType = graphql_1.AdvancedMetricType.Currency; this.code = 'aov'; this.allowProductSelection = false; } getTitle(ctx) { return `Average order value`; } calculateDataPoints(ctx, entities) { let legendLabel = 'Average order value'; if (ctx.channel.pricesIncludeTax) { legendLabel += ' (incl. tax)'; } else { legendLabel += ' (excl. tax)'; } if (!entities.length) { // Return 0 as average if no orders return [ { legendLabel, value: 0, }, ]; } const totalFieldName = ctx.channel.pricesIncludeTax ? 'totalWithTax' : 'total'; const total = entities .map((o) => o[totalFieldName]) .reduce((total, current) => total + current, 0); const average = Math.round(total / entities.length) / 100; return [ { legendLabel, value: average, }, ]; } } exports.AverageOrderValueMetric = AverageOrderValueMetric;