Limit RFV history query to period buyers
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m2s

This commit is contained in:
Cauê Faleiros
2026-06-17 15:26:24 -03:00
parent 3a907b0743
commit 7764933fd9
3 changed files with 202 additions and 142 deletions

View File

@@ -192,106 +192,118 @@ test('buildRfmClients scores segments from RFM history when period totals are sm
});
test('getRfmAnalytics classifies period buyers by history through the selected range end', async () => {
const originalQuery = pool.query;
const originalConnect = pool.connect;
const calls = [];
const mockClient = {
query: async (sql, params = []) => {
calls.push({ sql, params });
const isHistoryQuery = sql.includes('recency_days');
const isPeriodQuery = sql.includes('FROM orders');
const referenceDate = params[0];
pool.query = async (sql, params) => {
calls.push({ sql, params });
const isHistoryQuery = sql.includes('recency_days');
const referenceDate = params[0];
if (!isPeriodQuery) {
return { rows: [] };
}
if (isHistoryQuery) {
return {
rows: [
{
customer_key: '1',
name: 'Cliente Ontem',
phone: '1',
monetary: 5000,
frequency: 20,
quantity_purchased: 20,
last_purchase_date: '2026-06-14',
recency_days: referenceDate === '2026-06-14' ? 0 : 1
},
{
customer_key: '2',
name: 'Cliente Antigo',
phone: '2',
monetary: 50,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-01-01',
recency_days: 164
},
{
customer_key: '3',
name: 'Cliente Medio',
phone: '3',
monetary: 100,
frequency: 2,
quantity_purchased: 2,
last_purchase_date: '2026-03-01',
recency_days: 105
},
{
customer_key: 'name:Cliente Sem Fone',
name: 'Cliente Sem Fone',
phone: null,
monetary: 1000,
frequency: 10,
quantity_purchased: 10,
last_purchase_date: '2026-06-14',
recency_days: 1
}
]
};
}
if (isHistoryQuery) {
return {
rows: [
{
customer_key: '1',
name: 'Cliente Ontem',
phone: '1',
monetary: 5000,
frequency: 20,
quantity_purchased: 20,
last_purchase_date: '2026-06-14',
recency_days: referenceDate === '2026-06-14' ? 0 : 1
},
{
customer_key: '2',
name: 'Cliente Antigo',
phone: '2',
monetary: 50,
monetary: 100,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-01-01',
recency_days: 164
last_purchase_date: '2026-06-14'
},
{
customer_key: '3',
name: 'Cliente Medio',
phone: '3',
monetary: 100,
frequency: 2,
quantity_purchased: 2,
last_purchase_date: '2026-03-01',
recency_days: 105
customer_key: '4',
name: 'Cliente Novo no Periodo',
phone: '4',
monetary: 25,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-06-14'
},
{
customer_key: 'name:Cliente Sem Fone',
name: 'Cliente Sem Fone',
phone: null,
monetary: 1000,
frequency: 10,
quantity_purchased: 10,
last_purchase_date: '2026-06-14',
recency_days: 1
monetary: 30,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-06-14'
}
]
};
}
return {
rows: [
{
customer_key: '1',
name: 'Cliente Ontem',
phone: '1',
monetary: 100,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-06-14'
},
{
customer_key: '4',
name: 'Cliente Novo no Periodo',
phone: '4',
monetary: 25,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-06-14'
},
{
customer_key: 'name:Cliente Sem Fone',
name: 'Cliente Sem Fone',
phone: null,
monetary: 30,
frequency: 1,
quantity_purchased: 1,
last_purchase_date: '2026-06-14'
}
]
};
},
release: () => {}
};
pool.connect = async () => mockClient;
try {
const sevenDays = await getRfmAnalytics({ start: '2026-06-09', end: '2026-06-15' });
const yesterday = await getRfmAnalytics({ start: '2026-06-14', end: '2026-06-14' });
const selectCalls = calls.filter(call => call.sql.includes('FROM orders'));
assert.deepEqual(calls[0].params, ['2026-06-09', '2026-06-15']);
assert.doesNotMatch(calls[0].sql, /cliente_fone IS NOT NULL/);
assert.match(calls[1].sql, /\(\$1::date - MAX\(data_pedido_date\)\)::int/);
assert.match(calls[1].sql, /data_pedido_date <= \$1::date/);
assert.doesNotMatch(calls[1].sql, /cliente_fone IS NOT NULL/);
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-14']);
assert.match(calls[1].sql, /SET LOCAL statement_timeout/);
assert.deepEqual(selectCalls[0].params, ['2026-06-09', '2026-06-15']);
assert.doesNotMatch(selectCalls[0].sql, /cliente_fone IS NOT NULL/);
assert.match(selectCalls[1].sql, /\(\$1::date - MAX\(data_pedido_date\)\)::int/);
assert.match(selectCalls[1].sql, /data_pedido_date <= \$1::date/);
assert.match(selectCalls[1].sql, /NULLIF\(cliente_fone, ''\) = ANY\(\$2::text\[\]\)/);
assert.match(selectCalls[1].sql, /COALESCE\(NULLIF\(cliente_nome, ''\), 'Cliente Desconhecido'\) = ANY\(\$3::text\[\]\)/);
assert.doesNotMatch(selectCalls[1].sql, /cliente_fone IS NOT NULL/);
assert.deepEqual(selectCalls[1].params, ['2026-06-15', ['1', '4'], ['Cliente Sem Fone']]);
assert.deepEqual(selectCalls[2].params, ['2026-06-14', '2026-06-14']);
assert.deepEqual(selectCalls[3].params, ['2026-06-14', ['1', '4'], ['Cliente Sem Fone']]);
assert.equal(sevenDays.clients[0].segmentKey, 'champions');
assert.equal(yesterday.clients[0].segmentKey, 'champions');
assert.equal(yesterday.clients[0].frequency, 1);
@@ -306,6 +318,6 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
client.monetary === 30
)));
} finally {
pool.query = originalQuery;
pool.connect = originalConnect;
}
});