@strapi/content-releases 0.0.0-next.90a86f595c31de9a89f4255318bfb0cccb30ceed → 0.0.0-next.a9d79bec775daaf0da4e506b2aebafdb4ca95b06

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-8FCxPK8-.mjs → App-bpzO2Ljh.mjs} +616 -365
  2. package/dist/_chunks/App-bpzO2Ljh.mjs.map +1 -0
  3. package/dist/_chunks/{App-pspKUC-W.js → App-p8aKBitd.js} +606 -354
  4. package/dist/_chunks/App-p8aKBitd.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-m9eTk4UF.mjs → en-WuuhP6Bn.mjs} +15 -4
  10. package/dist/_chunks/en-WuuhP6Bn.mjs.map +1 -0
  11. package/dist/_chunks/{en-r9YocBH0.js → en-gcJJ5htG.js} +15 -4
  12. package/dist/_chunks/en-gcJJ5htG.js.map +1 -0
  13. package/dist/_chunks/{index-8aK7GzI5.mjs → index-AECgcaDa.mjs} +99 -22
  14. package/dist/_chunks/index-AECgcaDa.mjs.map +1 -0
  15. package/dist/_chunks/{index-nGaPcY9m.js → index-fP3qoWZ4.js} +88 -11
  16. package/dist/_chunks/index-fP3qoWZ4.js.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 +818 -432
  20. package/dist/server/index.js.map +1 -1
  21. package/dist/server/index.mjs +817 -432
  22. package/dist/server/index.mjs.map +1 -1
  23. package/package.json +13 -11
  24. package/dist/_chunks/App-8FCxPK8-.mjs.map +0 -1
  25. package/dist/_chunks/App-pspKUC-W.js.map +0 -1
  26. package/dist/_chunks/en-m9eTk4UF.mjs.map +0 -1
  27. package/dist/_chunks/en-r9YocBH0.js.map +0 -1
  28. package/dist/_chunks/index-8aK7GzI5.mjs.map +0 -1
  29. package/dist/_chunks/index-nGaPcY9m.js.map +0 -1
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  const utils = require("@strapi/utils");
3
+ const isEqual = require("lodash/isEqual");
3
4
  const lodash = require("lodash");
4
5
  const _ = require("lodash/fp");
5
6
  const EE = require("@strapi/strapi/dist/utils/ee");
@@ -24,6 +25,7 @@ function _interopNamespace(e) {
24
25
  n.default = e;
25
26
  return Object.freeze(n);
26
27
  }
28
+ const isEqual__default = /* @__PURE__ */ _interopDefault(isEqual);
27
29
  const ___default = /* @__PURE__ */ _interopDefault(_);
28
30
  const EE__default = /* @__PURE__ */ _interopDefault(EE);
29
31
  const yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
@@ -73,6 +75,32 @@ const ACTIONS = [
73
75
  pluginName: "content-releases"
74
76
  }
75
77
  ];
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
+ };
76
104
  async function deleteActionsOnDisableDraftAndPublish({
77
105
  oldContentTypes,
78
106
  contentTypes: contentTypes2
@@ -99,31 +127,196 @@ async function deleteActionsOnDeleteContentType({ oldContentTypes, contentTypes:
99
127
  });
100
128
  }
101
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
+ }
102
274
  const { features: features$2 } = require("@strapi/strapi/dist/utils/ee");
103
275
  const register = async ({ strapi: strapi2 }) => {
104
276
  if (features$2.isEnabled("cms-content-releases")) {
105
277
  await strapi2.admin.services.permission.actionProvider.registerMany(ACTIONS);
106
- strapi2.hook("strapi::content-types.beforeSync").register(deleteActionsOnDisableDraftAndPublish);
107
- strapi2.hook("strapi::content-types.afterSync").register(deleteActionsOnDeleteContentType);
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();
108
285
  }
109
- };
110
- const getService = (name, { strapi: strapi2 } = { strapi: global.strapi }) => {
111
- return strapi2.plugin("content-releases").service(name);
112
286
  };
113
287
  const { features: features$1 } = require("@strapi/strapi/dist/utils/ee");
114
288
  const bootstrap = async ({ strapi: strapi2 }) => {
115
289
  if (features$1.isEnabled("cms-content-releases")) {
290
+ const contentTypesWithDraftAndPublish = Object.keys(strapi2.contentTypes).filter(
291
+ (uid) => strapi2.contentTypes[uid]?.options?.draftAndPublish
292
+ );
116
293
  strapi2.db.lifecycles.subscribe({
117
- afterDelete(event) {
118
- const { model, result } = event;
119
- if (model.kind === "collectionType" && model.options?.draftAndPublish) {
120
- const { id } = result;
121
- strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
122
- where: {
123
- target_type: model.uid,
124
- target_id: id
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);
125
316
  }
126
- });
317
+ }
318
+ } catch (error) {
319
+ strapi2.log.error("Error while deleting release actions after entry delete", { error });
127
320
  }
128
321
  },
129
322
  /**
@@ -143,18 +336,75 @@ const bootstrap = async ({ strapi: strapi2 }) => {
143
336
  * We make this only after deleteMany is succesfully executed to avoid errors
144
337
  */
145
338
  async afterDeleteMany(event) {
146
- const { model, state } = event;
147
- const entriesToDelete = state.entriesToDelete;
148
- if (entriesToDelete) {
149
- await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
150
- where: {
151
- target_type: model.uid,
152
- target_id: {
153
- $in: entriesToDelete.map((entry) => entry.id)
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
+ }
154
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);
155
365
  }
366
+ }
367
+ } catch (error) {
368
+ strapi2.log.error("Error while deleting release actions after entry deleteMany", {
369
+ error
156
370
  });
157
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
+ }
158
408
  }
159
409
  });
160
410
  if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
@@ -164,6 +414,9 @@ const bootstrap = async ({ strapi: strapi2 }) => {
164
414
  );
165
415
  throw err;
166
416
  });
417
+ Object.entries(ALLOWED_WEBHOOK_EVENTS).forEach(([key, value]) => {
418
+ strapi2.webhookStore.addAllowedEvent(key, value);
419
+ });
167
420
  }
168
421
  }
169
422
  };
@@ -206,6 +459,14 @@ const schema$1 = {
206
459
  scheduledAt: {
207
460
  type: "datetime"
208
461
  },
462
+ timezone: {
463
+ type: "string"
464
+ },
465
+ status: {
466
+ type: "enumeration",
467
+ enum: ["ready", "blocked", "failed", "done", "empty"],
468
+ required: true
469
+ },
209
470
  actions: {
210
471
  type: "relation",
211
472
  relation: "oneToMany",
@@ -258,6 +519,9 @@ const schema = {
258
519
  relation: "manyToOne",
259
520
  target: RELEASE_MODEL_UID,
260
521
  inversedBy: "actions"
522
+ },
523
+ isEntryValid: {
524
+ type: "boolean"
261
525
  }
262
526
  }
263
527
  };
@@ -280,464 +544,563 @@ const getGroupName = (queryValue) => {
280
544
  return "contentType.displayName";
281
545
  }
282
546
  };
283
- const createReleaseService = ({ strapi: strapi2 }) => ({
284
- async create(releaseData, { user }) {
285
- const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
286
- const {
287
- validatePendingReleasesLimit,
288
- validateUniqueNameForPendingRelease,
289
- validateScheduledAtIsLaterThanNow
290
- } = getService("release-validation", { strapi: strapi2 });
291
- await Promise.all([
292
- validatePendingReleasesLimit(),
293
- validateUniqueNameForPendingRelease(releaseWithCreatorFields.name),
294
- validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
295
- ]);
296
- const release2 = await strapi2.entityService.create(RELEASE_MODEL_UID, {
297
- data: releaseWithCreatorFields
547
+ const createReleaseService = ({ strapi: strapi2 }) => {
548
+ const dispatchWebhook = (event, { isPublished, release: release2, error }) => {
549
+ strapi2.eventHub.emit(event, {
550
+ isPublished,
551
+ error,
552
+ release: release2
298
553
  });
299
- if (strapi2.features.future.isEnabled("contentReleasesScheduling") && releaseWithCreatorFields.scheduledAt) {
300
- const schedulingService = getService("scheduling", { strapi: strapi2 });
301
- await schedulingService.set(release2.id, release2.scheduledAt);
302
- }
303
- return release2;
304
- },
305
- async findOne(id, query = {}) {
306
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
307
- ...query
308
- });
309
- return release2;
310
- },
311
- findPage(query) {
312
- return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
313
- ...query,
314
- populate: {
315
- actions: {
316
- // @ts-expect-error Ignore missing properties
317
- count: true
554
+ };
555
+ return {
556
+ async create(releaseData, { user }) {
557
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
558
+ const {
559
+ validatePendingReleasesLimit,
560
+ validateUniqueNameForPendingRelease,
561
+ validateScheduledAtIsLaterThanNow
562
+ } = getService("release-validation", { strapi: strapi2 });
563
+ await Promise.all([
564
+ validatePendingReleasesLimit(),
565
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name),
566
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
567
+ ]);
568
+ const release2 = await strapi2.entityService.create(RELEASE_MODEL_UID, {
569
+ data: {
570
+ ...releaseWithCreatorFields,
571
+ status: "empty"
318
572
  }
573
+ });
574
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling") && releaseWithCreatorFields.scheduledAt) {
575
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
576
+ await schedulingService.set(release2.id, release2.scheduledAt);
319
577
  }
320
- });
321
- },
322
- async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
323
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
324
- where: {
325
- actions: {
326
- target_type: contentTypeUid,
327
- target_id: entryId
578
+ strapi2.telemetry.send("didCreateContentRelease");
579
+ return release2;
580
+ },
581
+ async findOne(id, query = {}) {
582
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
583
+ ...query
584
+ });
585
+ return release2;
586
+ },
587
+ findPage(query) {
588
+ return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
589
+ ...query,
590
+ populate: {
591
+ actions: {
592
+ // @ts-expect-error Ignore missing properties
593
+ count: true
594
+ }
595
+ }
596
+ });
597
+ },
598
+ async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
599
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
600
+ where: {
601
+ actions: {
602
+ target_type: contentTypeUid,
603
+ target_id: entryId
604
+ },
605
+ releasedAt: {
606
+ $null: true
607
+ }
328
608
  },
329
- releasedAt: {
330
- $null: true
609
+ populate: {
610
+ // Filter the action to get only the content type entry
611
+ actions: {
612
+ where: {
613
+ target_type: contentTypeUid,
614
+ target_id: entryId
615
+ }
616
+ }
331
617
  }
332
- },
333
- populate: {
334
- // Filter the action to get only the content type entry
335
- actions: {
336
- where: {
618
+ });
619
+ return releases.map((release2) => {
620
+ if (release2.actions?.length) {
621
+ const [actionForEntry] = release2.actions;
622
+ delete release2.actions;
623
+ return {
624
+ ...release2,
625
+ action: actionForEntry
626
+ };
627
+ }
628
+ return release2;
629
+ });
630
+ },
631
+ async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
632
+ const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
633
+ where: {
634
+ releasedAt: {
635
+ $null: true
636
+ },
637
+ actions: {
337
638
  target_type: contentTypeUid,
338
639
  target_id: entryId
339
640
  }
340
641
  }
642
+ });
643
+ const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
644
+ where: {
645
+ $or: [
646
+ {
647
+ id: {
648
+ $notIn: releasesRelated.map((release2) => release2.id)
649
+ }
650
+ },
651
+ {
652
+ actions: null
653
+ }
654
+ ],
655
+ releasedAt: {
656
+ $null: true
657
+ }
658
+ }
659
+ });
660
+ return releases.map((release2) => {
661
+ if (release2.actions?.length) {
662
+ const [actionForEntry] = release2.actions;
663
+ delete release2.actions;
664
+ return {
665
+ ...release2,
666
+ action: actionForEntry
667
+ };
668
+ }
669
+ return release2;
670
+ });
671
+ },
672
+ async update(id, releaseData, { user }) {
673
+ const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(
674
+ releaseData
675
+ );
676
+ const { validateUniqueNameForPendingRelease, validateScheduledAtIsLaterThanNow } = getService(
677
+ "release-validation",
678
+ { strapi: strapi2 }
679
+ );
680
+ await Promise.all([
681
+ validateUniqueNameForPendingRelease(releaseWithCreatorFields.name, id),
682
+ validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
683
+ ]);
684
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id);
685
+ if (!release2) {
686
+ throw new utils.errors.NotFoundError(`No release found for id ${id}`);
341
687
  }
342
- });
343
- return releases.map((release2) => {
344
- if (release2.actions?.length) {
345
- const [actionForEntry] = release2.actions;
346
- delete release2.actions;
347
- return {
348
- ...release2,
349
- action: actionForEntry
350
- };
688
+ if (release2.releasedAt) {
689
+ throw new utils.errors.ValidationError("Release already published");
351
690
  }
352
- return release2;
353
- });
354
- },
355
- async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
356
- const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
357
- where: {
358
- releasedAt: {
359
- $null: true
360
- },
361
- actions: {
362
- target_type: contentTypeUid,
363
- target_id: entryId
691
+ const updatedRelease = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
692
+ /*
693
+ * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
694
+ * is not compatible with the type we are passing here: UpdateRelease.Request['body']
695
+ */
696
+ // @ts-expect-error see above
697
+ data: releaseWithCreatorFields
698
+ });
699
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
700
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
701
+ if (releaseData.scheduledAt) {
702
+ await schedulingService.set(id, releaseData.scheduledAt);
703
+ } else if (release2.scheduledAt) {
704
+ schedulingService.cancel(id);
364
705
  }
365
706
  }
366
- });
367
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
368
- where: {
369
- $or: [
370
- {
371
- id: {
372
- $notIn: releasesRelated.map((release2) => release2.id)
373
- }
374
- },
375
- {
376
- actions: null
377
- }
378
- ],
379
- releasedAt: {
380
- $null: true
381
- }
707
+ this.updateReleaseStatus(id);
708
+ strapi2.telemetry.send("didUpdateContentRelease");
709
+ return updatedRelease;
710
+ },
711
+ async createAction(releaseId, action) {
712
+ const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
713
+ strapi: strapi2
714
+ });
715
+ await Promise.all([
716
+ validateEntryContentType(action.entry.contentType),
717
+ validateUniqueEntry(releaseId, action)
718
+ ]);
719
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId);
720
+ if (!release2) {
721
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
382
722
  }
383
- });
384
- return releases.map((release2) => {
385
- if (release2.actions?.length) {
386
- const [actionForEntry] = release2.actions;
387
- delete release2.actions;
388
- return {
389
- ...release2,
390
- action: actionForEntry
391
- };
723
+ if (release2.releasedAt) {
724
+ throw new utils.errors.ValidationError("Release already published");
392
725
  }
393
- return release2;
394
- });
395
- },
396
- async update(id, releaseData, { user }) {
397
- const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(releaseData);
398
- const { validateUniqueNameForPendingRelease, validateScheduledAtIsLaterThanNow } = getService(
399
- "release-validation",
400
- { strapi: strapi2 }
401
- );
402
- await Promise.all([
403
- validateUniqueNameForPendingRelease(releaseWithCreatorFields.name, id),
404
- validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
405
- ]);
406
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id);
407
- if (!release2) {
408
- throw new utils.errors.NotFoundError(`No release found for id ${id}`);
409
- }
410
- if (release2.releasedAt) {
411
- throw new utils.errors.ValidationError("Release already published");
412
- }
413
- const updatedRelease = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
414
- /*
415
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
416
- * is not compatible with the type we are passing here: UpdateRelease.Request['body']
417
- */
418
- // @ts-expect-error see above
419
- data: releaseWithCreatorFields
420
- });
421
- if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
422
- const schedulingService = getService("scheduling", { strapi: strapi2 });
423
- if (releaseData.scheduledAt) {
424
- await schedulingService.set(id, releaseData.scheduledAt);
425
- } else if (release2.scheduledAt) {
426
- schedulingService.cancel(id);
726
+ const { entry, type } = action;
727
+ const populatedEntry = await getPopulatedEntry(entry.contentType, entry.id, { strapi: strapi2 });
728
+ const isEntryValid = await getEntryValidStatus(entry.contentType, populatedEntry, { strapi: strapi2 });
729
+ const releaseAction2 = await strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
730
+ data: {
731
+ type,
732
+ contentType: entry.contentType,
733
+ locale: entry.locale,
734
+ isEntryValid,
735
+ entry: {
736
+ id: entry.id,
737
+ __type: entry.contentType,
738
+ __pivot: { field: "entry" }
739
+ },
740
+ release: releaseId
741
+ },
742
+ populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
743
+ });
744
+ this.updateReleaseStatus(releaseId);
745
+ return releaseAction2;
746
+ },
747
+ async findActions(releaseId, query) {
748
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
749
+ fields: ["id"]
750
+ });
751
+ if (!release2) {
752
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
427
753
  }
428
- }
429
- return updatedRelease;
430
- },
431
- async createAction(releaseId, action) {
432
- const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
433
- strapi: strapi2
434
- });
435
- await Promise.all([
436
- validateEntryContentType(action.entry.contentType),
437
- validateUniqueEntry(releaseId, action)
438
- ]);
439
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId);
440
- if (!release2) {
441
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
442
- }
443
- if (release2.releasedAt) {
444
- throw new utils.errors.ValidationError("Release already published");
445
- }
446
- const { entry, type } = action;
447
- return strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
448
- data: {
449
- type,
450
- contentType: entry.contentType,
451
- locale: entry.locale,
452
- entry: {
453
- id: entry.id,
454
- __type: entry.contentType,
455
- __pivot: { field: "entry" }
754
+ return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
755
+ ...query,
756
+ populate: {
757
+ entry: {
758
+ populate: "*"
759
+ }
456
760
  },
457
- release: releaseId
458
- },
459
- populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
460
- });
461
- },
462
- async findActions(releaseId, query) {
463
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
464
- fields: ["id"]
465
- });
466
- if (!release2) {
467
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
468
- }
469
- return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
470
- ...query,
471
- populate: {
472
- entry: {
473
- populate: "*"
761
+ filters: {
762
+ release: releaseId
474
763
  }
475
- },
476
- filters: {
477
- release: releaseId
478
- }
479
- });
480
- },
481
- async countActions(query) {
482
- return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
483
- },
484
- async groupActions(actions, groupBy) {
485
- const contentTypeUids = actions.reduce((acc, action) => {
486
- if (!acc.includes(action.contentType)) {
487
- acc.push(action.contentType);
488
- }
489
- return acc;
490
- }, []);
491
- const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
492
- contentTypeUids
493
- );
494
- const allLocalesDictionary = await this.getLocalesDataForActions();
495
- const formattedData = actions.map((action) => {
496
- const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
497
- return {
498
- ...action,
499
- locale: action.locale ? allLocalesDictionary[action.locale] : null,
500
- contentType: {
501
- displayName,
502
- mainFieldValue: action.entry[mainField],
503
- uid: action.contentType
764
+ });
765
+ },
766
+ async countActions(query) {
767
+ return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
768
+ },
769
+ async groupActions(actions, groupBy) {
770
+ const contentTypeUids = actions.reduce((acc, action) => {
771
+ if (!acc.includes(action.contentType)) {
772
+ acc.push(action.contentType);
504
773
  }
505
- };
506
- });
507
- const groupName = getGroupName(groupBy);
508
- return ___default.default.groupBy(groupName)(formattedData);
509
- },
510
- async getLocalesDataForActions() {
511
- if (!strapi2.plugin("i18n")) {
512
- return {};
513
- }
514
- const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
515
- return allLocales.reduce((acc, locale) => {
516
- acc[locale.code] = { name: locale.name, code: locale.code };
517
- return acc;
518
- }, {});
519
- },
520
- async getContentTypesDataForActions(contentTypesUids) {
521
- const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
522
- const contentTypesData = {};
523
- for (const contentTypeUid of contentTypesUids) {
524
- const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
525
- uid: contentTypeUid
774
+ return acc;
775
+ }, []);
776
+ const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
777
+ contentTypeUids
778
+ );
779
+ const allLocalesDictionary = await this.getLocalesDataForActions();
780
+ const formattedData = actions.map((action) => {
781
+ const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
782
+ return {
783
+ ...action,
784
+ locale: action.locale ? allLocalesDictionary[action.locale] : null,
785
+ contentType: {
786
+ displayName,
787
+ mainFieldValue: action.entry[mainField],
788
+ uid: action.contentType
789
+ }
790
+ };
526
791
  });
527
- contentTypesData[contentTypeUid] = {
528
- mainField: contentTypeConfig.settings.mainField,
529
- displayName: strapi2.getModel(contentTypeUid).info.displayName
530
- };
531
- }
532
- return contentTypesData;
533
- },
534
- getContentTypeModelsFromActions(actions) {
535
- const contentTypeUids = actions.reduce((acc, action) => {
536
- if (!acc.includes(action.contentType)) {
537
- acc.push(action.contentType);
792
+ const groupName = getGroupName(groupBy);
793
+ return ___default.default.groupBy(groupName)(formattedData);
794
+ },
795
+ async getLocalesDataForActions() {
796
+ if (!strapi2.plugin("i18n")) {
797
+ return {};
538
798
  }
539
- return acc;
540
- }, []);
541
- const contentTypeModelsMap = contentTypeUids.reduce(
542
- (acc, contentTypeUid) => {
543
- acc[contentTypeUid] = strapi2.getModel(contentTypeUid);
799
+ const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
800
+ return allLocales.reduce((acc, locale) => {
801
+ acc[locale.code] = { name: locale.name, code: locale.code };
544
802
  return acc;
545
- },
546
- {}
547
- );
548
- return contentTypeModelsMap;
549
- },
550
- async getAllComponents() {
551
- const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
552
- const components = await contentManagerComponentsService.findAllComponents();
553
- const componentsMap = components.reduce(
554
- (acc, component) => {
555
- acc[component.uid] = component;
803
+ }, {});
804
+ },
805
+ async getContentTypesDataForActions(contentTypesUids) {
806
+ const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
807
+ const contentTypesData = {};
808
+ for (const contentTypeUid of contentTypesUids) {
809
+ const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
810
+ uid: contentTypeUid
811
+ });
812
+ contentTypesData[contentTypeUid] = {
813
+ mainField: contentTypeConfig.settings.mainField,
814
+ displayName: strapi2.getModel(contentTypeUid).info.displayName
815
+ };
816
+ }
817
+ return contentTypesData;
818
+ },
819
+ getContentTypeModelsFromActions(actions) {
820
+ const contentTypeUids = actions.reduce((acc, action) => {
821
+ if (!acc.includes(action.contentType)) {
822
+ acc.push(action.contentType);
823
+ }
556
824
  return acc;
557
- },
558
- {}
559
- );
560
- return componentsMap;
561
- },
562
- async delete(releaseId) {
563
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
564
- populate: {
565
- actions: {
566
- fields: ["id"]
825
+ }, []);
826
+ const contentTypeModelsMap = contentTypeUids.reduce(
827
+ (acc, contentTypeUid) => {
828
+ acc[contentTypeUid] = strapi2.getModel(contentTypeUid);
829
+ return acc;
830
+ },
831
+ {}
832
+ );
833
+ return contentTypeModelsMap;
834
+ },
835
+ async getAllComponents() {
836
+ const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
837
+ const components = await contentManagerComponentsService.findAllComponents();
838
+ const componentsMap = components.reduce(
839
+ (acc, component) => {
840
+ acc[component.uid] = component;
841
+ return acc;
842
+ },
843
+ {}
844
+ );
845
+ return componentsMap;
846
+ },
847
+ async delete(releaseId) {
848
+ const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
849
+ populate: {
850
+ actions: {
851
+ fields: ["id"]
852
+ }
567
853
  }
854
+ });
855
+ if (!release2) {
856
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
568
857
  }
569
- });
570
- if (!release2) {
571
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
572
- }
573
- if (release2.releasedAt) {
574
- throw new utils.errors.ValidationError("Release already published");
575
- }
576
- await strapi2.db.transaction(async () => {
577
- await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
578
- where: {
579
- id: {
580
- $in: release2.actions.map((action) => action.id)
858
+ if (release2.releasedAt) {
859
+ throw new utils.errors.ValidationError("Release already published");
860
+ }
861
+ await strapi2.db.transaction(async () => {
862
+ await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
863
+ where: {
864
+ id: {
865
+ $in: release2.actions.map((action) => action.id)
866
+ }
581
867
  }
582
- }
868
+ });
869
+ await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
583
870
  });
584
- await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
585
- });
586
- return release2;
587
- },
588
- async publish(releaseId) {
589
- const releaseWithPopulatedActionEntries = await strapi2.entityService.findOne(
590
- RELEASE_MODEL_UID,
591
- releaseId,
592
- {
593
- populate: {
594
- actions: {
871
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling") && release2.scheduledAt) {
872
+ const schedulingService = getService("scheduling", { strapi: strapi2 });
873
+ await schedulingService.cancel(release2.id);
874
+ }
875
+ strapi2.telemetry.send("didDeleteContentRelease");
876
+ return release2;
877
+ },
878
+ async publish(releaseId) {
879
+ try {
880
+ const releaseWithPopulatedActionEntries = await strapi2.entityService.findOne(
881
+ RELEASE_MODEL_UID,
882
+ releaseId,
883
+ {
595
884
  populate: {
596
- entry: {
597
- fields: ["id"]
885
+ actions: {
886
+ populate: {
887
+ entry: {
888
+ fields: ["id"]
889
+ }
890
+ }
598
891
  }
599
892
  }
600
893
  }
894
+ );
895
+ if (!releaseWithPopulatedActionEntries) {
896
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
601
897
  }
602
- }
603
- );
604
- if (!releaseWithPopulatedActionEntries) {
605
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
606
- }
607
- if (releaseWithPopulatedActionEntries.releasedAt) {
608
- throw new utils.errors.ValidationError("Release already published");
609
- }
610
- if (releaseWithPopulatedActionEntries.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 releaseWithPopulatedActionEntries.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
- };
898
+ if (releaseWithPopulatedActionEntries.releasedAt) {
899
+ throw new utils.errors.ValidationError("Release already published");
623
900
  }
624
- if (action.type === "publish") {
625
- collectionTypeActions[contentTypeUid].entriestoPublishIds.push(action.entry.id);
626
- } else {
627
- collectionTypeActions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
901
+ if (releaseWithPopulatedActionEntries.actions.length === 0) {
902
+ throw new utils.errors.ValidationError("No entries to publish");
628
903
  }
629
- } else {
630
- singleTypeActions.push({
631
- uid: contentTypeUid,
632
- action: action.type,
633
- id: action.entry.id
634
- });
635
- }
636
- }
637
- const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
638
- const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
639
- await strapi2.db.transaction(async () => {
640
- for (const { uid, action, id } of singleTypeActions) {
641
- const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
642
- const entry = await strapi2.entityService.findOne(uid, id, { populate });
643
- try {
644
- if (action === "publish") {
645
- await entityManagerService.publish(entry, uid);
904
+ const collectionTypeActions = {};
905
+ const singleTypeActions = [];
906
+ for (const action of releaseWithPopulatedActionEntries.actions) {
907
+ const contentTypeUid = action.contentType;
908
+ if (strapi2.contentTypes[contentTypeUid].kind === "collectionType") {
909
+ if (!collectionTypeActions[contentTypeUid]) {
910
+ collectionTypeActions[contentTypeUid] = {
911
+ entriestoPublishIds: [],
912
+ entriesToUnpublishIds: []
913
+ };
914
+ }
915
+ if (action.type === "publish") {
916
+ collectionTypeActions[contentTypeUid].entriestoPublishIds.push(action.entry.id);
917
+ } else {
918
+ collectionTypeActions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
919
+ }
646
920
  } else {
647
- await entityManagerService.unpublish(entry, uid);
648
- }
649
- } catch (error) {
650
- if (error instanceof utils.errors.ApplicationError && (error.message === "already.published" || error.message === "already.draft"))
651
- ;
652
- else {
653
- throw error;
921
+ singleTypeActions.push({
922
+ uid: contentTypeUid,
923
+ action: action.type,
924
+ id: action.entry.id
925
+ });
654
926
  }
655
927
  }
656
- }
657
- for (const contentTypeUid of Object.keys(collectionTypeActions)) {
658
- const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
659
- const { entriestoPublishIds, entriesToUnpublishIds } = collectionTypeActions[contentTypeUid];
660
- const entriesToPublish = await strapi2.entityService.findMany(
661
- contentTypeUid,
662
- {
663
- filters: {
664
- id: {
665
- $in: entriestoPublishIds
928
+ const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
929
+ const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
930
+ await strapi2.db.transaction(async () => {
931
+ for (const { uid, action, id } of singleTypeActions) {
932
+ const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
933
+ const entry = await strapi2.entityService.findOne(uid, id, { populate });
934
+ try {
935
+ if (action === "publish") {
936
+ await entityManagerService.publish(entry, uid);
937
+ } else {
938
+ await entityManagerService.unpublish(entry, uid);
666
939
  }
667
- },
668
- populate
940
+ } catch (error) {
941
+ if (error instanceof utils.errors.ApplicationError && (error.message === "already.published" || error.message === "already.draft")) {
942
+ } else {
943
+ throw error;
944
+ }
945
+ }
669
946
  }
670
- );
671
- const entriesToUnpublish = await strapi2.entityService.findMany(
672
- contentTypeUid,
673
- {
674
- filters: {
675
- id: {
676
- $in: entriesToUnpublishIds
947
+ for (const contentTypeUid of Object.keys(collectionTypeActions)) {
948
+ const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
949
+ const { entriestoPublishIds, entriesToUnpublishIds } = collectionTypeActions[contentTypeUid];
950
+ const entriesToPublish = await strapi2.entityService.findMany(
951
+ contentTypeUid,
952
+ {
953
+ filters: {
954
+ id: {
955
+ $in: entriestoPublishIds
956
+ }
957
+ },
958
+ populate
677
959
  }
678
- },
679
- populate
960
+ );
961
+ const entriesToUnpublish = await strapi2.entityService.findMany(
962
+ contentTypeUid,
963
+ {
964
+ filters: {
965
+ id: {
966
+ $in: entriesToUnpublishIds
967
+ }
968
+ },
969
+ populate
970
+ }
971
+ );
972
+ if (entriesToPublish.length > 0) {
973
+ await entityManagerService.publishMany(entriesToPublish, contentTypeUid);
974
+ }
975
+ if (entriesToUnpublish.length > 0) {
976
+ await entityManagerService.unpublishMany(entriesToUnpublish, contentTypeUid);
977
+ }
680
978
  }
681
- );
682
- if (entriesToPublish.length > 0) {
683
- await entityManagerService.publishMany(entriesToPublish, contentTypeUid);
979
+ });
980
+ const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, releaseId, {
981
+ data: {
982
+ /*
983
+ * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">> looks like it's wrong
984
+ */
985
+ // @ts-expect-error see above
986
+ releasedAt: /* @__PURE__ */ new Date()
987
+ },
988
+ populate: {
989
+ actions: {
990
+ // @ts-expect-error is not expecting count but it is working
991
+ count: true
992
+ }
993
+ }
994
+ });
995
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
996
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
997
+ isPublished: true,
998
+ release: release2
999
+ });
684
1000
  }
685
- if (entriesToUnpublish.length > 0) {
686
- await entityManagerService.unpublishMany(entriesToUnpublish, contentTypeUid);
1001
+ strapi2.telemetry.send("didPublishContentRelease");
1002
+ return release2;
1003
+ } catch (error) {
1004
+ if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
1005
+ dispatchWebhook(ALLOWED_WEBHOOK_EVENTS.RELEASES_PUBLISH, {
1006
+ isPublished: false,
1007
+ error
1008
+ });
687
1009
  }
1010
+ strapi2.db.query(RELEASE_MODEL_UID).update({
1011
+ where: { id: releaseId },
1012
+ data: {
1013
+ status: "failed"
1014
+ }
1015
+ });
1016
+ throw error;
688
1017
  }
689
- });
690
- const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, releaseId, {
691
- data: {
692
- /*
693
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">> looks like it's wrong
694
- */
695
- // @ts-expect-error see above
696
- releasedAt: /* @__PURE__ */ new Date()
1018
+ },
1019
+ async updateAction(actionId, releaseId, update) {
1020
+ const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
1021
+ where: {
1022
+ id: actionId,
1023
+ release: {
1024
+ id: releaseId,
1025
+ releasedAt: {
1026
+ $null: true
1027
+ }
1028
+ }
1029
+ },
1030
+ data: update
1031
+ });
1032
+ if (!updatedAction) {
1033
+ throw new utils.errors.NotFoundError(
1034
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
1035
+ );
697
1036
  }
698
- });
699
- return release2;
700
- },
701
- async updateAction(actionId, releaseId, update) {
702
- const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
703
- where: {
704
- id: actionId,
705
- release: {
706
- id: releaseId,
707
- releasedAt: {
708
- $null: true
1037
+ return updatedAction;
1038
+ },
1039
+ async deleteAction(actionId, releaseId) {
1040
+ const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
1041
+ where: {
1042
+ id: actionId,
1043
+ release: {
1044
+ id: releaseId,
1045
+ releasedAt: {
1046
+ $null: true
1047
+ }
709
1048
  }
710
1049
  }
711
- },
712
- data: update
713
- });
714
- if (!updatedAction) {
715
- throw new utils.errors.NotFoundError(
716
- `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
717
- );
718
- }
719
- return updatedAction;
720
- },
721
- async deleteAction(actionId, releaseId) {
722
- const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
723
- where: {
724
- id: actionId,
725
- release: {
726
- id: releaseId,
727
- releasedAt: {
728
- $null: true
1050
+ });
1051
+ if (!deletedAction) {
1052
+ throw new utils.errors.NotFoundError(
1053
+ `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
1054
+ );
1055
+ }
1056
+ this.updateReleaseStatus(releaseId);
1057
+ return deletedAction;
1058
+ },
1059
+ async updateReleaseStatus(releaseId) {
1060
+ const [totalActions, invalidActions] = await Promise.all([
1061
+ this.countActions({
1062
+ filters: {
1063
+ release: releaseId
729
1064
  }
1065
+ }),
1066
+ this.countActions({
1067
+ filters: {
1068
+ release: releaseId,
1069
+ isEntryValid: false
1070
+ }
1071
+ })
1072
+ ]);
1073
+ if (totalActions > 0) {
1074
+ if (invalidActions > 0) {
1075
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1076
+ where: {
1077
+ id: releaseId
1078
+ },
1079
+ data: {
1080
+ status: "blocked"
1081
+ }
1082
+ });
730
1083
  }
1084
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1085
+ where: {
1086
+ id: releaseId
1087
+ },
1088
+ data: {
1089
+ status: "ready"
1090
+ }
1091
+ });
731
1092
  }
732
- });
733
- if (!deletedAction) {
734
- throw new utils.errors.NotFoundError(
735
- `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
736
- );
1093
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1094
+ where: {
1095
+ id: releaseId
1096
+ },
1097
+ data: {
1098
+ status: "empty"
1099
+ }
1100
+ });
737
1101
  }
738
- return deletedAction;
739
- }
740
- });
1102
+ };
1103
+ };
741
1104
  const createReleaseValidationService = ({ strapi: strapi2 }) => ({
742
1105
  async validateUniqueEntry(releaseId, releaseActionArgs) {
743
1106
  const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
@@ -862,8 +1225,23 @@ const services = {
862
1225
  };
863
1226
  const RELEASE_SCHEMA = yup__namespace.object().shape({
864
1227
  name: yup__namespace.string().trim().required(),
865
- // scheduledAt is a date, but we always receive strings from the client
866
- scheduledAt: yup__namespace.string().nullable()
1228
+ scheduledAt: yup__namespace.string().nullable(),
1229
+ isScheduled: yup__namespace.boolean().optional(),
1230
+ time: yup__namespace.string().when("isScheduled", {
1231
+ is: true,
1232
+ then: yup__namespace.string().trim().required(),
1233
+ otherwise: yup__namespace.string().nullable()
1234
+ }),
1235
+ timezone: yup__namespace.string().when("isScheduled", {
1236
+ is: true,
1237
+ then: yup__namespace.string().required().nullable(),
1238
+ otherwise: yup__namespace.string().nullable()
1239
+ }),
1240
+ date: yup__namespace.string().when("isScheduled", {
1241
+ is: true,
1242
+ then: yup__namespace.string().required().nullable(),
1243
+ otherwise: yup__namespace.string().nullable()
1244
+ })
867
1245
  }).required().noUnknown();
868
1246
  const validateRelease = utils.validateYupSchema(RELEASE_SCHEMA);
869
1247
  const releaseController = {
@@ -896,7 +1274,12 @@ const releaseController = {
896
1274
  }
897
1275
  };
898
1276
  });
899
- ctx.body = { data, meta: { pagination } };
1277
+ const pendingReleasesCount = await strapi.query(RELEASE_MODEL_UID).count({
1278
+ where: {
1279
+ releasedAt: null
1280
+ }
1281
+ });
1282
+ ctx.body = { data, meta: { pagination, pendingReleasesCount } };
900
1283
  }
901
1284
  },
902
1285
  async findOne(ctx) {
@@ -1267,6 +1650,9 @@ const getPlugin = () => {
1267
1650
  };
1268
1651
  }
1269
1652
  return {
1653
+ // Always return register, it handles its own feature check
1654
+ register,
1655
+ // Always return contentTypes to avoid losing data when the feature is disabled
1270
1656
  contentTypes
1271
1657
  };
1272
1658
  };