Smooth analytics page reloads
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 34s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 34s
This commit is contained in:
@@ -155,6 +155,23 @@ const initDB = async () => {
|
||||
|
||||
await pool.query(`CREATE INDEX IF NOT EXISTS idx_stock_campaign_queue_status ON stock_campaign_queue (status);`);
|
||||
await pool.query(`CREATE INDEX IF NOT EXISTS idx_orders_cliente_fone ON orders (cliente_fone);`);
|
||||
await pool.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_normalized_cliente_nome
|
||||
ON orders ((NULLIF(LOWER(TRIM(regexp_replace(COALESCE(cliente_nome, ''), '\\s+', ' ', 'g'))), '')));
|
||||
`).catch(err => {
|
||||
console.error('Notice: Could not create normalized client name index:', err.message);
|
||||
});
|
||||
await pool.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_normalized_cliente_nome_phone_date
|
||||
ON orders (
|
||||
(NULLIF(LOWER(TRIM(regexp_replace(COALESCE(cliente_nome, ''), '\\s+', ' ', 'g'))), '')),
|
||||
data_pedido_date DESC,
|
||||
id DESC
|
||||
)
|
||||
WHERE NULLIF(cliente_fone, '') IS NOT NULL;
|
||||
`).catch(err => {
|
||||
console.error('Notice: Could not create normalized client phone lookup index:', err.message);
|
||||
});
|
||||
await pool.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_customer_key_date
|
||||
ON orders (
|
||||
|
||||
@@ -17,31 +17,28 @@ const PRODUCT_NAME_SQL = `
|
||||
const NORMALIZED_CUSTOMER_NAME_SQL = "NULLIF(LOWER(TRIM(regexp_replace(COALESCE(cliente_nome, ''), '\\s+', ' ', 'g'))), '')";
|
||||
// Phone-less historical rows must follow the later known phone for the same client name.
|
||||
const CUSTOMER_IDENTITY_CTE = `
|
||||
WITH order_identity AS (
|
||||
WITH customer_phone_by_name AS (
|
||||
SELECT
|
||||
orders.*,
|
||||
${NORMALIZED_CUSTOMER_NAME_SQL} as normalized_customer_name
|
||||
FROM orders
|
||||
),
|
||||
customer_phone_by_name AS (
|
||||
SELECT
|
||||
normalized_customer_name,
|
||||
${NORMALIZED_CUSTOMER_NAME_SQL} as normalized_customer_name,
|
||||
(ARRAY_AGG(NULLIF(cliente_fone, '') ORDER BY data_pedido_date DESC NULLS LAST, id DESC)
|
||||
FILTER (WHERE NULLIF(cliente_fone, '') IS NOT NULL))[1] as canonical_phone
|
||||
FROM order_identity
|
||||
WHERE normalized_customer_name IS NOT NULL
|
||||
)[1] as canonical_phone
|
||||
FROM orders
|
||||
WHERE NULLIF(cliente_fone, '') IS NOT NULL
|
||||
AND ${NORMALIZED_CUSTOMER_NAME_SQL} IS NOT NULL
|
||||
GROUP BY normalized_customer_name
|
||||
),
|
||||
identity_orders AS (
|
||||
SELECT
|
||||
order_identity.*,
|
||||
orders.*,
|
||||
${NORMALIZED_CUSTOMER_NAME_SQL} as normalized_customer_name,
|
||||
COALESCE(
|
||||
NULLIF(order_identity.cliente_fone, ''),
|
||||
NULLIF(orders.cliente_fone, ''),
|
||||
customer_phone_by_name.canonical_phone,
|
||||
'name:' || COALESCE(NULLIF(order_identity.cliente_nome, ''), 'Cliente Desconhecido')
|
||||
'name:' || COALESCE(NULLIF(orders.cliente_nome, ''), 'Cliente Desconhecido')
|
||||
) as customer_key
|
||||
FROM order_identity
|
||||
LEFT JOIN customer_phone_by_name USING (normalized_customer_name)
|
||||
FROM orders
|
||||
LEFT JOIN customer_phone_by_name
|
||||
ON customer_phone_by_name.normalized_customer_name = ${NORMALIZED_CUSTOMER_NAME_SQL}
|
||||
)
|
||||
`;
|
||||
const CUSTOMER_KEY_SQL = 'customer_key';
|
||||
|
||||
@@ -601,10 +601,10 @@ test('getClientAnalytics groups phone-less history through the customer identity
|
||||
assert.equal(clients.length, 1);
|
||||
assert.equal(clients[0].customerKey, '(16) 99999-9999');
|
||||
assert.equal(clients[0].phone, '(16) 99999-9999');
|
||||
assert.match(calls[0].sql, /WITH order_identity AS/);
|
||||
assert.match(calls[0].sql, /customer_phone_by_name AS/);
|
||||
assert.match(calls[0].sql, /ARRAY_AGG\(NULLIF\(cliente_fone, ''\) ORDER BY data_pedido_date DESC NULLS LAST, id DESC\)/);
|
||||
assert.match(calls[0].sql, /NULLIF\(order_identity\.cliente_fone, ''\),\s+customer_phone_by_name\.canonical_phone/s);
|
||||
assert.match(calls[0].sql, /WHERE NULLIF\(cliente_fone, ''\) IS NOT NULL/);
|
||||
assert.match(calls[0].sql, /NULLIF\(orders\.cliente_fone, ''\),\s+customer_phone_by_name\.canonical_phone/s);
|
||||
assert.match(calls[0].sql, /FROM identity_orders/);
|
||||
assert.match(calls[0].sql, /GROUP BY customer_key/);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user