Fix duplicate top campaign clients by phone
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m22s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m22s
This commit is contained in:
@@ -25,24 +25,6 @@ const WHATSAPP_CUSTOMER_PHONE_SQL = `
|
||||
ELSE ${NORMALIZED_CUSTOMER_PHONE_SQL}
|
||||
END
|
||||
`;
|
||||
const CANONICAL_CAMPAIGN_CUSTOMER_NAME_SQL = `
|
||||
NULLIF(TRIM(regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(LOWER(COALESCE(cliente_nome, '')), '[^[:alnum:][:space:]]+', ' ', 'g'),
|
||||
'(^|[[:space:]])[0-9]{2,14}([[:space:]]|$)',
|
||||
' ',
|
||||
'g'
|
||||
),
|
||||
'(^|[[:space:]])(ltda|me|eireli|epp)([[:space:]]|$)',
|
||||
' ',
|
||||
'g'
|
||||
),
|
||||
'[[:space:]]+',
|
||||
' ',
|
||||
'g'
|
||||
)), '')
|
||||
`;
|
||||
const CUSTOMER_IDENTITY_CTE = `
|
||||
WITH customer_phone_by_name AS (
|
||||
SELECT
|
||||
@@ -177,7 +159,6 @@ const getTopClientsForCampaign = async ({ days, limit, start, end } = {}) => {
|
||||
WITH campaign_orders AS (
|
||||
SELECT
|
||||
orders.*,
|
||||
${CANONICAL_CAMPAIGN_CUSTOMER_NAME_SQL} as canonical_customer_name,
|
||||
${WHATSAPP_CUSTOMER_PHONE_SQL} as whatsapp_phone
|
||||
FROM orders
|
||||
)
|
||||
@@ -199,7 +180,9 @@ const getTopClientsForCampaign = async ({ days, limit, start, end } = {}) => {
|
||||
WHERE data_pedido_date >= $1::date
|
||||
AND data_pedido_date <= $2::date
|
||||
AND whatsapp_phone IS NOT NULL
|
||||
GROUP BY COALESCE(canonical_customer_name, whatsapp_phone)
|
||||
-- A campaign recipient is a WhatsApp destination, so each normalized
|
||||
-- phone number must produce exactly one exported customer.
|
||||
GROUP BY whatsapp_phone
|
||||
ORDER BY total_gasto DESC
|
||||
LIMIT $3;
|
||||
`, [range.start, range.end, normalizedLimit]);
|
||||
|
||||
Reference in New Issue
Block a user