From 4623855ac94834f3d50198e0478cd6c8debbd536 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Mon, 20 Jan 2025 12:34:06 +0100 Subject: [PATCH] fix filter parameter for migrate content --- src/modules/migrateContent/migrateContent.ts | 24 ++++------ .../migrateContent/utils/deliveryHelpers.ts | 2 + .../utiltsTests/delivery.test.ts | 48 +++++++++++++++++++ .../unit/syncContent/deliveryHelpers.test.ts | 13 +++++ 4 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 tests/integration/migrateContent/utiltsTests/delivery.test.ts diff --git a/src/modules/migrateContent/migrateContent.ts b/src/modules/migrateContent/migrateContent.ts index 18db798f..098bb76c 100644 --- a/src/modules/migrateContent/migrateContent.ts +++ b/src/modules/migrateContent/migrateContent.ts @@ -9,9 +9,9 @@ import { createDeliveryUrlParameters } from "./utils/deliveryHelpers.js"; const deliveryApiItemsLimit = 2000; type SyncContentFilterDeliveryOnlyParams = - | Exclude }> + | SuperiorOmit }>, "sourceDeliveryPreviewKey"> | Readonly< - { items: ReadonlyArray; depth: number; limit?: number; sourceDeliveryPreviewKey: string; language: string } + { items: ReadonlyArray; depth: number; limit?: number; language: string } >; // { items: ReadonlyArray; depth: number; limit?: number } is assignable to { items: ReadonlyArray } // therefore it was also removed by Exclude @@ -20,30 +20,20 @@ export const getItemsCodenames = async ( client: DeliveryClient, params: SyncContentFilterDeliveryOnlyParams, ) => { - if ("filter" in params) { - return client - .items() - .withCustomUrl(params.filter) - .toAllPromise() - .then(res => res.data.responses.flatMap(r => [...extractItemsCodenamesFromResponse(r.data)])); - } - const pageSize = getPageSize(params); const numberOfPages = "last" in params ? Math.ceil(params.last / pageSize) : deliveryApiItemsLimit; const parameters = getDeliveryUrlParams({ ...params, limit: pageSize }); - return await client + return client .items() .withParameters(parameters) - .toAllPromise({ - pages: numberOfPages, - }) + .toAllPromise({ pages: numberOfPages }) .then(res => res.data.responses.flatMap(r => [...extractItemsCodenamesFromResponse(r.data)])); }; export const getDeliveryUrlParams = ( - params: SuperiorOmit, + params: SyncContentFilterDeliveryOnlyParams, ) => { const defaultParams = { ...params, depth: params.depth ?? 0 }; @@ -61,6 +51,10 @@ export const getDeliveryUrlParams = ( ({ byTypesCodenames }) => createDeliveryUrlParameters({ ...defaultParams, inFilter: ["system.type", byTypesCodenames] }), ) + .with( + { filter: P.nonNullable }, + ({ filter }) => createDeliveryUrlParameters({ ...defaultParams, customFilter: filter }), + ) .otherwise(() => createDeliveryUrlParameters(defaultParams)); }; diff --git a/src/modules/migrateContent/utils/deliveryHelpers.ts b/src/modules/migrateContent/utils/deliveryHelpers.ts index 1c45b9f8..e8c9b86a 100644 --- a/src/modules/migrateContent/utils/deliveryHelpers.ts +++ b/src/modules/migrateContent/utils/deliveryHelpers.ts @@ -6,6 +6,7 @@ type DeliveryParamsOptions = Readonly<{ limit?: number; order?: Readonly<[string, "asc" | "desc"]>; inFilter?: Readonly<[string, ReadonlyArray]>; + customFilter?: string; }>; export const createDeliveryUrlParameters = (options: DeliveryParamsOptions) => [ @@ -14,4 +15,5 @@ export const createDeliveryUrlParameters = (options: DeliveryParamsOptions) => [ ...options.limit ? [new Parameters.LimitParameter(options.limit)] : [], ...options.order ? [new Parameters.OrderParameter(options.order[0], options.order[1])] : [], ...options.inFilter ? [new Filters.InFilter(options.inFilter[0], options.inFilter[1] as string[])] : [], + ...options.customFilter ? [new Parameters.CustomParameter(options.customFilter)] : [], ]; diff --git a/tests/integration/migrateContent/utiltsTests/delivery.test.ts b/tests/integration/migrateContent/utiltsTests/delivery.test.ts new file mode 100644 index 00000000..c75b657f --- /dev/null +++ b/tests/integration/migrateContent/utiltsTests/delivery.test.ts @@ -0,0 +1,48 @@ +import { createDeliveryClient } from "@kontent-ai/delivery-sdk"; +import { config as dotenvConfig } from "dotenv"; +import { describe, expect, it } from "vitest"; + +import { getItemsCodenames } from "../../../../src/modules/migrateContent/migrateContent.ts"; + +dotenvConfig(); + +const { SYNC_SOURCE_TEST_ENVIRONMENT_ID } = process.env; + +if (!SYNC_SOURCE_TEST_ENVIRONMENT_ID) { + throw new Error("SYNC_SOURCE_TEST_ENVIRONMENT_ID environment variable is not defined."); +} + +describe("getItemsCodenames", () => { + it.concurrent("should return correct items codenames using byTypesCodenames", async () => { + const client = createDeliveryClient({ environmentId: SYNC_SOURCE_TEST_ENVIRONMENT_ID }); + + const byTypesCodenames = ["no_change_base_type"]; + const language = "default"; + + const result = await getItemsCodenames(client, { byTypesCodenames, language }); + + expect(result).toStrictEqual(["sync_draft_item_1", "sync_item_1", "sync_item_2"]); + }); + + it.concurrent("should return correct items codenames using custom filter by type codename", async () => { + const client = createDeliveryClient({ environmentId: SYNC_SOURCE_TEST_ENVIRONMENT_ID }); + + const filter = "system.type=no_change_base_type"; + const language = "default"; + + const result = await getItemsCodenames(client, { filter, language }); + + expect(result).toStrictEqual(["sync_draft_item_1", "sync_item_1", "sync_item_2"]); + }); + + it.concurrent("should return correct items codenames with custom items codename", async () => { + const client = createDeliveryClient({ environmentId: SYNC_SOURCE_TEST_ENVIRONMENT_ID }); + + const filter = "system.codename[in]=sync_item_1,sync_item_2"; + const language = "default"; + + const result = await getItemsCodenames(client, { filter, language }); + + expect(result).toStrictEqual(["sync_item_1", "sync_item_2"]); + }); +}); diff --git a/tests/unit/syncContent/deliveryHelpers.test.ts b/tests/unit/syncContent/deliveryHelpers.test.ts index 986376bf..1449a51c 100644 --- a/tests/unit/syncContent/deliveryHelpers.test.ts +++ b/tests/unit/syncContent/deliveryHelpers.test.ts @@ -91,6 +91,19 @@ describe("getDeliveryUrlParams", () => { new Filters.InFilter("system.codename", items), ]); }); + + it("should return correct deliver url parameters for custom filter", () => { + const filter = "system.codename=123"; + + const parameters = getDeliveryUrlParams({ language: "default", limit: 100, filter }); + + expect(parameters).toStrictEqual([ + new Parameters.DepthParameter(0), + new Parameters.LanguageParameter("default"), + new Parameters.LimitParameter(100), + new Parameters.CustomParameter(filter), + ]); + }); }); const defaultItemProps = {