Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions composables/billing/importSelection.test.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
12 changes: 12 additions & 0 deletions composables/billing/importSelection.ts
Original file line number Diff line number Diff line change
@@ -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<T extends ImportSelectionRow>(
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)
}
4 changes: 3 additions & 1 deletion composables/billing/useBillingPriceImport.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
Loading