Skip to content

Commit f00465e

Browse files
Copilotthebuilder
andcommitted
Implement multiple culture support for create-document tool
Co-authored-by: thebuilder <[email protected]>
1 parent 9463617 commit f00465e

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

src/umb-management-api/tools/document/__tests__/create-document.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,49 @@ describe("create-document", () => {
7474
};
7575
expect(norm).toMatchSnapshot();
7676
});
77+
78+
it("should create a document with specific cultures", async () => {
79+
// Create document with specific cultures
80+
const docModel = {
81+
documentTypeId: ROOT_DOCUMENT_TYPE_ID,
82+
name: TEST_DOCUMENT_NAME,
83+
cultures: ["en-US", "da-DK"],
84+
values: [],
85+
};
86+
87+
const result = await CreateDocumentTool().handler(docModel, {
88+
signal: new AbortController().signal,
89+
});
90+
91+
expect(result).toMatchSnapshot();
92+
93+
const item = await DocumentTestHelper.findDocument(TEST_DOCUMENT_NAME);
94+
expect(item).toBeDefined();
95+
// Should have variants for both cultures
96+
expect(item!.variants).toHaveLength(2);
97+
const cultures = item!.variants.map(v => v.culture).sort();
98+
expect(cultures).toEqual(["da-DK", "en-US"]);
99+
});
100+
101+
it("should create a document with empty cultures array (null culture)", async () => {
102+
// Create document with empty cultures array - should behave like original
103+
const docModel = {
104+
documentTypeId: ROOT_DOCUMENT_TYPE_ID,
105+
name: TEST_DOCUMENT_NAME,
106+
cultures: [],
107+
values: [],
108+
};
109+
110+
const result = await CreateDocumentTool().handler(docModel, {
111+
signal: new AbortController().signal,
112+
});
113+
114+
expect(result).toMatchSnapshot();
115+
116+
const item = await DocumentTestHelper.findDocument(TEST_DOCUMENT_NAME);
117+
expect(item).toBeDefined();
118+
// Should have single variant with null culture
119+
expect(item!.variants).toHaveLength(1);
120+
expect(item!.variants[0].culture).toBeNull();
121+
});
77122
});

src/umb-management-api/tools/document/post/create-document.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const createDocumentSchema = z.object({
1010
documentTypeId: z.string().uuid("Must be a valid document type type UUID"),
1111
parentId: z.string().uuid("Must be a valid document UUID").optional(),
1212
name: z.string(),
13+
cultures: z.array(z.string()).optional().describe("Array of culture codes. If not provided, will fetch available cultures from Umbraco. If empty array provided, will create without culture (null culture)."),
1314
values: z
1415
.array(
1516
z.object({
@@ -25,7 +26,11 @@ const createDocumentSchema = z.object({
2526

2627
const CreateDocumentTool = CreateUmbracoTool(
2728
"create-document",
28-
`Creates a document,
29+
`Creates a document with support for multiple cultures.
30+
31+
If cultures parameter is provided, a variant will be created for each culture code.
32+
If cultures parameter is not provided, the tool will automatically fetch available cultures from Umbraco and create variants for all of them.
33+
If an empty cultures array is provided, a single variant with null culture will be created (original behavior).
2934
3035
Always follow these requirements when creating documents exactly, do not deviate in any way.
3136
@@ -633,6 +638,37 @@ const CreateDocumentTool = CreateUmbracoTool(
633638

634639
const documentId = uuidv4();
635640

641+
// Determine cultures to use
642+
let culturesToUse: (string | null)[] = [];
643+
644+
if (model.cultures === undefined) {
645+
// If cultures not provided, fetch available cultures from Umbraco
646+
try {
647+
const cultureResponse = await client.getCulture({ take: 100 });
648+
culturesToUse = cultureResponse.items.map(culture => culture.name);
649+
// If no cultures available, fallback to null culture
650+
if (culturesToUse.length === 0) {
651+
culturesToUse = [null];
652+
}
653+
} catch (error) {
654+
// If fetching cultures fails, fallback to null culture
655+
culturesToUse = [null];
656+
}
657+
} else if (model.cultures.length === 0) {
658+
// If empty array provided, use null culture (original behavior)
659+
culturesToUse = [null];
660+
} else {
661+
// Use provided cultures
662+
culturesToUse = model.cultures;
663+
}
664+
665+
// Create variants for each culture
666+
const variants = culturesToUse.map(culture => ({
667+
culture,
668+
name: model.name,
669+
segment: null,
670+
}));
671+
636672
const payload: CreateDocumentRequestModel = {
637673
id: documentId,
638674
documentType: {
@@ -645,13 +681,7 @@ const CreateDocumentTool = CreateUmbracoTool(
645681
: undefined,
646682
template: null,
647683
values: model.values,
648-
variants: [
649-
{
650-
culture: null,
651-
name: model.name,
652-
segment: null,
653-
},
654-
],
684+
variants,
655685
};
656686

657687
const response = await client.postDocument(payload);

0 commit comments

Comments
 (0)