Optimize product detail analytics loading
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 43s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 43s
This commit is contained in:
@@ -12,6 +12,7 @@ const {
|
||||
getClientDetailsAnalytics,
|
||||
getPreviousDate,
|
||||
getProductAnalytics,
|
||||
getProductDetailsAnalytics,
|
||||
getRecencyScore,
|
||||
getRfmAnalytics,
|
||||
getRfmSegment,
|
||||
@@ -392,6 +393,97 @@ test('getProductAnalytics returns exact product rows with stock and latest price
|
||||
}
|
||||
});
|
||||
|
||||
test('getProductDetailsAnalytics returns product identity and period chart without raw order download', async () => {
|
||||
const originalQuery = pool.query;
|
||||
const calls = [];
|
||||
|
||||
pool.query = async (sql, params = []) => {
|
||||
calls.push({ sql, params });
|
||||
|
||||
if (sql.includes('selected_product AS')) {
|
||||
return {
|
||||
rows: [{
|
||||
id: '919483307',
|
||||
name: 'BASE LISA CAMISETA COR PRETO TAMANHO - G',
|
||||
price: 11.9
|
||||
}]
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
rows: [
|
||||
{
|
||||
data_pedido_date: '2026-06-01',
|
||||
date_label: '01-06-2026',
|
||||
quantity_sold: 3,
|
||||
revenue: 35.7
|
||||
},
|
||||
{
|
||||
data_pedido_date: '2026-06-02',
|
||||
date_label: '02-06-2026',
|
||||
quantity_sold: 2,
|
||||
revenue: 23.8
|
||||
}
|
||||
]
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
const details = await getProductDetailsAnalytics('919483307', { start: '2026-06-01', end: '2026-06-22' });
|
||||
|
||||
assert.equal(calls.length, 2);
|
||||
assert.match(calls[0].sql, /WITH selected_product AS/);
|
||||
assert.match(calls[0].sql, /WHERE produto_id = \$1/);
|
||||
assert.deepEqual(calls[0].params, ['919483307']);
|
||||
assert.match(calls[1].sql, /produto_id = \$1/);
|
||||
assert.match(calls[1].sql, /data_pedido_date >= \$2::date/);
|
||||
assert.match(calls[1].sql, /data_pedido_date <= \$3::date/);
|
||||
assert.deepEqual(calls[1].params, ['919483307', '2026-06-01', '2026-06-22']);
|
||||
assert.deepEqual(details.productInfo, {
|
||||
id: '919483307',
|
||||
name: 'BASE LISA CAMISETA COR PRETO TAMANHO - G',
|
||||
price: 11.9
|
||||
});
|
||||
assert.deepEqual(details.chartData, [
|
||||
{ date: '01-06-2026', value: 3 },
|
||||
{ date: '02-06-2026', value: 2 }
|
||||
]);
|
||||
assert.equal(details.totalSold, 5);
|
||||
assert.equal(details.totalRevenue, 59.5);
|
||||
} finally {
|
||||
pool.query = originalQuery;
|
||||
}
|
||||
});
|
||||
|
||||
test('getProductDetailsAnalytics keeps known products visible with zero period sales', async () => {
|
||||
const originalQuery = pool.query;
|
||||
|
||||
pool.query = async (sql) => {
|
||||
if (sql.includes('selected_product AS')) {
|
||||
return {
|
||||
rows: [{
|
||||
id: 'stock-only',
|
||||
name: 'Produto sem venda no período',
|
||||
price: 0
|
||||
}]
|
||||
};
|
||||
}
|
||||
|
||||
return { rows: [] };
|
||||
};
|
||||
|
||||
try {
|
||||
const details = await getProductDetailsAnalytics('stock-only', { start: '2026-06-01', end: '2026-06-22' });
|
||||
|
||||
assert.equal(details.productInfo.id, 'stock-only');
|
||||
assert.equal(details.totalSold, 0);
|
||||
assert.equal(details.totalRevenue, 0);
|
||||
assert.deepEqual(details.chartData, []);
|
||||
} finally {
|
||||
pool.query = originalQuery;
|
||||
}
|
||||
});
|
||||
|
||||
test('getClientAnalytics returns opaque client tokens', async () => {
|
||||
const originalQuery = pool.query;
|
||||
const calls = [];
|
||||
|
||||
Reference in New Issue
Block a user