Apply recent hour pattern to client details
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m36s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m36s
This commit is contained in:
@@ -1116,7 +1116,7 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
periodFilters.push(`data_pedido_date <= $${periodParams.length}::date`);
|
||||
}
|
||||
|
||||
const [summaryResult, periodResult, patternResult] = await Promise.all([
|
||||
const [summaryResult, periodResult, weekdayPatternResult, hourPatternResult] = await Promise.all([
|
||||
pool.query(`
|
||||
${CUSTOMER_IDENTITY_CTE}
|
||||
SELECT
|
||||
@@ -1163,7 +1163,29 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
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])
|
||||
`, [resolvedCustomerKey]),
|
||||
pool.query(`
|
||||
${CUSTOMER_IDENTITY_CTE},
|
||||
order_events AS (
|
||||
SELECT DISTINCT ON (
|
||||
COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text)
|
||||
)
|
||||
created_at
|
||||
FROM identity_orders
|
||||
WHERE ${CUSTOMER_KEY_SQL} = $1
|
||||
AND data_pedido_date IS NOT NULL
|
||||
AND created_at >= NOW() - ($2::int * INTERVAL '1 day')
|
||||
ORDER BY
|
||||
COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text),
|
||||
created_at ASC NULLS LAST
|
||||
)
|
||||
SELECT
|
||||
EXTRACT(HOUR FROM created_at AT TIME ZONE 'America/Sao_Paulo')::int as hour,
|
||||
COUNT(*)::int as order_count
|
||||
FROM order_events
|
||||
GROUP BY hour
|
||||
ORDER BY hour ASC;
|
||||
`, [resolvedCustomerKey, CLIENT_PURCHASE_PATTERN_HOUR_LOOKBACK_DAYS])
|
||||
]);
|
||||
|
||||
const summary = summaryResult.rows[0] || {};
|
||||
@@ -1185,7 +1207,7 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
let periodSpent = 0;
|
||||
let periodItems = 0;
|
||||
|
||||
patternResult.rows.forEach(row => {
|
||||
weekdayPatternResult.rows.forEach(row => {
|
||||
const groupKey = getOrderGroupKey(row);
|
||||
|
||||
if (patternOrderKeys.has(groupKey)) return;
|
||||
@@ -1195,10 +1217,14 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
if (weekdayIndex !== null) {
|
||||
weekdayCounts[weekdayIndex].value += 1;
|
||||
}
|
||||
});
|
||||
|
||||
const orderHour = getHourFromTimestamp(row.created_at) ?? getHourFromTimestamp(row.data_pedido);
|
||||
if (orderHour !== null) {
|
||||
hourCounts[orderHour].value += 1;
|
||||
hourPatternResult.rows.forEach(row => {
|
||||
const orderHour = row.hour === null || row.hour === undefined ? null : Number(row.hour);
|
||||
const orderCount = toNumber(row.order_count);
|
||||
|
||||
if (orderHour !== null && Number.isInteger(orderHour) && hourCounts[orderHour]) {
|
||||
hourCounts[orderHour].value += orderCount;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1283,6 +1309,8 @@ const getClientDetailsAnalytics = async (clientToken, range = {}) => {
|
||||
chartData,
|
||||
purchaseWeekdays: weekdayCounts,
|
||||
purchaseHours: hourCounts,
|
||||
purchaseWeekdayRangeLabel: 'Todo período',
|
||||
purchaseHourRangeLabel: `Últimos ${CLIENT_PURCHASE_PATTERN_HOUR_LOOKBACK_DAYS} dias`,
|
||||
groupedOrders
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user