@strapi/content-releases 0.0.0-next.e1ede8c55a0e1e22ce20137bf238fc374bd5dd51 → 0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e

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 (32) hide show
  1. package/dist/_chunks/App-R-kksTW7.mjs +1308 -0
  2. package/dist/_chunks/App-R-kksTW7.mjs.map +1 -0
  3. package/dist/_chunks/{App-o5_WfqR-.js → App-WZHc_d3m.js} +783 -419
  4. package/dist/_chunks/App-WZHc_d3m.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-ngTk74JV.mjs → en-RdapH-9X.mjs} +22 -7
  10. package/dist/_chunks/en-RdapH-9X.mjs.map +1 -0
  11. package/dist/_chunks/{en-haKSQIo8.js → en-faJDuv3q.js} +22 -7
  12. package/dist/_chunks/en-faJDuv3q.js.map +1 -0
  13. package/dist/_chunks/{index-EdBmRHRU.js → index-k6fw6RYi.js} +240 -56
  14. package/dist/_chunks/index-k6fw6RYi.js.map +1 -0
  15. package/dist/_chunks/{index-XAQOX_IB.mjs → index-vpSczx8v.mjs} +255 -71
  16. package/dist/_chunks/index-vpSczx8v.mjs.map +1 -0
  17. package/dist/admin/index.js +2 -1
  18. package/dist/admin/index.js.map +1 -1
  19. package/dist/admin/index.mjs +3 -2
  20. package/dist/admin/index.mjs.map +1 -1
  21. package/dist/server/index.js +1130 -299
  22. package/dist/server/index.js.map +1 -1
  23. package/dist/server/index.mjs +1129 -300
  24. package/dist/server/index.mjs.map +1 -1
  25. package/package.json +15 -11
  26. package/dist/_chunks/App-g2P5kbSm.mjs +0 -945
  27. package/dist/_chunks/App-g2P5kbSm.mjs.map +0 -1
  28. package/dist/_chunks/App-o5_WfqR-.js.map +0 -1
  29. package/dist/_chunks/en-haKSQIo8.js.map +0 -1
  30. package/dist/_chunks/en-ngTk74JV.mjs.map +0 -1
  31. package/dist/_chunks/index-EdBmRHRU.js.map +0 -1
  32. package/dist/_chunks/index-XAQOX_IB.mjs.map +0 -1
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  const utils = require("@strapi/utils");
3
+ const isEqual = require("lodash/isEqual");
4
+ const lodash = require("lodash");
3
5
  const _ = require("lodash/fp");
6
+ const EE = require("@strapi/strapi/dist/utils/ee");
7
+ const nodeSchedule = require("node-schedule");
4
8
  const yup = require("yup");
5
9
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
6
10
  function _interopNamespace(e) {
@@ -21,7 +25,9 @@ function _interopNamespace(e) {
21
25
  n.default = e;
22
26
  return Object.freeze(n);
23
27
  }
28
+ const isEqual__default = /* @__PURE__ */ _interopDefault(isEqual);
24
29
  const ___default = /* @__PURE__ */ _interopDefault(_);
30
+ const EE__default = /* @__PURE__ */ _interopDefault(EE);
25
31
  const yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
26
32
  const RELEASE_MODEL_UID = "plugin::content-releases.release";
27
33
  const RELEASE_ACTION_MODEL_UID = "plugin::content-releases.release-action";
@@ -69,10 +75,355 @@ const ACTIONS = [
69
75
  pluginName: "content-releases"
70
76
  }
71
77
  ];
72
- const { features: features$1 } = require("@strapi/strapi/dist/utils/ee");
78
+ const ALLOWED_WEBHOOK_EVENTS = {
79
+ RELEASES_PUBLISH: "releases.publish"
80
+ };
81
+ const getService = (name, { strapi: strapi2 } = { strapi: global.strapi }) => {
82
+ return strapi2.plugin("content-releases").service(name);
83
+ };
84
+ const getPopulatedEntry = async (contentTypeUid, entryId, { strapi: strapi2 } = { strapi: global.strapi }) => {
85
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
86
+ const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
87
+ const entry = await strapi2.entityService.findOne(contentTypeUid, entryId, { populate });
88
+ return entry;
89
+ };
90
+ const getEntryValidStatus = async (contentTypeUid, entry, { strapi: strapi2 } = { strapi: global.strapi }) => {
91
+ try {
92
+ await strapi2.entityValidator.validateEntityCreation(
93
+ strapi2.getModel(contentTypeUid),
94
+ entry,
95
+ void 0,
96
+ // @ts-expect-error - FIXME: entity here is unnecessary
97
+ entry
98
+ );
99
+ return true;
100
+ } catch {
101
+ return false;
102
+ }
103
+ };
104
+ async function deleteActionsOnDisableDraftAndPublish({
105
+ oldContentTypes,
106
+ contentTypes: contentTypes2
107
+ }) {
108
+ if (!oldContentTypes) {
109
+ return;
110
+ }
111
+ for (const uid in contentTypes2) {
112
+ if (!oldContentTypes[uid]) {
113
+ continue;
114
+ }
115
+ const oldContentType = oldContentTypes[uid];
116
+ const contentType = contentTypes2[uid];
117
+ if (utils.contentTypes.hasDraftAndPublish(oldContentType) && !utils.contentTypes.hasDraftAndPublish(contentType)) {
118
+ await strapi.db?.queryBuilder(RELEASE_ACTION_MODEL_UID).delete().where({ contentType: uid }).execute();
119
+ }
120
+ }
121
+ }
122
+ async function deleteActionsOnDeleteContentType({ oldContentTypes, contentTypes: contentTypes2 }) {
123
+ const deletedContentTypes = lodash.difference(lodash.keys(oldContentTypes), lodash.keys(contentTypes2)) ?? [];
124
+ if (deletedContentTypes.length) {
125
+ await utils.mapAsync(deletedContentTypes, async (deletedContentTypeUID) => {
126
+ return strapi.db?.queryBuilder(RELEASE_ACTION_MODEL_UID).delete().where({ contentType: deletedContentTypeUID }).execute();
127
+ });
128
+ }
129
+ }
130
+ async function migrateIsValidAndStatusReleases() {
131
+ const releasesWithoutStatus = await strapi.db.query(RELEASE_MODEL_UID).findMany({
132
+ where: {
133
+ status: null,
134
+ releasedAt: null
135
+ },
136
+ populate: {
137
+ actions: {
138
+ populate: {
139
+ entry: true
140
+ }
141
+ }
142
+ }
143
+ });
144
+ utils.mapAsync(releasesWithoutStatus, async (release2) => {
145
+ const actions = release2.actions;
146
+ const notValidatedActions = actions.filter((action) => action.isEntryValid === null);
147
+ for (const action of notValidatedActions) {
148
+ if (action.entry) {
149
+ const populatedEntry = await getPopulatedEntry(action.contentType, action.entry.id, {
150
+ strapi
151
+ });
152
+ if (populatedEntry) {
153
+ const isEntryValid = getEntryValidStatus(action.contentType, populatedEntry, { strapi });
154
+ await strapi.db.query(RELEASE_ACTION_MODEL_UID).update({
155
+ where: {
156
+ id: action.id
157
+ },
158
+ data: {
159
+ isEntryValid
160
+ }
161
+ });
162
+ }
163
+ }
164
+ }
165
+ return getService("release", { strapi }).updateReleaseStatus(release2.id);
166
+ });
167
+ const publishedReleases = await strapi.db.query(RELEASE_MODEL_UID).findMany({
168
+ where: {
169
+ status: null,
170
+ releasedAt: {
171
+ $notNull: true
172
+ }
173
+ }
174
+ });
175
+ utils.mapAsync(publishedReleases, async (release2) => {
176
+ return strapi.db.query(RELEASE_MODEL_UID).update({
177
+ where: {
178
+ id: release2.id
179
+ },
180
+ data: {
181
+ status: "done"
182
+ }
183
+ });
184
+ });
185
+ }
186
+ async function revalidateChangedContentTypes({ oldContentTypes, contentTypes: contentTypes2 }) {
187
+ if (oldContentTypes !== void 0 && contentTypes2 !== void 0) {
188
+ const contentTypesWithDraftAndPublish = Object.keys(oldContentTypes).filter(
189
+ (uid) => oldContentTypes[uid]?.options?.draftAndPublish
190
+ );
191
+ const releasesAffected = /* @__PURE__ */ new Set();
192
+ utils.mapAsync(contentTypesWithDraftAndPublish, async (contentTypeUID) => {
193
+ const oldContentType = oldContentTypes[contentTypeUID];
194
+ const contentType = contentTypes2[contentTypeUID];
195
+ if (!isEqual__default.default(oldContentType?.attributes, contentType?.attributes)) {
196
+ const actions = await strapi.db.query(RELEASE_ACTION_MODEL_UID).findMany({
197
+ where: {
198
+ contentType: contentTypeUID
199
+ },
200
+ populate: {
201
+ entry: true,
202
+ release: true
203
+ }
204
+ });
205
+ await utils.mapAsync(actions, async (action) => {
206
+ if (action.entry && action.release) {
207
+ const populatedEntry = await getPopulatedEntry(contentTypeUID, action.entry.id, {
208
+ strapi
209
+ });
210
+ if (populatedEntry) {
211
+ const isEntryValid = await getEntryValidStatus(contentTypeUID, populatedEntry, {
212
+ strapi
213
+ });
214
+ releasesAffected.add(action.release.id);
215
+ await strapi.db.query(RELEASE_ACTION_MODEL_UID).update({
216
+ where: {
217
+ id: action.id
218
+ },
219
+ data: {
220
+ isEntryValid
221
+ }
222
+ });
223
+ }
224
+ }
225
+ });
226
+ }
227
+ }).then(() => {
228
+ utils.mapAsync(releasesAffected, async (releaseId) => {
229
+ return getService("release", { strapi }).updateReleaseStatus(releaseId);
230
+ });
231
+ });
232
+ }
233
+ }
234
+ async function disableContentTypeLocalized({ oldContentTypes, contentTypes: contentTypes2 }) {
235
+ if (!oldContentTypes) {
236
+ return;
237
+ }
238
+ for (const uid in contentTypes2) {
239
+ if (!oldContentTypes[uid]) {
240
+ continue;
241
+ }
242
+ const oldContentType = oldContentTypes[uid];
243
+ const contentType = contentTypes2[uid];
244
+ const i18nPlugin = strapi.plugin("i18n");
245
+ const { isLocalizedContentType } = i18nPlugin.service("content-types");
246
+ if (isLocalizedContentType(oldContentType) && !isLocalizedContentType(contentType)) {
247
+ await strapi.db.queryBuilder(RELEASE_ACTION_MODEL_UID).update({
248
+ locale: null
249
+ }).where({ contentType: uid }).execute();
250
+ }
251
+ }
252
+ }
253
+ async function enableContentTypeLocalized({ oldContentTypes, contentTypes: contentTypes2 }) {
254
+ if (!oldContentTypes) {
255
+ return;
256
+ }
257
+ for (const uid in contentTypes2) {
258
+ if (!oldContentTypes[uid]) {
259
+ continue;
260
+ }
261
+ const oldContentType = oldContentTypes[uid];
262
+ const contentType = contentTypes2[uid];
263
+ const i18nPlugin = strapi.plugin("i18n");
264
+ const { isLocalizedContentType } = i18nPlugin.service("content-types");
265
+ const { getDefaultLocale } = i18nPlugin.service("locales");
266
+ if (!isLocalizedContentType(oldContentType) && isLocalizedContentType(contentType)) {
267
+ const defaultLocale = await getDefaultLocale();
268
+ await strapi.db.queryBuilder(RELEASE_ACTION_MODEL_UID).update({
269
+ locale: defaultLocale
270
+ }).where({ contentType: uid }).execute();
271
+ }
272
+ }
273
+ }
274
+ const { features: features$2 } = require("@strapi/strapi/dist/utils/ee");
73
275
  const register = async ({ strapi: strapi2 }) => {
74
- if (features$1.isEnabled("cms-content-releases") && strapi2.features.future.isEnabled("contentReleases")) {
276
+ if (features$2.isEnabled("cms-content-releases")) {
75
277
  await strapi2.admin.services.permission.actionProvider.registerMany(ACTIONS);
278
+ strapi2.hook("strapi::content-types.beforeSync").register(deleteActionsOnDisableDraftAndPublish).register(disableContentTypeLocalized);
279
+ strapi2.hook("strapi::content-types.afterSync").register(deleteActionsOnDeleteContentType).register(enableContentTypeLocalized).register(revalidateChangedContentTypes).register(migrateIsValidAndStatusReleases);
280
+ }
281
+ if (strapi2.plugin("graphql")) {
282
+ const graphqlExtensionService = strapi2.plugin("graphql").service("extension");
283
+ graphqlExtensionService.shadowCRUD(RELEASE_MODEL_UID).disable();
284
+ graphqlExtensionService.shadowCRUD(RELEASE_ACTION_MODEL_UID).disable();
285
+ }
286
+ };
287
+ const { features: features$1 } = require("@strapi/strapi/dist/utils/ee");
288
+ const bootstrap = async ({ strapi: strapi2 }) => {
289
+ if (features$1.isEnabled("cms-content-releases")) {
290
+ const contentTypesWithDraftAndPublish = Object.keys(strapi2.contentTypes).filter(
291
+ (uid) => strapi2.contentTypes[uid]?.options?.draftAndPublish
292
+ );
293
+ strapi2.db.lifecycles.subscribe({
294
+ models: contentTypesWithDraftAndPublish,
295
+ async afterDelete(event) {
296
+ try {
297
+ const { model, result } = event;
298
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
299
+ const { id } = result;
300
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
301
+ where: {
302
+ actions: {
303
+ target_type: model.uid,
304
+ target_id: id
305
+ }
306
+ }
307
+ });
308
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
309
+ where: {
310
+ target_type: model.uid,
311
+ target_id: id
312
+ }
313
+ });
314
+ for (const release2 of releases) {
315
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
316
+ }
317
+ }
318
+ } catch (error) {
319
+ strapi2.log.error("Error while deleting release actions after entry delete", { error });
320
+ }
321
+ },
322
+ /**
323
+ * deleteMany hook doesn't return the deleted entries ids
324
+ * so we need to fetch them before deleting the entries to save the ids on our state
325
+ */
326
+ async beforeDeleteMany(event) {
327
+ const { model, params } = event;
328
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
329
+ const { where } = params;
330
+ const entriesToDelete = await strapi2.db.query(model.uid).findMany({ select: ["id"], where });
331
+ event.state.entriesToDelete = entriesToDelete;
332
+ }
333
+ },
334
+ /**
335
+ * We delete the release actions related to deleted entries
336
+ * We make this only after deleteMany is succesfully executed to avoid errors
337
+ */
338
+ async afterDeleteMany(event) {
339
+ try {
340
+ const { model, state } = event;
341
+ const entriesToDelete = state.entriesToDelete;
342
+ if (entriesToDelete) {
343
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
344
+ where: {
345
+ actions: {
346
+ target_type: model.uid,
347
+ target_id: {
348
+ $in: entriesToDelete.map(
349
+ (entry) => entry.id
350
+ )
351
+ }
352
+ }
353
+ }
354
+ });
355
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
356
+ where: {
357
+ target_type: model.uid,
358
+ target_id: {
359
+ $in: entriesToDelete.map((entry) => entry.id)
360
+ }
361
+ }
362
+ });
363
+ for (const release2 of releases) {
364
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
365
+ }
366
+ }
367
+ } catch (error) {
368
+ strapi2.log.error("Error while deleting release actions after entry deleteMany", {
369
+ error
370
+ });
371
+ }
372
+ },
373
+ async afterUpdate(event) {
374
+ try {
375
+ const { model, result } = event;
376
+ if (model.kind === "collectionType" && model.options?.draftAndPublish) {
377
+ const isEntryValid = await getEntryValidStatus(
378
+ model.uid,
379
+ result,
380
+ {
381
+ strapi: strapi2
382
+ }
383
+ );
384
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
385
+ where: {
386
+ target_type: model.uid,
387
+ target_id: result.id
388
+ },
389
+ data: {
390
+ isEntryValid
391
+ }
392
+ });
393
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
394
+ where: {
395
+ actions: {
396
+ target_type: model.uid,
397
+ target_id: result.id
398
+ }
399
+ }
400
+ });
401
+ for (const release2 of releases) {
402
+ getService("release", { strapi: strapi2 }).updateReleaseStatus(release2.id);
403
+ }
404
+ }
405
+ } catch (error) {
406
+ strapi2.log.error("Error while updating release actions after entry update", { error });
407
+ }
408
+ }
409
+ });
410
+ getService("scheduling", { strapi: strapi2 }).syncFromDatabase().catch((err) => {
411
+ strapi2.log.error(
412
+ "Error while syncing scheduled jobs from the database in the content-releases plugin. This could lead to errors in the releases scheduling."
413
+ );
414
+ throw err;
415
+ });
416
+ Object.entries(ALLOWED_WEBHOOK_EVENTS).forEach(([key, value]) => {
417
+ strapi2.webhookStore.addAllowedEvent(key, value);
418
+ });
419
+ }
420
+ };
421
+ const destroy = async ({ strapi: strapi2 }) => {
422
+ const scheduledJobs = getService("scheduling", {
423
+ strapi: strapi2
424
+ }).getAll();
425
+ for (const [, job] of scheduledJobs) {
426
+ job.cancel();
76
427
  }
77
428
  };
78
429
  const schema$1 = {
@@ -101,6 +452,17 @@ const schema$1 = {
101
452
  releasedAt: {
102
453
  type: "datetime"
103
454
  },
455
+ scheduledAt: {
456
+ type: "datetime"
457
+ },
458
+ timezone: {
459
+ type: "string"
460
+ },
461
+ status: {
462
+ type: "enumeration",
463
+ enum: ["ready", "blocked", "failed", "done", "empty"],
464
+ required: true
465
+ },
104
466
  actions: {
105
467
  type: "relation",
106
468
  relation: "oneToMany",
@@ -153,6 +515,9 @@ const schema = {
153
515
  relation: "manyToOne",
154
516
  target: RELEASE_MODEL_UID,
155
517
  inversedBy: "actions"
518
+ },
519
+ isEntryValid: {
520
+ type: "boolean"
156
521
  }
157
522
  }
158
523
  };
@@ -163,327 +528,593 @@ const contentTypes = {
163
528
  release: release$1,
164
529
  "release-action": releaseAction$1
165
530
  };
166
- const getService = (name, { strapi: strapi2 } = { strapi: global.strapi }) => {
167
- return strapi2.plugin("content-releases").service(name);
168
- };
169
531
  const getGroupName = (queryValue) => {
170
532
  switch (queryValue) {
171
533
  case "contentType":
172
- return "entry.contentType.displayName";
534
+ return "contentType.displayName";
173
535
  case "action":
174
536
  return "type";
175
537
  case "locale":
176
- return ___default.default.getOr("No locale", "entry.locale.name");
538
+ return ___default.default.getOr("No locale", "locale.name");
177
539
  default:
178
- return "entry.contentType.displayName";
540
+ return "contentType.displayName";
179
541
  }
180
542
  };
181
- const createReleaseService = ({ strapi: strapi2 }) => ({
182
- async create(releaseData, { user }) {
183
- const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
184
- return strapi2.entityService.create(RELEASE_MODEL_UID, {
185
- data: releaseWithCreatorFields
543
+ const createReleaseService = ({ strapi: strapi2 }) => {
544
+ const dispatchWebhook = (event, { isPublished, release: release2, error }) => {
545
+ strapi2.eventHub.emit(event, {
546
+ isPublished,
547
+ error,
548
+ release: release2
186
549
  });
187
- },
188
- async findOne(id, query = {}) {
189
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
190
- ...query
550
+ };
551
+ const publishSingleTypeAction = async (uid, actionType, entryId) => {
552
+ const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
553
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
554
+ const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
555
+ const entry = await strapi2.entityService.findOne(uid, entryId, { populate });
556
+ try {
557
+ if (actionType === "publish") {
558
+ await entityManagerService.publish(entry, uid);
559
+ } else {
560
+ await entityManagerService.unpublish(entry, uid);
561
+ }
562
+ } catch (error) {
563
+ if (error instanceof utils.errors.ApplicationError && (error.message === "already.published" || error.message === "already.draft"))
564
+ ;
565
+ else {
566
+ throw error;
567
+ }
568
+ }
569
+ };
570
+ const publishCollectionTypeAction = async (uid, entriesToPublishIds, entriestoUnpublishIds) => {
571
+ const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
572
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
573
+ const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
574
+ const entriesToPublish = await strapi2.entityService.findMany(uid, {
575
+ filters: {
576
+ id: {
577
+ $in: entriesToPublishIds
578
+ }
579
+ },
580
+ populate
191
581
  });
192
- return release2;
193
- },
194
- findPage(query) {
195
- return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
196
- ...query,
582
+ const entriesToUnpublish = await strapi2.entityService.findMany(uid, {
583
+ filters: {
584
+ id: {
585
+ $in: entriestoUnpublishIds
586
+ }
587
+ },
588
+ populate
589
+ });
590
+ if (entriesToPublish.length > 0) {
591
+ await entityManagerService.publishMany(entriesToPublish, uid);
592
+ }
593
+ if (entriesToUnpublish.length > 0) {
594
+ await entityManagerService.unpublishMany(entriesToUnpublish, uid);
595
+ }
596
+ };
597
+ const getFormattedActions = async (releaseId) => {
598
+ const actions = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).findMany({
599
+ where: {
600
+ release: {
601
+ id: releaseId
602
+ }
603
+ },
197
604
  populate: {
198
- actions: {
199
- // @ts-expect-error Ignore missing properties
200
- count: true
605
+ entry: {
606
+ fields: ["id"]
201
607
  }
202
608
  }
203
609
  });
204
- },
205
- async findManyForContentTypeEntry(contentTypeUid, entryId, {
206
- hasEntryAttached
207
- } = {
208
- hasEntryAttached: false
209
- }) {
210
- const whereActions = hasEntryAttached ? {
211
- // Find all Releases where the content type entry is present
212
- actions: {
213
- target_type: contentTypeUid,
214
- target_id: entryId
215
- }
216
- } : {
217
- // Find all Releases where the content type entry is not present
218
- $or: [
219
- {
220
- $not: {
221
- actions: {
610
+ if (actions.length === 0) {
611
+ throw new utils.errors.ValidationError("No entries to publish");
612
+ }
613
+ const collectionTypeActions = {};
614
+ const singleTypeActions = [];
615
+ for (const action of actions) {
616
+ const contentTypeUid = action.contentType;
617
+ if (strapi2.contentTypes[contentTypeUid].kind === "collectionType") {
618
+ if (!collectionTypeActions[contentTypeUid]) {
619
+ collectionTypeActions[contentTypeUid] = {
620
+ entriesToPublishIds: [],
621
+ entriesToUnpublishIds: []
622
+ };
623
+ }
624
+ if (action.type === "publish") {
625
+ collectionTypeActions[contentTypeUid].entriesToPublishIds.push(action.entry.id);
626
+ } else {
627
+ collectionTypeActions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
628
+ }
629
+ } else {
630
+ singleTypeActions.push({
631
+ uid: contentTypeUid,
632
+ action: action.type,
633
+ id: action.entry.id
634
+ });
635
+ }
636
+ }
637
+ return { collectionTypeActions, singleTypeActions };
638
+ };
639
+ return {
640
+ async create(releaseData, { user }) {
641
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
642
+ const {
643
+ validatePendingReleasesLimit,
644
+ validateUniqueNameForPendingRelease,
645
+ validateScheduledAtIsLaterThanNow
646
+ } = getService("release-validation", { strapi: strapi2 });
647
+ await Promise.all([
648
+ validatePendingReleasesLimit(),
649
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name),
650
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
651
+ ]);
652
+ const release2 = await strapi2.entityService.create(RELEASE_MODEL_UID, {
653
+ data: {
654
+ ...releaseWithCreatorFields,
655
+ status: "empty"
656
+ }
657
+ });
658
+ if (releaseWithCreatorFields.scheduledAt) {
659
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
660
+ await schedulingService.set(release2.id, release2.scheduledAt);
661
+ }
662
+ strapi2.telemetry.send("didCreateContentRelease");
663
+ return release2;
664
+ },
665
+ async findOne(id, query = {}) {
666
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
667
+ ...query
668
+ });
669
+ return release2;
670
+ },
671
+ findPage(query) {
672
+ return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
673
+ ...query,
674
+ populate: {
675
+ actions: {
676
+ // @ts-expect-error Ignore missing properties
677
+ count: true
678
+ }
679
+ }
680
+ });
681
+ },
682
+ async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
683
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
684
+ where: {
685
+ actions: {
686
+ target_type: contentTypeUid,
687
+ target_id: entryId
688
+ },
689
+ releasedAt: {
690
+ $null: true
691
+ }
692
+ },
693
+ populate: {
694
+ // Filter the action to get only the content type entry
695
+ actions: {
696
+ where: {
222
697
  target_type: contentTypeUid,
223
698
  target_id: entryId
224
699
  }
225
700
  }
226
- },
227
- {
228
- actions: null
229
701
  }
230
- ]
231
- };
232
- const populateAttachedAction = hasEntryAttached ? {
233
- // Filter the action to get only the content type entry
234
- actions: {
702
+ });
703
+ return releases.map((release2) => {
704
+ if (release2.actions?.length) {
705
+ const [actionForEntry] = release2.actions;
706
+ delete release2.actions;
707
+ return {
708
+ ...release2,
709
+ action: actionForEntry
710
+ };
711
+ }
712
+ return release2;
713
+ });
714
+ },
715
+ async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
716
+ const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
235
717
  where: {
236
- target_type: contentTypeUid,
237
- target_id: entryId
718
+ releasedAt: {
719
+ $null: true
720
+ },
721
+ actions: {
722
+ target_type: contentTypeUid,
723
+ target_id: entryId
724
+ }
238
725
  }
239
- }
240
- } : {};
241
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
242
- where: {
243
- ...whereActions,
244
- releasedAt: {
245
- $null: true
726
+ });
727
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
728
+ where: {
729
+ $or: [
730
+ {
731
+ id: {
732
+ $notIn: releasesRelated.map((release2) => release2.id)
733
+ }
734
+ },
735
+ {
736
+ actions: null
737
+ }
738
+ ],
739
+ releasedAt: {
740
+ $null: true
741
+ }
246
742
  }
247
- },
248
- populate: {
249
- ...populateAttachedAction
743
+ });
744
+ return releases.map((release2) => {
745
+ if (release2.actions?.length) {
746
+ const [actionForEntry] = release2.actions;
747
+ delete release2.actions;
748
+ return {
749
+ ...release2,
750
+ action: actionForEntry
751
+ };
752
+ }
753
+ return release2;
754
+ });
755
+ },
756
+ async update(id, releaseData, { user }) {
757
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(
758
+ releaseData
759
+ );
760
+ const { validateUniqueNameForPendingRelease, validateScheduledAtIsLaterThanNow } = getService(
761
+ "release-validation",
762
+ { strapi: strapi2 }
763
+ );
764
+ await Promise.all([
765
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name, id),
766
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
767
+ ]);
768
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id);
769
+ if (!release2) {
770
+ throw new utils.errors.NotFoundError(`No release found for id ${id}`);
250
771
  }
251
- });
252
- return releases.map((release2) => {
253
- if (release2.actions?.length) {
254
- const [actionForEntry] = release2.actions;
255
- delete release2.actions;
256
- return {
257
- ...release2,
258
- action: actionForEntry
259
- };
772
+ if (release2.releasedAt) {
773
+ throw new utils.errors.ValidationError("Release already published");
260
774
  }
261
- return release2;
262
- });
263
- },
264
- async update(id, releaseData, { user }) {
265
- const updatedRelease = await utils.setCreatorFields({ user, isEdition: true })(releaseData);
266
- const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
267
- /*
268
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
269
- * is not compatible with the type we are passing here: UpdateRelease.Request['body']
270
- */
271
- // @ts-expect-error see above
272
- data: updatedRelease
273
- });
274
- if (!release2) {
275
- throw new utils.errors.NotFoundError(`No release found for id ${id}`);
276
- }
277
- return release2;
278
- },
279
- async createAction(releaseId, action) {
280
- const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
281
- strapi: strapi2
282
- });
283
- await Promise.all([
284
- validateEntryContentType(action.entry.contentType),
285
- validateUniqueEntry(releaseId, action)
286
- ]);
287
- const { entry, type } = action;
288
- return strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
289
- data: {
290
- type,
291
- contentType: entry.contentType,
292
- locale: entry.locale,
293
- entry: {
294
- id: entry.id,
295
- __type: entry.contentType,
296
- __pivot: { field: "entry" }
297
- },
298
- release: releaseId
299
- },
300
- populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
301
- });
302
- },
303
- async findActions(releaseId, query) {
304
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
305
- fields: ["id"]
306
- });
307
- if (!release2) {
308
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
309
- }
310
- return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
311
- ...query,
312
- populate: {
313
- entry: true
314
- },
315
- filters: {
316
- release: releaseId
775
+ const updatedRelease = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
776
+ /*
777
+ * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
778
+ * is not compatible with the type we are passing here: UpdateRelease.Request['body']
779
+ */
780
+ // @ts-expect-error see above
781
+ data: releaseWithCreatorFields
782
+ });
783
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
784
+ if (releaseData.scheduledAt) {
785
+ await schedulingService.set(id, releaseData.scheduledAt);
786
+ } else if (release2.scheduledAt) {
787
+ schedulingService.cancel(id);
317
788
  }
318
- });
319
- },
320
- async countActions(query) {
321
- return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
322
- },
323
- async groupActions(actions, groupBy) {
324
- const contentTypeUids = actions.reduce((acc, action) => {
325
- if (!acc.includes(action.contentType)) {
326
- acc.push(action.contentType);
789
+ this.updateReleaseStatus(id);
790
+ strapi2.telemetry.send("didUpdateContentRelease");
791
+ return updatedRelease;
792
+ },
793
+ async createAction(releaseId, action) {
794
+ const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
795
+ strapi: strapi2
796
+ });
797
+ await Promise.all([
798
+ validateEntryContentType(action.entry.contentType),
799
+ validateUniqueEntry(releaseId, action)
800
+ ]);
801
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId);
802
+ if (!release2) {
803
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
327
804
  }
328
- return acc;
329
- }, []);
330
- const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
331
- contentTypeUids
332
- );
333
- const allLocales = await strapi2.plugin("i18n").service("locales").find();
334
- const allLocalesDictionary = allLocales.reduce((acc, locale) => {
335
- acc[locale.code] = { name: locale.name, code: locale.code };
336
- return acc;
337
- }, {});
338
- const formattedData = actions.map((action) => {
339
- const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
340
- return {
341
- ...action,
342
- entry: {
343
- id: action.entry.id,
344
- contentType: {
345
- displayName,
346
- mainFieldValue: action.entry[mainField]
805
+ if (release2.releasedAt) {
806
+ throw new utils.errors.ValidationError("Release already published");
807
+ }
808
+ const { entry, type } = action;
809
+ const populatedEntry = await getPopulatedEntry(entry.contentType, entry.id, { strapi: strapi2 });
810
+ const isEntryValid = await getEntryValidStatus(entry.contentType, populatedEntry, { strapi: strapi2 });
811
+ const releaseAction2 = await strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
812
+ data: {
813
+ type,
814
+ contentType: entry.contentType,
815
+ locale: entry.locale,
816
+ isEntryValid,
817
+ entry: {
818
+ id: entry.id,
819
+ __type: entry.contentType,
820
+ __pivot: { field: "entry" }
347
821
  },
348
- locale: action.locale ? allLocalesDictionary[action.locale] : null,
349
- status: action.entry.publishedAt ? "published" : "draft"
350
- }
351
- };
352
- });
353
- const groupName = getGroupName(groupBy);
354
- return ___default.default.groupBy(groupName)(formattedData);
355
- },
356
- async getContentTypesDataForActions(contentTypesUids) {
357
- const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
358
- const contentTypesData = {};
359
- for (const contentTypeUid of contentTypesUids) {
360
- const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
361
- uid: contentTypeUid
822
+ release: releaseId
823
+ },
824
+ populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
362
825
  });
363
- contentTypesData[contentTypeUid] = {
364
- mainField: contentTypeConfig.settings.mainField,
365
- displayName: strapi2.getModel(contentTypeUid).info.displayName
366
- };
367
- }
368
- return contentTypesData;
369
- },
370
- async delete(releaseId) {
371
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
372
- populate: {
373
- actions: {
374
- fields: ["id"]
375
- }
826
+ this.updateReleaseStatus(releaseId);
827
+ return releaseAction2;
828
+ },
829
+ async findActions(releaseId, query) {
830
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
831
+ fields: ["id"]
832
+ });
833
+ if (!release2) {
834
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
376
835
  }
377
- });
378
- if (!release2) {
379
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
380
- }
381
- if (release2.releasedAt) {
382
- throw new utils.errors.ValidationError("Release already published");
383
- }
384
- await strapi2.db.transaction(async () => {
385
- await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
386
- where: {
387
- id: {
388
- $in: release2.actions.map((action) => action.id)
836
+ return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
837
+ ...query,
838
+ populate: {
839
+ entry: {
840
+ populate: "*"
389
841
  }
842
+ },
843
+ filters: {
844
+ release: releaseId
390
845
  }
391
846
  });
392
- await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
393
- });
394
- return release2;
395
- },
396
- async publish(releaseId) {
397
- const releaseWithPopulatedActionEntries = await strapi2.entityService.findOne(
398
- RELEASE_MODEL_UID,
399
- releaseId,
400
- {
847
+ },
848
+ async countActions(query) {
849
+ return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
850
+ },
851
+ async groupActions(actions, groupBy) {
852
+ const contentTypeUids = actions.reduce((acc, action) => {
853
+ if (!acc.includes(action.contentType)) {
854
+ acc.push(action.contentType);
855
+ }
856
+ return acc;
857
+ }, []);
858
+ const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
859
+ contentTypeUids
860
+ );
861
+ const allLocalesDictionary = await this.getLocalesDataForActions();
862
+ const formattedData = actions.map((action) => {
863
+ const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
864
+ return {
865
+ ...action,
866
+ locale: action.locale ? allLocalesDictionary[action.locale] : null,
867
+ contentType: {
868
+ displayName,
869
+ mainFieldValue: action.entry[mainField],
870
+ uid: action.contentType
871
+ }
872
+ };
873
+ });
874
+ const groupName = getGroupName(groupBy);
875
+ return ___default.default.groupBy(groupName)(formattedData);
876
+ },
877
+ async getLocalesDataForActions() {
878
+ if (!strapi2.plugin("i18n")) {
879
+ return {};
880
+ }
881
+ const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
882
+ return allLocales.reduce((acc, locale) => {
883
+ acc[locale.code] = { name: locale.name, code: locale.code };
884
+ return acc;
885
+ }, {});
886
+ },
887
+ async getContentTypesDataForActions(contentTypesUids) {
888
+ const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
889
+ const contentTypesData = {};
890
+ for (const contentTypeUid of contentTypesUids) {
891
+ const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
892
+ uid: contentTypeUid
893
+ });
894
+ contentTypesData[contentTypeUid] = {
895
+ mainField: contentTypeConfig.settings.mainField,
896
+ displayName: strapi2.getModel(contentTypeUid).info.displayName
897
+ };
898
+ }
899
+ return contentTypesData;
900
+ },
901
+ getContentTypeModelsFromActions(actions) {
902
+ const contentTypeUids = actions.reduce((acc, action) => {
903
+ if (!acc.includes(action.contentType)) {
904
+ acc.push(action.contentType);
905
+ }
906
+ return acc;
907
+ }, []);
908
+ const contentTypeModelsMap = contentTypeUids.reduce(
909
+ (acc, contentTypeUid) => {
910
+ acc[contentTypeUid] = strapi2.getModel(contentTypeUid);
911
+ return acc;
912
+ },
913
+ {}
914
+ );
915
+ return contentTypeModelsMap;
916
+ },
917
+ async getAllComponents() {
918
+ const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
919
+ const components = await contentManagerComponentsService.findAllComponents();
920
+ const componentsMap = components.reduce(
921
+ (acc, component) => {
922
+ acc[component.uid] = component;
923
+ return acc;
924
+ },
925
+ {}
926
+ );
927
+ return componentsMap;
928
+ },
929
+ async delete(releaseId) {
930
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
401
931
  populate: {
402
932
  actions: {
403
- populate: {
404
- entry: true
405
- }
933
+ fields: ["id"]
406
934
  }
407
935
  }
936
+ });
937
+ if (!release2) {
938
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
408
939
  }
409
- );
410
- if (!releaseWithPopulatedActionEntries) {
411
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
412
- }
413
- if (releaseWithPopulatedActionEntries.releasedAt) {
414
- throw new utils.errors.ValidationError("Release already published");
415
- }
416
- if (releaseWithPopulatedActionEntries.actions.length === 0) {
417
- throw new utils.errors.ValidationError("No entries to publish");
418
- }
419
- const actions = {};
420
- for (const action of releaseWithPopulatedActionEntries.actions) {
421
- const contentTypeUid = action.contentType;
422
- if (!actions[contentTypeUid]) {
423
- actions[contentTypeUid] = {
424
- publish: [],
425
- unpublish: []
426
- };
940
+ if (release2.releasedAt) {
941
+ throw new utils.errors.ValidationError("Release already published");
427
942
  }
428
- if (action.type === "publish") {
429
- actions[contentTypeUid].publish.push(action.entry);
430
- } else {
431
- actions[contentTypeUid].unpublish.push(action.entry);
943
+ await strapi2.db.transaction(async () => {
944
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
945
+ where: {
946
+ id: {
947
+ $in: release2.actions.map((action) => action.id)
948
+ }
949
+ }
950
+ });
951
+ await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
952
+ });
953
+ if (release2.scheduledAt) {
954
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
955
+ await schedulingService.cancel(release2.id);
432
956
  }
433
- }
434
- const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
435
- await strapi2.db.transaction(async () => {
436
- for (const contentTypeUid of Object.keys(actions)) {
437
- const { publish, unpublish } = actions[contentTypeUid];
438
- if (publish.length > 0) {
439
- await entityManagerService.publishMany(publish, contentTypeUid);
957
+ strapi2.telemetry.send("didDeleteContentRelease");
958
+ return release2;
959
+ },
960
+ async publish(releaseId) {
961
+ const {
962
+ release: release2,
963
+ error
964
+ } = await strapi2.db.transaction(async ({ trx }) => {
965
+ const lockedRelease = await strapi2.db?.queryBuilder(RELEASE_MODEL_UID).where({ id: releaseId }).select(["id", "name", "releasedAt", "status"]).first().transacting(trx).forUpdate().execute();
966
+ if (!lockedRelease) {
967
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
968
+ }
969
+ if (lockedRelease.releasedAt) {
970
+ throw new utils.errors.ValidationError("Release already published");
440
971
  }
441
- if (unpublish.length > 0) {
442
- await entityManagerService.unpublishMany(unpublish, contentTypeUid);
972
+ if (lockedRelease.status === "failed") {
973
+ throw new utils.errors.ValidationError("Release failed to publish");
443
974
  }
975
+ try {
976
+ strapi2.log.info(`[Content Releases] Starting to publish release ${lockedRelease.name}`);
977
+ const { collectionTypeActions, singleTypeActions } = await getFormattedActions(
978
+ releaseId
979
+ );
980
+ await strapi2.db.transaction(async () => {
981
+ for (const { uid, action, id } of singleTypeActions) {
982
+ await publishSingleTypeAction(uid, action, id);
983
+ }
984
+ for (const contentTypeUid of Object.keys(collectionTypeActions)) {
985
+ const uid = contentTypeUid;
986
+ await publishCollectionTypeAction(
987
+ uid,
988
+ collectionTypeActions[uid].entriesToPublishIds,
989
+ collectionTypeActions[uid].entriesToUnpublishIds
990
+ );
991
+ }
992
+ });
993
+ const release22 = await strapi2.db.query(RELEASE_MODEL_UID).update({
994
+ where: {
995
+ id: releaseId
996
+ },
997
+ data: {
998
+ status: "done",
999
+ releasedAt: /* @__PURE__ */ new Date()
1000
+ }
1001
+ });
1002
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
1003
+ isPublished: true,
1004
+ release: release22
1005
+ });
1006
+ strapi2.telemetry.send("didPublishContentRelease");
1007
+ return { release: release22, error: null };
1008
+ } catch (error2) {
1009
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
1010
+ isPublished: false,
1011
+ error: error2
1012
+ });
1013
+ await strapi2.db?.queryBuilder(RELEASE_MODEL_UID).where({ id: releaseId }).update({
1014
+ status: "failed"
1015
+ }).transacting(trx).execute();
1016
+ return {
1017
+ release: null,
1018
+ error: error2
1019
+ };
1020
+ }
1021
+ });
1022
+ if (error) {
1023
+ throw error;
444
1024
  }
445
- });
446
- const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, releaseId, {
447
- data: {
448
- /*
449
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">> looks like it's wrong
450
- */
451
- // @ts-expect-error see above
452
- releasedAt: /* @__PURE__ */ new Date()
1025
+ return release2;
1026
+ },
1027
+ async updateAction(actionId, releaseId, update) {
1028
+ const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
1029
+ where: {
1030
+ id: actionId,
1031
+ release: {
1032
+ id: releaseId,
1033
+ releasedAt: {
1034
+ $null: true
1035
+ }
1036
+ }
1037
+ },
1038
+ data: update
1039
+ });
1040
+ if (!updatedAction) {
1041
+ throw new utils.errors.NotFoundError(
1042
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
1043
+ );
453
1044
  }
454
- });
455
- return release2;
456
- },
457
- async updateAction(actionId, releaseId, update) {
458
- const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
459
- where: {
460
- id: actionId,
461
- release: releaseId
462
- },
463
- data: update
464
- });
465
- if (!updatedAction) {
466
- throw new utils.errors.NotFoundError(
467
- `Action with id ${actionId} not found in release with id ${releaseId}`
468
- );
469
- }
470
- return updatedAction;
471
- },
472
- async deleteAction(actionId, releaseId) {
473
- const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
474
- where: {
475
- id: actionId,
476
- release: releaseId
1045
+ return updatedAction;
1046
+ },
1047
+ async deleteAction(actionId, releaseId) {
1048
+ const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
1049
+ where: {
1050
+ id: actionId,
1051
+ release: {
1052
+ id: releaseId,
1053
+ releasedAt: {
1054
+ $null: true
1055
+ }
1056
+ }
1057
+ }
1058
+ });
1059
+ if (!deletedAction) {
1060
+ throw new utils.errors.NotFoundError(
1061
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
1062
+ );
477
1063
  }
478
- });
479
- if (!deletedAction) {
480
- throw new utils.errors.NotFoundError(
481
- `Action with id ${actionId} not found in release with id ${releaseId}`
482
- );
1064
+ this.updateReleaseStatus(releaseId);
1065
+ return deletedAction;
1066
+ },
1067
+ async updateReleaseStatus(releaseId) {
1068
+ const [totalActions, invalidActions] = await Promise.all([
1069
+ this.countActions({
1070
+ filters: {
1071
+ release: releaseId
1072
+ }
1073
+ }),
1074
+ this.countActions({
1075
+ filters: {
1076
+ release: releaseId,
1077
+ isEntryValid: false
1078
+ }
1079
+ })
1080
+ ]);
1081
+ if (totalActions > 0) {
1082
+ if (invalidActions > 0) {
1083
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1084
+ where: {
1085
+ id: releaseId
1086
+ },
1087
+ data: {
1088
+ status: "blocked"
1089
+ }
1090
+ });
1091
+ }
1092
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1093
+ where: {
1094
+ id: releaseId
1095
+ },
1096
+ data: {
1097
+ status: "ready"
1098
+ }
1099
+ });
1100
+ }
1101
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1102
+ where: {
1103
+ id: releaseId
1104
+ },
1105
+ data: {
1106
+ status: "empty"
1107
+ }
1108
+ });
483
1109
  }
484
- return deletedAction;
1110
+ };
1111
+ };
1112
+ class AlreadyOnReleaseError extends utils.errors.ApplicationError {
1113
+ constructor(message) {
1114
+ super(message);
1115
+ this.name = "AlreadyOnReleaseError";
485
1116
  }
486
- });
1117
+ }
487
1118
  const createReleaseValidationService = ({ strapi: strapi2 }) => ({
488
1119
  async validateUniqueEntry(releaseId, releaseActionArgs) {
489
1120
  const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
@@ -496,7 +1127,7 @@ const createReleaseValidationService = ({ strapi: strapi2 }) => ({
496
1127
  (action) => Number(action.entry.id) === Number(releaseActionArgs.entry.id) && action.contentType === releaseActionArgs.entry.contentType
497
1128
  );
498
1129
  if (isEntryInRelease) {
499
- throw new utils.errors.ValidationError(
1130
+ throw new AlreadyOnReleaseError(
500
1131
  `Entry with id ${releaseActionArgs.entry.id} and contentType ${releaseActionArgs.entry.contentType} already exists in release with id ${releaseId}`
501
1132
  );
502
1133
  }
@@ -511,11 +1142,120 @@ const createReleaseValidationService = ({ strapi: strapi2 }) => ({
511
1142
  `Content type with uid ${contentTypeUid} does not have draftAndPublish enabled`
512
1143
  );
513
1144
  }
1145
+ },
1146
+ async validatePendingReleasesLimit() {
1147
+ const maximumPendingReleases = (
1148
+ // @ts-expect-error - options is not typed into features
1149
+ EE__default.default.features.get("cms-content-releases")?.options?.maximumReleases || 3
1150
+ );
1151
+ const [, pendingReleasesCount] = await strapi2.db.query(RELEASE_MODEL_UID).findWithCount({
1152
+ filters: {
1153
+ releasedAt: {
1154
+ $null: true
1155
+ }
1156
+ }
1157
+ });
1158
+ if (pendingReleasesCount >= maximumPendingReleases) {
1159
+ throw new utils.errors.ValidationError("You have reached the maximum number of pending releases");
1160
+ }
1161
+ },
1162
+ async validateUniqueNameForPendingRelease(name, id) {
1163
+ const pendingReleases = await strapi2.entityService.findMany(RELEASE_MODEL_UID, {
1164
+ filters: {
1165
+ releasedAt: {
1166
+ $null: true
1167
+ },
1168
+ name,
1169
+ ...id && { id: { $ne: id } }
1170
+ }
1171
+ });
1172
+ const isNameUnique = pendingReleases.length === 0;
1173
+ if (!isNameUnique) {
1174
+ throw new utils.errors.ValidationError(`Release with name ${name} already exists`);
1175
+ }
1176
+ },
1177
+ async validateScheduledAtIsLaterThanNow(scheduledAt) {
1178
+ if (scheduledAt && new Date(scheduledAt) <= /* @__PURE__ */ new Date()) {
1179
+ throw new utils.errors.ValidationError("Scheduled at must be later than now");
1180
+ }
514
1181
  }
515
1182
  });
516
- const services = { release: createReleaseService, "release-validation": createReleaseValidationService };
1183
+ const createSchedulingService = ({ strapi: strapi2 }) => {
1184
+ const scheduledJobs = /* @__PURE__ */ new Map();
1185
+ return {
1186
+ async set(releaseId, scheduleDate) {
1187
+ const release2 = await strapi2.db.query(RELEASE_MODEL_UID).findOne({ where: { id: releaseId, releasedAt: null } });
1188
+ if (!release2) {
1189
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
1190
+ }
1191
+ const job = nodeSchedule.scheduleJob(scheduleDate, async () => {
1192
+ try {
1193
+ await getService("release").publish(releaseId);
1194
+ } catch (error) {
1195
+ }
1196
+ this.cancel(releaseId);
1197
+ });
1198
+ if (scheduledJobs.has(releaseId)) {
1199
+ this.cancel(releaseId);
1200
+ }
1201
+ scheduledJobs.set(releaseId, job);
1202
+ return scheduledJobs;
1203
+ },
1204
+ cancel(releaseId) {
1205
+ if (scheduledJobs.has(releaseId)) {
1206
+ scheduledJobs.get(releaseId).cancel();
1207
+ scheduledJobs.delete(releaseId);
1208
+ }
1209
+ return scheduledJobs;
1210
+ },
1211
+ getAll() {
1212
+ return scheduledJobs;
1213
+ },
1214
+ /**
1215
+ * On bootstrap, we can use this function to make sure to sync the scheduled jobs from the database that are not yet released
1216
+ * This is useful in case the server was restarted and the scheduled jobs were lost
1217
+ * This also could be used to sync different Strapi instances in case of a cluster
1218
+ */
1219
+ async syncFromDatabase() {
1220
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
1221
+ where: {
1222
+ scheduledAt: {
1223
+ $gte: /* @__PURE__ */ new Date()
1224
+ },
1225
+ releasedAt: null
1226
+ }
1227
+ });
1228
+ for (const release2 of releases) {
1229
+ this.set(release2.id, release2.scheduledAt);
1230
+ }
1231
+ return scheduledJobs;
1232
+ }
1233
+ };
1234
+ };
1235
+ const services = {
1236
+ release: createReleaseService,
1237
+ "release-validation": createReleaseValidationService,
1238
+ scheduling: createSchedulingService
1239
+ };
517
1240
  const RELEASE_SCHEMA = yup__namespace.object().shape({
518
- name: yup__namespace.string().trim().required()
1241
+ name: yup__namespace.string().trim().required(),
1242
+ scheduledAt: yup__namespace.string().nullable(),
1243
+ isScheduled: yup__namespace.boolean().optional(),
1244
+ time: yup__namespace.string().when("isScheduled", {
1245
+ is: true,
1246
+ then: yup__namespace.string().trim().required(),
1247
+ otherwise: yup__namespace.string().nullable()
1248
+ }),
1249
+ timezone: yup__namespace.string().when("isScheduled", {
1250
+ is: true,
1251
+ then: yup__namespace.string().required().nullable(),
1252
+ otherwise: yup__namespace.string().nullable()
1253
+ }),
1254
+ date: yup__namespace.string().when("isScheduled", {
1255
+ is: true,
1256
+ then: yup__namespace.string().required().nullable(),
1257
+ otherwise: yup__namespace.string().nullable()
1258
+ })
519
1259
  }).required().noUnknown();
520
1260
  const validateRelease = utils.validateYupSchema(RELEASE_SCHEMA);
521
1261
  const releaseController = {
@@ -532,9 +1272,7 @@ const releaseController = {
532
1272
  const contentTypeUid = query.contentTypeUid;
533
1273
  const entryId = query.entryId;
534
1274
  const hasEntryAttached = typeof query.hasEntryAttached === "string" ? JSON.parse(query.hasEntryAttached) : false;
535
- const data = await releaseService.findManyForContentTypeEntry(contentTypeUid, entryId, {
536
- hasEntryAttached
537
- });
1275
+ const data = hasEntryAttached ? await releaseService.findManyWithContentTypeEntryAttached(contentTypeUid, entryId) : await releaseService.findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId);
538
1276
  ctx.body = { data };
539
1277
  } else {
540
1278
  const query = await permissionsManager.sanitizeQuery(ctx.query);
@@ -550,26 +1288,30 @@ const releaseController = {
550
1288
  }
551
1289
  };
552
1290
  });
553
- ctx.body = { data, meta: { pagination } };
1291
+ const pendingReleasesCount = await strapi.query(RELEASE_MODEL_UID).count({
1292
+ where: {
1293
+ releasedAt: null
1294
+ }
1295
+ });
1296
+ ctx.body = { data, meta: { pagination, pendingReleasesCount } };
554
1297
  }
555
1298
  },
556
1299
  async findOne(ctx) {
557
1300
  const id = ctx.params.id;
558
1301
  const releaseService = getService("release", { strapi });
559
1302
  const release2 = await releaseService.findOne(id, { populate: ["createdBy"] });
560
- const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
561
- ability: ctx.state.userAbility,
562
- model: RELEASE_MODEL_UID
563
- });
564
- const sanitizedRelease = await permissionsManager.sanitizeOutput(release2);
1303
+ if (!release2) {
1304
+ throw new utils.errors.NotFoundError(`Release not found for id: ${id}`);
1305
+ }
565
1306
  const count = await releaseService.countActions({
566
1307
  filters: {
567
1308
  release: id
568
1309
  }
569
1310
  });
570
- if (!release2) {
571
- throw new utils.errors.NotFoundError(`Release not found for id: ${id}`);
572
- }
1311
+ const sanitizedRelease = {
1312
+ ...release2,
1313
+ createdBy: release2.createdBy ? strapi.admin.services.user.sanitizeUser(release2.createdBy) : null
1314
+ };
573
1315
  const data = {
574
1316
  ...sanitizedRelease,
575
1317
  actions: {
@@ -622,8 +1364,27 @@ const releaseController = {
622
1364
  const id = ctx.params.id;
623
1365
  const releaseService = getService("release", { strapi });
624
1366
  const release2 = await releaseService.publish(id, { user });
1367
+ const [countPublishActions, countUnpublishActions] = await Promise.all([
1368
+ releaseService.countActions({
1369
+ filters: {
1370
+ release: id,
1371
+ type: "publish"
1372
+ }
1373
+ }),
1374
+ releaseService.countActions({
1375
+ filters: {
1376
+ release: id,
1377
+ type: "unpublish"
1378
+ }
1379
+ })
1380
+ ]);
625
1381
  ctx.body = {
626
- data: release2
1382
+ data: release2,
1383
+ meta: {
1384
+ totalEntries: countPublishActions + countUnpublishActions,
1385
+ totalPublishedEntries: countPublishActions,
1386
+ totalUnpublishedEntries: countUnpublishActions
1387
+ }
627
1388
  };
628
1389
  }
629
1390
  };
@@ -650,6 +1411,38 @@ const releaseActionController = {
650
1411
  data: releaseAction2
651
1412
  };
652
1413
  },
1414
+ async createMany(ctx) {
1415
+ const releaseId = ctx.params.releaseId;
1416
+ const releaseActionsArgs = ctx.request.body;
1417
+ await Promise.all(
1418
+ releaseActionsArgs.map((releaseActionArgs) => validateReleaseAction(releaseActionArgs))
1419
+ );
1420
+ const releaseService = getService("release", { strapi });
1421
+ const releaseActions = await strapi.db.transaction(async () => {
1422
+ const releaseActions2 = await Promise.all(
1423
+ releaseActionsArgs.map(async (releaseActionArgs) => {
1424
+ try {
1425
+ const action = await releaseService.createAction(releaseId, releaseActionArgs);
1426
+ return action;
1427
+ } catch (error) {
1428
+ if (error instanceof AlreadyOnReleaseError) {
1429
+ return null;
1430
+ }
1431
+ throw error;
1432
+ }
1433
+ })
1434
+ );
1435
+ return releaseActions2;
1436
+ });
1437
+ const newReleaseActions = releaseActions.filter((action) => action !== null);
1438
+ ctx.body = {
1439
+ data: newReleaseActions,
1440
+ meta: {
1441
+ entriesAlreadyInRelease: releaseActions.length - newReleaseActions.length,
1442
+ totalEntries: releaseActions.length
1443
+ }
1444
+ };
1445
+ },
653
1446
  async findMany(ctx) {
654
1447
  const releaseId = ctx.params.releaseId;
655
1448
  const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
@@ -662,11 +1455,30 @@ const releaseActionController = {
662
1455
  sort: query.groupBy === "action" ? "type" : query.groupBy,
663
1456
  ...query
664
1457
  });
665
- const groupedData = await releaseService.groupActions(results, query.groupBy);
1458
+ const contentTypeOutputSanitizers = results.reduce((acc, action) => {
1459
+ if (acc[action.contentType]) {
1460
+ return acc;
1461
+ }
1462
+ const contentTypePermissionsManager = strapi.admin.services.permission.createPermissionsManager({
1463
+ ability: ctx.state.userAbility,
1464
+ model: action.contentType
1465
+ });
1466
+ acc[action.contentType] = contentTypePermissionsManager.sanitizeOutput;
1467
+ return acc;
1468
+ }, {});
1469
+ const sanitizedResults = await utils.mapAsync(results, async (action) => ({
1470
+ ...action,
1471
+ entry: await contentTypeOutputSanitizers[action.contentType](action.entry)
1472
+ }));
1473
+ const groupedData = await releaseService.groupActions(sanitizedResults, query.groupBy);
1474
+ const contentTypes2 = releaseService.getContentTypeModelsFromActions(results);
1475
+ const components = await releaseService.getAllComponents();
666
1476
  ctx.body = {
667
1477
  data: groupedData,
668
1478
  meta: {
669
- pagination
1479
+ pagination,
1480
+ contentTypes: contentTypes2,
1481
+ components
670
1482
  }
671
1483
  };
672
1484
  },
@@ -688,10 +1500,8 @@ const releaseActionController = {
688
1500
  async delete(ctx) {
689
1501
  const actionId = ctx.params.actionId;
690
1502
  const releaseId = ctx.params.releaseId;
691
- const deletedReleaseAction = await getService("release", { strapi }).deleteAction(
692
- actionId,
693
- releaseId
694
- );
1503
+ const releaseService = getService("release", { strapi });
1504
+ const deletedReleaseAction = await releaseService.deleteAction(actionId, releaseId);
695
1505
  ctx.body = {
696
1506
  data: deletedReleaseAction
697
1507
  };
@@ -818,6 +1628,22 @@ const releaseAction = {
818
1628
  ]
819
1629
  }
820
1630
  },
1631
+ {
1632
+ method: "POST",
1633
+ path: "/:releaseId/actions/bulk",
1634
+ handler: "release-action.createMany",
1635
+ config: {
1636
+ policies: [
1637
+ "admin::isAuthenticatedAdmin",
1638
+ {
1639
+ name: "admin::hasPermissions",
1640
+ config: {
1641
+ actions: ["plugin::content-releases.create-action"]
1642
+ }
1643
+ }
1644
+ ]
1645
+ }
1646
+ },
821
1647
  {
822
1648
  method: "GET",
823
1649
  path: "/:releaseId/actions",
@@ -874,9 +1700,11 @@ const routes = {
874
1700
  };
875
1701
  const { features } = require("@strapi/strapi/dist/utils/ee");
876
1702
  const getPlugin = () => {
877
- if (features.isEnabled("cms-content-releases") && strapi.features.future.isEnabled("contentReleases")) {
1703
+ if (features.isEnabled("cms-content-releases")) {
878
1704
  return {
879
1705
  register,
1706
+ bootstrap,
1707
+ destroy,
880
1708
  contentTypes,
881
1709
  services,
882
1710
  controllers,
@@ -884,6 +1712,9 @@ const getPlugin = () => {
884
1712
  };
885
1713
  }
886
1714
  return {
1715
+ // Always return register, it handles its own feature check
1716
+ register,
1717
+ // Always return contentTypes to avoid losing data when the feature is disabled
887
1718
  contentTypes
888
1719
  };
889
1720
  };