Reapply "Optimize RFV all-period analytics"

This reverts commit 19a29956d2.
This commit is contained in:
Cauê Faleiros
2026-06-17 11:16:27 -03:00
parent bb69e79af6
commit 4b3d65587e
2 changed files with 63 additions and 9 deletions

View File

@@ -197,7 +197,7 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
pool.query = async (sql, params) => {
calls.push({ sql, params });
const isHistoryQuery = sql.includes('recency_days');
const isHistoryQuery = sql.includes('AND data_pedido_date <= $1::date');
const referenceDate = params[0];
if (isHistoryQuery) {
@@ -282,3 +282,48 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
pool.query = originalQuery;
}
});
test('getRfmAnalytics reuses period rows as RFV history for all-period ranges', async () => {
const originalQuery = pool.query;
const calls = [];
pool.query = async (sql, params) => {
calls.push({ sql, params });
return {
rows: [
{
customer_key: '1',
name: 'Cliente Frequente',
phone: '1',
monetary: 5000,
frequency: 20,
quantity_purchased: 20,
last_purchase_date: '2026-06-14',
recency_days: 1
},
{
customer_key: '2',
name: 'Cliente Antigo',
phone: '2',
monetary: 50,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-01-01',
recency_days: 165
}
]
};
};
try {
const result = await getRfmAnalytics({ start: '2000-01-01', end: '2026-06-15' });
assert.equal(calls.length, 1);
assert.match(calls[0].sql, /\(\$2::date - MAX\(data_pedido_date\)\)::int/);
assert.deepEqual(calls[0].params, ['2000-01-01', '2026-06-15']);
assert.equal(result.clients.length, 2);
assert.equal(result.segments.reduce((total, segment) => total + segment.count, 0), 2);
} finally {
pool.query = originalQuery;
}
});