@strapi/content-releases 0.0.0-next.95a939e004e74915357523e3adb118a31fef57ed → 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.
@@ -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
  };
@@ -209,6 +462,11 @@ const schema$1 = {
209
462
  timezone: {
210
463
  type: "string"
211
464
  },
465
+ status: {
466
+ type: "enumeration",
467
+ enum: ["ready", "blocked", "failed", "done", "empty"],
468
+ required: true
469
+ },
212
470
  actions: {
213
471
  type: "relation",
214
472
  relation: "oneToMany",
@@ -261,6 +519,9 @@ const schema = {
261
519
  relation: "manyToOne",
262
520
  target: RELEASE_MODEL_UID,
263
521
  inversedBy: "actions"
522
+ },
523
+ isEntryValid: {
524
+ type: "boolean"
264
525
  }
265
526
  }
266
527
  };
@@ -283,464 +544,563 @@ const getGroupName = (queryValue) => {
283
544
  return "contentType.displayName";
284
545
  }
285
546
  };
286
- const createReleaseService = ({ strapi: strapi2 }) => ({
287
- async create(releaseData, { user }) {
288
- const releaseWithCreatorFields = await utils.setCreatorFields({ user })(releaseData);
289
- const {
290
- validatePendingReleasesLimit,
291
- validateUniqueNameForPendingRelease,
292
- validateScheduledAtIsLaterThanNow
293
- } = getService("release-validation", { strapi: strapi2 });
294
- await Promise.all([
295
- validatePendingReleasesLimit(),
296
- validateUniqueNameForPendingRelease(releaseWithCreatorFields.name),
297
- validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
298
- ]);
299
- const release2 = await strapi2.entityService.create(RELEASE_MODEL_UID, {
300
- 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
301
553
  });
302
- if (strapi2.features.future.isEnabled("contentReleasesScheduling") && releaseWithCreatorFields.scheduledAt) {
303
- const schedulingService = getService("scheduling", { strapi: strapi2 });
304
- await schedulingService.set(release2.id, release2.scheduledAt);
305
- }
306
- return release2;
307
- },
308
- async findOne(id, query = {}) {
309
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id, {
310
- ...query
311
- });
312
- return release2;
313
- },
314
- findPage(query) {
315
- return strapi2.entityService.findPage(RELEASE_MODEL_UID, {
316
- ...query,
317
- populate: {
318
- actions: {
319
- // @ts-expect-error Ignore missing properties
320
- 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"
321
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);
322
577
  }
323
- });
324
- },
325
- async findManyWithContentTypeEntryAttached(contentTypeUid, entryId) {
326
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
327
- where: {
328
- actions: {
329
- target_type: contentTypeUid,
330
- 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
+ }
331
608
  },
332
- releasedAt: {
333
- $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
+ }
334
617
  }
335
- },
336
- populate: {
337
- // Filter the action to get only the content type entry
338
- actions: {
339
- 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: {
340
638
  target_type: contentTypeUid,
341
639
  target_id: entryId
342
640
  }
343
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}`);
344
687
  }
345
- });
346
- return releases.map((release2) => {
347
- if (release2.actions?.length) {
348
- const [actionForEntry] = release2.actions;
349
- delete release2.actions;
350
- return {
351
- ...release2,
352
- action: actionForEntry
353
- };
688
+ if (release2.releasedAt) {
689
+ throw new utils.errors.ValidationError("Release already published");
354
690
  }
355
- return release2;
356
- });
357
- },
358
- async findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId) {
359
- const releasesRelated = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
360
- where: {
361
- releasedAt: {
362
- $null: true
363
- },
364
- actions: {
365
- target_type: contentTypeUid,
366
- 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);
367
705
  }
368
706
  }
369
- });
370
- const releases = await strapi2.db.query(RELEASE_MODEL_UID).findMany({
371
- where: {
372
- $or: [
373
- {
374
- id: {
375
- $notIn: releasesRelated.map((release2) => release2.id)
376
- }
377
- },
378
- {
379
- actions: null
380
- }
381
- ],
382
- releasedAt: {
383
- $null: true
384
- }
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}`);
385
722
  }
386
- });
387
- return releases.map((release2) => {
388
- if (release2.actions?.length) {
389
- const [actionForEntry] = release2.actions;
390
- delete release2.actions;
391
- return {
392
- ...release2,
393
- action: actionForEntry
394
- };
723
+ if (release2.releasedAt) {
724
+ throw new utils.errors.ValidationError("Release already published");
395
725
  }
396
- return release2;
397
- });
398
- },
399
- async update(id, releaseData, { user }) {
400
- const releaseWithCreatorFields = await utils.setCreatorFields({ user, isEdition: true })(releaseData);
401
- const { validateUniqueNameForPendingRelease, validateScheduledAtIsLaterThanNow } = getService(
402
- "release-validation",
403
- { strapi: strapi2 }
404
- );
405
- await Promise.all([
406
- validateUniqueNameForPendingRelease(releaseWithCreatorFields.name, id),
407
- validateScheduledAtIsLaterThanNow(releaseWithCreatorFields.scheduledAt)
408
- ]);
409
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, id);
410
- if (!release2) {
411
- throw new utils.errors.NotFoundError(`No release found for id ${id}`);
412
- }
413
- if (release2.releasedAt) {
414
- throw new utils.errors.ValidationError("Release already published");
415
- }
416
- const updatedRelease = await strapi2.entityService.update(RELEASE_MODEL_UID, id, {
417
- /*
418
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">>
419
- * is not compatible with the type we are passing here: UpdateRelease.Request['body']
420
- */
421
- // @ts-expect-error see above
422
- data: releaseWithCreatorFields
423
- });
424
- if (strapi2.features.future.isEnabled("contentReleasesScheduling")) {
425
- const schedulingService = getService("scheduling", { strapi: strapi2 });
426
- if (releaseData.scheduledAt) {
427
- await schedulingService.set(id, releaseData.scheduledAt);
428
- } else if (release2.scheduledAt) {
429
- 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}`);
430
753
  }
431
- }
432
- return updatedRelease;
433
- },
434
- async createAction(releaseId, action) {
435
- const { validateEntryContentType, validateUniqueEntry } = getService("release-validation", {
436
- strapi: strapi2
437
- });
438
- await Promise.all([
439
- validateEntryContentType(action.entry.contentType),
440
- validateUniqueEntry(releaseId, action)
441
- ]);
442
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId);
443
- if (!release2) {
444
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
445
- }
446
- if (release2.releasedAt) {
447
- throw new utils.errors.ValidationError("Release already published");
448
- }
449
- const { entry, type } = action;
450
- return strapi2.entityService.create(RELEASE_ACTION_MODEL_UID, {
451
- data: {
452
- type,
453
- contentType: entry.contentType,
454
- locale: entry.locale,
455
- entry: {
456
- id: entry.id,
457
- __type: entry.contentType,
458
- __pivot: { field: "entry" }
754
+ return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
755
+ ...query,
756
+ populate: {
757
+ entry: {
758
+ populate: "*"
759
+ }
459
760
  },
460
- release: releaseId
461
- },
462
- populate: { release: { fields: ["id"] }, entry: { fields: ["id"] } }
463
- });
464
- },
465
- async findActions(releaseId, query) {
466
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
467
- fields: ["id"]
468
- });
469
- if (!release2) {
470
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
471
- }
472
- return strapi2.entityService.findPage(RELEASE_ACTION_MODEL_UID, {
473
- ...query,
474
- populate: {
475
- entry: {
476
- populate: "*"
761
+ filters: {
762
+ release: releaseId
477
763
  }
478
- },
479
- filters: {
480
- release: releaseId
481
- }
482
- });
483
- },
484
- async countActions(query) {
485
- return strapi2.entityService.count(RELEASE_ACTION_MODEL_UID, query);
486
- },
487
- async groupActions(actions, groupBy) {
488
- const contentTypeUids = actions.reduce((acc, action) => {
489
- if (!acc.includes(action.contentType)) {
490
- acc.push(action.contentType);
491
- }
492
- return acc;
493
- }, []);
494
- const allReleaseContentTypesDictionary = await this.getContentTypesDataForActions(
495
- contentTypeUids
496
- );
497
- const allLocalesDictionary = await this.getLocalesDataForActions();
498
- const formattedData = actions.map((action) => {
499
- const { mainField, displayName } = allReleaseContentTypesDictionary[action.contentType];
500
- return {
501
- ...action,
502
- locale: action.locale ? allLocalesDictionary[action.locale] : null,
503
- contentType: {
504
- displayName,
505
- mainFieldValue: action.entry[mainField],
506
- 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);
507
773
  }
508
- };
509
- });
510
- const groupName = getGroupName(groupBy);
511
- return ___default.default.groupBy(groupName)(formattedData);
512
- },
513
- async getLocalesDataForActions() {
514
- if (!strapi2.plugin("i18n")) {
515
- return {};
516
- }
517
- const allLocales = await strapi2.plugin("i18n").service("locales").find() || [];
518
- return allLocales.reduce((acc, locale) => {
519
- acc[locale.code] = { name: locale.name, code: locale.code };
520
- return acc;
521
- }, {});
522
- },
523
- async getContentTypesDataForActions(contentTypesUids) {
524
- const contentManagerContentTypeService = strapi2.plugin("content-manager").service("content-types");
525
- const contentTypesData = {};
526
- for (const contentTypeUid of contentTypesUids) {
527
- const contentTypeConfig = await contentManagerContentTypeService.findConfiguration({
528
- 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
+ };
529
791
  });
530
- contentTypesData[contentTypeUid] = {
531
- mainField: contentTypeConfig.settings.mainField,
532
- displayName: strapi2.getModel(contentTypeUid).info.displayName
533
- };
534
- }
535
- return contentTypesData;
536
- },
537
- getContentTypeModelsFromActions(actions) {
538
- const contentTypeUids = actions.reduce((acc, action) => {
539
- if (!acc.includes(action.contentType)) {
540
- 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 {};
541
798
  }
542
- return acc;
543
- }, []);
544
- const contentTypeModelsMap = contentTypeUids.reduce(
545
- (acc, contentTypeUid) => {
546
- 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 };
547
802
  return acc;
548
- },
549
- {}
550
- );
551
- return contentTypeModelsMap;
552
- },
553
- async getAllComponents() {
554
- const contentManagerComponentsService = strapi2.plugin("content-manager").service("components");
555
- const components = await contentManagerComponentsService.findAllComponents();
556
- const componentsMap = components.reduce(
557
- (acc, component) => {
558
- 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
+ }
559
824
  return acc;
560
- },
561
- {}
562
- );
563
- return componentsMap;
564
- },
565
- async delete(releaseId) {
566
- const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
567
- populate: {
568
- actions: {
569
- 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
+ }
570
853
  }
854
+ });
855
+ if (!release2) {
856
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
571
857
  }
572
- });
573
- if (!release2) {
574
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
575
- }
576
- if (release2.releasedAt) {
577
- throw new utils.errors.ValidationError("Release already published");
578
- }
579
- await strapi2.db.transaction(async () => {
580
- await strapi2.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
581
- where: {
582
- id: {
583
- $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
+ }
584
867
  }
585
- }
868
+ });
869
+ await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
586
870
  });
587
- await strapi2.entityService.delete(RELEASE_MODEL_UID, releaseId);
588
- });
589
- return release2;
590
- },
591
- async publish(releaseId) {
592
- const releaseWithPopulatedActionEntries = await strapi2.entityService.findOne(
593
- RELEASE_MODEL_UID,
594
- releaseId,
595
- {
596
- populate: {
597
- 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
+ {
598
884
  populate: {
599
- entry: {
600
- fields: ["id"]
885
+ actions: {
886
+ populate: {
887
+ entry: {
888
+ fields: ["id"]
889
+ }
890
+ }
601
891
  }
602
892
  }
603
893
  }
894
+ );
895
+ if (!releaseWithPopulatedActionEntries) {
896
+ throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
604
897
  }
605
- }
606
- );
607
- if (!releaseWithPopulatedActionEntries) {
608
- throw new utils.errors.NotFoundError(`No release found for id ${releaseId}`);
609
- }
610
- if (releaseWithPopulatedActionEntries.releasedAt) {
611
- throw new utils.errors.ValidationError("Release already published");
612
- }
613
- if (releaseWithPopulatedActionEntries.actions.length === 0) {
614
- throw new utils.errors.ValidationError("No entries to publish");
615
- }
616
- const collectionTypeActions = {};
617
- const singleTypeActions = [];
618
- for (const action of releaseWithPopulatedActionEntries.actions) {
619
- const contentTypeUid = action.contentType;
620
- if (strapi2.contentTypes[contentTypeUid].kind === "collectionType") {
621
- if (!collectionTypeActions[contentTypeUid]) {
622
- collectionTypeActions[contentTypeUid] = {
623
- entriestoPublishIds: [],
624
- entriesToUnpublishIds: []
625
- };
898
+ if (releaseWithPopulatedActionEntries.releasedAt) {
899
+ throw new utils.errors.ValidationError("Release already published");
626
900
  }
627
- if (action.type === "publish") {
628
- collectionTypeActions[contentTypeUid].entriestoPublishIds.push(action.entry.id);
629
- } else {
630
- collectionTypeActions[contentTypeUid].entriesToUnpublishIds.push(action.entry.id);
901
+ if (releaseWithPopulatedActionEntries.actions.length === 0) {
902
+ throw new utils.errors.ValidationError("No entries to publish");
631
903
  }
632
- } else {
633
- singleTypeActions.push({
634
- uid: contentTypeUid,
635
- action: action.type,
636
- id: action.entry.id
637
- });
638
- }
639
- }
640
- const entityManagerService = strapi2.plugin("content-manager").service("entity-manager");
641
- const populateBuilderService = strapi2.plugin("content-manager").service("populate-builder");
642
- await strapi2.db.transaction(async () => {
643
- for (const { uid, action, id } of singleTypeActions) {
644
- const populate = await populateBuilderService(uid).populateDeep(Infinity).build();
645
- const entry = await strapi2.entityService.findOne(uid, id, { populate });
646
- try {
647
- if (action === "publish") {
648
- 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
+ }
649
920
  } else {
650
- await entityManagerService.unpublish(entry, uid);
651
- }
652
- } catch (error) {
653
- if (error instanceof utils.errors.ApplicationError && (error.message === "already.published" || error.message === "already.draft"))
654
- ;
655
- else {
656
- throw error;
921
+ singleTypeActions.push({
922
+ uid: contentTypeUid,
923
+ action: action.type,
924
+ id: action.entry.id
925
+ });
657
926
  }
658
927
  }
659
- }
660
- for (const contentTypeUid of Object.keys(collectionTypeActions)) {
661
- const populate = await populateBuilderService(contentTypeUid).populateDeep(Infinity).build();
662
- const { entriestoPublishIds, entriesToUnpublishIds } = collectionTypeActions[contentTypeUid];
663
- const entriesToPublish = await strapi2.entityService.findMany(
664
- contentTypeUid,
665
- {
666
- filters: {
667
- id: {
668
- $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);
669
939
  }
670
- },
671
- 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
+ }
672
946
  }
673
- );
674
- const entriesToUnpublish = await strapi2.entityService.findMany(
675
- contentTypeUid,
676
- {
677
- filters: {
678
- id: {
679
- $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
680
959
  }
681
- },
682
- 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
+ }
683
978
  }
684
- );
685
- if (entriesToPublish.length > 0) {
686
- 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
+ });
687
1000
  }
688
- if (entriesToUnpublish.length > 0) {
689
- 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
+ });
690
1009
  }
1010
+ strapi2.db.query(RELEASE_MODEL_UID).update({
1011
+ where: { id: releaseId },
1012
+ data: {
1013
+ status: "failed"
1014
+ }
1015
+ });
1016
+ throw error;
691
1017
  }
692
- });
693
- const release2 = await strapi2.entityService.update(RELEASE_MODEL_UID, releaseId, {
694
- data: {
695
- /*
696
- * The type returned from the entity service: Partial<Input<"plugin::content-releases.release">> looks like it's wrong
697
- */
698
- // @ts-expect-error see above
699
- 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
+ );
700
1036
  }
701
- });
702
- return release2;
703
- },
704
- async updateAction(actionId, releaseId, update) {
705
- const updatedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).update({
706
- where: {
707
- id: actionId,
708
- release: {
709
- id: releaseId,
710
- releasedAt: {
711
- $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
+ }
712
1048
  }
713
1049
  }
714
- },
715
- data: update
716
- });
717
- if (!updatedAction) {
718
- throw new utils.errors.NotFoundError(
719
- `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
720
- );
721
- }
722
- return updatedAction;
723
- },
724
- async deleteAction(actionId, releaseId) {
725
- const deletedAction = await strapi2.db.query(RELEASE_ACTION_MODEL_UID).delete({
726
- where: {
727
- id: actionId,
728
- release: {
729
- id: releaseId,
730
- releasedAt: {
731
- $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
732
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
+ });
733
1083
  }
1084
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1085
+ where: {
1086
+ id: releaseId
1087
+ },
1088
+ data: {
1089
+ status: "ready"
1090
+ }
1091
+ });
734
1092
  }
735
- });
736
- if (!deletedAction) {
737
- throw new utils.errors.NotFoundError(
738
- `Action with id ${actionId} not found in release with id ${releaseId} or it is already published`
739
- );
1093
+ return strapi2.db.query(RELEASE_MODEL_UID).update({
1094
+ where: {
1095
+ id: releaseId
1096
+ },
1097
+ data: {
1098
+ status: "empty"
1099
+ }
1100
+ });
740
1101
  }
741
- return deletedAction;
742
- }
743
- });
1102
+ };
1103
+ };
744
1104
  const createReleaseValidationService = ({ strapi: strapi2 }) => ({
745
1105
  async validateUniqueEntry(releaseId, releaseActionArgs) {
746
1106
  const release2 = await strapi2.entityService.findOne(RELEASE_MODEL_UID, releaseId, {
@@ -865,11 +1225,21 @@ const services = {
865
1225
  };
866
1226
  const RELEASE_SCHEMA = yup__namespace.object().shape({
867
1227
  name: yup__namespace.string().trim().required(),
868
- // scheduledAt is a date, but we always receive strings from the client
869
1228
  scheduledAt: yup__namespace.string().nullable(),
870
- timezone: yup__namespace.string().when("scheduledAt", {
871
- is: (scheduledAt) => !!scheduledAt,
872
- then: yup__namespace.string().required(),
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(),
873
1243
  otherwise: yup__namespace.string().nullable()
874
1244
  })
875
1245
  }).required().noUnknown();
@@ -904,7 +1274,12 @@ const releaseController = {
904
1274
  }
905
1275
  };
906
1276
  });
907
- 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 } };
908
1283
  }
909
1284
  },
910
1285
  async findOne(ctx) {
@@ -1275,6 +1650,9 @@ const getPlugin = () => {
1275
1650
  };
1276
1651
  }
1277
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
1278
1656
  contentTypes
1279
1657
  };
1280
1658
  };