Show RFM as customer snapshot
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 49s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 49s
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -256,6 +256,9 @@ test('getRfmAnalytics calculates recency against selected range end date', async
|
||||
assert.equal(yesterday.clients[0].recencyDays, 0);
|
||||
assert.equal(yesterday.clients[0].frequency, 1);
|
||||
assert.equal(yesterday.clients[0].monetary, 100);
|
||||
assert.equal(yesterday.clients.length, 3);
|
||||
assert.ok(yesterday.clients.some(client => client.recencyScore === 1));
|
||||
assert.ok(yesterday.clients.some(client => client.phone === '2' && client.monetary === 0 && client.frequency === 0));
|
||||
} finally {
|
||||
pool.query = originalQuery;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user