Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix filter parameter for migrate content #120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 9 additions & 15 deletions src/modules/migrateContent/migrateContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { createDeliveryUrlParameters } from "./utils/deliveryHelpers.js";
const deliveryApiItemsLimit = 2000;

type SyncContentFilterDeliveryOnlyParams =
| Exclude<MigrateContentFilterParams, { items: ReadonlyArray<string> }>
| SuperiorOmit<Exclude<MigrateContentFilterParams, { items: ReadonlyArray<string> }>, "sourceDeliveryPreviewKey">
| Readonly<
{ items: ReadonlyArray<string>; depth: number; limit?: number; sourceDeliveryPreviewKey: string; language: string }
{ items: ReadonlyArray<string>; depth: number; limit?: number; language: string }
>;
// { items: ReadonlyArray<string>; depth: number; limit?: number } is assignable to { items: ReadonlyArray<string> }
// therefore it was also removed by Exclude
Expand All @@ -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<SyncContentFilterDeliveryOnlyParams, "sourceDeliveryPreviewKey">,
params: SyncContentFilterDeliveryOnlyParams,
) => {
const defaultParams = { ...params, depth: params.depth ?? 0 };

Expand All @@ -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));
};

Expand Down
2 changes: 2 additions & 0 deletions src/modules/migrateContent/utils/deliveryHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type DeliveryParamsOptions = Readonly<{
limit?: number;
order?: Readonly<[string, "asc" | "desc"]>;
inFilter?: Readonly<[string, ReadonlyArray<string>]>;
customFilter?: string;
}>;

export const createDeliveryUrlParameters = (options: DeliveryParamsOptions) => [
Expand All @@ -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)] : [],
];
48 changes: 48 additions & 0 deletions tests/integration/migrateContent/utiltsTests/delivery.test.ts
Original file line number Diff line number Diff line change
@@ -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"]);
});
});
13 changes: 13 additions & 0 deletions tests/unit/syncContent/deliveryHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading