Fix data in refunds/cancellations charts

This commit is contained in:
2024-12-25 15:31:14 -05:00
parent 7320733ffb
commit 7c3c80fa42
2 changed files with 99 additions and 4 deletions

View File

@@ -1527,7 +1527,82 @@ export class EventsService {
prevPeriodEnd = prevRange.end;
}
// Load both current and previous period data
// For refunds and cancellations, we need to fetch those specific events
if (metric === 'refunds' || metric === 'cancellations') {
const [currentEvents] = await Promise.all([
this.getEvents({
...params,
startDate: periodStart.toISO(),
endDate: periodEnd.toISO(),
metricId: metric === 'refunds' ? METRIC_IDS.PAYMENT_REFUNDED : METRIC_IDS.CANCELED_ORDER
})
]);
// Transform events
const transformedEvents = this._transformEvents(currentEvents.data || []);
// Initialize daily stats map with all dates in range using TimeManager's day start
const dailyStats = new Map();
let currentDate = periodStart;
while (currentDate <= periodEnd) {
const dateKey = currentDate.toFormat('yyyy-MM-dd');
dailyStats.set(dateKey, {
date: currentDate.toISO(),
timestamp: dateKey,
total: 0,
count: 0,
reasons: {},
items: []
});
currentDate = this.timeManager.getDayStart(currentDate.plus({ days: 1 }));
}
// Process events
for (const event of transformedEvents) {
const datetime = this.timeManager.toDateTime(event.attributes?.datetime);
if (!datetime) continue;
// Get the day start for this event's time
const dayStart = this.timeManager.getDayStart(datetime);
const dateKey = dayStart.toFormat('yyyy-MM-dd');
if (!dailyStats.has(dateKey)) continue;
const dayStats = dailyStats.get(dateKey);
const props = event.event_properties || {};
const amount = Number(metric === 'refunds' ? props.PaymentAmount : props.TotalAmount || 0);
const reason = props.CancelReason || props.OrderMessage || 'No reason provided';
const orderId = props.OrderId || props.FromOrder;
dayStats.total += amount;
dayStats.count++;
dayStats.reasons[reason] = (dayStats.reasons[reason] || 0) + 1;
dayStats.items.push({
orderId,
amount,
reason,
datetime: datetime.toISO()
});
dailyStats.set(dateKey, dayStats);
}
// Convert to array and sort by date
const stats = Array.from(dailyStats.values())
.sort((a, b) => a.date.localeCompare(b.date))
.map(day => ({
...day,
[metric === 'refunds' ? 'refunds' : 'canceledOrders']: {
total: day.total,
count: day.count,
reasons: day.reasons,
items: day.items
}
}));
return stats;
}
// For other metrics, continue with existing logic
const [currentResponse, prevResponse] = await Promise.all([
this.getEvents({
...params,