From 7b64e257fec5f571edb006a5b3d9bced4454fc12 Mon Sep 17 00:00:00 2001 From: sofyan48 Date: Wed, 25 Nov 2026 23:05:51 +0000 Subject: [PATCH] Preserve price-import selection when refreshing previews --- composables/billing/importSelection.test.ts | 22 ++++++++++++++++++++ composables/billing/importSelection.ts | 12 +++++++++++ composables/billing/useBillingPriceImport.ts | 4 +++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 composables/billing/importSelection.test.ts create mode 100644 composables/billing/importSelection.ts diff --git a/composables/billing/importSelection.test.ts b/composables/billing/importSelection.test.ts new file mode 100644 index 0000000..13c1160 --- /dev/null +++ b/composables/billing/importSelection.test.ts @@ -0,0 +1,22 @@ +import { expect, test } from 'bun:test' +import { resolveImportSelection } from './importSelection' + +const rows = [ + { row_key: 'create', action: 'create', applicable: true }, + { row_key: 'sync', action: 'update_sync', applicable: true }, + { row_key: 'manual', action: 'overwrite', applicable: true }, + { row_key: 'review', action: 'review', applicable: false } +] +const selectable = (row: typeof rows[number]) => row.applicable + +test('initial preview selects recommendations, not manual-price overwrites', () => { + expect(resolveImportSelection(rows, undefined, selectable)).toEqual(['create', 'sync']) +}) +test('refresh preserves cleared and partial selections and drops missing keys', () => { + expect(resolveImportSelection(rows, [], selectable)).toEqual([]) + expect(resolveImportSelection(rows, ['sync', 'gone', 'review'], selectable)).toEqual(['sync']) +}) +test('explicit overwrite and failed-row retry selections remain available', () => { + expect(resolveImportSelection(rows, ['manual'], selectable)).toEqual(['manual']) + expect(resolveImportSelection(rows, ['create'], selectable)).toEqual(['create']) +}) diff --git a/composables/billing/importSelection.ts b/composables/billing/importSelection.ts new file mode 100644 index 0000000..886d558 --- /dev/null +++ b/composables/billing/importSelection.ts @@ -0,0 +1,12 @@ +export type ImportSelectionRow = { row_key: string; action: string } + +// A refresh must not silently reselect a row the administrator cleared. +// Newly discovered overwrites always require an explicit selection. +export function resolveImportSelection( + rows: readonly T[], + previous: readonly string[] | undefined, + selectable: (row: T) => boolean +): string[] { + const keys = previous === undefined ? null : new Set(previous) + return rows.filter(row => selectable(row) && (keys ? keys.has(row.row_key) : row.action !== 'overwrite')).map(row => row.row_key) +} diff --git a/composables/billing/useBillingPriceImport.ts b/composables/billing/useBillingPriceImport.ts index 19bf65c..064422b 100644 --- a/composables/billing/useBillingPriceImport.ts +++ b/composables/billing/useBillingPriceImport.ts @@ -1,3 +1,4 @@ +import { resolveImportSelection } from './importSelection' import { billingImportApplySupportError, createBillingImportIdempotencyKey, billingImportStorageKey, isDefinitiveBillingImportFailure, readBillingImportPending, validateBillingImportResult, writeBillingImportPending, type BillingImportPendingApply } from '~/composables/billingImport' import { billingCatalogSources, defaultBillingImportPreferences, readBillingImportPreferences, writeBillingImportPreferences, type BillingCatalogSource, type BillingImportPreferences } from '~/composables/billingPreferences' import { computed, ref, reactive, watch, onMounted, onScopeDispose, provide, inject, type InjectionKey } from 'vue' @@ -280,6 +281,7 @@ const validationError = computed(() => { const requiresOverwrite = computed(() => (preview.value?.rows || []).some((row: any) => selected.value.includes(row.row_key) && row.action === 'overwrite')) async function previewPrices(updates = false, preferredSelection?: string[]) { if (busy.value || pendingApply.value || importUnavailable.value) return + const priorSelection = preferredSelection ?? (preview.value ? [...selected.value] : undefined) loading.value = true error.value = '' invalidatePreview() @@ -310,7 +312,7 @@ async function previewPrices(updates = false, preferredSelection?: string[]) { }) if (disposed || session !== sessionVersion || controller.signal.aborted || version !== inputVersion) return preview.value = result - selected.value = (result.rows || []).filter((row: any) => selectable(row) && (!preferredSelection || preferredSelection.includes(row.row_key))).map((row: any) => row.row_key) + selected.value = resolveImportSelection(result.rows || [], priorSelection, selectable) confirmOverwrite.value = false } catch (cause: any) { if (!disposed && session === sessionVersion && !controller.signal.aborted && version === inputVersion) {