Optimize RFV all-period scoring
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 51s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 51s
This commit is contained in:
@@ -197,7 +197,7 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
|
||||
const mockClient = {
|
||||
query: async (sql, params = []) => {
|
||||
calls.push({ sql, params });
|
||||
const isHistoryQuery = sql.includes('recency_days');
|
||||
const isHistoryQuery = sql.includes('= ANY');
|
||||
const isPeriodQuery = sql.includes('FROM orders');
|
||||
const referenceDate = params[0];
|
||||
|
||||
@@ -261,7 +261,8 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
|
||||
monetary: 100,
|
||||
frequency: 1,
|
||||
quantity_purchased: 1,
|
||||
last_purchase_date: '2026-06-14'
|
||||
last_purchase_date: '2026-06-14',
|
||||
recency_days: 1
|
||||
},
|
||||
{
|
||||
customer_key: '4',
|
||||
@@ -270,7 +271,8 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
|
||||
monetary: 25,
|
||||
frequency: 1,
|
||||
quantity_purchased: 1,
|
||||
last_purchase_date: '2026-06-14'
|
||||
last_purchase_date: '2026-06-14',
|
||||
recency_days: 1
|
||||
},
|
||||
{
|
||||
customer_key: 'name:Cliente Sem Fone',
|
||||
@@ -279,7 +281,8 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
|
||||
monetary: 30,
|
||||
frequency: 1,
|
||||
quantity_purchased: 1,
|
||||
last_purchase_date: '2026-06-14'
|
||||
last_purchase_date: '2026-06-14',
|
||||
recency_days: 1
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -321,3 +324,59 @@ test('getRfmAnalytics classifies period buyers by history through the selected r
|
||||
pool.connect = originalConnect;
|
||||
}
|
||||
});
|
||||
|
||||
test('getRfmAnalytics reuses period rows as RFV history for all-period ranges', async () => {
|
||||
const originalConnect = pool.connect;
|
||||
const calls = [];
|
||||
const mockClient = {
|
||||
query: async (sql, params = []) => {
|
||||
calls.push({ sql, params });
|
||||
|
||||
if (!sql.includes('FROM orders')) {
|
||||
return { rows: [] };
|
||||
}
|
||||
|
||||
return {
|
||||
rows: [
|
||||
{
|
||||
customer_key: '1',
|
||||
name: 'Cliente Frequente',
|
||||
phone: '1',
|
||||
monetary: 5000,
|
||||
frequency: 20,
|
||||
quantity_purchased: 20,
|
||||
last_purchase_date: '2026-06-14',
|
||||
recency_days: 1
|
||||
},
|
||||
{
|
||||
customer_key: 'name:Cliente Sem Fone',
|
||||
name: 'Cliente Sem Fone',
|
||||
phone: null,
|
||||
monetary: 50,
|
||||
frequency: 1,
|
||||
quantity_purchased: 1,
|
||||
last_purchase_date: '2026-01-01',
|
||||
recency_days: 165
|
||||
}
|
||||
]
|
||||
};
|
||||
},
|
||||
release: () => {}
|
||||
};
|
||||
pool.connect = async () => mockClient;
|
||||
|
||||
try {
|
||||
const result = await getRfmAnalytics({ start: '2000-01-01', end: '2026-06-15' });
|
||||
const selectCalls = calls.filter(call => call.sql.includes('FROM orders'));
|
||||
|
||||
assert.equal(selectCalls.length, 1);
|
||||
assert.match(selectCalls[0].sql, /\(\$2::date - MAX\(data_pedido_date\)\)::int/);
|
||||
assert.doesNotMatch(selectCalls[0].sql, /= ANY/);
|
||||
assert.deepEqual(selectCalls[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);
|
||||
assert.ok(result.clients.some(client => client.customerKey === 'name:Cliente Sem Fone' && client.phone === ''));
|
||||
} finally {
|
||||
pool.connect = originalConnect;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user