Skip to content

Commit

Permalink
add collections to validation
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Sep 9, 2024
1 parent 11fb29e commit ef88a45
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/modules/sync/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { elementTypes } from "./constants/elements.js";
import { contentTypesFileName, contentTypeSnippetsFileName, taxonomiesFileName } from "./constants/filename.js";
import { ElementsTypes } from "./types/contractModels.js";
import { DiffModel } from "./types/diffModel.js";
import { PatchOperation } from "./types/patchOperation.js";
import { getTargetCodename, PatchOperation } from "./types/patchOperation.js";

export const validateSyncModelFolder = async (folderPath: string) => {
const stats = await fs.stat(folderPath);
Expand Down Expand Up @@ -39,6 +39,17 @@ export const validateDiffedModel = async (
`Content type (codename: ${typeCodename}) is being used and can't be removed (In given environment exists content items of given content type)`
);

const collectionsUsage = await getUsedCollectionsCodenames(
client,
new Set(
diffedModel.collections.filter(c => c.op === "remove").map(c => getTargetCodename(c)).filter(notNullOrUndefined),
),
);

const collectionsUsageErrors = collectionsUsage.map(collectionCodename =>
`Collection (codename: ${collectionCodename}) is being used and can't be removed (In given environment exists content items of given collection)`
);

const changeElementTypeOps = getElementChangeTypeOps([
...diffedModel.contentTypes.updated,
...diffedModel.contentTypeSnippets.updated,
Expand All @@ -50,7 +61,7 @@ export const validateDiffedModel = async (
)
);

return [...typeUsageErrors, ...changeElementOpsErrors];
return [...typeUsageErrors, ...collectionsUsageErrors, ...changeElementOpsErrors];
};

type ElementTypeCodename = Readonly<{ type: ElementsTypes; codename: string }>;
Expand Down Expand Up @@ -87,6 +98,19 @@ const getUsedContentTypesCodenames = async (client: ManagementClient, contentTyp
.filter(notNullOrUndefined);
};

const getUsedCollectionsCodenames = async (client: ManagementClient, collectionCodenames: ReadonlySet<string>) => {
const promises = [...collectionCodenames].map(collectionCodename => () =>
client
.listLanguageVariantsByCollection()
.byCollectionCodename(collectionCodename)
.toPromise()
.then(res => res.data.items.length ? collectionCodename : null)
);

return (await serially(promises))
.filter(notNullOrUndefined);
};

const getElementChangeTypeOps = (
updateOps: ReadonlyArray<[string, ReadonlyArray<PatchOperation>]>,
) =>
Expand Down

0 comments on commit ef88a45

Please sign in to comment.