Score RFV clients without phone
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m46s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m46s
This commit is contained in:
@@ -7,6 +7,7 @@ const PRODUCT_NAME_SQL = `
|
||||
ELSE NULLIF(TRIM(regexp_replace(split_part(COALESCE(produto_descricao, 'Unknown'), ' TAMANHO', 1), '${SIZE_SUFFIX_SQL_PATTERN}', '', 'i')), '')
|
||||
END
|
||||
`;
|
||||
const CUSTOMER_KEY_SQL = "COALESCE(NULLIF(cliente_fone, ''), 'name:' || COALESCE(NULLIF(cliente_nome, ''), 'Cliente Desconhecido'))";
|
||||
|
||||
const normalizeDateParam = (value) => {
|
||||
if (!value) return null;
|
||||
@@ -235,7 +236,8 @@ const getClientAnalytics = async (range = {}) => {
|
||||
const { params, whereClause } = buildDateFilter(range);
|
||||
const result = await pool.query(`
|
||||
SELECT
|
||||
COALESCE(NULLIF(cliente_nome, ''), 'Cliente Desconhecido') as name,
|
||||
${CUSTOMER_KEY_SQL} as customer_key,
|
||||
MAX(COALESCE(NULLIF(cliente_nome, ''), 'Cliente Desconhecido')) as name,
|
||||
MAX(NULLIF(cliente_fone, '')) as phone,
|
||||
COALESCE(SUM(quantidade), 0) as quantity_purchased,
|
||||
COALESCE(SUM(quantidade * valor_unitario), 0) as total_spent,
|
||||
@@ -243,11 +245,12 @@ const getClientAnalytics = async (range = {}) => {
|
||||
MAX(data_pedido_date) as last_purchase_date
|
||||
FROM orders
|
||||
${whereClause}
|
||||
GROUP BY COALESCE(NULLIF(cliente_nome, ''), 'Cliente Desconhecido')
|
||||
GROUP BY customer_key
|
||||
ORDER BY total_spent DESC;
|
||||
`, params);
|
||||
|
||||
return result.rows.map(row => ({
|
||||
customerKey: row.customer_key,
|
||||
name: row.name,
|
||||
phone: row.phone || '',
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
@@ -268,23 +271,23 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
const [periodResult, historyResult] = await Promise.all([
|
||||
pool.query(`
|
||||
SELECT
|
||||
${CUSTOMER_KEY_SQL} as customer_key,
|
||||
MAX(cliente_nome) as name,
|
||||
cliente_fone as phone,
|
||||
MAX(NULLIF(cliente_fone, '')) as phone,
|
||||
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
|
||||
FROM orders
|
||||
${whereClause}
|
||||
AND cliente_fone IS NOT NULL
|
||||
AND cliente_fone != ''
|
||||
GROUP BY cliente_fone
|
||||
GROUP BY customer_key
|
||||
ORDER BY monetary DESC;
|
||||
`, params),
|
||||
pool.query(`
|
||||
SELECT
|
||||
${CUSTOMER_KEY_SQL} as customer_key,
|
||||
MAX(cliente_nome) as name,
|
||||
cliente_fone as phone,
|
||||
MAX(NULLIF(cliente_fone, '')) as phone,
|
||||
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,
|
||||
@@ -293,30 +296,30 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
FROM orders
|
||||
WHERE data_pedido_date IS NOT NULL
|
||||
AND data_pedido_date <= ${recencyReferenceDate}
|
||||
AND cliente_fone IS NOT NULL
|
||||
AND cliente_fone != ''
|
||||
GROUP BY cliente_fone;
|
||||
GROUP BY customer_key;
|
||||
`, historyParams)
|
||||
]);
|
||||
|
||||
const historyClients = buildRfmClients(historyResult.rows.map(row => ({
|
||||
customerKey: row.customer_key,
|
||||
name: row.name,
|
||||
phone: row.phone,
|
||||
phone: row.phone || '',
|
||||
monetary: toNumber(row.monetary),
|
||||
frequency: toNumber(row.frequency),
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
lastPurchaseDate: row.last_purchase_date,
|
||||
recencyDays: toNumber(row.recency_days)
|
||||
})));
|
||||
const tagsByPhone = new Map(historyClients.map(client => [client.phone, client]));
|
||||
const tagsByCustomerKey = new Map(historyClients.map(client => [client.customerKey, client]));
|
||||
|
||||
const clients = periodResult.rows.map(row => {
|
||||
const taggedClient = tagsByPhone.get(row.phone);
|
||||
const taggedClient = tagsByCustomerKey.get(row.customer_key);
|
||||
if (!taggedClient) {
|
||||
const newCustomerSegment = getRfmSegment(3, 1);
|
||||
return {
|
||||
customerKey: row.customer_key,
|
||||
name: row.name,
|
||||
phone: row.phone,
|
||||
phone: row.phone || '',
|
||||
monetary: toNumber(row.monetary),
|
||||
frequency: toNumber(row.frequency),
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
@@ -334,8 +337,9 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
|
||||
return {
|
||||
...taggedClient,
|
||||
customerKey: row.customer_key,
|
||||
name: row.name,
|
||||
phone: row.phone,
|
||||
phone: row.phone || '',
|
||||
monetary: toNumber(row.monetary),
|
||||
frequency: toNumber(row.frequency),
|
||||
quantityPurchased: toNumber(row.quantity_purchased),
|
||||
|
||||
Reference in New Issue
Block a user