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
9 changes: 5 additions & 4 deletions src/lib/api/wcaAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('wcaAPI', () => {
body: JSON.stringify({
formatVersion: '2.1.1',
persons: [{ registrantId: 1 }],
id: 'Comp',
}),
})
);
Expand Down Expand Up @@ -158,7 +159,7 @@ describe('wcaAPI', () => {
'https://wca.test/api/v0/competitions/Comp/wcif',
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({ formatVersion: '2.0', name: 'New' }),
body: JSON.stringify({ formatVersion: '2.0', name: 'New', id: 'Comp' }),
})
);
});
Expand Down Expand Up @@ -195,16 +196,16 @@ describe('wcaAPI', () => {
);
});

it('patches WCIF to the unchanged update endpoint', async () => {
it('patches WCIF with the required id and fetched format version', async () => {
mockFetch({ json: vi.fn().mockResolvedValue({ id: 'Comp' }) });

await patchWcif('Comp', { formatVersion: '2.0', name: 'Updated' } as any);
await patchWcif('Comp', { formatVersion: '2.2', name: 'Updated' });

expect(globalThis.fetch).toHaveBeenCalledWith(
'https://wca.test/api/v0/competitions/Comp/wcif',
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({ formatVersion: '2.0', name: 'Updated' }),
body: JSON.stringify({ formatVersion: '2.2', name: 'Updated', id: 'Comp' }),
})
);
});
Expand Down
12 changes: 8 additions & 4 deletions src/lib/api/wcaAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export const getWcif = (competitionId: string): Promise<Competition> =>

export const patchWcif = (
competitionId: string,
wcif: Partial<Competition>
wcif: Partial<Competition> & Pick<Competition, 'formatVersion'>
): Promise<Competition> =>
wcaApiFetch(wcifPath(competitionId), {
method: 'PATCH',
body: JSON.stringify(withoutV2PersonalBests(wcif)),
body: JSON.stringify(withoutV2PersonalBests({ ...wcif, id: competitionId })),
});

export const checkWcif = (wcif: Competition): Promise<void> =>
Expand All @@ -93,8 +93,12 @@ export const saveWcifChanges = (
(key) => previousWcif[key as keyof Competition] !== newWcif[key as keyof Competition]
);
if (keysDiff.length === 0) return Promise.resolve();
const keysForPatch = ['formatVersion', ...keysDiff];
return patchWcif(newWcif.id, pick(newWcif, keysForPatch));
const changedWcifKeys = keysDiff.filter((key) => key !== 'formatVersion');
const changes = {
formatVersion: newWcif.formatVersion,
...pick(newWcif, changedWcifKeys),
};
return patchWcif(newWcif.id, changes);
};

export const searchPersons = (query: string): Promise<WcaPersonSearchResult[]> =>
Expand Down
7 changes: 5 additions & 2 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ export const uploadCurrentWCIFChanges =
return;
}

const keysForPatch = ['formatVersion', ...Array.from(changedKeys)];
const changes = pick(wcif, keysForPatch);
const changedWcifKeys = Array.from(changedKeys).filter((key) => key !== 'formatVersion');
const changes = {
formatVersion: wcif.formatVersion,
...pick(wcif, changedWcifKeys),
};

dispatch(updateUploading(true));
checkWcif(wcif)
Expand Down
Loading