Fix analytics date fallback and RFV scoring
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m7s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m7s
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { pool } = require('../db');
|
||||
const { ORDER_DATE_SQL } = require('../sql/orderDateSql');
|
||||
|
||||
const SIZE_SUFFIX_SQL_PATTERN = '\\s+-\\s+(?:(?:PP|P|M|G|GG|XG|XGG|EG|EGG|EXG|U|UNICO|ÚNICO|\\d{2})(?:/(?:PP|P|M|G|GG|XG|XGG|EG|EGG|EXG|U|UNICO|ÚNICO|\\d{2}))*)$';
|
||||
const PRODUCT_NAME_SQL = `
|
||||
@@ -34,18 +35,18 @@ const normalizeDateParam = (value) => {
|
||||
|
||||
const buildDateFilter = ({ start, end } = {}) => {
|
||||
const params = [];
|
||||
const filters = ['data_pedido_date IS NOT NULL'];
|
||||
const filters = [`${ORDER_DATE_SQL} IS NOT NULL`];
|
||||
const normalizedStart = normalizeDateParam(start);
|
||||
const normalizedEnd = normalizeDateParam(end);
|
||||
|
||||
if (normalizedStart) {
|
||||
params.push(normalizedStart);
|
||||
filters.push(`data_pedido_date >= $${params.length}::date`);
|
||||
filters.push(`${ORDER_DATE_SQL} >= $${params.length}::date`);
|
||||
}
|
||||
|
||||
if (normalizedEnd) {
|
||||
params.push(normalizedEnd);
|
||||
filters.push(`data_pedido_date <= $${params.length}::date`);
|
||||
filters.push(`${ORDER_DATE_SQL} <= $${params.length}::date`);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -212,8 +213,8 @@ const getProductAnalytics = async (range = {}) => {
|
||||
COALESCE(SUM(quantidade), 0) as quantity_sold,
|
||||
COALESCE(SUM(quantidade * valor_unitario), 0) as revenue,
|
||||
COUNT(*)::int as order_line_count,
|
||||
MIN(data_pedido_date) as first_sale_date,
|
||||
MAX(data_pedido_date) as last_sale_date
|
||||
MIN(${ORDER_DATE_SQL}) as first_sale_date,
|
||||
MAX(${ORDER_DATE_SQL}) as last_sale_date
|
||||
FROM orders
|
||||
${whereClause}
|
||||
GROUP BY name
|
||||
@@ -242,7 +243,7 @@ const getClientAnalytics = async (range = {}) => {
|
||||
COALESCE(SUM(quantidade), 0) as quantity_purchased,
|
||||
COALESCE(SUM(quantidade * valor_unitario), 0) as total_spent,
|
||||
COUNT(DISTINCT COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text))::int as order_count,
|
||||
MAX(data_pedido_date) as last_purchase_date
|
||||
MAX(${ORDER_DATE_SQL}) as last_purchase_date
|
||||
FROM orders
|
||||
${whereClause}
|
||||
GROUP BY customer_key
|
||||
@@ -277,8 +278,8 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
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((${periodRecencyReferenceDate} - MAX(data_pedido_date))::int, 0) as recency_days
|
||||
MAX(${ORDER_DATE_SQL}) as last_purchase_date,
|
||||
GREATEST((${periodRecencyReferenceDate} - MAX(${ORDER_DATE_SQL}))::int, 0) as recency_days
|
||||
FROM orders
|
||||
${whereClause}
|
||||
GROUP BY customer_key
|
||||
@@ -293,11 +294,11 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
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
|
||||
MAX(${ORDER_DATE_SQL}) as last_purchase_date,
|
||||
GREATEST((${recencyReferenceDate} - MAX(${ORDER_DATE_SQL}))::int, 0) as recency_days
|
||||
FROM orders
|
||||
WHERE data_pedido_date IS NOT NULL
|
||||
AND data_pedido_date <= ${recencyReferenceDate}
|
||||
WHERE ${ORDER_DATE_SQL} IS NOT NULL
|
||||
AND ${ORDER_DATE_SQL} <= ${recencyReferenceDate}
|
||||
GROUP BY customer_key;
|
||||
`, historyParams);
|
||||
|
||||
@@ -383,5 +384,6 @@ module.exports = {
|
||||
getDashboardAnalytics,
|
||||
getProductAnalytics,
|
||||
normalizeDateParam,
|
||||
ORDER_DATE_SQL,
|
||||
scoreTertile
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user