Skip to content

Commit 2c9e43d

Browse files
committed
fix: Update bulk action handlers to use correct payload structure with versioned links
1 parent 8a4a84c commit 2c9e43d

File tree

1 file changed

+124
-21
lines changed

1 file changed

+124
-21
lines changed

src/handlers/bulk-action-handlers.ts

Lines changed: 124 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ interface BulkActionResponse {
2626
error?: any
2727
}
2828

29+
// Define the correct types for bulk action payloads
30+
interface VersionedLink {
31+
sys: {
32+
type: "Link"
33+
linkType: "Entry" | "Asset"
34+
id: string
35+
version?: number
36+
}
37+
}
38+
2939
type BulkUnpublishParams = BulkPublishParams
3040

3141
type BulkValidateParams = {
@@ -41,6 +51,45 @@ export const bulkActionHandlers = {
4151

4252
const contentfulClient = await getContentfulClient()
4353

54+
// Get the current version of each entity
55+
const entityVersions: VersionedLink[] = await Promise.all(
56+
args.entities.map(async (entity) => {
57+
try {
58+
// Get the current version of the entity
59+
const currentEntity = entity.sys.type === "Entry"
60+
? await contentfulClient.entry.get({
61+
spaceId,
62+
environmentId,
63+
entryId: entity.sys.id
64+
})
65+
: await contentfulClient.asset.get({
66+
spaceId,
67+
environmentId,
68+
assetId: entity.sys.id
69+
});
70+
71+
return {
72+
sys: {
73+
type: "Link",
74+
linkType: entity.sys.type,
75+
id: entity.sys.id,
76+
version: currentEntity.sys.version
77+
}
78+
};
79+
} catch (error) {
80+
console.error(`Error fetching entity ${entity.sys.id}: ${error}`);
81+
// Return without version if we can't get it
82+
return {
83+
sys: {
84+
type: "Link",
85+
linkType: entity.sys.type,
86+
id: entity.sys.id
87+
}
88+
};
89+
}
90+
})
91+
);
92+
4493
// Create the bulk action
4594
const bulkAction = await contentfulClient.bulkAction.publish(
4695
{
@@ -52,13 +101,7 @@ export const bulkActionHandlers = {
52101
sys: {
53102
type: "Array",
54103
},
55-
items: args.entities.map((entity) => ({
56-
sys: {
57-
type: "Link",
58-
linkType: entity.sys.type,
59-
id: entity.sys.id,
60-
},
61-
})),
104+
items: entityVersions,
62105
},
63106
},
64107
)
@@ -99,6 +142,45 @@ export const bulkActionHandlers = {
99142

100143
const contentfulClient = await getContentfulClient()
101144

145+
// Get the current version of each entity
146+
const entityVersions: VersionedLink[] = await Promise.all(
147+
args.entities.map(async (entity) => {
148+
try {
149+
// Get the current version of the entity
150+
const currentEntity = entity.sys.type === "Entry"
151+
? await contentfulClient.entry.get({
152+
spaceId,
153+
environmentId,
154+
entryId: entity.sys.id
155+
})
156+
: await contentfulClient.asset.get({
157+
spaceId,
158+
environmentId,
159+
assetId: entity.sys.id
160+
});
161+
162+
return {
163+
sys: {
164+
type: "Link",
165+
linkType: entity.sys.type,
166+
id: entity.sys.id,
167+
version: currentEntity.sys.version
168+
}
169+
};
170+
} catch (error) {
171+
console.error(`Error fetching entity ${entity.sys.id}: ${error}`);
172+
// Return without version if we can't get it
173+
return {
174+
sys: {
175+
type: "Link",
176+
linkType: entity.sys.type,
177+
id: entity.sys.id
178+
}
179+
};
180+
}
181+
})
182+
);
183+
102184
// Create the bulk action
103185
const bulkAction = await contentfulClient.bulkAction.unpublish(
104186
{
@@ -110,13 +192,7 @@ export const bulkActionHandlers = {
110192
sys: {
111193
type: "Array",
112194
},
113-
items: args.entities.map((entity) => ({
114-
sys: {
115-
type: "Link",
116-
linkType: entity.sys.type,
117-
id: entity.sys.id,
118-
},
119-
})),
195+
items: entityVersions,
120196
},
121197
},
122198
)
@@ -157,6 +233,39 @@ export const bulkActionHandlers = {
157233

158234
const contentfulClient = await getContentfulClient()
159235

236+
// Get the current version of each entry
237+
const entityVersions: VersionedLink[] = await Promise.all(
238+
args.entryIds.map(async (id) => {
239+
try {
240+
// Get the current version of the entry
241+
const currentEntry = await contentfulClient.entry.get({
242+
spaceId,
243+
environmentId,
244+
entryId: id
245+
});
246+
247+
return {
248+
sys: {
249+
type: "Link",
250+
linkType: "Entry",
251+
id,
252+
version: currentEntry.sys.version
253+
}
254+
};
255+
} catch (error) {
256+
console.error(`Error fetching entry ${id}: ${error}`);
257+
// Return without version if we can't get it
258+
return {
259+
sys: {
260+
type: "Link",
261+
linkType: "Entry",
262+
id
263+
}
264+
};
265+
}
266+
})
267+
);
268+
160269
// Create the bulk action
161270
const bulkAction = await contentfulClient.bulkAction.validate(
162271
{
@@ -168,13 +277,7 @@ export const bulkActionHandlers = {
168277
sys: {
169278
type: "Array",
170279
},
171-
items: args.entryIds.map((id) => ({
172-
sys: {
173-
type: "Link",
174-
linkType: "Entry",
175-
id,
176-
},
177-
})),
280+
items: entityVersions,
178281
},
179282
},
180283
)

0 commit comments

Comments
 (0)