Reapply "Fix RFV period classification"
This reverts commit 28af45c315.
This commit is contained in:
@@ -262,11 +262,9 @@ 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 tagReference = getPreviousDate(normalizedStart) || normalizedEnd;
|
const recencyReferenceDate = normalizedEnd ? '$1::date' : 'CURRENT_DATE';
|
||||||
const recencyReferenceDate = tagReference ? '$1::date' : 'CURRENT_DATE';
|
const historyParams = normalizedEnd ? [normalizedEnd] : [];
|
||||||
const historyParams = tagReference ? [tagReference] : [];
|
|
||||||
|
|
||||||
const [periodResult, historyResult] = await Promise.all([
|
const [periodResult, historyResult] = await Promise.all([
|
||||||
pool.query(`
|
pool.query(`
|
||||||
|
|||||||
@@ -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 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 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 endDate = params[0];
|
const referenceDate = params[0];
|
||||||
|
|
||||||
if (isHistoryQuery) {
|
if (isHistoryQuery) {
|
||||||
return {
|
return {
|
||||||
@@ -211,7 +211,7 @@ test('getRfmAnalytics groups period buyers by their RFM tag before the selected
|
|||||||
frequency: 20,
|
frequency: 20,
|
||||||
quantity_purchased: 20,
|
quantity_purchased: 20,
|
||||||
last_purchase_date: '2026-06-14',
|
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',
|
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.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-08']);
|
assert.deepEqual(calls[1].params, ['2026-06-15']);
|
||||||
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-13']);
|
assert.deepEqual(calls[3].params, ['2026-06-14']);
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user