@strapi/content-releases 0.0.0-experimental.check-license → 0.0.0-experimental.d362bf200f5f9359a4bbd4a549603de5ee1f04ca

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/dist/_chunks/App-1LckaIGY.js +1352 -0
  2. package/dist/_chunks/App-1LckaIGY.js.map +1 -0
  3. package/dist/_chunks/App-X01LBg5V.mjs +1329 -0
  4. package/dist/_chunks/App-X01LBg5V.mjs.map +1 -0
  5. package/dist/_chunks/PurchaseContentReleases-Clm0iACO.mjs +51 -0
  6. package/dist/_chunks/PurchaseContentReleases-Clm0iACO.mjs.map +1 -0
  7. package/dist/_chunks/PurchaseContentReleases-YhAPgpG9.js +51 -0
  8. package/dist/_chunks/PurchaseContentReleases-YhAPgpG9.js.map +1 -0
  9. package/dist/_chunks/en-RdapH-9X.mjs +77 -0
  10. package/dist/_chunks/en-RdapH-9X.mjs.map +1 -0
  11. package/dist/_chunks/en-faJDuv3q.js +77 -0
  12. package/dist/_chunks/en-faJDuv3q.js.map +1 -0
  13. package/dist/_chunks/index-OD9AlD-6.mjs +1033 -0
  14. package/dist/_chunks/index-OD9AlD-6.mjs.map +1 -0
  15. package/dist/_chunks/index-cYWov2wa.js +1054 -0
  16. package/dist/_chunks/index-cYWov2wa.js.map +1 -0
  17. package/dist/admin/index.js +3 -35
  18. package/dist/admin/index.js.map +1 -1
  19. package/dist/admin/index.mjs +3 -35
  20. package/dist/admin/index.mjs.map +1 -1
  21. package/dist/admin/src/components/CMReleasesContainer.d.ts +1 -0
  22. package/dist/admin/src/components/RelativeTime.d.ts +28 -0
  23. package/dist/admin/src/components/ReleaseActionMenu.d.ts +26 -0
  24. package/dist/admin/src/components/ReleaseActionOptions.d.ts +9 -0
  25. package/dist/admin/src/components/ReleaseModal.d.ts +16 -0
  26. package/dist/admin/src/constants.d.ts +58 -0
  27. package/dist/admin/src/index.d.ts +3 -15
  28. package/dist/admin/src/pages/App.d.ts +1 -0
  29. package/dist/admin/src/pages/PurchaseContentReleases.d.ts +2 -0
  30. package/dist/admin/src/pages/ReleaseDetailsPage.d.ts +2 -0
  31. package/dist/admin/src/pages/ReleasesPage.d.ts +8 -0
  32. package/dist/admin/src/pages/tests/mockReleaseDetailsPageData.d.ts +181 -0
  33. package/dist/admin/src/pages/tests/mockReleasesPageData.d.ts +39 -0
  34. package/dist/admin/src/pluginId.d.ts +1 -0
  35. package/dist/admin/src/services/axios.d.ts +29 -0
  36. package/dist/admin/src/services/release.d.ts +369 -0
  37. package/dist/admin/src/store/hooks.d.ts +7 -0
  38. package/dist/admin/src/utils/prefixPluginTranslations.d.ts +3 -0
  39. package/dist/admin/src/utils/time.d.ts +1 -0
  40. package/dist/server/index.js +1715 -6
  41. package/dist/server/index.js.map +1 -1
  42. package/dist/server/index.mjs +1693 -6
  43. package/dist/server/index.mjs.map +1 -1
  44. package/dist/server/src/bootstrap.d.ts +5 -0
  45. package/dist/server/src/bootstrap.d.ts.map +1 -0
  46. package/dist/server/src/constants.d.ts +12 -0
  47. package/dist/server/src/constants.d.ts.map +1 -0
  48. package/dist/server/src/content-types/index.d.ts +99 -0
  49. package/dist/server/src/content-types/index.d.ts.map +1 -0
  50. package/dist/server/src/content-types/release/index.d.ts +48 -0
  51. package/dist/server/src/content-types/release/index.d.ts.map +1 -0
  52. package/dist/server/src/content-types/release/schema.d.ts +47 -0
  53. package/dist/server/src/content-types/release/schema.d.ts.map +1 -0
  54. package/dist/server/src/content-types/release-action/index.d.ts +50 -0
  55. package/dist/server/src/content-types/release-action/index.d.ts.map +1 -0
  56. package/dist/server/src/content-types/release-action/schema.d.ts +49 -0
  57. package/dist/server/src/content-types/release-action/schema.d.ts.map +1 -0
  58. package/dist/server/src/controllers/index.d.ts +19 -0
  59. package/dist/server/src/controllers/index.d.ts.map +1 -0
  60. package/dist/server/src/controllers/release-action.d.ts +10 -0
  61. package/dist/server/src/controllers/release-action.d.ts.map +1 -0
  62. package/dist/server/src/controllers/release.d.ts +11 -0
  63. package/dist/server/src/controllers/release.d.ts.map +1 -0
  64. package/dist/server/src/controllers/validation/release-action.d.ts +8 -0
  65. package/dist/server/src/controllers/validation/release-action.d.ts.map +1 -0
  66. package/dist/server/src/controllers/validation/release.d.ts +2 -0
  67. package/dist/server/src/controllers/validation/release.d.ts.map +1 -0
  68. package/dist/server/src/destroy.d.ts +5 -0
  69. package/dist/server/src/destroy.d.ts.map +1 -0
  70. package/dist/server/src/index.d.ts +2092 -1
  71. package/dist/server/src/index.d.ts.map +1 -0
  72. package/dist/server/src/migrations/index.d.ts +13 -0
  73. package/dist/server/src/migrations/index.d.ts.map +1 -0
  74. package/dist/server/src/register.d.ts +5 -1
  75. package/dist/server/src/register.d.ts.map +1 -0
  76. package/dist/server/src/routes/index.d.ts +35 -0
  77. package/dist/server/src/routes/index.d.ts.map +1 -0
  78. package/dist/server/src/routes/release-action.d.ts +18 -0
  79. package/dist/server/src/routes/release-action.d.ts.map +1 -0
  80. package/dist/server/src/routes/release.d.ts +18 -0
  81. package/dist/server/src/routes/release.d.ts.map +1 -0
  82. package/dist/server/src/services/index.d.ts +1826 -0
  83. package/dist/server/src/services/index.d.ts.map +1 -0
  84. package/dist/server/src/services/release.d.ts +66 -0
  85. package/dist/server/src/services/release.d.ts.map +1 -0
  86. package/dist/server/src/services/scheduling.d.ts +18 -0
  87. package/dist/server/src/services/scheduling.d.ts.map +1 -0
  88. package/dist/server/src/services/validation.d.ts +18 -0
  89. package/dist/server/src/services/validation.d.ts.map +1 -0
  90. package/dist/server/src/utils/index.d.ts +14 -0
  91. package/dist/server/src/utils/index.d.ts.map +1 -0
  92. package/dist/shared/contracts/release-actions.d.ts +131 -0
  93. package/dist/shared/contracts/release-actions.d.ts.map +1 -0
  94. package/dist/shared/contracts/releases.d.ts +166 -0
  95. package/dist/shared/contracts/releases.d.ts.map +1 -0
  96. package/dist/shared/types.d.ts +24 -0
  97. package/dist/shared/types.d.ts.map +1 -0
  98. package/dist/shared/validation-schemas.d.ts +2 -0
  99. package/dist/shared/validation-schemas.d.ts.map +1 -0
  100. package/package.json +39 -30
  101. package/dist/_chunks/en-21947221.js +0 -5
  102. package/dist/_chunks/en-21947221.js.map +0 -1
  103. package/dist/_chunks/en-92157b33.mjs +0 -5
  104. package/dist/_chunks/en-92157b33.mjs.map +0 -1
  105. package/dist/admin/src/tests/index.test.d.ts +0 -0
  106. package/dist/server/src/__tests__/index.test.d.ts +0 -2
@@ -1,12 +1,1721 @@
1
1
  "use strict";
2
- const { features } = require("@strapi/strapi/dist/utils/ee");
3
- const register = () => {
4
- if (features.isEnabled("cms-content-releases")) {
5
- console.log("cms-content-releases is enabled");
2
+ const utils = require("@strapi/utils");
3
+ const isEqual = require("lodash/isEqual");
4
+ const lodash = require("lodash");
5
+ const _ = require("lodash/fp");
6
+ const nodeSchedule = require("node-schedule");
7
+ const yup = require("yup");
8
+ const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
9
+ function _interopNamespace(e) {
10
+ if (e && e.__esModule)
11
+ return e;
12
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
13
+ if (e) {
14
+ for (const k in e) {
15
+ if (k !== "default") {
16
+ const d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: () => e[k]
20
+ });
21
+ }
22
+ }
6
23
  }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+ const isEqual__default = /* @__PURE__ */ _interopDefault(isEqual);
28
+ const ___default = /* @__PURE__ */ _interopDefault(_);
29
+ const yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
30
+ const RELEASE_MODEL_UID = "plugin::content-releases.release";
31
+ const RELEASE_ACTION_MODEL_UID = "plugin::content-releases.release-action";
32
+ const ACTIONS = [
33
+ {
34
+ section: "plugins",
35
+ displayName: "Read",
36
+ uid: "read",
37
+ pluginName: "content-releases"
38
+ },
39
+ {
40
+ section: "plugins",
41
+ displayName: "Create",
42
+ uid: "create",
43
+ pluginName: "content-releases"
44
+ },
45
+ {
46
+ section: "plugins",
47
+ displayName: "Edit",
48
+ uid: "update",
49
+ pluginName: "content-releases"
50
+ },
51
+ {
52
+ section: "plugins",
53
+ displayName: "Delete",
54
+ uid: "delete",
55
+ pluginName: "content-releases"
56
+ },
57
+ {
58
+ section: "plugins",
59
+ displayName: "Publish",
60
+ uid: "publish",
61
+ pluginName: "content-releases"
62
+ },
63
+ {
64
+ section: "plugins",
65
+ displayName: "Remove an entry from a release",
66
+ uid: "delete-action",
67
+ pluginName: "content-releases"
68
+ },
69
+ {
70
+ section: "plugins",
71
+ displayName: "Add an entry to a release",
72
+ uid: "create-action",
73
+ pluginName: "content-releases"
74
+ }
75
+ ];
76
+ const ALLOWED_WEBHOOK_EVENTS = {
77
+ RELEASES_PUBLISH: "releases.publish"
78
+ };
79
+ const getService = (name, { strapi: strapi2 } = { strapi: global.strapi }) => {
80
+ return strapi2.plugin("content-releases").service(name);
81
+ };
82
+ const getPopulatedEntry = async (contentTypeUid, entryId, { strapi: strapi2 } = { strapi: global.strapi }) => {
83
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
84
+ const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
85
+ const entry = await strapi2.db.query(contentTypeUid).findOne({
86
+ where: { id: entryId },
87
+ populate
88
+ });
89
+ return entry;
90
+ };
91
+ const getEntryValidStatus = async (contentTypeUid, entry, { strapi: strapi2 } = { strapi: global.strapi }) => {
92
+ try {
93
+ await strapi2.entityValidator.validateEntityCreation(
94
+ strapi2.getModel(contentTypeUid),
95
+ entry,
96
+ void 0,
97
+ // @ts-expect-error - FIXME: entity here is unnecessary
98
+ entry
99
+ );
100
+ return true;
101
+ } catch {
102
+ return false;
103
+ }
104
+ };
105
+ async function deleteActionsOnDisableDraftAndPublish({
106
+ oldContentTypes,
107
+ contentTypes: contentTypes2
108
+ }) {
109
+ if (!oldContentTypes) {
110
+ return;
111
+ }
112
+ for (const uid in contentTypes2) {
113
+ if (!oldContentTypes[uid]) {
114
+ continue;
115
+ }
116
+ const oldContentType = oldContentTypes[uid];
117
+ const contentType = contentTypes2[uid];
118
+ if (utils.contentTypes.hasDraftAndPublish(oldContentType) && !utils.contentTypes.hasDraftAndPublish(contentType)) {
119
+ await strapi.db?.queryBuilder(RELEASE_ACTION_MODEL_UID).delete().where({ contentType: uid }).execute();
120
+ }
121
+ }
122
+ }
123
+ async function deleteActionsOnDeleteContentType({ oldContentTypes, contentTypes: contentTypes2 }) {
124
+ const deletedContentTypes = lodash.difference(lodash.keys(oldContentTypes), lodash.keys(contentTypes2)) ?? [];
125
+ if (deletedContentTypes.length) {
126
+ await utils.async.map(deletedContentTypes, async (deletedContentTypeUID) => {
127
+ return strapi.db?.queryBuilder(RELEASE_ACTION_MODEL_UID).delete().where({ contentType: deletedContentTypeUID }).execute();
128
+ });
129
+ }
130
+ }
131
+ async function migrateIsValidAndStatusReleases() {
132
+ const releasesWithoutStatus = await strapi.db.query(RELEASE_MODEL_UID).findMany({
133
+ where: {
134
+ status: null,
135
+ releasedAt: null
136
+ },
137
+ populate: {
138
+ actions: {
139
+ populate: {
140
+ entry: true
141
+ }
142
+ }
143
+ }
144
+ });
145
+ utils.async.map(releasesWithoutStatus, async (release2) => {
146
+ const actions = release2.actions;
147
+ const notValidatedActions = actions.filter((action) => action.isEntryValid === null);
148
+ for (const action of notValidatedActions) {
149
+ if (action.entry) {
150
+ const populatedEntry = await getPopulatedEntry(action.contentType, action.entry.id, {
151
+ strapi
152
+ });
153
+ if (populatedEntry) {
154
+ const isEntryValid = getEntryValidStatus(action.contentType, populatedEntry, { strapi });
155
+ await strapi.db.query(RELEASE_ACTION_MODEL_UID).update({
156
+ where: {
157
+ id: action.id
158
+ },
159
+ data: {
160
+ isEntryValid
161
+ }
162
+ });
163
+ }
164
+ }
165
+ }
166
+ return getService("release", { strapi }).updateReleaseStatus(release2.id);
167
+ });
168
+ const publishedReleases = await strapi.db.query(RELEASE_MODEL_UID).findMany({
169
+ where: {
170
+ status: null,
171
+ releasedAt: {
172
+ $notNull: true
173
+ }
174
+ }
175
+ });
176
+ utils.async.map(publishedReleases, async (release2) => {
177
+ return strapi.db.query(RELEASE_MODEL_UID).update({
178
+ where: {
179
+ id: release2.id
180
+ },
181
+ data: {
182
+ status: "done"
183
+ }
184
+ });
185
+ });
186
+ }
187
+ async function revalidateChangedContentTypes({ oldContentTypes, contentTypes: contentTypes2 }) {
188
+ if (oldContentTypes !== void 0 && contentTypes2 !== void 0) {
189
+ const contentTypesWithDraftAndPublish = Object.keys(oldContentTypes).filter(
190
+ (uid) => oldContentTypes[uid]?.options?.draftAndPublish
191
+ );
192
+ const releasesAffected = /* @__PURE__ */ new Set();
193
+ utils.async.map(contentTypesWithDraftAndPublish, async (contentTypeUID) => {
194
+ const oldContentType = oldContentTypes[contentTypeUID];
195
+ const contentType = contentTypes2[contentTypeUID];
196
+ if (!isEqual__default.default(oldContentType?.attributes, contentType?.attributes)) {
197
+ const actions = await strapi.db.query(RELEASE_ACTION_MODEL_UID).findMany({
198
+ where: {
199
+ contentType: contentTypeUID
200
+ },
201
+ populate: {
202
+ entry: true,
203
+ release: true
204
+ }
205
+ });
206
+ await utils.async.map(actions, async (action) => {
207
+ if (action.entry && action.release) {
208
+ const populatedEntry = await getPopulatedEntry(contentTypeUID, action.entry.id, {
209
+ strapi
210
+ });
211
+ if (populatedEntry) {
212
+ const isEntryValid = await getEntryValidStatus(contentTypeUID, populatedEntry, {
213
+ strapi
214
+ });
215
+ releasesAffected.add(action.release.id);
216
+ await strapi.db.query(RELEASE_ACTION_MODEL_UID).update({
217
+ where: {
218
+ id: action.id
219
+ },
220
+ data: {
221
+ isEntryValid
222
+ }
223
+ });
224
+ }
225
+ }
226
+ });
227
+ }
228
+ }).then(() => {
229
+ utils.async.map(releasesAffected, async (releaseId) => {
230
+ return getService("release", { strapi }).updateReleaseStatus(releaseId);
231
+ });
232
+ });
233
+ }
234
+ }
235
+ async function disableContentTypeLocalized({ oldContentTypes, contentTypes: contentTypes2 }) {
236
+ if (!oldContentTypes) {
237
+ return;
238
+ }
239
+ for (const uid in contentTypes2) {
240
+ if (!oldContentTypes[uid]) {
241
+ continue;
242
+ }
243
+ const oldContentType = oldContentTypes[uid];
244
+ const contentType = contentTypes2[uid];
245
+ const i18nPlugin = strapi.plugin("i18n");
246
+ const { isLocalizedContentType } = i18nPlugin.service("content-types");
247
+ if (isLocalizedContentType(oldContentType) && !isLocalizedContentType(contentType)) {
248
+ await strapi.db.queryBuilder(RELEASE_ACTION_MODEL_UID).update({
249
+ locale: null
250
+ }).where({ contentType: uid }).execute();
251
+ }
252
+ }
253
+ }
254
+ async function enableContentTypeLocalized({ oldContentTypes, contentTypes: contentTypes2 }) {
255
+ if (!oldContentTypes) {
256
+ return;
257
+ }
258
+ for (const uid in contentTypes2) {
259
+ if (!oldContentTypes[uid]) {
260
+ continue;
261
+ }
262
+ const oldContentType = oldContentTypes[uid];
263
+ const contentType = contentTypes2[uid];
264
+ const i18nPlugin = strapi.plugin("i18n");
265
+ const { isLocalizedContentType } = i18nPlugin.service("content-types");
266
+ const { getDefaultLocale } = i18nPlugin.service("locales");
267
+ if (!isLocalizedContentType(oldContentType) && isLocalizedContentType(contentType)) {
268
+ const defaultLocale = await getDefaultLocale();
269
+ await strapi.db.queryBuilder(RELEASE_ACTION_MODEL_UID).update({
270
+ locale: defaultLocale
271
+ }).where({ contentType: uid }).execute();
272
+ }
273
+ }
274
+ }
275
+ const register = async ({ strapi: strapi2 }) => {
276
+ if (strapi2.ee.features.isEnabled("cms-content-releases")) {
277
+ await strapi2.admin.services.permission.actionProvider.registerMany(ACTIONS);
278
+ strapi2.hook("strapi::content-types.beforeSync").register(disableContentTypeLocalized).register(deleteActionsOnDisableDraftAndPublish);
279
+ strapi2.hook("strapi::content-types.afterSync").register(deleteActionsOnDeleteContentType).register(enableContentTypeLocalized).register(revalidateChangedContentTypes).register(migrateIsValidAndStatusReleases);
280
+ }
281
+ if (strapi2.plugin("graphql")) {
282
+ const graphqlExtensionService = strapi2.plugin("graphql").service("extension");
283
+ graphqlExtensionService.shadowCRUD(RELEASE_MODEL_UID).disable();
284
+ graphqlExtensionService.shadowCRUD(RELEASE_ACTION_MODEL_UID).disable();
285
+ }
286
+ };
287
+ const bootstrap = async ({ strapi: strapi2 }) => {
288
+ if (strapi2.ee.features.isEnabled("cms-content-releases")) {
289
+ const contentTypesWithDraftAndPublish = Object.keys(strapi2.contentTypes).filter(
290
+ (uid) => strapi2.contentTypes[uid]?.options?.draftAndPublish
291
+ );
292
+ strapi2.db.lifecycles.subscribe({
293
+ models: contentTypesWithDraftAndPublish,
294
+ async afterDelete(event) {
295
+ try {
296
+ const { model, result } = event;
297
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
298
+ const { id } = result;
299
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
300
+ where: {
301
+ actions: {
302
+ target_type: model.uid,
303
+ target_id: id
304
+ }
305
+ }
306
+ });
307
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
308
+ where: {
309
+ target_type: model.uid,
310
+ target_id: id
311
+ }
312
+ });
313
+ for (const release2 of releases) {
314
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
315
+ }
316
+ }
317
+ } catch (error) {
318
+ strapi2.log.error("Error while deleting release actions after entry delete", { error });
319
+ }
320
+ },
321
+ /**
322
+ * deleteMany hook doesn't return the deleted entries ids
323
+ * so we need to fetch them before deleting the entries to save the ids on our state
324
+ */
325
+ async beforeDeleteMany(event) {
326
+ const { model, params } = event;
327
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
328
+ const { where } = params;
329
+ const entriesToDelete = await strapi2.db.query(model.uid).findMany({ select: ["id"], where });
330
+ event.state.entriesToDelete = entriesToDelete;
331
+ }
332
+ },
333
+ /**
334
+ * We delete the release actions related to deleted entries
335
+ * We make this only after deleteMany is succesfully executed to avoid errors
336
+ */
337
+ async afterDeleteMany(event) {
338
+ try {
339
+ const { model, state } = event;
340
+ const entriesToDelete = state.entriesToDelete;
341
+ if (entriesToDelete) {
342
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
343
+ where: {
344
+ actions: {
345
+ target_type: model.uid,
346
+ target_id: {
347
+ $in: entriesToDelete.map((entry) => entry.id)
348
+ }
349
+ }
350
+ }
351
+ });
352
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
353
+ where: {
354
+ target_type: model.uid,
355
+ target_id: {
356
+ $in: entriesToDelete.map((entry) => entry.id)
357
+ }
358
+ }
359
+ });
360
+ for (const release2 of releases) {
361
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
362
+ }
363
+ }
364
+ } catch (error) {
365
+ strapi2.log.error("Error while deleting release actions after entry deleteMany", {
366
+ error
367
+ });
368
+ }
369
+ },
370
+ async afterUpdate(event) {
371
+ try {
372
+ const { model, result } = event;
373
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
374
+ const isEntryValid = await getEntryValidStatus(model.uid, result, {
375
+ strapi: strapi2
376
+ });
377
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
378
+ where: {
379
+ target_type: model.uid,
380
+ target_id: result.id
381
+ },
382
+ data: {
383
+ isEntryValid
384
+ }
385
+ });
386
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
387
+ where: {
388
+ actions: {
389
+ target_type: model.uid,
390
+ target_id: result.id
391
+ }
392
+ }
393
+ });
394
+ for (const release2 of releases) {
395
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
396
+ }
397
+ }
398
+ } catch (error) {
399
+ strapi2.log.error("Error while updating release actions after entry update", { error });
400
+ }
401
+ }
402
+ });
403
+ getService("scheduling", { strapi: strapi2 }).syncFromDatabase().catch((err) => {
404
+ strapi2.log.error(
405
+ "Error while syncing scheduled jobs from the database in the content-releases plugin. This could lead to errors in the releases scheduling."
406
+ );
407
+ throw err;
408
+ });
409
+ Object.entries(ALLOWED_WEBHOOK_EVENTS).forEach(([key, value]) => {
410
+ strapi2.webhookStore.addAllowedEvent(key, value);
411
+ });
412
+ }
413
+ };
414
+ const destroy = async ({ strapi: strapi2 }) => {
415
+ const scheduledJobs = getService("scheduling", {
416
+ strapi: strapi2
417
+ }).getAll();
418
+ for (const [, job] of scheduledJobs) {
419
+ job.cancel();
420
+ }
421
+ };
422
+ const schema$1 = {
423
+ collectionName: "strapi_releases",
424
+ info: {
425
+ singularName: "release",
426
+ pluralName: "releases",
427
+ displayName: "Release"
428
+ },
429
+ options: {
430
+ draftAndPublish: false
431
+ },
432
+ pluginOptions: {
433
+ "content-manager": {
434
+ visible: false
435
+ },
436
+ "content-type-builder": {
437
+ visible: false
438
+ }
439
+ },
440
+ attributes: {
441
+ name: {
442
+ type: "string",
443
+ required: true
444
+ },
445
+ releasedAt: {
446
+ type: "datetime"
447
+ },
448
+ scheduledAt: {
449
+ type: "datetime"
450
+ },
451
+ timezone: {
452
+ type: "string"
453
+ },
454
+ status: {
455
+ type: "enumeration",
456
+ enum: ["ready", "blocked", "failed", "done", "empty"],
457
+ required: true
458
+ },
459
+ actions: {
460
+ type: "relation",
461
+ relation: "oneToMany",
462
+ target: RELEASE_ACTION_MODEL_UID,
463
+ mappedBy: "release"
464
+ }
465
+ }
466
+ };
467
+ const release$1 = {
468
+ schema: schema$1
469
+ };
470
+ const schema = {
471
+ collectionName: "strapi_release_actions",
472
+ info: {
473
+ singularName: "release-action",
474
+ pluralName: "release-actions",
475
+ displayName: "Release Action"
476
+ },
477
+ options: {
478
+ draftAndPublish: false
479
+ },
480
+ pluginOptions: {
481
+ "content-manager": {
482
+ visible: false
483
+ },
484
+ "content-type-builder": {
485
+ visible: false
486
+ }
487
+ },
488
+ attributes: {
489
+ type: {
490
+ type: "enumeration",
491
+ enum: ["publish", "unpublish"],
492
+ required: true
493
+ },
494
+ entry: {
495
+ type: "relation",
496
+ relation: "morphToOne",
497
+ configurable: false
498
+ },
499
+ contentType: {
500
+ type: "string",
501
+ required: true
502
+ },
503
+ locale: {
504
+ type: "string"
505
+ },
506
+ release: {
507
+ type: "relation",
508
+ relation: "manyToOne",
509
+ target: RELEASE_MODEL_UID,
510
+ inversedBy: "actions"
511
+ },
512
+ isEntryValid: {
513
+ type: "boolean"
514
+ }
515
+ }
516
+ };
517
+ const releaseAction$1 = {
518
+ schema
519
+ };
520
+ const contentTypes = {
521
+ release: release$1,
522
+ "release-action": releaseAction$1
7
523
  };
8
- const index = {
9
- register
524
+ const getGroupName = (queryValue) => {
525
+ switch (queryValue) {
526
+ case "contentType":
527
+ return "contentType.displayName";
528
+ case "action":
529
+ return "type";
530
+ case "locale":
531
+ return ___default.default.getOr("No locale", "locale.name");
532
+ default:
533
+ return "contentType.displayName";
534
+ }
535
+ };
536
+ const createReleaseService = ({ strapi: strapi2 }) => {
537
+ const dispatchWebhook = (event, { isPublished, release: release2, error }) => {
538
+ strapi2.eventHub.emit(event, {
539
+ isPublished,
540
+ error,
541
+ release: release2
542
+ });
543
+ };
544
+ const publishSingleTypeAction = async (uid, actionType, entryId) => {
545
+ const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
546
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
547
+ const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
548
+ const entry = await strapi2.entityService.findOne(uid, entryId, { populate });
549
+ try {
550
+ if (actionType === "publish") {
551
+ await entityManagerService.publish(entry, uid);
552
+ } else {
553
+ await entityManagerService.unpublish(entry, uid);
554
+ }
555
+ } catch (error) {
556
+ if (error instanceof utils.errors.ApplicationError && (error.message === "already.published" || error.message === "already.draft"))
557
+ ;
558
+ else {
559
+ throw error;
560
+ }
561
+ }
562
+ };
563
+ const publishCollectionTypeAction = async (uid, entriesToPublishIds, entriestoUnpublishIds) => {
564
+ const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
565
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
566
+ const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
567
+ const entriesToPublish = await strapi2.entityService.findMany(uid, {
568
+ filters: {
569
+ id: {
570
+ $in: entriesToPublishIds
571
+ }
572
+ },
573
+ populate
574
+ });
575
+ const entriesToUnpublish = await strapi2.entityService.findMany(uid, {
576
+ filters: {
577
+ id: {
578
+ $in: entriestoUnpublishIds
579
+ }
580
+ },
581
+ populate
582
+ });
583
+ if (entriesToPublish.length > 0) {
584
+ await entityManagerService.publishMany(entriesToPublish, uid);
585
+ }
586
+ if (entriesToUnpublish.length > 0) {
587
+ await entityManagerService.unpublishMany(entriesToUnpublish, uid);
588
+ }
589
+ };
590
+ const getFormattedActions = async (releaseId) => {
591
+ const actions = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).findMany({
592
+ where: {
593
+ release: {
594
+ id: releaseId
595
+ }
596
+ },
597
+ populate: {
598
+ entry: {
599
+ fields: ["id"]
600
+ }
601
+ }
602
+ });
603
+ if (actions.length === 0) {
604
+ throw new utils.errors.ValidationError("No entries to publish");
605
+ }
606
+ const collectionTypeActions = {};
607
+ const singleTypeActions = [];
608
+ for (const action of actions) {
609
+ const contentTypeUid = action.contentType;
610
+ if (strapi2.contentTypes[contentTypeUid].kind === "collectionType") {
611
+ if (!collectionTypeActions[contentTypeUid]) {
612
+ collectionTypeActions[contentTypeUid] = {
613
+ entriesToPublishIds: [],
614
+ entriesToUnpublishIds: []
615
+ };
616
+ }
617
+ if (action.type === "publish") {
618
+ collectionTypeActions[contentTypeUid].entriesToPublishIds.push(action.entry.id);
619
+ } else {
620
+ collectionTypeActions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
621
+ }
622
+ } else {
623
+ singleTypeActions.push({
624
+ uid: contentTypeUid,
625
+ action: action.type,
626
+ id: action.entry.id
627
+ });
628
+ }
629
+ }
630
+ return { collectionTypeActions, singleTypeActions };
631
+ };
632
+ return {
633
+ async create(releaseData, { user }) {
634
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
635
+ const {
636
+ validatePendingReleasesLimit,
637
+ validateUniqueNameForPendingRelease,
638
+ validateScheduledAtIsLaterThanNow
639
+ } = getService("release-validation", { strapi: strapi2 });
640
+ await Promise.all([
641
+ validatePendingReleasesLimit(),
642
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name),
643
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
644
+ ]);
645
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).create({
646
+ data: {
647
+ ...releaseWithCreatorFields,
648
+ status: "empty"
649
+ }
650
+ });
651
+ if (releaseWithCreatorFields.scheduledAt) {
652
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
653
+ await schedulingService.set(release2.id, release2.scheduledAt);
654
+ }
655
+ strapi2.telemetry.send("didCreateContentRelease");
656
+ return release2;
657
+ },
658
+ async findOne(id, query = {}) {
659
+ const dbQuery = strapi2.get("query-params").transform(RELEASE_MODEL_UID, query);
660
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({
661
+ ...dbQuery,
662
+ where: { id }
663
+ });
664
+ return release2;
665
+ },
666
+ findPage(query) {
667
+ const dbQuery = strapi2.get("query-params").transform(RELEASE_MODEL_UID, query ?? {});
668
+ return strapi2.db.query(RELEASE_MODEL_UID).findPage({
669
+ ...dbQuery,
670
+ populate: {
671
+ actions: {
672
+ count: true
673
+ }
674
+ }
675
+ });
676
+ },
677
+ async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
678
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
679
+ where: {
680
+ actions: {
681
+ target_type: contentTypeUid,
682
+ target_id: entryId
683
+ },
684
+ releasedAt: {
685
+ $null: true
686
+ }
687
+ },
688
+ populate: {
689
+ // Filter the action to get only the content type entry
690
+ actions: {
691
+ where: {
692
+ target_type: contentTypeUid,
693
+ target_id: entryId
694
+ }
695
+ }
696
+ }
697
+ });
698
+ return releases.map((release2) => {
699
+ if (release2.actions?.length) {
700
+ const [actionForEntry] = release2.actions;
701
+ delete release2.actions;
702
+ return {
703
+ ...release2,
704
+ action: actionForEntry
705
+ };
706
+ }
707
+ return release2;
708
+ });
709
+ },
710
+ async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
711
+ const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
712
+ where: {
713
+ releasedAt: {
714
+ $null: true
715
+ },
716
+ actions: {
717
+ target_type: contentTypeUid,
718
+ target_id: entryId
719
+ }
720
+ }
721
+ });
722
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
723
+ where: {
724
+ $or: [
725
+ {
726
+ id: {
727
+ $notIn: releasesRelated.map((release2) => release2.id)
728
+ }
729
+ },
730
+ {
731
+ actions: null
732
+ }
733
+ ],
734
+ releasedAt: {
735
+ $null: true
736
+ }
737
+ }
738
+ });
739
+ return releases.map((release2) => {
740
+ if (release2.actions?.length) {
741
+ const [actionForEntry] = release2.actions;
742
+ delete release2.actions;
743
+ return {
744
+ ...release2,
745
+ action: actionForEntry
746
+ };
747
+ }
748
+ return release2;
749
+ });
750
+ },
751
+ async update(id, releaseData, { user }) {
752
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(
753
+ releaseData
754
+ );
755
+ const { validateUniqueNameForPendingRelease, validateScheduledAtIsLaterThanNow } = getService(
756
+ "release-validation",
757
+ { strapi: strapi2 }
758
+ );
759
+ await Promise.all([
760
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name, id),
761
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
762
+ ]);
763
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({ where: { id } });
764
+ if (!release2) {
765
+ throw new utils.errors.NotFoundError(`No release found for id ${id}`);
766
+ }
767
+ if (release2.releasedAt) {
768
+ throw new utils.errors.ValidationError("Release already published");
769
+ }
770
+ const updatedRelease = await strapi2.db.query(RELEASE_MODEL_UID).update({
771
+ where: { id },
772
+ data: releaseWithCreatorFields
773
+ });
774
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
775
+ if (releaseData.scheduledAt) {
776
+ await schedulingService.set(id, releaseData.scheduledAt);
777
+ } else if (release2.scheduledAt) {
778
+ schedulingService.cancel(id);
779
+ }
780
+ this.updateReleaseStatus(id);
781
+ strapi2.telemetry.send("didUpdateContentRelease");
782
+ return updatedRelease;
783
+ },
784
+ async createAction(releaseId, action) {
785
+ const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
786
+ strapi: strapi2
787
+ });
788
+ await Promise.all([
789
+ validateEntryContentType(action.entry.contentType),
790
+ validateUniqueEntry(releaseId, action)
791
+ ]);
792
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({ where: { id: releaseId } });
793
+ if (!release2) {
794
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
795
+ }
796
+ if (release2.releasedAt) {
797
+ throw new utils.errors.ValidationError("Release already published");
798
+ }
799
+ const { entry, type } = action;
800
+ const populatedEntry = await getPopulatedEntry(entry.contentType, entry.id, { strapi: strapi2 });
801
+ const isEntryValid = await getEntryValidStatus(entry.contentType, populatedEntry, { strapi: strapi2 });
802
+ const releaseAction2 = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).create({
803
+ data: {
804
+ type,
805
+ contentType: entry.contentType,
806
+ locale: entry.locale,
807
+ isEntryValid,
808
+ entry: {
809
+ id: entry.id,
810
+ __type: entry.contentType,
811
+ __pivot: { field: "entry" }
812
+ },
813
+ release: releaseId
814
+ },
815
+ populate: { release: { select: ["id"] }, entry: { select: ["id"] } }
816
+ });
817
+ this.updateReleaseStatus(releaseId);
818
+ return releaseAction2;
819
+ },
820
+ async findActions(releaseId, query) {
821
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({
822
+ where: { id: releaseId },
823
+ select: ["id"]
824
+ });
825
+ if (!release2) {
826
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
827
+ }
828
+ const dbQuery = strapi2.get("query-params").transform(RELEASE_ACTION_MODEL_UID, query ?? {});
829
+ return strapi2.db.query(RELEASE_ACTION_MODEL_UID).findPage({
830
+ ...dbQuery,
831
+ populate: {
832
+ entry: {
833
+ populate: "*"
834
+ }
835
+ },
836
+ where: {
837
+ release: releaseId
838
+ }
839
+ });
840
+ },
841
+ async countActions(query) {
842
+ const dbQuery = strapi2.get("query-params").transform(RELEASE_ACTION_MODEL_UID, query ?? {});
843
+ return strapi2.db.query(RELEASE_ACTION_MODEL_UID).count(dbQuery);
844
+ },
845
+ async groupActions(actions, groupBy) {
846
+ const contentTypeUids = actions.reduce((acc, action) => {
847
+ if (!acc.includes(action.contentType)) {
848
+ acc.push(action.contentType);
849
+ }
850
+ return acc;
851
+ }, []);
852
+ const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
853
+ contentTypeUids
854
+ );
855
+ const allLocalesDictionary = await this.getLocalesDataForActions();
856
+ const formattedData = actions.map((action) => {
857
+ const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
858
+ return {
859
+ ...action,
860
+ locale: action.locale ? allLocalesDictionary[action.locale] : null,
861
+ contentType: {
862
+ displayName,
863
+ mainFieldValue: action.entry[mainField],
864
+ uid: action.contentType
865
+ }
866
+ };
867
+ });
868
+ const groupName = getGroupName(groupBy);
869
+ return ___default.default.groupBy(groupName)(formattedData);
870
+ },
871
+ async getLocalesDataForActions() {
872
+ if (!strapi2.plugin("i18n")) {
873
+ return {};
874
+ }
875
+ const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
876
+ return allLocales.reduce((acc, locale) => {
877
+ acc[locale.code] = { name: locale.name, code: locale.code };
878
+ return acc;
879
+ }, {});
880
+ },
881
+ async getContentTypesDataForActions(contentTypesUids) {
882
+ const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
883
+ const contentTypesData = {};
884
+ for (const contentTypeUid of contentTypesUids) {
885
+ const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
886
+ uid: contentTypeUid
887
+ });
888
+ contentTypesData[contentTypeUid] = {
889
+ mainField: contentTypeConfig.settings.mainField,
890
+ displayName: strapi2.getModel(contentTypeUid).info.displayName
891
+ };
892
+ }
893
+ return contentTypesData;
894
+ },
895
+ getContentTypeModelsFromActions(actions) {
896
+ const contentTypeUids = actions.reduce((acc, action) => {
897
+ if (!acc.includes(action.contentType)) {
898
+ acc.push(action.contentType);
899
+ }
900
+ return acc;
901
+ }, []);
902
+ const contentTypeModelsMap = contentTypeUids.reduce(
903
+ (acc, contentTypeUid) => {
904
+ acc[contentTypeUid] = strapi2.getModel(contentTypeUid);
905
+ return acc;
906
+ },
907
+ {}
908
+ );
909
+ return contentTypeModelsMap;
910
+ },
911
+ async getAllComponents() {
912
+ const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
913
+ const components = await contentManagerComponentsService.findAllComponents();
914
+ const componentsMap = components.reduce(
915
+ (acc, component) => {
916
+ acc[component.uid] = component;
917
+ return acc;
918
+ },
919
+ {}
920
+ );
921
+ return componentsMap;
922
+ },
923
+ async delete(releaseId) {
924
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({
925
+ where: { id: releaseId },
926
+ populate: {
927
+ actions: {
928
+ select: ["id"]
929
+ }
930
+ }
931
+ });
932
+ if (!release2) {
933
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
934
+ }
935
+ if (release2.releasedAt) {
936
+ throw new utils.errors.ValidationError("Release already published");
937
+ }
938
+ await strapi2.db.transaction(async () => {
939
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
940
+ where: {
941
+ id: {
942
+ $in: release2.actions.map((action) => action.id)
943
+ }
944
+ }
945
+ });
946
+ await strapi2.db.query(RELEASE_MODEL_UID).delete({
947
+ where: {
948
+ id: releaseId
949
+ }
950
+ });
951
+ });
952
+ if (release2.scheduledAt) {
953
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
954
+ await schedulingService.cancel(release2.id);
955
+ }
956
+ strapi2.telemetry.send("didDeleteContentRelease");
957
+ return release2;
958
+ },
959
+ async publish(releaseId) {
960
+ const {
961
+ release: release2,
962
+ error
963
+ } = await strapi2.db.transaction(async ({ trx }) => {
964
+ const lockedRelease = await strapi2.db?.queryBuilder(RELEASE_MODEL_UID).where({ id: releaseId }).select(["id", "name", "releasedAt", "status"]).first().transacting(trx).forUpdate().execute();
965
+ if (!lockedRelease) {
966
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
967
+ }
968
+ if (lockedRelease.releasedAt) {
969
+ throw new utils.errors.ValidationError("Release already published");
970
+ }
971
+ if (lockedRelease.status === "failed") {
972
+ throw new utils.errors.ValidationError("Release failed to publish");
973
+ }
974
+ try {
975
+ strapi2.log.info(`[Content Releases] Starting to publish release ${lockedRelease.name}`);
976
+ const { collectionTypeActions, singleTypeActions } = await getFormattedActions(
977
+ releaseId
978
+ );
979
+ await strapi2.db.transaction(async () => {
980
+ for (const { uid, action, id } of singleTypeActions) {
981
+ await publishSingleTypeAction(uid, action, id);
982
+ }
983
+ for (const contentTypeUid of Object.keys(collectionTypeActions)) {
984
+ const uid = contentTypeUid;
985
+ await publishCollectionTypeAction(
986
+ uid,
987
+ collectionTypeActions[uid].entriesToPublishIds,
988
+ collectionTypeActions[uid].entriesToUnpublishIds
989
+ );
990
+ }
991
+ });
992
+ const release22 = await strapi2.db.query(RELEASE_MODEL_UID).update({
993
+ where: {
994
+ id: releaseId
995
+ },
996
+ data: {
997
+ status: "done",
998
+ releasedAt: /* @__PURE__ */ new Date()
999
+ }
1000
+ });
1001
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
1002
+ isPublished: true,
1003
+ release: release22
1004
+ });
1005
+ strapi2.telemetry.send("didPublishContentRelease");
1006
+ return { release: release22, error: null };
1007
+ } catch (error2) {
1008
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
1009
+ isPublished: false,
1010
+ error: error2
1011
+ });
1012
+ await strapi2.db?.queryBuilder(RELEASE_MODEL_UID).where({ id: releaseId }).update({
1013
+ status: "failed"
1014
+ }).transacting(trx).execute();
1015
+ return {
1016
+ release: null,
1017
+ error: error2
1018
+ };
1019
+ }
1020
+ });
1021
+ if (error instanceof Error) {
1022
+ throw error;
1023
+ }
1024
+ return release2;
1025
+ },
1026
+ async updateAction(actionId, releaseId, update) {
1027
+ const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
1028
+ where: {
1029
+ id: actionId,
1030
+ release: {
1031
+ id: releaseId,
1032
+ releasedAt: {
1033
+ $null: true
1034
+ }
1035
+ }
1036
+ },
1037
+ data: update
1038
+ });
1039
+ if (!updatedAction) {
1040
+ throw new utils.errors.NotFoundError(
1041
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
1042
+ );
1043
+ }
1044
+ return updatedAction;
1045
+ },
1046
+ async deleteAction(actionId, releaseId) {
1047
+ const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
1048
+ where: {
1049
+ id: actionId,
1050
+ release: {
1051
+ id: releaseId,
1052
+ releasedAt: {
1053
+ $null: true
1054
+ }
1055
+ }
1056
+ }
1057
+ });
1058
+ if (!deletedAction) {
1059
+ throw new utils.errors.NotFoundError(
1060
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
1061
+ );
1062
+ }
1063
+ this.updateReleaseStatus(releaseId);
1064
+ return deletedAction;
1065
+ },
1066
+ async updateReleaseStatus(releaseId) {
1067
+ const [totalActions, invalidActions] = await Promise.all([
1068
+ this.countActions({
1069
+ filters: {
1070
+ release: releaseId
1071
+ }
1072
+ }),
1073
+ this.countActions({
1074
+ filters: {
1075
+ release: releaseId,
1076
+ isEntryValid: false
1077
+ }
1078
+ })
1079
+ ]);
1080
+ if (totalActions > 0) {
1081
+ if (invalidActions > 0) {
1082
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1083
+ where: {
1084
+ id: releaseId
1085
+ },
1086
+ data: {
1087
+ status: "blocked"
1088
+ }
1089
+ });
1090
+ }
1091
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1092
+ where: {
1093
+ id: releaseId
1094
+ },
1095
+ data: {
1096
+ status: "ready"
1097
+ }
1098
+ });
1099
+ }
1100
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1101
+ where: {
1102
+ id: releaseId
1103
+ },
1104
+ data: {
1105
+ status: "empty"
1106
+ }
1107
+ });
1108
+ }
1109
+ };
1110
+ };
1111
+ class AlreadyOnReleaseError extends utils.errors.ApplicationError {
1112
+ constructor(message) {
1113
+ super(message);
1114
+ this.name = "AlreadyOnReleaseError";
1115
+ }
1116
+ }
1117
+ const createReleaseValidationService = ({ strapi: strapi2 }) => ({
1118
+ async validateUniqueEntry(releaseId, releaseActionArgs) {
1119
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({
1120
+ where: {
1121
+ id: releaseId
1122
+ },
1123
+ populate: { actions: { populate: { entry: { select: ["id"] } } } }
1124
+ });
1125
+ if (!release2) {
1126
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
1127
+ }
1128
+ const isEntryInRelease = release2.actions.some(
1129
+ (action) => Number(action.entry.id) === Number(releaseActionArgs.entry.id) && action.contentType === releaseActionArgs.entry.contentType
1130
+ );
1131
+ if (isEntryInRelease) {
1132
+ throw new AlreadyOnReleaseError(
1133
+ `Entry with id ${releaseActionArgs.entry.id} and contentType ${releaseActionArgs.entry.contentType} already exists in release with id ${releaseId}`
1134
+ );
1135
+ }
1136
+ },
1137
+ validateEntryContentType(contentTypeUid) {
1138
+ const contentType = strapi2.contentType(contentTypeUid);
1139
+ if (!contentType) {
1140
+ throw new utils.errors.NotFoundError(`No content type found for uid ${contentTypeUid}`);
1141
+ }
1142
+ if (!contentType.options?.draftAndPublish) {
1143
+ throw new utils.errors.ValidationError(
1144
+ `Content type with uid ${contentTypeUid} does not have draftAndPublish enabled`
1145
+ );
1146
+ }
1147
+ },
1148
+ async validatePendingReleasesLimit() {
1149
+ const maximumPendingReleases = strapi2.ee.features.get("cms-content-releases")?.options?.maximumReleases || 3;
1150
+ const [, pendingReleasesCount] = await strapi2.db.query(RELEASE_MODEL_UID).findWithCount({
1151
+ filters: {
1152
+ releasedAt: {
1153
+ $null: true
1154
+ }
1155
+ }
1156
+ });
1157
+ if (pendingReleasesCount >= maximumPendingReleases) {
1158
+ throw new utils.errors.ValidationError("You have reached the maximum number of pending releases");
1159
+ }
1160
+ },
1161
+ async validateUniqueNameForPendingRelease(name, id) {
1162
+ const pendingReleases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
1163
+ where: {
1164
+ releasedAt: {
1165
+ $null: true
1166
+ },
1167
+ name,
1168
+ ...id && { id: { $ne: id } }
1169
+ }
1170
+ });
1171
+ const isNameUnique = pendingReleases.length === 0;
1172
+ if (!isNameUnique) {
1173
+ throw new utils.errors.ValidationError(`Release with name ${name} already exists`);
1174
+ }
1175
+ },
1176
+ async validateScheduledAtIsLaterThanNow(scheduledAt) {
1177
+ if (scheduledAt && new Date(scheduledAt) <= /* @__PURE__ */ new Date()) {
1178
+ throw new utils.errors.ValidationError("Scheduled at must be later than now");
1179
+ }
1180
+ }
1181
+ });
1182
+ const createSchedulingService = ({ strapi: strapi2 }) => {
1183
+ const scheduledJobs = /* @__PURE__ */ new Map();
1184
+ return {
1185
+ async set(releaseId, scheduleDate) {
1186
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({ where: { id: releaseId, releasedAt: null } });
1187
+ if (!release2) {
1188
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
1189
+ }
1190
+ const job = nodeSchedule.scheduleJob(scheduleDate, async () => {
1191
+ try {
1192
+ await getService("release").publish(releaseId);
1193
+ } catch (error) {
1194
+ }
1195
+ this.cancel(releaseId);
1196
+ });
1197
+ if (scheduledJobs.has(releaseId)) {
1198
+ this.cancel(releaseId);
1199
+ }
1200
+ scheduledJobs.set(releaseId, job);
1201
+ return scheduledJobs;
1202
+ },
1203
+ cancel(releaseId) {
1204
+ if (scheduledJobs.has(releaseId)) {
1205
+ scheduledJobs.get(releaseId).cancel();
1206
+ scheduledJobs.delete(releaseId);
1207
+ }
1208
+ return scheduledJobs;
1209
+ },
1210
+ getAll() {
1211
+ return scheduledJobs;
1212
+ },
1213
+ /**
1214
+ * On bootstrap, we can use this function to make sure to sync the scheduled jobs from the database that are not yet released
1215
+ * This is useful in case the server was restarted and the scheduled jobs were lost
1216
+ * This also could be used to sync different Strapi instances in case of a cluster
1217
+ */
1218
+ async syncFromDatabase() {
1219
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
1220
+ where: {
1221
+ scheduledAt: {
1222
+ $gte: /* @__PURE__ */ new Date()
1223
+ },
1224
+ releasedAt: null
1225
+ }
1226
+ });
1227
+ for (const release2 of releases) {
1228
+ this.set(release2.id, release2.scheduledAt);
1229
+ }
1230
+ return scheduledJobs;
1231
+ }
1232
+ };
1233
+ };
1234
+ const services = {
1235
+ release: createReleaseService,
1236
+ "release-validation": createReleaseValidationService,
1237
+ scheduling: createSchedulingService
1238
+ };
1239
+ const RELEASE_SCHEMA = yup__namespace.object().shape({
1240
+ name: yup__namespace.string().trim().required(),
1241
+ scheduledAt: yup__namespace.string().nullable(),
1242
+ isScheduled: yup__namespace.boolean().optional(),
1243
+ time: yup__namespace.string().when("isScheduled", {
1244
+ is: true,
1245
+ then: yup__namespace.string().trim().required(),
1246
+ otherwise: yup__namespace.string().nullable()
1247
+ }),
1248
+ timezone: yup__namespace.string().when("isScheduled", {
1249
+ is: true,
1250
+ then: yup__namespace.string().required().nullable(),
1251
+ otherwise: yup__namespace.string().nullable()
1252
+ }),
1253
+ date: yup__namespace.string().when("isScheduled", {
1254
+ is: true,
1255
+ then: yup__namespace.string().required().nullable(),
1256
+ otherwise: yup__namespace.string().nullable()
1257
+ })
1258
+ }).required().noUnknown();
1259
+ const validateRelease = utils.validateYupSchema(RELEASE_SCHEMA);
1260
+ const releaseController = {
1261
+ async findMany(ctx) {
1262
+ const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
1263
+ ability: ctx.state.userAbility,
1264
+ model: RELEASE_MODEL_UID
1265
+ });
1266
+ await permissionsManager.validateQuery(ctx.query);
1267
+ const releaseService = getService("release", { strapi });
1268
+ const isFindManyForContentTypeEntry = Boolean(ctx.query?.contentTypeUid && ctx.query?.entryId);
1269
+ if (isFindManyForContentTypeEntry) {
1270
+ const query = await permissionsManager.sanitizeQuery(ctx.query);
1271
+ const contentTypeUid = query.contentTypeUid;
1272
+ const entryId = query.entryId;
1273
+ const hasEntryAttached = typeof query.hasEntryAttached === "string" ? JSON.parse(query.hasEntryAttached) : false;
1274
+ const data = hasEntryAttached ? await releaseService.findManyWithContentTypeEntryAttached(contentTypeUid, entryId) : await releaseService.findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId);
1275
+ ctx.body = { data };
1276
+ } else {
1277
+ const query = await permissionsManager.sanitizeQuery(ctx.query);
1278
+ const { results, pagination } = await releaseService.findPage(query);
1279
+ const data = results.map((release2) => {
1280
+ const { actions, ...releaseData } = release2;
1281
+ return {
1282
+ ...releaseData,
1283
+ actions: {
1284
+ meta: {
1285
+ count: actions.count
1286
+ }
1287
+ }
1288
+ };
1289
+ });
1290
+ const pendingReleasesCount = await strapi.db.query(RELEASE_MODEL_UID).count({
1291
+ where: {
1292
+ releasedAt: null
1293
+ }
1294
+ });
1295
+ ctx.body = { data, meta: { pagination, pendingReleasesCount } };
1296
+ }
1297
+ },
1298
+ async findOne(ctx) {
1299
+ const id = ctx.params.id;
1300
+ const releaseService = getService("release", { strapi });
1301
+ const release2 = await releaseService.findOne(id, { populate: ["createdBy"] });
1302
+ if (!release2) {
1303
+ throw new utils.errors.NotFoundError(`Release not found for id: ${id}`);
1304
+ }
1305
+ const count = await releaseService.countActions({
1306
+ filters: {
1307
+ release: id
1308
+ }
1309
+ });
1310
+ const sanitizedRelease = {
1311
+ ...release2,
1312
+ createdBy: release2.createdBy ? strapi.admin.services.user.sanitizeUser(release2.createdBy) : null
1313
+ };
1314
+ const data = {
1315
+ ...sanitizedRelease,
1316
+ actions: {
1317
+ meta: {
1318
+ count
1319
+ }
1320
+ }
1321
+ };
1322
+ ctx.body = { data };
1323
+ },
1324
+ async create(ctx) {
1325
+ const user = ctx.state.user;
1326
+ const releaseArgs = ctx.request.body;
1327
+ await validateRelease(releaseArgs);
1328
+ const releaseService = getService("release", { strapi });
1329
+ const release2 = await releaseService.create(releaseArgs, { user });
1330
+ const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
1331
+ ability: ctx.state.userAbility,
1332
+ model: RELEASE_MODEL_UID
1333
+ });
1334
+ ctx.body = {
1335
+ data: await permissionsManager.sanitizeOutput(release2)
1336
+ };
1337
+ },
1338
+ async update(ctx) {
1339
+ const user = ctx.state.user;
1340
+ const releaseArgs = ctx.request.body;
1341
+ const id = ctx.params.id;
1342
+ await validateRelease(releaseArgs);
1343
+ const releaseService = getService("release", { strapi });
1344
+ const release2 = await releaseService.update(id, releaseArgs, { user });
1345
+ const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
1346
+ ability: ctx.state.userAbility,
1347
+ model: RELEASE_MODEL_UID
1348
+ });
1349
+ ctx.body = {
1350
+ data: await permissionsManager.sanitizeOutput(release2)
1351
+ };
1352
+ },
1353
+ async delete(ctx) {
1354
+ const id = ctx.params.id;
1355
+ const releaseService = getService("release", { strapi });
1356
+ const release2 = await releaseService.delete(id);
1357
+ ctx.body = {
1358
+ data: release2
1359
+ };
1360
+ },
1361
+ async publish(ctx) {
1362
+ const user = ctx.state.user;
1363
+ const id = ctx.params.id;
1364
+ const releaseService = getService("release", { strapi });
1365
+ const release2 = await releaseService.publish(id, { user });
1366
+ const [countPublishActions, countUnpublishActions] = await Promise.all([
1367
+ releaseService.countActions({
1368
+ filters: {
1369
+ release: id,
1370
+ type: "publish"
1371
+ }
1372
+ }),
1373
+ releaseService.countActions({
1374
+ filters: {
1375
+ release: id,
1376
+ type: "unpublish"
1377
+ }
1378
+ })
1379
+ ]);
1380
+ ctx.body = {
1381
+ data: release2,
1382
+ meta: {
1383
+ totalEntries: countPublishActions + countUnpublishActions,
1384
+ totalPublishedEntries: countPublishActions,
1385
+ totalUnpublishedEntries: countUnpublishActions
1386
+ }
1387
+ };
1388
+ }
1389
+ };
1390
+ const RELEASE_ACTION_SCHEMA = utils.yup.object().shape({
1391
+ entry: utils.yup.object().shape({
1392
+ id: utils.yup.strapiID().required(),
1393
+ contentType: utils.yup.string().required()
1394
+ }).required(),
1395
+ type: utils.yup.string().oneOf(["publish", "unpublish"]).required()
1396
+ });
1397
+ const RELEASE_ACTION_UPDATE_SCHEMA = utils.yup.object().shape({
1398
+ type: utils.yup.string().oneOf(["publish", "unpublish"]).required()
1399
+ });
1400
+ const validateReleaseAction = utils.validateYupSchema(RELEASE_ACTION_SCHEMA);
1401
+ const validateReleaseActionUpdateSchema = utils.validateYupSchema(RELEASE_ACTION_UPDATE_SCHEMA);
1402
+ const releaseActionController = {
1403
+ async create(ctx) {
1404
+ const releaseId = ctx.params.releaseId;
1405
+ const releaseActionArgs = ctx.request.body;
1406
+ await validateReleaseAction(releaseActionArgs);
1407
+ const releaseService = getService("release", { strapi });
1408
+ const releaseAction2 = await releaseService.createAction(releaseId, releaseActionArgs);
1409
+ ctx.body = {
1410
+ data: releaseAction2
1411
+ };
1412
+ },
1413
+ async createMany(ctx) {
1414
+ const releaseId = ctx.params.releaseId;
1415
+ const releaseActionsArgs = ctx.request.body;
1416
+ await Promise.all(
1417
+ releaseActionsArgs.map((releaseActionArgs) => validateReleaseAction(releaseActionArgs))
1418
+ );
1419
+ const releaseService = getService("release", { strapi });
1420
+ const releaseActions = await strapi.db.transaction(async () => {
1421
+ const releaseActions2 = await Promise.all(
1422
+ releaseActionsArgs.map(async (releaseActionArgs) => {
1423
+ try {
1424
+ const action = await releaseService.createAction(releaseId, releaseActionArgs);
1425
+ return action;
1426
+ } catch (error) {
1427
+ if (error instanceof AlreadyOnReleaseError) {
1428
+ return null;
1429
+ }
1430
+ throw error;
1431
+ }
1432
+ })
1433
+ );
1434
+ return releaseActions2;
1435
+ });
1436
+ const newReleaseActions = releaseActions.filter((action) => action !== null);
1437
+ ctx.body = {
1438
+ data: newReleaseActions,
1439
+ meta: {
1440
+ entriesAlreadyInRelease: releaseActions.length - newReleaseActions.length,
1441
+ totalEntries: releaseActions.length
1442
+ }
1443
+ };
1444
+ },
1445
+ async findMany(ctx) {
1446
+ const releaseId = ctx.params.releaseId;
1447
+ const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
1448
+ ability: ctx.state.userAbility,
1449
+ model: RELEASE_ACTION_MODEL_UID
1450
+ });
1451
+ const query = await permissionsManager.sanitizeQuery(ctx.query);
1452
+ const releaseService = getService("release", { strapi });
1453
+ const { results, pagination } = await releaseService.findActions(releaseId, {
1454
+ sort: query.groupBy === "action" ? "type" : query.groupBy,
1455
+ ...query
1456
+ });
1457
+ const contentTypeOutputSanitizers = results.reduce((acc, action) => {
1458
+ if (acc[action.contentType]) {
1459
+ return acc;
1460
+ }
1461
+ const contentTypePermissionsManager = strapi.admin.services.permission.createPermissionsManager({
1462
+ ability: ctx.state.userAbility,
1463
+ model: action.contentType
1464
+ });
1465
+ acc[action.contentType] = contentTypePermissionsManager.sanitizeOutput;
1466
+ return acc;
1467
+ }, {});
1468
+ const sanitizedResults = await utils.async.map(results, async (action) => ({
1469
+ ...action,
1470
+ entry: await contentTypeOutputSanitizers[action.contentType](action.entry)
1471
+ }));
1472
+ const groupedData = await releaseService.groupActions(sanitizedResults, query.groupBy);
1473
+ const contentTypes2 = releaseService.getContentTypeModelsFromActions(results);
1474
+ const components = await releaseService.getAllComponents();
1475
+ ctx.body = {
1476
+ data: groupedData,
1477
+ meta: {
1478
+ pagination,
1479
+ contentTypes: contentTypes2,
1480
+ components
1481
+ }
1482
+ };
1483
+ },
1484
+ async update(ctx) {
1485
+ const actionId = ctx.params.actionId;
1486
+ const releaseId = ctx.params.releaseId;
1487
+ const releaseActionUpdateArgs = ctx.request.body;
1488
+ await validateReleaseActionUpdateSchema(releaseActionUpdateArgs);
1489
+ const releaseService = getService("release", { strapi });
1490
+ const updatedAction = await releaseService.updateAction(
1491
+ actionId,
1492
+ releaseId,
1493
+ releaseActionUpdateArgs
1494
+ );
1495
+ ctx.body = {
1496
+ data: updatedAction
1497
+ };
1498
+ },
1499
+ async delete(ctx) {
1500
+ const actionId = ctx.params.actionId;
1501
+ const releaseId = ctx.params.releaseId;
1502
+ const releaseService = getService("release", { strapi });
1503
+ const deletedReleaseAction = await releaseService.deleteAction(actionId, releaseId);
1504
+ ctx.body = {
1505
+ data: deletedReleaseAction
1506
+ };
1507
+ }
1508
+ };
1509
+ const controllers = { release: releaseController, "release-action": releaseActionController };
1510
+ const release = {
1511
+ type: "admin",
1512
+ routes: [
1513
+ {
1514
+ method: "POST",
1515
+ path: "/",
1516
+ handler: "release.create",
1517
+ config: {
1518
+ policies: [
1519
+ "admin::isAuthenticatedAdmin",
1520
+ {
1521
+ name: "admin::hasPermissions",
1522
+ config: {
1523
+ actions: ["plugin::content-releases.create"]
1524
+ }
1525
+ }
1526
+ ]
1527
+ }
1528
+ },
1529
+ {
1530
+ method: "GET",
1531
+ path: "/",
1532
+ handler: "release.findMany",
1533
+ config: {
1534
+ policies: [
1535
+ "admin::isAuthenticatedAdmin",
1536
+ {
1537
+ name: "admin::hasPermissions",
1538
+ config: {
1539
+ actions: ["plugin::content-releases.read"]
1540
+ }
1541
+ }
1542
+ ]
1543
+ }
1544
+ },
1545
+ {
1546
+ method: "GET",
1547
+ path: "/:id",
1548
+ handler: "release.findOne",
1549
+ config: {
1550
+ policies: [
1551
+ "admin::isAuthenticatedAdmin",
1552
+ {
1553
+ name: "admin::hasPermissions",
1554
+ config: {
1555
+ actions: ["plugin::content-releases.read"]
1556
+ }
1557
+ }
1558
+ ]
1559
+ }
1560
+ },
1561
+ {
1562
+ method: "PUT",
1563
+ path: "/:id",
1564
+ handler: "release.update",
1565
+ config: {
1566
+ policies: [
1567
+ "admin::isAuthenticatedAdmin",
1568
+ {
1569
+ name: "admin::hasPermissions",
1570
+ config: {
1571
+ actions: ["plugin::content-releases.update"]
1572
+ }
1573
+ }
1574
+ ]
1575
+ }
1576
+ },
1577
+ {
1578
+ method: "DELETE",
1579
+ path: "/:id",
1580
+ handler: "release.delete",
1581
+ config: {
1582
+ policies: [
1583
+ "admin::isAuthenticatedAdmin",
1584
+ {
1585
+ name: "admin::hasPermissions",
1586
+ config: {
1587
+ actions: ["plugin::content-releases.delete"]
1588
+ }
1589
+ }
1590
+ ]
1591
+ }
1592
+ },
1593
+ {
1594
+ method: "POST",
1595
+ path: "/:id/publish",
1596
+ handler: "release.publish",
1597
+ config: {
1598
+ policies: [
1599
+ "admin::isAuthenticatedAdmin",
1600
+ {
1601
+ name: "admin::hasPermissions",
1602
+ config: {
1603
+ actions: ["plugin::content-releases.publish"]
1604
+ }
1605
+ }
1606
+ ]
1607
+ }
1608
+ }
1609
+ ]
1610
+ };
1611
+ const releaseAction = {
1612
+ type: "admin",
1613
+ routes: [
1614
+ {
1615
+ method: "POST",
1616
+ path: "/:releaseId/actions",
1617
+ handler: "release-action.create",
1618
+ config: {
1619
+ policies: [
1620
+ "admin::isAuthenticatedAdmin",
1621
+ {
1622
+ name: "admin::hasPermissions",
1623
+ config: {
1624
+ actions: ["plugin::content-releases.create-action"]
1625
+ }
1626
+ }
1627
+ ]
1628
+ }
1629
+ },
1630
+ {
1631
+ method: "POST",
1632
+ path: "/:releaseId/actions/bulk",
1633
+ handler: "release-action.createMany",
1634
+ config: {
1635
+ policies: [
1636
+ "admin::isAuthenticatedAdmin",
1637
+ {
1638
+ name: "admin::hasPermissions",
1639
+ config: {
1640
+ actions: ["plugin::content-releases.create-action"]
1641
+ }
1642
+ }
1643
+ ]
1644
+ }
1645
+ },
1646
+ {
1647
+ method: "GET",
1648
+ path: "/:releaseId/actions",
1649
+ handler: "release-action.findMany",
1650
+ config: {
1651
+ policies: [
1652
+ "admin::isAuthenticatedAdmin",
1653
+ {
1654
+ name: "admin::hasPermissions",
1655
+ config: {
1656
+ actions: ["plugin::content-releases.read"]
1657
+ }
1658
+ }
1659
+ ]
1660
+ }
1661
+ },
1662
+ {
1663
+ method: "PUT",
1664
+ path: "/:releaseId/actions/:actionId",
1665
+ handler: "release-action.update",
1666
+ config: {
1667
+ policies: [
1668
+ "admin::isAuthenticatedAdmin",
1669
+ {
1670
+ name: "admin::hasPermissions",
1671
+ config: {
1672
+ actions: ["plugin::content-releases.update"]
1673
+ }
1674
+ }
1675
+ ]
1676
+ }
1677
+ },
1678
+ {
1679
+ method: "DELETE",
1680
+ path: "/:releaseId/actions/:actionId",
1681
+ handler: "release-action.delete",
1682
+ config: {
1683
+ policies: [
1684
+ "admin::isAuthenticatedAdmin",
1685
+ {
1686
+ name: "admin::hasPermissions",
1687
+ config: {
1688
+ actions: ["plugin::content-releases.delete-action"]
1689
+ }
1690
+ }
1691
+ ]
1692
+ }
1693
+ }
1694
+ ]
1695
+ };
1696
+ const routes = {
1697
+ release,
1698
+ "release-action": releaseAction
1699
+ };
1700
+ const getPlugin = () => {
1701
+ if (strapi.ee.features.isEnabled("cms-content-releases")) {
1702
+ return {
1703
+ register,
1704
+ bootstrap,
1705
+ destroy,
1706
+ contentTypes,
1707
+ services,
1708
+ controllers,
1709
+ routes
1710
+ };
1711
+ }
1712
+ return {
1713
+ // Always return register, it handles its own feature check
1714
+ register,
1715
+ // Always return contentTypes to avoid losing data when the feature is disabled
1716
+ contentTypes
1717
+ };
10
1718
  };
1719
+ const index = getPlugin();
11
1720
  module.exports = index;
12
1721
  //# sourceMappingURL=index.js.map