Fix analytics date fallback and RFV scoring
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m7s

This commit is contained in:
Cauê Faleiros
2026-06-17 12:04:58 -03:00
parent f88ae2d491
commit aedfedca76
6 changed files with 131 additions and 150 deletions

View File

@@ -9,10 +9,26 @@ const {
getRfmAnalytics,
getRfmSegment,
normalizeDateParam,
ORDER_DATE_SQL,
scoreTertile
} = require('../services/analyticsService');
const { pool } = require('../db');
const compactSql = (sql) => sql.replace(/\s+/g, ' ').trim();
const expectedDateFilter = ({ startPlaceholder, endPlaceholder } = {}) => {
const filters = [`${ORDER_DATE_SQL} IS NOT NULL`];
if (startPlaceholder) {
filters.push(`${ORDER_DATE_SQL} >= ${startPlaceholder}::date`);
}
if (endPlaceholder) {
filters.push(`${ORDER_DATE_SQL} <= ${endPlaceholder}::date`);
}
return `WHERE ${filters.join(' AND ')}`;
};
test('normalizeDateParam accepts strict ISO dates', () => {
assert.equal(normalizeDateParam('2026-05-28'), '2026-05-28');
});
@@ -28,8 +44,8 @@ test('buildDateFilter builds bounded date predicates', () => {
assert.deepEqual(filter.params, ['2026-05-01', '2026-05-28']);
assert.equal(
filter.whereClause,
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date >= $1::date AND data_pedido_date <= $2::date'
compactSql(filter.whereClause),
compactSql(expectedDateFilter({ startPlaceholder: '$1', endPlaceholder: '$2' }))
);
});
@@ -38,8 +54,8 @@ test('buildDateFilter builds Hoje as an inclusive single-day date predicate', ()
assert.deepEqual(filter.params, ['2026-06-15', '2026-06-15']);
assert.equal(
filter.whereClause,
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date >= $1::date AND data_pedido_date <= $2::date'
compactSql(filter.whereClause),
compactSql(expectedDateFilter({ startPlaceholder: '$1', endPlaceholder: '$2' }))
);
});
@@ -48,8 +64,8 @@ test('buildDateFilter builds Ontem as an inclusive single-day date predicate', (
assert.deepEqual(filter.params, ['2026-06-14', '2026-06-14']);
assert.equal(
filter.whereClause,
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date >= $1::date AND data_pedido_date <= $2::date'
compactSql(filter.whereClause),
compactSql(expectedDateFilter({ startPlaceholder: '$1', endPlaceholder: '$2' }))
);
});
@@ -58,8 +74,8 @@ test('buildDateFilter builds Ultimos 7 dias as an inclusive calendar range', ()
assert.deepEqual(filter.params, ['2026-06-09', '2026-06-15']);
assert.equal(
filter.whereClause,
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date >= $1::date AND data_pedido_date <= $2::date'
compactSql(filter.whereClause),
compactSql(expectedDateFilter({ startPlaceholder: '$1', endPlaceholder: '$2' }))
);
});
@@ -68,8 +84,8 @@ test('buildDateFilter builds custom single-day ranges inclusively', () => {
assert.deepEqual(filter.params, ['2026-06-10', '2026-06-10']);
assert.equal(
filter.whereClause,
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date >= $1::date AND data_pedido_date <= $2::date'
compactSql(filter.whereClause),
compactSql(expectedDateFilter({ startPlaceholder: '$1', endPlaceholder: '$2' }))
);
});
@@ -78,11 +94,20 @@ test('buildDateFilter ignores invalid bounds', () => {
assert.deepEqual(filter.params, ['2026-05-28']);
assert.equal(
filter.whereClause,
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date <= $1::date'
compactSql(filter.whereClause),
compactSql(expectedDateFilter({ endPlaceholder: '$1' }))
);
});
test('buildDateFilter falls back to parsing the stored display date when normalized dates are missing', () => {
const filter = buildDateFilter({ start: '2026-05-01', end: '2026-05-28' });
const whereClause = compactSql(filter.whereClause);
assert.match(whereClause, /COALESCE\( data_pedido_date,/);
assert.match(whereClause, /data_pedido ~ '\^\\d\{4\}/);
assert.match(whereClause, /data_pedido ~ '\^\\d\{1,2\}/);
});
test('getPreviousDate returns the calendar day before an ISO date', () => {
assert.equal(getPreviousDate('2026-06-15'), '2026-06-14');
assert.equal(getPreviousDate('2026-03-01'), '2026-02-28');
@@ -197,7 +222,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('AND data_pedido_date <= $1::date');
const isHistoryQuery = params.length === 1;
const referenceDate = params[0];
if (isHistoryQuery) {
@@ -232,6 +257,16 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
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
}
]
};
@@ -256,6 +291,15 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
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'
}
]
};
@@ -266,8 +310,8 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
const yesterday = await getRfmAnalytics({ start: '2026-06-14', end: '2026-06-14' });
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.match(compactSql(calls[1].sql), /\(\$1::date - MAX\( COALESCE\( data_pedido_date,/);
assert.match(compactSql(calls[1].sql), /COALESCE\( data_pedido_date,.* <= \$1::date/);
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']);
@@ -275,9 +319,15 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
assert.equal(yesterday.clients[0].segmentKey, 'champions');
assert.equal(yesterday.clients[0].frequency, 1);
assert.equal(yesterday.clients[0].monetary, 100);
assert.equal(yesterday.clients.length, 2);
assert.equal(yesterday.clients.length, 3);
assert.ok(!yesterday.clients.some(client => client.phone === '2'));
assert.ok(yesterday.clients.some(client => client.phone === '4' && client.segmentKey === 'new_customers'));
assert.ok(yesterday.clients.some(client => (
client.customerKey === 'name:Cliente Sem Fone' &&
client.phone === '' &&
client.segmentKey === 'champions' &&
client.monetary === 30
)));
} finally {
pool.query = originalQuery;
}
@@ -319,7 +369,7 @@ test('getRfmAnalytics reuses period rows as RFV history for all-period ranges',
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.match(compactSql(calls[0].sql), /\(\$2::date - MAX\( COALESCE\( data_pedido_date,/);
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);