diff --git a/src/Client.test.ts b/src/Client.test.ts index e62385b4..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`; + 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 8ec24fd7..95b11717 100644 --- a/src/endpoints/Coverage/Client.ts +++ b/src/endpoints/Coverage/Client.ts @@ -66,11 +66,13 @@ 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 }); return coverage[0] || null; }