Stabilize RFM segment scoring across date filters
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 51s

This commit is contained in:
Cauê Faleiros
2026-06-15 11:03:11 -03:00
parent fc7a50fddf
commit de6a263ec9
2 changed files with 94 additions and 17 deletions

View File

@@ -103,13 +103,15 @@ const buildRfmSegments = (clients) => {
const buildRfmClients = (baseClients) => {
const recencyValues = baseClients.map(client => client.recencyDays);
const frequencyValues = baseClients.map(client => client.frequency);
const monetaryValues = baseClients.map(client => client.monetary);
const frequencyValues = baseClients.map(client => client.rfmFrequency ?? client.frequency);
const monetaryValues = baseClients.map(client => client.rfmMonetary ?? client.monetary);
return baseClients.map(client => {
const frequencyForScore = client.rfmFrequency ?? client.frequency;
const monetaryForScore = client.rfmMonetary ?? client.monetary;
const recencyScore = scoreTertile(client.recencyDays, recencyValues, false);
const frequencyScore = scoreTertile(client.frequency, frequencyValues, true);
const monetaryScore = scoreTertile(client.monetary, monetaryValues, true);
const frequencyScore = scoreTertile(frequencyForScore, frequencyValues, true);
const monetaryScore = scoreTertile(monetaryForScore, monetaryValues, true);
const valueScore = Math.min(3, Math.max(1, Math.round((frequencyScore + monetaryScore) / 2)));
const segment = getRfmSegment(recencyScore, valueScore);
@@ -260,19 +262,45 @@ const getRfmAnalytics = async (range = {}) => {
}
const result = await pool.query(`
WITH period_clients AS (
SELECT
MAX(cliente_nome) as name,
cliente_fone as phone,
COALESCE(SUM(quantidade * valor_unitario), 0) as monetary,
COUNT(DISTINCT COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text))::int as frequency,
COALESCE(SUM(quantidade), 0) as quantity_purchased,
MAX(data_pedido_date) as last_purchase_date
FROM orders
${whereClause}
AND cliente_fone IS NOT NULL
AND cliente_fone != ''
GROUP BY cliente_fone
),
rfm_clients AS (
SELECT
cliente_fone as phone,
COALESCE(SUM(quantidade * valor_unitario), 0) as rfm_monetary,
COUNT(DISTINCT COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text))::int as rfm_frequency,
GREATEST((${recencyReferenceDate} - MAX(data_pedido_date))::int, 0) as recency_days
FROM orders
WHERE data_pedido_date IS NOT NULL
AND data_pedido_date <= ${recencyReferenceDate}
AND cliente_fone IS NOT NULL
AND cliente_fone != ''
GROUP BY cliente_fone
)
SELECT
MAX(cliente_nome) as name,
cliente_fone as phone,
COALESCE(SUM(quantidade * valor_unitario), 0) as monetary,
COUNT(DISTINCT COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text))::int as frequency,
COALESCE(SUM(quantidade), 0) as quantity_purchased,
MAX(data_pedido_date) as last_purchase_date,
GREATEST((${recencyReferenceDate} - MAX(data_pedido_date))::int, 0) as recency_days
FROM orders
${whereClause}
AND cliente_fone IS NOT NULL
AND cliente_fone != ''
GROUP BY cliente_fone
period_clients.name,
period_clients.phone,
period_clients.monetary,
period_clients.frequency,
period_clients.quantity_purchased,
period_clients.last_purchase_date,
rfm_clients.rfm_monetary,
rfm_clients.rfm_frequency,
rfm_clients.recency_days
FROM period_clients
INNER JOIN rfm_clients ON rfm_clients.phone = period_clients.phone
ORDER BY monetary DESC
LIMIT 1000;
`, queryParams);
@@ -284,7 +312,9 @@ const getRfmAnalytics = async (range = {}) => {
frequency: toNumber(row.frequency),
quantityPurchased: toNumber(row.quantity_purchased),
lastPurchaseDate: row.last_purchase_date,
recencyDays: toNumber(row.recency_days)
recencyDays: toNumber(row.recency_days),
rfmFrequency: toNumber(row.rfm_frequency),
rfmMonetary: toNumber(row.rfm_monetary)
}));
const clients = buildRfmClients(baseClients);