Aggregate RFM period buyers by prior tag
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 55s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 55s
This commit is contained in:
@@ -53,6 +53,15 @@ const buildDateFilter = ({ start, end } = {}) => {
|
||||
};
|
||||
};
|
||||
|
||||
const getPreviousDate = (value) => {
|
||||
const normalizedDate = normalizeDateParam(value);
|
||||
if (!normalizedDate) return null;
|
||||
|
||||
const date = new Date(`${normalizedDate}T00:00:00.000Z`);
|
||||
date.setUTCDate(date.getUTCDate() - 1);
|
||||
return date.toISOString().slice(0, 10);
|
||||
};
|
||||
|
||||
const toNumber = (value) => Number(value || 0);
|
||||
|
||||
const RFM_SEGMENTS = {
|
||||
@@ -253,9 +262,11 @@ const getClientAnalytics = async (range = {}) => {
|
||||
|
||||
const getRfmAnalytics = async (range = {}) => {
|
||||
const { params, whereClause } = buildDateFilter(range);
|
||||
const normalizedStart = normalizeDateParam(range.start);
|
||||
const normalizedEnd = normalizeDateParam(range.end);
|
||||
const recencyReferenceDate = normalizedEnd ? '$1::date' : 'CURRENT_DATE';
|
||||
const historyParams = normalizedEnd ? [normalizedEnd] : [];
|
||||
const tagReference = getPreviousDate(normalizedStart) || normalizedEnd;
|
||||
const recencyReferenceDate = tagReference ? '$1::date' : 'CURRENT_DATE';
|
||||
const historyParams = tagReference ? [tagReference] : [];
|
||||
|
||||
const [periodResult, historyResult] = await Promise.all([
|
||||
pool.query(`
|
||||
@@ -292,13 +303,7 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
`, historyParams)
|
||||
]);
|
||||
|
||||
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 => ({
|
||||
const historyClients = buildRfmClients(historyResult.rows.map(row => ({
|
||||
name: row.name,
|
||||
phone: row.phone,
|
||||
monetary: toNumber(row.monetary),
|
||||
@@ -306,14 +311,39 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
lastPurchaseDate: row.last_purchase_date,
|
||||
recencyDays: toNumber(row.recency_days)
|
||||
}))).map(client => {
|
||||
const periodClient = periodByPhone.get(client.phone);
|
||||
})));
|
||||
const tagsByPhone = new Map(historyClients.map(client => [client.phone, client]));
|
||||
|
||||
const clients = periodResult.rows.map(row => {
|
||||
const taggedClient = tagsByPhone.get(row.phone);
|
||||
if (!taggedClient) {
|
||||
const newCustomerSegment = getRfmSegment(3, 1);
|
||||
return {
|
||||
name: row.name,
|
||||
phone: row.phone,
|
||||
monetary: toNumber(row.monetary),
|
||||
frequency: toNumber(row.frequency),
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
lastPurchaseDate: row.last_purchase_date,
|
||||
recencyDays: 0,
|
||||
recencyScore: 3,
|
||||
frequencyScore: 1,
|
||||
monetaryScore: 1,
|
||||
valueScore: 1,
|
||||
rfmScore: '311',
|
||||
segmentKey: newCustomerSegment.key,
|
||||
segmentLabel: newCustomerSegment.label
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...client,
|
||||
monetary: periodClient?.monetary || 0,
|
||||
frequency: periodClient?.frequency || 0,
|
||||
quantityPurchased: periodClient?.quantityPurchased || 0
|
||||
...taggedClient,
|
||||
name: row.name,
|
||||
phone: row.phone,
|
||||
monetary: toNumber(row.monetary),
|
||||
frequency: toNumber(row.frequency),
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
lastPurchaseDate: row.last_purchase_date
|
||||
};
|
||||
}).sort((a, b) => {
|
||||
if (b.recencyScore !== a.recencyScore) return b.recencyScore - a.recencyScore;
|
||||
@@ -339,6 +369,7 @@ module.exports = {
|
||||
buildDateFilter,
|
||||
buildRfmClients,
|
||||
buildRfmSegments,
|
||||
getPreviousDate,
|
||||
getRfmAnalytics,
|
||||
getRfmSegment,
|
||||
getClientAnalytics,
|
||||
|
||||
Reference in New Issue
Block a user