Fix product performance grouped JSON build

This commit is contained in:
M_Kececi
2026-07-05 15:30:25 +03:00
parent c602e1e119
commit 5b512fd30a
4 changed files with 777 additions and 31 deletions
@@ -1706,6 +1706,12 @@ const activeSalesBreakdownPagination = computed({
}
})
const activeBackendGroupedSortSignature = computed(() => {
if (!backendGroupedSupportedTab(activeTab.value)) return ''
const state = tablePagination[activeTab.value] || {}
return `${activeTab.value}|${state.sortBy || ''}|${state.descending !== false ? 1 : 0}`
})
function normalizeTablePagination (value) {
return {
page: Number(value?.page || 1),
@@ -3014,6 +3020,7 @@ function groupedBucketSortValue (sourceRows, sortBy) {
function sortProductGroupedTableRows (tableRows, sortBy, descending) {
const source = Array.isArray(tableRows) ? tableRows : []
if (backendGroupedSupportedTab(activeTab.value)) return source
if (!sortBy || !source.some(row => row?.__group)) {
return sortFlatTableRows(source, sortBy, descending)
}
@@ -4619,14 +4626,17 @@ async function loadBackendGroupedRows (options = {}) {
backendGroupedLoading.value = true
const filtersKey = JSON.stringify(filterState.filters)
const expandThroughLevel = selectedExpandThroughLevel.value
const requestKey = `${tabKey}|${expandThroughLevel}|${selectedDetailMainGroup.value || ''}|${filtersKey}`
const sortState = tableSortState(tabKey)
const requestKey = `${tabKey}|${expandThroughLevel}|${selectedDetailMainGroup.value || ''}|${filtersKey}|${sortState.sortBy}|${sortState.descending ? 1 : 0}`
const params = {
mode: tabKey,
groupLevels: activeGroupLevels.value.map(level => level.key),
expandedKeys: activeExpandedGroupKeys(),
expandThroughLevel,
limit: tabKey === 'products' || tabKey === 'product_detail' || tabKey === 'idle' ? productKpiFetchLimit : 50000,
filters: filterState.filters
filters: filterState.filters,
sortBy: sortState.sortBy,
descending: sortState.descending
}
if (tabKey === 'product_detail') {
params.urunAnaGrubu = selectedDetailMainGroup.value
@@ -4635,7 +4645,8 @@ async function loadBackendGroupedRows (options = {}) {
const startedAt = performance.now()
const rows = await performanceStore.fetchGroupedRows(params, { force: options.force === true })
const currentFilterState = backendGroupedFilterState(activeTab.value)
const currentKey = `${activeTab.value}|${selectedExpandThroughLevel.value}|${selectedDetailMainGroup.value || ''}|${JSON.stringify(currentFilterState.filters)}`
const currentSortState = tableSortState(activeTab.value)
const currentKey = `${activeTab.value}|${selectedExpandThroughLevel.value}|${selectedDetailMainGroup.value || ''}|${JSON.stringify(currentFilterState.filters)}|${currentSortState.sortBy}|${currentSortState.descending ? 1 : 0}`
if (requestKey !== currentKey) return
const normalizedRows = Array.isArray(rows) ? rows.map(row => normalizeGroupedDisplayRow(tabKey, row)) : []
backendGroupedRows.value = {
@@ -4657,17 +4668,7 @@ async function loadBackendGroupedRows (options = {}) {
function normalizeGroupedDisplayRow (tabKey, row) {
const nextRow = { ...row }
normalizeGroupedProductHierarchyValues(nextRow)
if (tabKey === 'order_product_customers' || tabKey === 'order_market_details') {
return withOrderPeriodScores(nextRow)
}
const next = withPeriodScores(withGeneralMargins(withProductMargins(nextRow)))
if (tabKey !== 'products' && tabKey !== 'product_detail' && tabKey !== 'idle') {
next.customer_score_90d = Number(next.customer_score_90d || customerSalesPeriodScore(periodMetricSource(next, '90d')))
next.customer_score_180d = Number(next.customer_score_180d || customerSalesPeriodScore(periodMetricSource(next, '180d')))
next.customer_score_365d = Number(next.customer_score_365d || customerSalesPeriodScore(periodMetricSource(next, '365d')))
next.customer_score_total = Number(next.customer_score_total || customerSalesPeriodScore(periodMetricSource(next, 'total')))
}
return next
return nextRow
}
function normalizeGroupedProductHierarchyValues (row) {
@@ -5048,6 +5049,11 @@ watch(activeTab, () => {
setupTopScrollbarSync()
})
watch(activeBackendGroupedSortSignature, () => {
if (!backendGroupedSupportedTab(activeTab.value) || loading.value) return
scheduleLoadBackendGroupedRows()
})
watch(activeDisplayedTableRows, () => {
setupTopScrollbarSync()
}, { flush: 'post' })