Use all-time client purchase patterns
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m25s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m25s
This commit is contained in:
@@ -910,7 +910,7 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
periodFilters.push(`data_pedido_date <= $${periodParams.length}::date`);
|
||||
}
|
||||
|
||||
const [summaryResult, periodResult] = await Promise.all([
|
||||
const [summaryResult, periodResult, patternResult] = await Promise.all([
|
||||
pool.query(`
|
||||
${CUSTOMER_IDENTITY_CTE}
|
||||
SELECT
|
||||
@@ -944,7 +944,20 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
FROM identity_orders
|
||||
WHERE ${periodFilters.join(' AND ')}
|
||||
ORDER BY data_pedido_date DESC, COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text) DESC;
|
||||
`, periodParams)
|
||||
`, periodParams),
|
||||
pool.query(`
|
||||
${CUSTOMER_IDENTITY_CTE}
|
||||
SELECT
|
||||
data_pedido,
|
||||
data_pedido_date,
|
||||
created_at,
|
||||
pedido_id,
|
||||
valor_pedido
|
||||
FROM identity_orders
|
||||
WHERE ${CUSTOMER_KEY_SQL} = $1
|
||||
AND data_pedido_date IS NOT NULL
|
||||
ORDER BY data_pedido_date DESC, COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text) DESC;
|
||||
`, [resolvedCustomerKey])
|
||||
]);
|
||||
|
||||
const summary = summaryResult.rows[0] || {};
|
||||
@@ -962,25 +975,29 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
let periodSpent = 0;
|
||||
let periodItems = 0;
|
||||
|
||||
patternResult.rows.forEach(row => {
|
||||
const groupKey = getOrderGroupKey(row);
|
||||
|
||||
if (patternOrderKeys.has(groupKey)) return;
|
||||
patternOrderKeys.add(groupKey);
|
||||
|
||||
const weekdayIndex = getWeekdayIndex(row.data_pedido_date) ?? getWeekdayIndex(row.data_pedido);
|
||||
if (weekdayIndex !== null) {
|
||||
weekdayCounts[weekdayIndex].value += 1;
|
||||
}
|
||||
|
||||
const orderHour = getHourFromTimestamp(row.created_at) ?? getHourFromTimestamp(row.data_pedido);
|
||||
if (orderHour !== null) {
|
||||
hourCounts[orderHour].value += 1;
|
||||
}
|
||||
});
|
||||
|
||||
periodResult.rows.forEach(row => {
|
||||
const itemRevenue = toNumber(row.quantidade) * toNumber(row.valor_unitario);
|
||||
const dateKey = getDateOnly(row.data_pedido_date) || getDateOnly(row.data_pedido) || '';
|
||||
const dateLabel = row.data_pedido || dateKey;
|
||||
const groupKey = getOrderGroupKey(row);
|
||||
|
||||
if (!patternOrderKeys.has(groupKey)) {
|
||||
patternOrderKeys.add(groupKey);
|
||||
const weekdayIndex = getWeekdayIndex(row.data_pedido_date) ?? getWeekdayIndex(row.data_pedido);
|
||||
if (weekdayIndex !== null) {
|
||||
weekdayCounts[weekdayIndex].value += 1;
|
||||
}
|
||||
|
||||
const orderHour = getHourFromTimestamp(row.created_at) ?? getHourFromTimestamp(row.data_pedido);
|
||||
if (orderHour !== null) {
|
||||
hourCounts[orderHour].value += 1;
|
||||
}
|
||||
}
|
||||
|
||||
periodSpent += itemRevenue;
|
||||
periodItems += toNumber(row.quantidade);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user