@strapi/content-releases 0.0.0-next.56199ab7a5f3320e0debcbe4a24fe0b8cd599e21 → 0.0.0-next.583e758623dc82206a4b2758d01dd5948b6e3f6a

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