Revert "Fix RFV period classification"

This reverts commit 1889c172d0.
This commit is contained in:
Cauê Faleiros
2026-06-17 11:08:59 -03:00
parent 19a29956d2
commit 28af45c315
2 changed files with 9 additions and 7 deletions

View File

@@ -262,9 +262,11 @@ const getClientAnalytics = async (range = {}) => {
const getRfmAnalytics = async (range = {}) => { const getRfmAnalytics = async (range = {}) => {
const { params, whereClause } = buildDateFilter(range); const { params, whereClause } = buildDateFilter(range);
const normalizedStart = normalizeDateParam(range.start);
const normalizedEnd = normalizeDateParam(range.end); const normalizedEnd = normalizeDateParam(range.end);
const recencyReferenceDate = normalizedEnd ? '$1::date' : 'CURRENT_DATE'; const tagReference = getPreviousDate(normalizedStart) || normalizedEnd;
const historyParams = normalizedEnd ? [normalizedEnd] : []; const recencyReferenceDate = tagReference ? '$1::date' : 'CURRENT_DATE';
const historyParams = tagReference ? [tagReference] : [];
const [periodResult, historyResult] = await Promise.all([ const [periodResult, historyResult] = await Promise.all([
pool.query(` pool.query(`

View File

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