From c9bd8c6828883ed63fa5a475c0bc8899da201f31 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Fri, 20 Dec 2024 17:34:19 +0100 Subject: [PATCH 1/3] [DEV-19015] Add determinism to `Coverage.getByExternalReferenceId()` It will be possible to have multiple *deleted* entries with the same `external_reference_id` --- src/Client.test.ts | 2 +- src/endpoints/Coverage/Client.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Client.test.ts b/src/Client.test.ts index e62385b4..683c6ccb 100644 --- a/src/Client.test.ts +++ b/src/Client.test.ts @@ -108,7 +108,7 @@ describe('Client', () => { }); const result = await prezlySdk.coverage.getByExternalReferenceId(externalReferenceId); - const search = `?include_deleted=on&query=%7B%22external_reference_id%22%3A%7B%22%24in%22%3A%5B%22${externalReferenceId}%22%5D%7D%7D`; + const search = `?include_deleted=on&query=%7B%22external_reference_id%22%3A%7B%22%24in%22%3A%5B%22${externalReferenceId}%22%5D%7D%7D&sort=-id`; expect(fetch).toHaveBeenCalledWith(`${defaultCoverageApiUrl}${search}`, { ...DEFAULT_REQUEST_PROPS, method: Method.GET, diff --git a/src/endpoints/Coverage/Client.ts b/src/endpoints/Coverage/Client.ts index 8ec24fd7..a0d701ef 100644 --- a/src/endpoints/Coverage/Client.ts +++ b/src/endpoints/Coverage/Client.ts @@ -71,6 +71,7 @@ export function createClient(api: DeferredJobsApiClient) { const { coverage } = await search({ includeDeleted: true, query, + sortOrder: '-id', // Prefer newer records }); return coverage[0] || null; } From 73dcbcc0d4b76e29684b9ba9e44d272205c0cd3e Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Fri, 20 Dec 2024 17:55:27 +0100 Subject: [PATCH 2/3] [DEV-19015] Switch to `deleted_at` order -- preferring non-deleted records first --- src/Client.test.ts | 2 +- src/endpoints/Coverage/Client.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client.test.ts b/src/Client.test.ts index 683c6ccb..bbfec20e 100644 --- a/src/Client.test.ts +++ b/src/Client.test.ts @@ -108,7 +108,7 @@ describe('Client', () => { }); const result = await prezlySdk.coverage.getByExternalReferenceId(externalReferenceId); - const search = `?include_deleted=on&query=%7B%22external_reference_id%22%3A%7B%22%24in%22%3A%5B%22${externalReferenceId}%22%5D%7D%7D&sort=-id`; + const search = `?include_deleted=on&query=%7B%22external_reference_id%22%3A%7B%22%24in%22%3A%5B%22${externalReferenceId}%22%5D%7D%7D&sort=deleted_at`; expect(fetch).toHaveBeenCalledWith(`${defaultCoverageApiUrl}${search}`, { ...DEFAULT_REQUEST_PROPS, method: Method.GET, diff --git a/src/endpoints/Coverage/Client.ts b/src/endpoints/Coverage/Client.ts index a0d701ef..e8f71d24 100644 --- a/src/endpoints/Coverage/Client.ts +++ b/src/endpoints/Coverage/Client.ts @@ -71,7 +71,7 @@ export function createClient(api: DeferredJobsApiClient) { const { coverage } = await search({ includeDeleted: true, query, - sortOrder: '-id', // Prefer newer records + sortOrder: 'deleted_at', // Prefer non-deleted records first }); return coverage[0] || null; } From f5519aaab611d7e42d6c8434bfb81ad0580ea0f1 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Fri, 20 Dec 2024 17:56:17 +0100 Subject: [PATCH 3/3] [DEV-19015] Allow disabling `includeDeleted` for `getByExternalReferenceId()` --- src/endpoints/Coverage/Client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/endpoints/Coverage/Client.ts b/src/endpoints/Coverage/Client.ts index e8f71d24..95b11717 100644 --- a/src/endpoints/Coverage/Client.ts +++ b/src/endpoints/Coverage/Client.ts @@ -66,10 +66,11 @@ export function createClient(api: DeferredJobsApiClient) { async function getByExternalReferenceId( externalReferenceId: string, + options: { includeDeleted?: boolean } = {}, ): Promise { const query = JSON.stringify({ external_reference_id: { $in: [externalReferenceId] } }); const { coverage } = await search({ - includeDeleted: true, + includeDeleted: options.includeDeleted ?? true, query, sortOrder: 'deleted_at', // Prefer non-deleted records first });