Skip to content

Commit b9b038f

Browse files
committed
perf(ZMSKVR-1022): reduce API calls by fetching available days only once
- Remove refetch on provider selection change (no longer needed since we fetch all providers) - Rename refetchAvailableDaysForSelection to fetchAvailableDaysForSelection - Update test to reflect new client-side filtering behavior
1 parent 0dd3c80 commit b9b038f

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

zmscitizenview/src/components/Appointment/AppointmentSelection.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ const showSelectionForProvider = async (provider: OfficeImpl) => {
477477
error.value = false;
478478
selectedDay.value = undefined;
479479
selectedTimeslot.value = 0;
480-
await refetchAvailableDaysForSelection();
480+
await fetchAvailableDaysForSelection();
481481
};
482482
483483
const TODAY = new Date();
@@ -761,7 +761,7 @@ const handleError = (data: any): void => {
761761
};
762762
763763
// API calls
764-
const refetchAvailableDaysForSelection = async (): Promise<void> => {
764+
const fetchAvailableDaysForSelection = async (): Promise<void> => {
765765
// Always fetch available days for ALL providers to maintain the full list
766766
const allProviderIds = (selectableProviders.value || []).map((p) =>
767767
Number(p.id)
@@ -1067,7 +1067,11 @@ function scheduleRefreshAfterProviderChange() {
10671067
const prevSelectedDay = selectedDay.value
10681068
? new Date(selectedDay.value)
10691069
: undefined;
1070-
await refetchAvailableDaysForSelection();
1070+
1071+
// No need to refetch availableDays - we already have all provider data from initial fetch
1072+
// Just update the UI based on current selection
1073+
isSwitchingProvider.value = false;
1074+
10711075
// Selection changed while awaiting? Abort this cycle.
10721076
if (selectionSnapshot !== JSON.stringify(selectedProviders.value)) {
10731077
return;

zmscitizenview/tests/unit/Appointment/AppointmentSelection.spec.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -518,50 +518,35 @@ describe("AppointmentSelection", () => {
518518
await wrapper.vm.showSelectionForProvider({ name: 'Office X', id: 10351880, address: { street: 'Test', house_number: '1' } });
519519
await nextTick();
520520

521-
// Mock the availableDays to simulate what would be fetched for selected providers
521+
// Mock availableDays where 2025-06-16 only has provider 10470, and 2025-06-17 has provider 10351880
522+
// This means when we select only 10351880, the current date (2025-06-16) becomes invalid
522523
wrapper.vm.availableDays = [
523-
{ time: '2025-06-16', providerIDs: '10351880,10470' },
524-
{ time: '2025-06-17', providerIDs: '10351880,10470' },
524+
{ time: '2025-06-16', providerIDs: '10470' },
525+
{ time: '2025-06-17', providerIDs: '10351880' },
525526
];
526527

527-
// Set current date explicitly to the one without appointments
528+
// Set current date to 2025-06-16 (only available for provider 10470)
528529
wrapper.vm.selectedDay = new Date('2025-06-16');
529530
await nextTick();
530531
await wrapper.vm.getAppointmentsOfDay('2025-06-16');
531532
await nextTick();
532533

533-
// Control subsequent refetches: first refetch returns both days, final refetch returns only 2025-06-17
534-
let refetchCall = 0;
535-
(fetchAvailableDays as Mock).mockImplementation(() => {
536-
refetchCall += 1;
537-
if (refetchCall === 1) {
538-
return Promise.resolve({
539-
availableDays: [
540-
{ time: '2025-06-16', providerIDs: '10351880,10470' },
541-
{ time: '2025-06-17', providerIDs: '10351880,10470' }
542-
]
543-
});
544-
}
545-
return Promise.resolve({
546-
availableDays: [
547-
{ time: '2025-06-17', providerIDs: '10351880' }
548-
]
549-
});
550-
});
551-
552-
// Simulate provider change to trigger nearest-date selection pipeline with deep change
553-
wrapper.vm.selectedProviders = { '10351880': false, '10470': true } as any;
534+
// Initially both providers are selected
535+
wrapper.vm.selectedProviders = { '10351880': true, '10470': true } as any;
554536
await nextTick();
555537
await flushPromises();
538+
539+
// Now deselect provider 10470, leaving only 10351880 selected
540+
// Since 2025-06-16 only has 10470, the date should snap to 2025-06-17
556541
wrapper.vm.selectedProviders = { '10351880': true, '10470': false } as any;
557542
await nextTick();
558543
await flushPromises();
559-
// Wait for debounced pipeline (150ms) + fetch/updates to complete
560-
await new Promise(r => setTimeout(r, 900));
544+
// Wait for debounced pipeline (150ms) + updates to complete
545+
await new Promise(r => setTimeout(r, 300));
561546
await nextTick();
562547
await flushPromises();
563548

564-
// Should snap to 2025-06-17 as nearest available date after provider change
549+
// Should snap to 2025-06-17 as nearest available date for selected provider
565550
expect(wrapper.vm.selectedDay).toEqual(new Date('2025-06-17'));
566551
});
567552

0 commit comments

Comments
 (0)