Add client purchase pattern analytics
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 59s

This commit is contained in:
Cauê Faleiros
2026-07-01 10:25:07 -03:00
parent b99b60b32e
commit a26bc8813e
6 changed files with 284 additions and 3 deletions

View File

@@ -11,6 +11,7 @@ const {
getClientAnalytics,
getClientDetailsAnalytics,
getClientFilterOptions,
getClientPurchasePatternAnalytics,
getDashboardAnalytics,
getPreviousDate,
getProductAnalytics,
@@ -791,6 +792,40 @@ test('getClientFilterOptions returns all distinct order metadata options', async
}
});
test('getClientPurchasePatternAnalytics returns all-time weekday and hour counts', async () => {
const originalQuery = pool.query;
pool.query = async (sql, params = []) => {
assert.deepEqual(params, []);
assert.match(sql, /order_events AS/);
assert.match(sql, /DISTINCT ON \(\s+customer_key,\s+COALESCE\(NULLIF\(pedido_id, ''\), data_pedido \|\| '_' \|\| valor_pedido::text\)\s+\)/);
assert.match(sql, /EXTRACT\(DOW FROM data_pedido_date\)::int as weekday/);
assert.match(sql, /EXTRACT\(HOUR FROM created_at AT TIME ZONE 'America\/Sao_Paulo'\)::int as hour/);
return {
rows: [
{ weekday: 1, hour: 9, order_count: 2 },
{ weekday: 5, hour: 18, order_count: 1 },
{ weekday: 5, hour: null, order_count: 1 }
]
};
};
try {
const pattern = await getClientPurchasePatternAnalytics();
assert.equal(pattern.purchaseWeekdays.length, 7);
assert.equal(pattern.purchaseHours.length, 24);
assert.deepEqual(pattern.purchaseWeekdays[1], { label: 'Seg', value: 2 });
assert.deepEqual(pattern.purchaseWeekdays[5], { label: 'Sex', value: 2 });
assert.deepEqual(pattern.purchaseHours[9], { label: '09h', value: 2 });
assert.deepEqual(pattern.purchaseHours[18], { label: '18h', value: 1 });
assert.deepEqual(pattern.purchaseHours[0], { label: '00h', value: 0 });
} finally {
pool.query = originalQuery;
}
});
test('getClientDetailsAnalytics resolves legacy name tokens to the canonical phone client', async () => {
const originalQuery = pool.query;
const calls = [];