@strapi/content-releases 0.0.0-experimental.fb8e9fec2e10d6b55e3aee59c4eb76bb7a11432a → 0.0.0-experimental.fc1ac2acd58c8a5a858679956b6d102ac5ee4011

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 (105) hide show
  1. package/dist/_chunks/App-0Er6xxcq.mjs +1358 -0
  2. package/dist/_chunks/App-0Er6xxcq.mjs.map +1 -0
  3. package/dist/_chunks/App-C768ulk4.js +1381 -0
  4. package/dist/_chunks/App-C768ulk4.js.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-MyLPoISH.mjs → en-WuuhP6Bn.mjs} +21 -6
  10. package/dist/_chunks/en-WuuhP6Bn.mjs.map +1 -0
  11. package/dist/_chunks/{en-gYDqKYFd.js → en-gcJJ5htG.js} +21 -6
  12. package/dist/_chunks/en-gcJJ5htG.js.map +1 -0
  13. package/dist/_chunks/{index-KJa1Rb5F.js → index-BLSMpbpZ.js} +164 -44
  14. package/dist/_chunks/index-BLSMpbpZ.js.map +1 -0
  15. package/dist/_chunks/{index-c4zRX_sg.mjs → index-fJx1up7m.mjs} +178 -58
  16. package/dist/_chunks/index-fJx1up7m.mjs.map +1 -0
  17. package/dist/admin/index.js +1 -1
  18. package/dist/admin/index.mjs +2 -2
  19. package/dist/admin/src/components/CMReleasesContainer.d.ts +1 -0
  20. package/dist/admin/src/components/RelativeTime.d.ts +28 -0
  21. package/dist/admin/src/components/ReleaseActionMenu.d.ts +26 -0
  22. package/dist/admin/src/components/ReleaseActionOptions.d.ts +9 -0
  23. package/dist/admin/src/components/ReleaseModal.d.ts +16 -0
  24. package/dist/admin/src/constants.d.ts +58 -0
  25. package/dist/admin/src/index.d.ts +3 -0
  26. package/dist/admin/src/pages/App.d.ts +1 -0
  27. package/dist/admin/src/pages/PurchaseContentReleases.d.ts +2 -0
  28. package/dist/admin/src/pages/ReleaseDetailsPage.d.ts +2 -0
  29. package/dist/admin/src/pages/ReleasesPage.d.ts +8 -0
  30. package/dist/admin/src/pages/tests/mockReleaseDetailsPageData.d.ts +181 -0
  31. package/dist/admin/src/pages/tests/mockReleasesPageData.d.ts +39 -0
  32. package/dist/admin/src/pluginId.d.ts +1 -0
  33. package/dist/admin/src/services/axios.d.ts +29 -0
  34. package/dist/admin/src/services/release.d.ts +369 -0
  35. package/dist/admin/src/store/hooks.d.ts +7 -0
  36. package/dist/admin/src/utils/time.d.ts +1 -0
  37. package/dist/server/index.js +889 -436
  38. package/dist/server/index.js.map +1 -1
  39. package/dist/server/index.mjs +889 -436
  40. package/dist/server/index.mjs.map +1 -1
  41. package/dist/server/src/bootstrap.d.ts +5 -0
  42. package/dist/server/src/bootstrap.d.ts.map +1 -0
  43. package/dist/server/src/constants.d.ts +12 -0
  44. package/dist/server/src/constants.d.ts.map +1 -0
  45. package/dist/server/src/content-types/index.d.ts +99 -0
  46. package/dist/server/src/content-types/index.d.ts.map +1 -0
  47. package/dist/server/src/content-types/release/index.d.ts +48 -0
  48. package/dist/server/src/content-types/release/index.d.ts.map +1 -0
  49. package/dist/server/src/content-types/release/schema.d.ts +47 -0
  50. package/dist/server/src/content-types/release/schema.d.ts.map +1 -0
  51. package/dist/server/src/content-types/release-action/index.d.ts +50 -0
  52. package/dist/server/src/content-types/release-action/index.d.ts.map +1 -0
  53. package/dist/server/src/content-types/release-action/schema.d.ts +49 -0
  54. package/dist/server/src/content-types/release-action/schema.d.ts.map +1 -0
  55. package/dist/server/src/controllers/index.d.ts +18 -0
  56. package/dist/server/src/controllers/index.d.ts.map +1 -0
  57. package/dist/server/src/controllers/release-action.d.ts +9 -0
  58. package/dist/server/src/controllers/release-action.d.ts.map +1 -0
  59. package/dist/server/src/controllers/release.d.ts +11 -0
  60. package/dist/server/src/controllers/release.d.ts.map +1 -0
  61. package/dist/server/src/controllers/validation/release-action.d.ts +3 -0
  62. package/dist/server/src/controllers/validation/release-action.d.ts.map +1 -0
  63. package/dist/server/src/controllers/validation/release.d.ts +2 -0
  64. package/dist/server/src/controllers/validation/release.d.ts.map +1 -0
  65. package/dist/server/src/destroy.d.ts +5 -0
  66. package/dist/server/src/destroy.d.ts.map +1 -0
  67. package/dist/server/src/index.d.ts +3838 -0
  68. package/dist/server/src/index.d.ts.map +1 -0
  69. package/dist/server/src/migrations/index.d.ts +10 -0
  70. package/dist/server/src/migrations/index.d.ts.map +1 -0
  71. package/dist/server/src/register.d.ts +5 -0
  72. package/dist/server/src/register.d.ts.map +1 -0
  73. package/dist/server/src/routes/index.d.ts +35 -0
  74. package/dist/server/src/routes/index.d.ts.map +1 -0
  75. package/dist/server/src/routes/release-action.d.ts +18 -0
  76. package/dist/server/src/routes/release-action.d.ts.map +1 -0
  77. package/dist/server/src/routes/release.d.ts +18 -0
  78. package/dist/server/src/routes/release.d.ts.map +1 -0
  79. package/dist/server/src/services/index.d.ts +3572 -0
  80. package/dist/server/src/services/index.d.ts.map +1 -0
  81. package/dist/server/src/services/release.d.ts +1812 -0
  82. package/dist/server/src/services/release.d.ts.map +1 -0
  83. package/dist/server/src/services/scheduling.d.ts +18 -0
  84. package/dist/server/src/services/scheduling.d.ts.map +1 -0
  85. package/dist/server/src/services/validation.d.ts +14 -0
  86. package/dist/server/src/services/validation.d.ts.map +1 -0
  87. package/dist/server/src/utils/index.d.ts +18 -0
  88. package/dist/server/src/utils/index.d.ts.map +1 -0
  89. package/dist/shared/contracts/release-actions.d.ts +105 -0
  90. package/dist/shared/contracts/release-actions.d.ts.map +1 -0
  91. package/dist/shared/contracts/releases.d.ts +166 -0
  92. package/dist/shared/contracts/releases.d.ts.map +1 -0
  93. package/dist/shared/types.d.ts +24 -0
  94. package/dist/shared/types.d.ts.map +1 -0
  95. package/dist/shared/validation-schemas.d.ts +2 -0
  96. package/dist/shared/validation-schemas.d.ts.map +1 -0
  97. package/package.json +19 -23
  98. package/dist/_chunks/App-L1jSxCiL.mjs +0 -1015
  99. package/dist/_chunks/App-L1jSxCiL.mjs.map +0 -1
  100. package/dist/_chunks/App-_20W9dYa.js +0 -1037
  101. package/dist/_chunks/App-_20W9dYa.js.map +0 -1
  102. package/dist/_chunks/en-MyLPoISH.mjs.map +0 -1
  103. package/dist/_chunks/en-gYDqKYFd.js.map +0 -1
  104. package/dist/_chunks/index-KJa1Rb5F.js.map +0 -1
  105. package/dist/_chunks/index-c4zRX_sg.mjs.map +0 -1
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  const utils = require("@strapi/utils");
3
+ const isEqual = require("lodash/isEqual");
3
4
  const lodash = require("lodash");
4
5
  const _ = require("lodash/fp");
5
- const EE = require("@strapi/strapi/dist/utils/ee");
6
+ const nodeSchedule = require("node-schedule");
6
7
  const yup = require("yup");
7
8
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
8
9
  function _interopNamespace(e) {
@@ -23,8 +24,8 @@ function _interopNamespace(e) {
23
24
  n.default = e;
24
25
  return Object.freeze(n);
25
26
  }
27
+ const isEqual__default = /* @__PURE__ */ _interopDefault(isEqual);
26
28
  const ___default = /* @__PURE__ */ _interopDefault(_);
27
- const EE__default = /* @__PURE__ */ _interopDefault(EE);
28
29
  const yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
29
30
  const RELEASE_MODEL_UID = "plugin::content-releases.release";
30
31
  const RELEASE_ACTION_MODEL_UID = "plugin::content-releases.release-action";
@@ -72,54 +73,178 @@ const ACTIONS = [
72
73
  pluginName: "content-releases"
73
74
  }
74
75
  ];
75
- async function deleteActionsOnDisableDraftAndPublish({
76
- oldContentTypes,
77
- contentTypes: contentTypes2
78
- }) {
79
- if (!oldContentTypes) {
80
- return;
81
- }
82
- for (const uid in contentTypes2) {
83
- if (!oldContentTypes[uid]) {
84
- continue;
85
- }
86
- const oldContentType = oldContentTypes[uid];
87
- const contentType = contentTypes2[uid];
88
- if (utils.contentTypes.hasDraftAndPublish(oldContentType) && !utils.contentTypes.hasDraftAndPublish(contentType)) {
89
- await strapi.db?.queryBuilder(RELEASE_ACTION_MODEL_UID).delete().where({ contentType: uid }).execute();
90
- }
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.entityService.findOne(contentTypeUid, entryId, { populate });
86
+ return entry;
87
+ };
88
+ const getEntryValidStatus = async (contentTypeUid, entry, { strapi: strapi2 } = { strapi: global.strapi }) => {
89
+ try {
90
+ await strapi2.entityValidator.validateEntityCreation(
91
+ strapi2.getModel(contentTypeUid),
92
+ entry,
93
+ void 0,
94
+ // @ts-expect-error - FIXME: entity here is unnecessary
95
+ entry
96
+ );
97
+ return true;
98
+ } catch {
99
+ return false;
91
100
  }
92
- }
101
+ };
93
102
  async function deleteActionsOnDeleteContentType({ oldContentTypes, contentTypes: contentTypes2 }) {
94
103
  const deletedContentTypes = lodash.difference(lodash.keys(oldContentTypes), lodash.keys(contentTypes2)) ?? [];
95
104
  if (deletedContentTypes.length) {
96
- await utils.mapAsync(deletedContentTypes, async (deletedContentTypeUID) => {
105
+ await utils.async.map(deletedContentTypes, async (deletedContentTypeUID) => {
97
106
  return strapi.db?.queryBuilder(RELEASE_ACTION_MODEL_UID).delete().where({ contentType: deletedContentTypeUID }).execute();
98
107
  });
99
108
  }
100
109
  }
101
- const { features: features$2 } = require("@strapi/strapi/dist/utils/ee");
110
+ async function migrateIsValidAndStatusReleases() {
111
+ const releasesWithoutStatus = await strapi.db.query(RELEASE_MODEL_UID).findMany({
112
+ where: {
113
+ status: null,
114
+ releasedAt: null
115
+ },
116
+ populate: {
117
+ actions: {
118
+ populate: {
119
+ entry: true
120
+ }
121
+ }
122
+ }
123
+ });
124
+ utils.async.map(releasesWithoutStatus, async (release2) => {
125
+ const actions = release2.actions;
126
+ const notValidatedActions = actions.filter((action) => action.isEntryValid === null);
127
+ for (const action of notValidatedActions) {
128
+ if (action.entry) {
129
+ const populatedEntry = await getPopulatedEntry(action.contentType, action.entry.id, {
130
+ strapi
131
+ });
132
+ if (populatedEntry) {
133
+ const isEntryValid = getEntryValidStatus(action.contentType, populatedEntry, { strapi });
134
+ await strapi.db.query(RELEASE_ACTION_MODEL_UID).update({
135
+ where: {
136
+ id: action.id
137
+ },
138
+ data: {
139
+ isEntryValid
140
+ }
141
+ });
142
+ }
143
+ }
144
+ }
145
+ return getService("release", { strapi }).updateReleaseStatus(release2.id);
146
+ });
147
+ const publishedReleases = await strapi.db.query(RELEASE_MODEL_UID).findMany({
148
+ where: {
149
+ status: null,
150
+ releasedAt: {
151
+ $notNull: true
152
+ }
153
+ }
154
+ });
155
+ utils.async.map(publishedReleases, async (release2) => {
156
+ return strapi.db.query(RELEASE_MODEL_UID).update({
157
+ where: {
158
+ id: release2.id
159
+ },
160
+ data: {
161
+ status: "done"
162
+ }
163
+ });
164
+ });
165
+ }
166
+ async function revalidateChangedContentTypes({ oldContentTypes, contentTypes: contentTypes2 }) {
167
+ if (oldContentTypes !== void 0 && contentTypes2 !== void 0) {
168
+ const contentTypesWithDraftAndPublish = Object.keys(oldContentTypes);
169
+ const releasesAffected = /* @__PURE__ */ new Set();
170
+ utils.async.map(contentTypesWithDraftAndPublish, async (contentTypeUID) => {
171
+ const oldContentType = oldContentTypes[contentTypeUID];
172
+ const contentType = contentTypes2[contentTypeUID];
173
+ if (!isEqual__default.default(oldContentType?.attributes, contentType?.attributes)) {
174
+ const actions = await strapi.db.query(RELEASE_ACTION_MODEL_UID).findMany({
175
+ where: {
176
+ contentType: contentTypeUID
177
+ },
178
+ populate: {
179
+ entry: true,
180
+ release: true
181
+ }
182
+ });
183
+ await utils.async.map(actions, async (action) => {
184
+ if (action.entry) {
185
+ const populatedEntry = await getPopulatedEntry(contentTypeUID, action.entry.id, {
186
+ strapi
187
+ });
188
+ if (populatedEntry) {
189
+ const isEntryValid = await getEntryValidStatus(contentTypeUID, populatedEntry, {
190
+ strapi
191
+ });
192
+ releasesAffected.add(action.release.id);
193
+ await strapi.db.query(RELEASE_ACTION_MODEL_UID).update({
194
+ where: {
195
+ id: action.id
196
+ },
197
+ data: {
198
+ isEntryValid
199
+ }
200
+ });
201
+ }
202
+ }
203
+ });
204
+ }
205
+ }).then(() => {
206
+ utils.async.map(releasesAffected, async (releaseId) => {
207
+ return getService("release", { strapi }).updateReleaseStatus(releaseId);
208
+ });
209
+ });
210
+ }
211
+ }
102
212
  const register = async ({ strapi: strapi2 }) => {
103
- if (features$2.isEnabled("cms-content-releases")) {
213
+ if (strapi2.ee.features.isEnabled("cms-content-releases")) {
104
214
  await strapi2.admin.services.permission.actionProvider.registerMany(ACTIONS);
105
- strapi2.hook("strapi::content-types.beforeSync").register(deleteActionsOnDisableDraftAndPublish);
106
- strapi2.hook("strapi::content-types.afterSync").register(deleteActionsOnDeleteContentType);
215
+ strapi2.hook("strapi::content-types.afterSync").register(deleteActionsOnDeleteContentType).register(revalidateChangedContentTypes).register(migrateIsValidAndStatusReleases);
107
216
  }
108
217
  };
109
- const { features: features$1 } = require("@strapi/strapi/dist/utils/ee");
110
218
  const bootstrap = async ({ strapi: strapi2 }) => {
111
- if (features$1.isEnabled("cms-content-releases")) {
219
+ if (strapi2.ee.features.isEnabled("cms-content-releases")) {
220
+ const contentTypesWithDraftAndPublish = Object.keys(strapi2.contentTypes);
112
221
  strapi2.db.lifecycles.subscribe({
113
- afterDelete(event) {
114
- const { model, result } = event;
115
- if (model.kind === "collectionType" && model.options?.draftAndPublish) {
116
- const { id } = result;
117
- strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
118
- where: {
119
- target_type: model.uid,
120
- target_id: id
222
+ models: contentTypesWithDraftAndPublish,
223
+ async afterDelete(event) {
224
+ try {
225
+ const { model, result } = event;
226
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
227
+ const { id } = result;
228
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
229
+ where: {
230
+ actions: {
231
+ target_type: model.uid,
232
+ target_id: id
233
+ }
234
+ }
235
+ });
236
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
237
+ where: {
238
+ target_type: model.uid,
239
+ target_id: id
240
+ }
241
+ });
242
+ for (const release2 of releases) {
243
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
121
244
  }
122
- });
245
+ }
246
+ } catch (error) {
247
+ strapi2.log.error("Error while deleting release actions after entry delete", { error });
123
248
  }
124
249
  },
125
250
  /**
@@ -128,7 +253,7 @@ const bootstrap = async ({ strapi: strapi2 }) => {
128
253
  */
129
254
  async beforeDeleteMany(event) {
130
255
  const { model, params } = event;
131
- if (model.kind === "collectionType" && model.options?.draftAndPublish) {
256
+ if (model.kind === "collectionType") {
132
257
  const { where } = params;
133
258
  const entriesToDelete = await strapi2.db.query(model.uid).findMany({ select: ["id"], where });
134
259
  event.state.entriesToDelete = entriesToDelete;
@@ -139,20 +264,98 @@ const bootstrap = async ({ strapi: strapi2 }) => {
139
264
  * We make this only after deleteMany is succesfully executed to avoid errors
140
265
  */
141
266
  async afterDeleteMany(event) {
142
- const { model, state } = event;
143
- const entriesToDelete = state.entriesToDelete;
144
- if (entriesToDelete) {
145
- await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
146
- where: {
147
- target_type: model.uid,
148
- target_id: {
149
- $in: entriesToDelete.map((entry) => entry.id)
267
+ try {
268
+ const { model, state } = event;
269
+ const entriesToDelete = state.entriesToDelete;
270
+ if (entriesToDelete) {
271
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
272
+ where: {
273
+ actions: {
274
+ target_type: model.uid,
275
+ target_id: {
276
+ $in: entriesToDelete.map(
277
+ (entry) => entry.id
278
+ )
279
+ }
280
+ }
281
+ }
282
+ });
283
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
284
+ where: {
285
+ target_type: model.uid,
286
+ target_id: {
287
+ $in: entriesToDelete.map((entry) => entry.id)
288
+ }
150
289
  }
290
+ });
291
+ for (const release2 of releases) {
292
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
151
293
  }
294
+ }
295
+ } catch (error) {
296
+ strapi2.log.error("Error while deleting release actions after entry deleteMany", {
297
+ error
152
298
  });
153
299
  }
300
+ },
301
+ async afterUpdate(event) {
302
+ try {
303
+ const { model, result } = event;
304
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
305
+ const isEntryValid = await getEntryValidStatus(
306
+ model.uid,
307
+ result,
308
+ {
309
+ strapi: strapi2
310
+ }
311
+ );
312
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
313
+ where: {
314
+ target_type: model.uid,
315
+ target_id: result.id
316
+ },
317
+ data: {
318
+ isEntryValid
319
+ }
320
+ });
321
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
322
+ where: {
323
+ actions: {
324
+ target_type: model.uid,
325
+ target_id: result.id
326
+ }
327
+ }
328
+ });
329
+ for (const release2 of releases) {
330
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
331
+ }
332
+ }
333
+ } catch (error) {
334
+ strapi2.log.error("Error while updating release actions after entry update", { error });
335
+ }
154
336
  }
155
337
  });
338
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
339
+ getService("scheduling", { strapi: strapi2 }).syncFromDatabase().catch((err) => {
340
+ strapi2.log.error(
341
+ "Error while syncing scheduled jobs from the database in the content-releases plugin. This could lead to errors in the releases scheduling."
342
+ );
343
+ throw err;
344
+ });
345
+ Object.entries(ALLOWED_WEBHOOK_EVENTS).forEach(([key, value]) => {
346
+ strapi2.webhookStore.addAllowedEvent(key, value);
347
+ });
348
+ }
349
+ }
350
+ };
351
+ const destroy = async ({ strapi: strapi2 }) => {
352
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
353
+ const scheduledJobs = getService("scheduling", {
354
+ strapi: strapi2
355
+ }).getAll();
356
+ for (const [, job] of scheduledJobs) {
357
+ job.cancel();
358
+ }
156
359
  }
157
360
  };
158
361
  const schema$1 = {
@@ -181,6 +384,17 @@ const schema$1 = {
181
384
  releasedAt: {
182
385
  type: "datetime"
183
386
  },
387
+ scheduledAt: {
388
+ type: "datetime"
389
+ },
390
+ timezone: {
391
+ type: "string"
392
+ },
393
+ status: {
394
+ type: "enumeration",
395
+ enum: ["ready", "blocked", "failed", "done", "empty"],
396
+ required: true
397
+ },
184
398
  actions: {
185
399
  type: "relation",
186
400
  relation: "oneToMany",
@@ -233,6 +447,9 @@ const schema = {
233
447
  relation: "manyToOne",
234
448
  target: RELEASE_MODEL_UID,
235
449
  inversedBy: "actions"
450
+ },
451
+ isEntryValid: {
452
+ type: "boolean"
236
453
  }
237
454
  }
238
455
  };
@@ -243,9 +460,6 @@ const contentTypes = {
243
460
  release: release$1,
244
461
  "release-action": releaseAction$1
245
462
  };
246
- const getService = (name, { strapi: strapi2 } = { strapi: global.strapi }) => {
247
- return strapi2.plugin("content-releases").service(name);
248
- };
249
463
  const getGroupName = (queryValue) => {
250
464
  switch (queryValue) {
251
465
  case "contentType":
@@ -258,415 +472,563 @@ const getGroupName = (queryValue) => {
258
472
  return "contentType.displayName";
259
473
  }
260
474
  };
261
- const createReleaseService = ({ strapi: strapi2 }) => ({
262
- async create(releaseData, { user }) {
263
- const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
264
- const { validatePendingReleasesLimit, validateUniqueNameForPendingRelease } = getService(
265
- "release-validation",
266
- { strapi: strapi2 }
267
- );
268
- await Promise.all([
269
- validatePendingReleasesLimit(),
270
- validateUniqueNameForPendingRelease(releaseWithCreatorFields.name)
271
- ]);
272
- return strapi2.entityService.create(RELEASE_MODEL_UID, {
273
- data: releaseWithCreatorFields
475
+ const createReleaseService = ({ strapi: strapi2 }) => {
476
+ const dispatchWebhook = (event, { isPublished, release: release2, error }) => {
477
+ strapi2.eventHub.emit(event, {
478
+ isPublished,
479
+ error,
480
+ release: release2
274
481
  });
275
- },
276
- async findOne(id, query = {}) {
277
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
278
- ...query
279
- });
280
- return release2;
281
- },
282
- findPage(query) {
283
- return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
284
- ...query,
285
- populate: {
286
- actions: {
287
- // @ts-expect-error Ignore missing properties
288
- count: true
482
+ };
483
+ return {
484
+ async create(releaseData, { user }) {
485
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
486
+ const {
487
+ validatePendingReleasesLimit,
488
+ validateUniqueNameForPendingRelease,
489
+ validateScheduledAtIsLaterThanNow
490
+ } = getService("release-validation", { strapi: strapi2 });
491
+ await Promise.all([
492
+ validatePendingReleasesLimit(),
493
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name),
494
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
495
+ ]);
496
+ const release2 = await strapi2.entityService.create(RELEASE_MODEL_UID, {
497
+ data: {
498
+ ...releaseWithCreatorFields,
499
+ status: "empty"
289
500
  }
501
+ });
502
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling") && releaseWithCreatorFields.scheduledAt) {
503
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
504
+ await schedulingService.set(release2.id, release2.scheduledAt);
290
505
  }
291
- });
292
- },
293
- async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
294
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
295
- where: {
296
- actions: {
297
- target_type: contentTypeUid,
298
- target_id: entryId
299
- },
300
- releasedAt: {
301
- $null: true
506
+ strapi2.telemetry.send("didCreateContentRelease");
507
+ return release2;
508
+ },
509
+ async findOne(id, query = {}) {
510
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
511
+ ...query
512
+ });
513
+ return release2;
514
+ },
515
+ findPage(query) {
516
+ return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
517
+ ...query,
518
+ populate: {
519
+ actions: {
520
+ // @ts-expect-error Ignore missing properties
521
+ count: true
522
+ }
302
523
  }
303
- },
304
- populate: {
305
- // Filter the action to get only the content type entry
306
- actions: {
307
- where: {
524
+ });
525
+ },
526
+ async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
527
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
528
+ where: {
529
+ actions: {
308
530
  target_type: contentTypeUid,
309
531
  target_id: entryId
532
+ },
533
+ releasedAt: {
534
+ $null: true
310
535
  }
311
- }
312
- }
313
- });
314
- return releases.map((release2) => {
315
- if (release2.actions?.length) {
316
- const [actionForEntry] = release2.actions;
317
- delete release2.actions;
318
- return {
319
- ...release2,
320
- action: actionForEntry
321
- };
322
- }
323
- return release2;
324
- });
325
- },
326
- async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
327
- const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
328
- where: {
329
- releasedAt: {
330
- $null: true
331
536
  },
332
- actions: {
333
- target_type: contentTypeUid,
334
- target_id: entryId
335
- }
336
- }
337
- });
338
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
339
- where: {
340
- $or: [
341
- {
342
- id: {
343
- $notIn: releasesRelated.map((release2) => release2.id)
537
+ populate: {
538
+ // Filter the action to get only the content type entry
539
+ actions: {
540
+ where: {
541
+ target_type: contentTypeUid,
542
+ target_id: entryId
344
543
  }
544
+ }
545
+ }
546
+ });
547
+ return releases.map((release2) => {
548
+ if (release2.actions?.length) {
549
+ const [actionForEntry] = release2.actions;
550
+ delete release2.actions;
551
+ return {
552
+ ...release2,
553
+ action: actionForEntry
554
+ };
555
+ }
556
+ return release2;
557
+ });
558
+ },
559
+ async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
560
+ const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
561
+ where: {
562
+ releasedAt: {
563
+ $null: true
345
564
  },
346
- {
347
- actions: null
565
+ actions: {
566
+ target_type: contentTypeUid,
567
+ target_id: entryId
568
+ }
569
+ }
570
+ });
571
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
572
+ where: {
573
+ $or: [
574
+ {
575
+ id: {
576
+ $notIn: releasesRelated.map((release2) => release2.id)
577
+ }
578
+ },
579
+ {
580
+ actions: null
581
+ }
582
+ ],
583
+ releasedAt: {
584
+ $null: true
348
585
  }
349
- ],
350
- releasedAt: {
351
- $null: true
352
586
  }
587
+ });
588
+ return releases.map((release2) => {
589
+ if (release2.actions?.length) {
590
+ const [actionForEntry] = release2.actions;
591
+ delete release2.actions;
592
+ return {
593
+ ...release2,
594
+ action: actionForEntry
595
+ };
596
+ }
597
+ return release2;
598
+ });
599
+ },
600
+ async update(id, releaseData, { user }) {
601
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(
602
+ releaseData
603
+ );
604
+ const { validateUniqueNameForPendingRelease, validateScheduledAtIsLaterThanNow } = getService(
605
+ "release-validation",
606
+ { strapi: strapi2 }
607
+ );
608
+ await Promise.all([
609
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name, id),
610
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
611
+ ]);
612
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id);
613
+ if (!release2) {
614
+ throw new utils.errors.NotFoundError(`No release found for id ${id}`);
353
615
  }
354
- });
355
- return releases.map((release2) => {
356
- if (release2.actions?.length) {
357
- const [actionForEntry] = release2.actions;
358
- delete release2.actions;
359
- return {
360
- ...release2,
361
- action: actionForEntry
362
- };
616
+ if (release2.releasedAt) {
617
+ throw new utils.errors.ValidationError("Release already published");
363
618
  }
364
- return release2;
365
- });
366
- },
367
- async update(id, releaseData, { user }) {
368
- const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(releaseData);
369
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id);
370
- if (!release2) {
371
- throw new utils.errors.NotFoundError(`No release found for id ${id}`);
372
- }
373
- if (release2.releasedAt) {
374
- throw new utils.errors.ValidationError("Release already published");
375
- }
376
- const updatedRelease = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
377
- /*
378
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
379
- * is not compatible with the type we are passing here: UpdateRelease.Request['body']
380
- */
381
- // @ts-expect-error see above
382
- data: releaseWithCreatorFields
383
- });
384
- return updatedRelease;
385
- },
386
- async createAction(releaseId, action) {
387
- const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
388
- strapi: strapi2
389
- });
390
- await Promise.all([
391
- validateEntryContentType(action.entry.contentType),
392
- validateUniqueEntry(releaseId, action)
393
- ]);
394
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId);
395
- if (!release2) {
396
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
397
- }
398
- if (release2.releasedAt) {
399
- throw new utils.errors.ValidationError("Release already published");
400
- }
401
- const { entry, type } = action;
402
- return strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
403
- data: {
404
- type,
405
- contentType: entry.contentType,
406
- locale: entry.locale,
407
- entry: {
408
- id: entry.id,
409
- __type: entry.contentType,
410
- __pivot: { field: "entry" }
411
- },
412
- release: releaseId
413
- },
414
- populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
415
- });
416
- },
417
- async findActions(releaseId, query) {
418
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
419
- fields: ["id"]
420
- });
421
- if (!release2) {
422
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
423
- }
424
- return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
425
- ...query,
426
- populate: {
427
- entry: {
428
- populate: "*"
619
+ const updatedRelease = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
620
+ /*
621
+ * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
622
+ * is not compatible with the type we are passing here: UpdateRelease.Request['body']
623
+ */
624
+ // @ts-expect-error see above
625
+ data: releaseWithCreatorFields
626
+ });
627
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
628
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
629
+ if (releaseData.scheduledAt) {
630
+ await schedulingService.set(id, releaseData.scheduledAt);
631
+ } else if (release2.scheduledAt) {
632
+ schedulingService.cancel(id);
429
633
  }
430
- },
431
- filters: {
432
- release: releaseId
433
634
  }
434
- });
435
- },
436
- async countActions(query) {
437
- return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
438
- },
439
- async groupActions(actions, groupBy) {
440
- const contentTypeUids = actions.reduce((acc, action) => {
441
- if (!acc.includes(action.contentType)) {
442
- acc.push(action.contentType);
635
+ this.updateReleaseStatus(id);
636
+ strapi2.telemetry.send("didUpdateContentRelease");
637
+ return updatedRelease;
638
+ },
639
+ async createAction(releaseId, action) {
640
+ const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
641
+ strapi: strapi2
642
+ });
643
+ await Promise.all([
644
+ validateEntryContentType(action.entry.contentType),
645
+ validateUniqueEntry(releaseId, action)
646
+ ]);
647
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId);
648
+ if (!release2) {
649
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
443
650
  }
444
- return acc;
445
- }, []);
446
- const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
447
- contentTypeUids
448
- );
449
- const allLocalesDictionary = await this.getLocalesDataForActions();
450
- const formattedData = actions.map((action) => {
451
- const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
452
- return {
453
- ...action,
454
- locale: action.locale ? allLocalesDictionary[action.locale] : null,
455
- contentType: {
456
- displayName,
457
- mainFieldValue: action.entry[mainField],
458
- uid: action.contentType
459
- }
460
- };
461
- });
462
- const groupName = getGroupName(groupBy);
463
- return ___default.default.groupBy(groupName)(formattedData);
464
- },
465
- async getLocalesDataForActions() {
466
- if (!strapi2.plugin("i18n")) {
467
- return {};
468
- }
469
- const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
470
- return allLocales.reduce((acc, locale) => {
471
- acc[locale.code] = { name: locale.name, code: locale.code };
472
- return acc;
473
- }, {});
474
- },
475
- async getContentTypesDataForActions(contentTypesUids) {
476
- const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
477
- const contentTypesData = {};
478
- for (const contentTypeUid of contentTypesUids) {
479
- const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
480
- uid: contentTypeUid
651
+ if (release2.releasedAt) {
652
+ throw new utils.errors.ValidationError("Release already published");
653
+ }
654
+ const { entry, type } = action;
655
+ const populatedEntry = await getPopulatedEntry(entry.contentType, entry.id, { strapi: strapi2 });
656
+ const isEntryValid = await getEntryValidStatus(entry.contentType, populatedEntry, { strapi: strapi2 });
657
+ const releaseAction2 = await strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
658
+ data: {
659
+ type,
660
+ contentType: entry.contentType,
661
+ locale: entry.locale,
662
+ isEntryValid,
663
+ entry: {
664
+ id: entry.id,
665
+ __type: entry.contentType,
666
+ __pivot: { field: "entry" }
667
+ },
668
+ release: releaseId
669
+ },
670
+ populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
481
671
  });
482
- contentTypesData[contentTypeUid] = {
483
- mainField: contentTypeConfig.settings.mainField,
484
- displayName: strapi2.getModel(contentTypeUid).info.displayName
485
- };
486
- }
487
- return contentTypesData;
488
- },
489
- getContentTypeModelsFromActions(actions) {
490
- const contentTypeUids = actions.reduce((acc, action) => {
491
- if (!acc.includes(action.contentType)) {
492
- acc.push(action.contentType);
672
+ this.updateReleaseStatus(releaseId);
673
+ return releaseAction2;
674
+ },
675
+ async findActions(releaseId, query) {
676
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
677
+ fields: ["id"]
678
+ });
679
+ if (!release2) {
680
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
493
681
  }
494
- return acc;
495
- }, []);
496
- const contentTypeModelsMap = contentTypeUids.reduce(
497
- (acc, contentTypeUid) => {
498
- acc[contentTypeUid] = strapi2.getModel(contentTypeUid);
682
+ return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
683
+ ...query,
684
+ populate: {
685
+ entry: {
686
+ populate: "*"
687
+ }
688
+ },
689
+ filters: {
690
+ release: releaseId
691
+ }
692
+ });
693
+ },
694
+ async countActions(query) {
695
+ return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
696
+ },
697
+ async groupActions(actions, groupBy) {
698
+ const contentTypeUids = actions.reduce((acc, action) => {
699
+ if (!acc.includes(action.contentType)) {
700
+ acc.push(action.contentType);
701
+ }
499
702
  return acc;
500
- },
501
- {}
502
- );
503
- return contentTypeModelsMap;
504
- },
505
- async getAllComponents() {
506
- const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
507
- const components = await contentManagerComponentsService.findAllComponents();
508
- const componentsMap = components.reduce(
509
- (acc, component) => {
510
- acc[component.uid] = component;
703
+ }, []);
704
+ const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
705
+ contentTypeUids
706
+ );
707
+ const allLocalesDictionary = await this.getLocalesDataForActions();
708
+ const formattedData = actions.map((action) => {
709
+ const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
710
+ return {
711
+ ...action,
712
+ locale: action.locale ? allLocalesDictionary[action.locale] : null,
713
+ contentType: {
714
+ displayName,
715
+ mainFieldValue: action.entry[mainField],
716
+ uid: action.contentType
717
+ }
718
+ };
719
+ });
720
+ const groupName = getGroupName(groupBy);
721
+ return ___default.default.groupBy(groupName)(formattedData);
722
+ },
723
+ async getLocalesDataForActions() {
724
+ if (!strapi2.plugin("i18n")) {
725
+ return {};
726
+ }
727
+ const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
728
+ return allLocales.reduce((acc, locale) => {
729
+ acc[locale.code] = { name: locale.name, code: locale.code };
511
730
  return acc;
512
- },
513
- {}
514
- );
515
- return componentsMap;
516
- },
517
- async delete(releaseId) {
518
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
519
- populate: {
520
- actions: {
521
- fields: ["id"]
522
- }
731
+ }, {});
732
+ },
733
+ async getContentTypesDataForActions(contentTypesUids) {
734
+ const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
735
+ const contentTypesData = {};
736
+ for (const contentTypeUid of contentTypesUids) {
737
+ const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
738
+ uid: contentTypeUid
739
+ });
740
+ contentTypesData[contentTypeUid] = {
741
+ mainField: contentTypeConfig.settings.mainField,
742
+ displayName: strapi2.getModel(contentTypeUid).info.displayName
743
+ };
523
744
  }
524
- });
525
- if (!release2) {
526
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
527
- }
528
- if (release2.releasedAt) {
529
- throw new utils.errors.ValidationError("Release already published");
530
- }
531
- await strapi2.db.transaction(async () => {
532
- await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
533
- where: {
534
- id: {
535
- $in: release2.actions.map((action) => action.id)
536
- }
745
+ return contentTypesData;
746
+ },
747
+ getContentTypeModelsFromActions(actions) {
748
+ const contentTypeUids = actions.reduce((acc, action) => {
749
+ if (!acc.includes(action.contentType)) {
750
+ acc.push(action.contentType);
537
751
  }
538
- });
539
- await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
540
- });
541
- return release2;
542
- },
543
- async publish(releaseId) {
544
- const releaseWithPopulatedActionEntries = await strapi2.entityService.findOne(
545
- RELEASE_MODEL_UID,
546
- releaseId,
547
- {
752
+ return acc;
753
+ }, []);
754
+ const contentTypeModelsMap = contentTypeUids.reduce(
755
+ (acc, contentTypeUid) => {
756
+ acc[contentTypeUid] = strapi2.getModel(contentTypeUid);
757
+ return acc;
758
+ },
759
+ {}
760
+ );
761
+ return contentTypeModelsMap;
762
+ },
763
+ async getAllComponents() {
764
+ const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
765
+ const components = await contentManagerComponentsService.findAllComponents();
766
+ const componentsMap = components.reduce(
767
+ (acc, component) => {
768
+ acc[component.uid] = component;
769
+ return acc;
770
+ },
771
+ {}
772
+ );
773
+ return componentsMap;
774
+ },
775
+ async delete(releaseId) {
776
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
548
777
  populate: {
549
778
  actions: {
550
- populate: {
551
- entry: {
552
- fields: ["id"]
553
- }
554
- }
779
+ fields: ["id"]
555
780
  }
556
781
  }
782
+ });
783
+ if (!release2) {
784
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
557
785
  }
558
- );
559
- if (!releaseWithPopulatedActionEntries) {
560
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
561
- }
562
- if (releaseWithPopulatedActionEntries.releasedAt) {
563
- throw new utils.errors.ValidationError("Release already published");
564
- }
565
- if (releaseWithPopulatedActionEntries.actions.length === 0) {
566
- throw new utils.errors.ValidationError("No entries to publish");
567
- }
568
- const actions = {};
569
- for (const action of releaseWithPopulatedActionEntries.actions) {
570
- const contentTypeUid = action.contentType;
571
- if (!actions[contentTypeUid]) {
572
- actions[contentTypeUid] = {
573
- entriestoPublishIds: [],
574
- entriesToUnpublishIds: []
575
- };
786
+ if (release2.releasedAt) {
787
+ throw new utils.errors.ValidationError("Release already published");
576
788
  }
577
- if (action.type === "publish") {
578
- actions[contentTypeUid].entriestoPublishIds.push(action.entry.id);
579
- } else {
580
- actions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
789
+ await strapi2.db.transaction(async () => {
790
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
791
+ where: {
792
+ id: {
793
+ $in: release2.actions.map((action) => action.id)
794
+ }
795
+ }
796
+ });
797
+ await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
798
+ });
799
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling") && release2.scheduledAt) {
800
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
801
+ await schedulingService.cancel(release2.id);
581
802
  }
582
- }
583
- const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
584
- const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
585
- await strapi2.db.transaction(async () => {
586
- for (const contentTypeUid of Object.keys(actions)) {
587
- const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
588
- const { entriestoPublishIds, entriesToUnpublishIds } = actions[contentTypeUid];
589
- const entriesToPublish = await strapi2.entityService.findMany(
590
- contentTypeUid,
803
+ strapi2.telemetry.send("didDeleteContentRelease");
804
+ return release2;
805
+ },
806
+ async publish(releaseId) {
807
+ try {
808
+ const releaseWithPopulatedActionEntries = await strapi2.entityService.findOne(
809
+ RELEASE_MODEL_UID,
810
+ releaseId,
591
811
  {
592
- filters: {
593
- id: {
594
- $in: entriestoPublishIds
812
+ populate: {
813
+ actions: {
814
+ populate: {
815
+ entry: {
816
+ fields: ["id"]
817
+ }
818
+ }
595
819
  }
596
- },
597
- populate
820
+ }
598
821
  }
599
822
  );
600
- const entriesToUnpublish = await strapi2.entityService.findMany(
601
- contentTypeUid,
602
- {
603
- filters: {
604
- id: {
605
- $in: entriesToUnpublishIds
823
+ if (!releaseWithPopulatedActionEntries) {
824
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
825
+ }
826
+ if (releaseWithPopulatedActionEntries.releasedAt) {
827
+ throw new utils.errors.ValidationError("Release already published");
828
+ }
829
+ if (releaseWithPopulatedActionEntries.actions.length === 0) {
830
+ throw new utils.errors.ValidationError("No entries to publish");
831
+ }
832
+ const collectionTypeActions = {};
833
+ const singleTypeActions = [];
834
+ for (const action of releaseWithPopulatedActionEntries.actions) {
835
+ const contentTypeUid = action.contentType;
836
+ if (strapi2.contentTypes[contentTypeUid].kind === "collectionType") {
837
+ if (!collectionTypeActions[contentTypeUid]) {
838
+ collectionTypeActions[contentTypeUid] = {
839
+ entriestoPublishIds: [],
840
+ entriesToUnpublishIds: []
841
+ };
842
+ }
843
+ if (action.type === "publish") {
844
+ collectionTypeActions[contentTypeUid].entriestoPublishIds.push(action.entry.id);
845
+ } else {
846
+ collectionTypeActions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
847
+ }
848
+ } else {
849
+ singleTypeActions.push({
850
+ uid: contentTypeUid,
851
+ action: action.type,
852
+ id: action.entry.id
853
+ });
854
+ }
855
+ }
856
+ const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
857
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
858
+ await strapi2.db.transaction(async () => {
859
+ for (const { uid, action, id } of singleTypeActions) {
860
+ const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
861
+ const entry = await strapi2.entityService.findOne(uid, id, { populate });
862
+ try {
863
+ if (action === "publish") {
864
+ await entityManagerService.publish(entry, uid);
865
+ } else {
866
+ await entityManagerService.unpublish(entry, uid);
606
867
  }
607
- },
608
- populate
868
+ } catch (error) {
869
+ if (error instanceof utils.errors.ApplicationError && (error.message === "already.published" || error.message === "already.draft")) {
870
+ } else {
871
+ throw error;
872
+ }
873
+ }
609
874
  }
610
- );
611
- if (entriesToPublish.length > 0) {
612
- await entityManagerService.publishMany(entriesToPublish, contentTypeUid);
875
+ for (const contentTypeUid of Object.keys(collectionTypeActions)) {
876
+ const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
877
+ const { entriestoPublishIds, entriesToUnpublishIds } = collectionTypeActions[contentTypeUid];
878
+ const entriesToPublish = await strapi2.entityService.findMany(
879
+ contentTypeUid,
880
+ {
881
+ filters: {
882
+ id: {
883
+ $in: entriestoPublishIds
884
+ }
885
+ },
886
+ populate
887
+ }
888
+ );
889
+ const entriesToUnpublish = await strapi2.entityService.findMany(
890
+ contentTypeUid,
891
+ {
892
+ filters: {
893
+ id: {
894
+ $in: entriesToUnpublishIds
895
+ }
896
+ },
897
+ populate
898
+ }
899
+ );
900
+ if (entriesToPublish.length > 0) {
901
+ await entityManagerService.publishMany(entriesToPublish, contentTypeUid);
902
+ }
903
+ if (entriesToUnpublish.length > 0) {
904
+ await entityManagerService.unpublishMany(entriesToUnpublish, contentTypeUid);
905
+ }
906
+ }
907
+ });
908
+ const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, releaseId, {
909
+ data: {
910
+ /*
911
+ * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">> looks like it's wrong
912
+ */
913
+ // @ts-expect-error see above
914
+ releasedAt: /* @__PURE__ */ new Date()
915
+ },
916
+ populate: {
917
+ actions: {
918
+ // @ts-expect-error is not expecting count but it is working
919
+ count: true
920
+ }
921
+ }
922
+ });
923
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
924
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
925
+ isPublished: true,
926
+ release: release2
927
+ });
613
928
  }
614
- if (entriesToUnpublish.length > 0) {
615
- await entityManagerService.unpublishMany(entriesToUnpublish, contentTypeUid);
929
+ strapi2.telemetry.send("didPublishContentRelease");
930
+ return release2;
931
+ } catch (error) {
932
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
933
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
934
+ isPublished: false,
935
+ error
936
+ });
616
937
  }
938
+ strapi2.db.query(RELEASE_MODEL_UID).update({
939
+ where: { id: releaseId },
940
+ data: {
941
+ status: "failed"
942
+ }
943
+ });
944
+ throw error;
617
945
  }
618
- });
619
- const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, releaseId, {
620
- data: {
621
- /*
622
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">> looks like it's wrong
623
- */
624
- // @ts-expect-error see above
625
- releasedAt: /* @__PURE__ */ new Date()
946
+ },
947
+ async updateAction(actionId, releaseId, update) {
948
+ const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
949
+ where: {
950
+ id: actionId,
951
+ release: {
952
+ id: releaseId,
953
+ releasedAt: {
954
+ $null: true
955
+ }
956
+ }
957
+ },
958
+ data: update
959
+ });
960
+ if (!updatedAction) {
961
+ throw new utils.errors.NotFoundError(
962
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
963
+ );
626
964
  }
627
- });
628
- return release2;
629
- },
630
- async updateAction(actionId, releaseId, update) {
631
- const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
632
- where: {
633
- id: actionId,
634
- release: {
635
- id: releaseId,
636
- releasedAt: {
637
- $null: true
965
+ return updatedAction;
966
+ },
967
+ async deleteAction(actionId, releaseId) {
968
+ const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
969
+ where: {
970
+ id: actionId,
971
+ release: {
972
+ id: releaseId,
973
+ releasedAt: {
974
+ $null: true
975
+ }
638
976
  }
639
977
  }
640
- },
641
- data: update
642
- });
643
- if (!updatedAction) {
644
- throw new utils.errors.NotFoundError(
645
- `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
646
- );
647
- }
648
- return updatedAction;
649
- },
650
- async deleteAction(actionId, releaseId) {
651
- const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
652
- where: {
653
- id: actionId,
654
- release: {
655
- id: releaseId,
656
- releasedAt: {
657
- $null: true
978
+ });
979
+ if (!deletedAction) {
980
+ throw new utils.errors.NotFoundError(
981
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
982
+ );
983
+ }
984
+ this.updateReleaseStatus(releaseId);
985
+ return deletedAction;
986
+ },
987
+ async updateReleaseStatus(releaseId) {
988
+ const [totalActions, invalidActions] = await Promise.all([
989
+ this.countActions({
990
+ filters: {
991
+ release: releaseId
992
+ }
993
+ }),
994
+ this.countActions({
995
+ filters: {
996
+ release: releaseId,
997
+ isEntryValid: false
658
998
  }
999
+ })
1000
+ ]);
1001
+ if (totalActions > 0) {
1002
+ if (invalidActions > 0) {
1003
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1004
+ where: {
1005
+ id: releaseId
1006
+ },
1007
+ data: {
1008
+ status: "blocked"
1009
+ }
1010
+ });
659
1011
  }
1012
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1013
+ where: {
1014
+ id: releaseId
1015
+ },
1016
+ data: {
1017
+ status: "ready"
1018
+ }
1019
+ });
660
1020
  }
661
- });
662
- if (!deletedAction) {
663
- throw new utils.errors.NotFoundError(
664
- `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
665
- );
1021
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1022
+ where: {
1023
+ id: releaseId
1024
+ },
1025
+ data: {
1026
+ status: "empty"
1027
+ }
1028
+ });
666
1029
  }
667
- return deletedAction;
668
- }
669
- });
1030
+ };
1031
+ };
670
1032
  const createReleaseValidationService = ({ strapi: strapi2 }) => ({
671
1033
  async validateUniqueEntry(releaseId, releaseActionArgs) {
672
1034
  const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
@@ -689,17 +1051,9 @@ const createReleaseValidationService = ({ strapi: strapi2 }) => ({
689
1051
  if (!contentType) {
690
1052
  throw new utils.errors.NotFoundError(`No content type found for uid ${contentTypeUid}`);
691
1053
  }
692
- if (!contentType.options?.draftAndPublish) {
693
- throw new utils.errors.ValidationError(
694
- `Content type with uid ${contentTypeUid} does not have draftAndPublish enabled`
695
- );
696
- }
697
1054
  },
698
1055
  async validatePendingReleasesLimit() {
699
- const maximumPendingReleases = (
700
- // @ts-expect-error - options is not typed into features
701
- EE__default.default.features.get("cms-content-releases")?.options?.maximumReleases || 3
702
- );
1056
+ const maximumPendingReleases = strapi2.ee.features.get("cms-content-releases")?.options?.maximumReleases || 3;
703
1057
  const [, pendingReleasesCount] = await strapi2.db.query(RELEASE_MODEL_UID).findWithCount({
704
1058
  filters: {
705
1059
  releasedAt: {
@@ -711,27 +1065,103 @@ const createReleaseValidationService = ({ strapi: strapi2 }) => ({
711
1065
  throw new utils.errors.ValidationError("You have reached the maximum number of pending releases");
712
1066
  }
713
1067
  },
714
- async validateUniqueNameForPendingRelease(name) {
1068
+ async validateUniqueNameForPendingRelease(name, id) {
715
1069
  const pendingReleases = await strapi2.entityService.findMany(RELEASE_MODEL_UID, {
716
1070
  filters: {
717
1071
  releasedAt: {
718
1072
  $null: true
719
1073
  },
720
- name
1074
+ name,
1075
+ ...id && { id: { $ne: id } }
721
1076
  }
722
1077
  });
723
1078
  const isNameUnique = pendingReleases.length === 0;
724
1079
  if (!isNameUnique) {
725
1080
  throw new utils.errors.ValidationError(`Release with name ${name} already exists`);
726
1081
  }
1082
+ },
1083
+ async validateScheduledAtIsLaterThanNow(scheduledAt) {
1084
+ if (scheduledAt && new Date(scheduledAt) <= /* @__PURE__ */ new Date()) {
1085
+ throw new utils.errors.ValidationError("Scheduled at must be later than now");
1086
+ }
727
1087
  }
728
1088
  });
1089
+ const createSchedulingService = ({ strapi: strapi2 }) => {
1090
+ const scheduledJobs = /* @__PURE__ */ new Map();
1091
+ return {
1092
+ async set(releaseId, scheduleDate) {
1093
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({ where: { id: releaseId, releasedAt: null } });
1094
+ if (!release2) {
1095
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
1096
+ }
1097
+ const job = nodeSchedule.scheduleJob(scheduleDate, async () => {
1098
+ try {
1099
+ await getService("release").publish(releaseId);
1100
+ } catch (error) {
1101
+ }
1102
+ this.cancel(releaseId);
1103
+ });
1104
+ if (scheduledJobs.has(releaseId)) {
1105
+ this.cancel(releaseId);
1106
+ }
1107
+ scheduledJobs.set(releaseId, job);
1108
+ return scheduledJobs;
1109
+ },
1110
+ cancel(releaseId) {
1111
+ if (scheduledJobs.has(releaseId)) {
1112
+ scheduledJobs.get(releaseId).cancel();
1113
+ scheduledJobs.delete(releaseId);
1114
+ }
1115
+ return scheduledJobs;
1116
+ },
1117
+ getAll() {
1118
+ return scheduledJobs;
1119
+ },
1120
+ /**
1121
+ * On bootstrap, we can use this function to make sure to sync the scheduled jobs from the database that are not yet released
1122
+ * This is useful in case the server was restarted and the scheduled jobs were lost
1123
+ * This also could be used to sync different Strapi instances in case of a cluster
1124
+ */
1125
+ async syncFromDatabase() {
1126
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
1127
+ where: {
1128
+ scheduledAt: {
1129
+ $gte: /* @__PURE__ */ new Date()
1130
+ },
1131
+ releasedAt: null
1132
+ }
1133
+ });
1134
+ for (const release2 of releases) {
1135
+ this.set(release2.id, release2.scheduledAt);
1136
+ }
1137
+ return scheduledJobs;
1138
+ }
1139
+ };
1140
+ };
729
1141
  const services = {
730
1142
  release: createReleaseService,
731
- "release-validation": createReleaseValidationService
1143
+ "release-validation": createReleaseValidationService,
1144
+ ...strapi.features.future.isEnabled("contentReleasesScheduling") ? { scheduling: createSchedulingService } : {}
732
1145
  };
733
1146
  const RELEASE_SCHEMA = yup__namespace.object().shape({
734
- name: yup__namespace.string().trim().required()
1147
+ name: yup__namespace.string().trim().required(),
1148
+ scheduledAt: yup__namespace.string().nullable(),
1149
+ isScheduled: yup__namespace.boolean().optional(),
1150
+ time: yup__namespace.string().when("isScheduled", {
1151
+ is: true,
1152
+ then: yup__namespace.string().trim().required(),
1153
+ otherwise: yup__namespace.string().nullable()
1154
+ }),
1155
+ timezone: yup__namespace.string().when("isScheduled", {
1156
+ is: true,
1157
+ then: yup__namespace.string().required().nullable(),
1158
+ otherwise: yup__namespace.string().nullable()
1159
+ }),
1160
+ date: yup__namespace.string().when("isScheduled", {
1161
+ is: true,
1162
+ then: yup__namespace.string().required().nullable(),
1163
+ otherwise: yup__namespace.string().nullable()
1164
+ })
735
1165
  }).required().noUnknown();
736
1166
  const validateRelease = utils.validateYupSchema(RELEASE_SCHEMA);
737
1167
  const releaseController = {
@@ -764,26 +1194,30 @@ const releaseController = {
764
1194
  }
765
1195
  };
766
1196
  });
767
- ctx.body = { data, meta: { pagination } };
1197
+ const pendingReleasesCount = await strapi.query(RELEASE_MODEL_UID).count({
1198
+ where: {
1199
+ releasedAt: null
1200
+ }
1201
+ });
1202
+ ctx.body = { data, meta: { pagination, pendingReleasesCount } };
768
1203
  }
769
1204
  },
770
1205
  async findOne(ctx) {
771
1206
  const id = ctx.params.id;
772
1207
  const releaseService = getService("release", { strapi });
773
1208
  const release2 = await releaseService.findOne(id, { populate: ["createdBy"] });
774
- const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
775
- ability: ctx.state.userAbility,
776
- model: RELEASE_MODEL_UID
777
- });
778
- const sanitizedRelease = await permissionsManager.sanitizeOutput(release2);
1209
+ if (!release2) {
1210
+ throw new utils.errors.NotFoundError(`Release not found for id: ${id}`);
1211
+ }
779
1212
  const count = await releaseService.countActions({
780
1213
  filters: {
781
1214
  release: id
782
1215
  }
783
1216
  });
784
- if (!release2) {
785
- throw new utils.errors.NotFoundError(`Release not found for id: ${id}`);
786
- }
1217
+ const sanitizedRelease = {
1218
+ ...release2,
1219
+ createdBy: release2.createdBy ? strapi.admin.services.user.sanitizeUser(release2.createdBy) : null
1220
+ };
787
1221
  const data = {
788
1222
  ...sanitizedRelease,
789
1223
  actions: {
@@ -836,8 +1270,27 @@ const releaseController = {
836
1270
  const id = ctx.params.id;
837
1271
  const releaseService = getService("release", { strapi });
838
1272
  const release2 = await releaseService.publish(id, { user });
1273
+ const [countPublishActions, countUnpublishActions] = await Promise.all([
1274
+ releaseService.countActions({
1275
+ filters: {
1276
+ release: id,
1277
+ type: "publish"
1278
+ }
1279
+ }),
1280
+ releaseService.countActions({
1281
+ filters: {
1282
+ release: id,
1283
+ type: "unpublish"
1284
+ }
1285
+ })
1286
+ ]);
839
1287
  ctx.body = {
840
- data: release2
1288
+ data: release2,
1289
+ meta: {
1290
+ totalEntries: countPublishActions + countUnpublishActions,
1291
+ totalPublishedEntries: countPublishActions,
1292
+ totalUnpublishedEntries: countUnpublishActions
1293
+ }
841
1294
  };
842
1295
  }
843
1296
  };
@@ -887,7 +1340,7 @@ const releaseActionController = {
887
1340
  acc[action.contentType] = contentTypePermissionsManager.sanitizeOutput;
888
1341
  return acc;
889
1342
  }, {});
890
- const sanitizedResults = await utils.mapAsync(results, async (action) => ({
1343
+ const sanitizedResults = await utils.async.map(results, async (action) => ({
891
1344
  ...action,
892
1345
  entry: await contentTypeOutputSanitizers[action.contentType](action.entry)
893
1346
  }));
@@ -1103,12 +1556,12 @@ const routes = {
1103
1556
  release,
1104
1557
  "release-action": releaseAction
1105
1558
  };
1106
- const { features } = require("@strapi/strapi/dist/utils/ee");
1107
1559
  const getPlugin = () => {
1108
- if (features.isEnabled("cms-content-releases")) {
1560
+ if (strapi.ee.features.isEnabled("cms-content-releases")) {
1109
1561
  return {
1110
1562
  register,
1111
1563
  bootstrap,
1564
+ destroy,
1112
1565
  contentTypes,
1113
1566
  services,
1114
1567
  controllers,