Fix RFV period classification
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 48s

This commit is contained in:
Cauê Faleiros
2026-06-17 10:20:12 -03:00
parent ce72231560
commit 1889c172d0
2 changed files with 7 additions and 9 deletions

View File

@@ -191,14 +191,14 @@ test('buildRfmClients scores segments from RFM history when period totals are sm
assert.equal(yesterdayBuyer.monetaryScore, 3);
});
test('getRfmAnalytics groups period buyers by their RFM tag before the selected period', async () => {
test('getRfmAnalytics classifies period buyers by history through the selected range end', async () => {
const originalQuery = pool.query;
const calls = [];
pool.query = async (sql, params) => {
calls.push({ sql, params });
const isHistoryQuery = sql.includes('recency_days');
const endDate = params[0];
const referenceDate = params[0];
if (isHistoryQuery) {
return {
@@ -211,7 +211,7 @@ test('getRfmAnalytics groups period buyers by their RFM tag before the selected
frequency: 20,
quantity_purchased: 20,
last_purchase_date: '2026-06-14',
recency_days: endDate === '2026-06-13' ? 0 : 1
recency_days: referenceDate === '2026-06-14' ? 0 : 1
},
{
customer_key: '2',
@@ -268,9 +268,9 @@ test('getRfmAnalytics groups period buyers by their RFM tag before the selected
assert.deepEqual(calls[0].params, ['2026-06-09', '2026-06-15']);
assert.match(calls[1].sql, /\(\$1::date - MAX\(data_pedido_date\)\)::int/);
assert.match(calls[1].sql, /data_pedido_date <= \$1::date/);
assert.deepEqual(calls[1].params, ['2026-06-08']);
assert.deepEqual(calls[1].params, ['2026-06-15']);
assert.deepEqual(calls[2].params, ['2026-06-14', '2026-06-14']);
assert.deepEqual(calls[3].params, ['2026-06-13']);
assert.deepEqual(calls[3].params, ['2026-06-14']);
assert.equal(sevenDays.clients[0].segmentKey, 'champions');
assert.equal(yesterday.clients[0].segmentKey, 'champions');
assert.equal(yesterday.clients[0].frequency, 1);