Show RFM as customer snapshot
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 49s

This commit is contained in:
Cauê Faleiros
2026-06-15 12:36:04 -03:00
parent 76b1c545f1
commit 1956e62697
2 changed files with 17 additions and 14 deletions

View File

@@ -292,7 +292,13 @@ const getRfmAnalytics = async (range = {}) => {
`, historyParams)
]);
const historyClients = buildRfmClients(historyResult.rows.map(row => ({
const periodByPhone = new Map(periodResult.rows.map(row => [row.phone, {
monetary: toNumber(row.monetary),
frequency: toNumber(row.frequency),
quantityPurchased: toNumber(row.quantity_purchased)
}]));
const clients = buildRfmClients(historyResult.rows.map(row => ({
name: row.name,
phone: row.phone,
monetary: toNumber(row.monetary),
@@ -300,22 +306,16 @@ const getRfmAnalytics = async (range = {}) => {
quantityPurchased: toNumber(row.quantity_purchased),
lastPurchaseDate: row.last_purchase_date,
recencyDays: toNumber(row.recency_days)
})));
const rfmByPhone = new Map(historyClients.map(client => [client.phone, client]));
const clients = periodResult.rows.map(row => {
const rfmClient = rfmByPhone.get(row.phone);
}))).map(client => {
const periodClient = periodByPhone.get(client.phone);
return {
...rfmClient,
name: row.name,
phone: row.phone,
monetary: toNumber(row.monetary),
frequency: toNumber(row.frequency),
quantityPurchased: toNumber(row.quantity_purchased),
lastPurchaseDate: row.last_purchase_date
...client,
monetary: periodClient?.monetary || 0,
frequency: periodClient?.frequency || 0,
quantityPurchased: periodClient?.quantityPurchased || 0
};
}).filter(client => client.segmentKey).sort((a, b) => {
}).sort((a, b) => {
if (b.recencyScore !== a.recencyScore) return b.recencyScore - a.recencyScore;
if (b.valueScore !== a.valueScore) return b.valueScore - a.valueScore;
return b.monetary - a.monetary;