Skip to content

Commit 808402b

Browse files
authored
Merge pull request #23 from thebuilder/copilot/fix-5a189ded-0637-4f01-b385-1d5ad098b0a2
Add multiple culture support to create-document tool
2 parents 3726209 + 06cc29d commit 808402b

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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 (null culture)
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 (original behavior)
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: 24 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 or empty array, will create single variant with null culture."),
1314
values: z
1415
.array(
1516
z.object({
@@ -25,7 +26,10 @@ 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 or is an empty array, will create a single variant with null culture (original behavior).
2933
3034
Always follow these requirements when creating documents exactly, do not deviate in any way.
3135
@@ -633,6 +637,24 @@ const CreateDocumentTool = CreateUmbracoTool(
633637

634638
const documentId = uuidv4();
635639

640+
// Determine cultures to use
641+
let culturesToUse: (string | null)[] = [];
642+
643+
if (model.cultures === undefined || model.cultures.length === 0) {
644+
// If cultures not provided or empty array, use original behavior (null culture)
645+
culturesToUse = [null];
646+
} else {
647+
// Use provided cultures
648+
culturesToUse = model.cultures;
649+
}
650+
651+
// Create variants for each culture
652+
const variants = culturesToUse.map(culture => ({
653+
culture,
654+
name: model.name,
655+
segment: null,
656+
}));
657+
636658
const payload: CreateDocumentRequestModel = {
637659
id: documentId,
638660
documentType: {
@@ -645,13 +667,7 @@ const CreateDocumentTool = CreateUmbracoTool(
645667
: undefined,
646668
template: null,
647669
values: model.values,
648-
variants: [
649-
{
650-
culture: null,
651-
name: model.name,
652-
segment: null,
653-
},
654-
],
670+
variants,
655671
};
656672

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

0 commit comments

Comments
 (0)