Fix RFM date range consistency
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m5s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m5s
This commit is contained in:
@@ -101,6 +101,35 @@ 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);
|
||||
|
||||
return baseClients.map(client => {
|
||||
const recencyScore = scoreTertile(client.recencyDays, recencyValues, false);
|
||||
const frequencyScore = scoreTertile(client.frequency, frequencyValues, true);
|
||||
const monetaryScore = scoreTertile(client.monetary, monetaryValues, true);
|
||||
const valueScore = Math.min(3, Math.max(1, Math.round((frequencyScore + monetaryScore) / 2)));
|
||||
const segment = getRfmSegment(recencyScore, valueScore);
|
||||
|
||||
return {
|
||||
...client,
|
||||
recencyScore,
|
||||
frequencyScore,
|
||||
monetaryScore,
|
||||
valueScore,
|
||||
rfmScore: `${recencyScore}${frequencyScore}${monetaryScore}`,
|
||||
segmentKey: segment.key,
|
||||
segmentLabel: segment.label
|
||||
};
|
||||
}).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;
|
||||
});
|
||||
};
|
||||
|
||||
const getDashboardAnalytics = async (range = {}) => {
|
||||
const { params, whereClause } = buildDateFilter(range);
|
||||
const [totalsResult, salesResult, revenueResult] = await Promise.all([
|
||||
@@ -222,6 +251,14 @@ const getClientAnalytics = async (range = {}) => {
|
||||
|
||||
const getRfmAnalytics = async (range = {}) => {
|
||||
const { params, whereClause } = buildDateFilter(range);
|
||||
const queryParams = [...params];
|
||||
const normalizedEnd = normalizeDateParam(range.end);
|
||||
const recencyReferenceDate = normalizedEnd ? `$${queryParams.length + 1}::date` : 'CURRENT_DATE';
|
||||
|
||||
if (normalizedEnd) {
|
||||
queryParams.push(normalizedEnd);
|
||||
}
|
||||
|
||||
const result = await pool.query(`
|
||||
SELECT
|
||||
MAX(cliente_nome) as name,
|
||||
@@ -230,7 +267,7 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
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((CURRENT_DATE - MAX(data_pedido_date))::int, 0) as recency_days
|
||||
GREATEST((${recencyReferenceDate} - MAX(data_pedido_date))::int, 0) as recency_days
|
||||
FROM orders
|
||||
${whereClause}
|
||||
AND cliente_fone IS NOT NULL
|
||||
@@ -238,7 +275,7 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
GROUP BY cliente_fone
|
||||
ORDER BY monetary DESC
|
||||
LIMIT 1000;
|
||||
`, params);
|
||||
`, queryParams);
|
||||
|
||||
const baseClients = result.rows.map(row => ({
|
||||
name: row.name,
|
||||
@@ -250,32 +287,7 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
recencyDays: toNumber(row.recency_days)
|
||||
}));
|
||||
|
||||
const recencyValues = baseClients.map(client => client.recencyDays);
|
||||
const frequencyValues = baseClients.map(client => client.frequency);
|
||||
const monetaryValues = baseClients.map(client => client.monetary);
|
||||
|
||||
const clients = baseClients.map(client => {
|
||||
const recencyScore = scoreTertile(client.recencyDays, recencyValues, false);
|
||||
const frequencyScore = scoreTertile(client.frequency, frequencyValues, true);
|
||||
const monetaryScore = scoreTertile(client.monetary, monetaryValues, true);
|
||||
const valueScore = Math.min(3, Math.max(1, Math.round((frequencyScore + monetaryScore) / 2)));
|
||||
const segment = getRfmSegment(recencyScore, valueScore);
|
||||
|
||||
return {
|
||||
...client,
|
||||
recencyScore,
|
||||
frequencyScore,
|
||||
monetaryScore,
|
||||
valueScore,
|
||||
rfmScore: `${recencyScore}${frequencyScore}${monetaryScore}`,
|
||||
segmentKey: segment.key,
|
||||
segmentLabel: segment.label
|
||||
};
|
||||
}).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;
|
||||
});
|
||||
const clients = buildRfmClients(baseClients);
|
||||
|
||||
return {
|
||||
range: {
|
||||
@@ -293,6 +305,7 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
|
||||
module.exports = {
|
||||
buildDateFilter,
|
||||
buildRfmClients,
|
||||
buildRfmSegments,
|
||||
getRfmAnalytics,
|
||||
getRfmSegment,
|
||||
|
||||
Reference in New Issue
Block a user