@webiny/api-headless-cms 5.38.0 → 5.39.0-beta.0

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.
@@ -22,6 +22,7 @@ var _searchableFields = require("./contentEntry/searchableFields");
22
22
  var _filterAsync = require("../utils/filterAsync");
23
23
  var _apiSecurity = require("@webiny/api-security/");
24
24
  var _constants = require("../constants");
25
+ var _date = require("../utils/date");
25
26
  const STATUS_DRAFT = _types.CONTENT_ENTRY_STATUS.DRAFT;
26
27
  exports.STATUS_DRAFT = STATUS_DRAFT;
27
28
  const STATUS_PUBLISHED = _types.CONTENT_ENTRY_STATUS.PUBLISHED;
@@ -178,24 +179,27 @@ const createSort = sort => {
178
179
  }
179
180
  return sort;
180
181
  };
182
+ const getIdentity = (input, defaultValue) => {
183
+ const identity = input !== null && input !== void 0 && input.id && input !== null && input !== void 0 && input.displayName && input !== null && input !== void 0 && input.type ? input : defaultValue;
184
+ if (!identity) {
185
+ return null;
186
+ }
187
+ return {
188
+ id: identity.id,
189
+ displayName: identity.displayName,
190
+ type: identity.type
191
+ };
192
+ };
181
193
  const createContentEntryCrud = params => {
182
194
  const {
183
195
  storageOperations,
184
196
  entriesPermissions,
185
197
  modelsPermissions,
186
198
  context,
187
- getIdentity,
199
+ getIdentity: getSecurityIdentity,
188
200
  getTenant,
189
201
  getLocale
190
202
  } = params;
191
- const getCreatedBy = () => {
192
- const identity = getIdentity();
193
- return {
194
- id: identity.id,
195
- displayName: identity.displayName,
196
- type: identity.type
197
- };
198
- };
199
203
 
200
204
  /**
201
205
  * Create
@@ -450,7 +454,7 @@ const createContentEntryCrud = params => {
450
454
  * Or if searching for the owner set that value - in the case that user can see other entries than their own.
451
455
  */
452
456
  if (await entriesPermissions.canAccessOnlyOwnRecords()) {
453
- where.ownedBy = getIdentity().id;
457
+ where.ownedBy = getSecurityIdentity().id;
454
458
  }
455
459
 
456
460
  /**
@@ -510,8 +514,8 @@ const createContentEntryCrud = params => {
510
514
  });
511
515
  }
512
516
  };
513
- const createEntry = async (model, inputData, options) => {
514
- var _inputData$wbyAco_loc;
517
+ const createEntry = async (model, rawInput, options) => {
518
+ var _rawInput$wbyAco_loca;
515
519
  await entriesPermissions.ensure({
516
520
  rwd: "w"
517
521
  });
@@ -522,7 +526,7 @@ const createContentEntryCrud = params => {
522
526
  /**
523
527
  * Make sure we only work with fields that are defined in the model.
524
528
  */
525
- const initialInput = mapAndCleanCreateInputData(model, inputData);
529
+ const initialInput = mapAndCleanCreateInputData(model, rawInput);
526
530
  await (0, _entryDataValidation.validateModelEntryDataOrThrow)({
527
531
  context,
528
532
  model,
@@ -536,16 +540,17 @@ const createContentEntryCrud = params => {
536
540
  validateEntries: true
537
541
  });
538
542
  const locale = getLocale();
539
- const owner = getCreatedBy();
543
+ const identity = getSecurityIdentity();
540
544
  const {
541
545
  id,
542
546
  entryId,
543
547
  version
544
- } = createEntryId(inputData);
548
+ } = createEntryId(rawInput);
545
549
  /**
546
550
  * There is a possibility that user sends an ID in the input, so we will use that one.
547
551
  * There is no check if the ID is unique or not, that is up to the user.
548
552
  */
553
+ const currentDate = new Date();
549
554
  const entry = {
550
555
  webinyVersion: context.WEBINY_VERSION,
551
556
  tenant: getTenant().id,
@@ -553,17 +558,18 @@ const createContentEntryCrud = params => {
553
558
  id,
554
559
  modelId: model.modelId,
555
560
  locale: locale.code,
556
- createdOn: new Date().toISOString(),
557
- savedOn: new Date().toISOString(),
558
- createdBy: owner,
559
- ownedBy: owner,
560
- modifiedBy: null,
561
+ createdOn: (0, _date.getDate)(rawInput.createdOn, currentDate),
562
+ savedOn: (0, _date.getDate)(rawInput.savedOn, currentDate),
563
+ publishedOn: (0, _date.getDate)(rawInput.publishedOn),
564
+ createdBy: getIdentity(rawInput.createdBy, identity),
565
+ ownedBy: getIdentity(rawInput.ownedBy, identity),
566
+ modifiedBy: getIdentity(rawInput.modifiedBy, null),
561
567
  version,
562
568
  locked: false,
563
569
  status: STATUS_DRAFT,
564
570
  values: input,
565
571
  location: {
566
- folderId: ((_inputData$wbyAco_loc = inputData.wbyAco_location) === null || _inputData$wbyAco_loc === void 0 ? void 0 : _inputData$wbyAco_loc.folderId) || _constants.ROOT_FOLDER
572
+ folderId: ((_rawInput$wbyAco_loca = rawInput.wbyAco_location) === null || _rawInput$wbyAco_loca === void 0 ? void 0 : _rawInput$wbyAco_loca.folderId) || _constants.ROOT_FOLDER
567
573
  }
568
574
  };
569
575
  let storageEntry = null;
@@ -600,7 +606,7 @@ const createContentEntryCrud = params => {
600
606
  });
601
607
  }
602
608
  };
603
- const createEntryRevisionFrom = async (model, sourceId, inputData, options) => {
609
+ const createEntryRevisionFrom = async (model, sourceId, rawInput, options) => {
604
610
  await entriesPermissions.ensure({
605
611
  rwd: "w"
606
612
  });
@@ -611,7 +617,7 @@ const createContentEntryCrud = params => {
611
617
  /**
612
618
  * Make sure we only work with fields that are defined in the model.
613
619
  */
614
- const input = mapAndCleanUpdatedInputData(model, inputData);
620
+ const input = mapAndCleanUpdatedInputData(model, rawInput);
615
621
 
616
622
  /**
617
623
  * Entries are identified by a common parent ID + Revision number.
@@ -650,25 +656,22 @@ const createContentEntryCrud = params => {
650
656
  await entriesPermissions.ensure({
651
657
  owns: originalEntry.createdBy
652
658
  });
653
- const identity = getIdentity();
654
659
  const latestId = latestStorageEntry ? latestStorageEntry.id : sourceId;
655
660
  const {
656
661
  id,
657
662
  version: nextVersion
658
663
  } = increaseEntryIdVersion(latestId);
664
+ const currentDate = new Date();
659
665
  const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
660
666
  id,
661
667
  version: nextVersion,
662
- savedOn: new Date().toISOString(),
663
- createdOn: new Date().toISOString(),
664
- createdBy: {
665
- id: identity.id,
666
- displayName: identity.displayName,
667
- type: identity.type
668
- },
669
- modifiedBy: null,
668
+ savedOn: (0, _date.getDate)(rawInput.savedOn, currentDate),
669
+ createdOn: (0, _date.getDate)(rawInput.createdOn, currentDate),
670
+ publishedOn: (0, _date.getDate)(rawInput.publishedOn, originalEntry.publishedOn),
671
+ createdBy: getIdentity(rawInput.createdBy, originalEntry.createdBy),
672
+ modifiedBy: getIdentity(rawInput.modifiedBy, null),
673
+ ownedBy: getIdentity(rawInput.ownedBy, originalEntry.ownedBy),
670
674
  locked: false,
671
- publishedOn: undefined,
672
675
  status: STATUS_DRAFT,
673
676
  values
674
677
  });
@@ -710,8 +713,8 @@ const createContentEntryCrud = params => {
710
713
  });
711
714
  }
712
715
  };
713
- const updateEntry = async (model, id, inputData, metaInput, options) => {
714
- var _inputData$wbyAco_loc2;
716
+ const updateEntry = async (model, id, rawInput, metaInput, options) => {
717
+ var _rawInput$wbyAco_loca2;
715
718
  await entriesPermissions.ensure({
716
719
  rwd: "w"
717
720
  });
@@ -722,7 +725,7 @@ const createContentEntryCrud = params => {
722
725
  /**
723
726
  * Make sure we only work with fields that are defined in the model.
724
727
  */
725
- const input = mapAndCleanUpdatedInputData(model, inputData);
728
+ const input = mapAndCleanUpdatedInputData(model, rawInput);
726
729
 
727
730
  /**
728
731
  * The entry we are going to update.
@@ -754,6 +757,7 @@ const createContentEntryCrud = params => {
754
757
  input: initialValues,
755
758
  validateEntries: false
756
759
  });
760
+
757
761
  /**
758
762
  * If users wants to remove a key from meta values, they need to send meta key with the null value.
759
763
  */
@@ -762,13 +766,17 @@ const createContentEntryCrud = params => {
762
766
  * We always send the full entry to the hooks and storage operations update.
763
767
  */
764
768
  const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
765
- savedOn: new Date().toISOString(),
766
- modifiedBy: getCreatedBy(),
769
+ savedOn: (0, _date.getDate)(rawInput.savedOn, new Date()),
770
+ createdOn: (0, _date.getDate)(rawInput.createdOn, originalEntry.createdOn),
771
+ publishedOn: (0, _date.getDate)(rawInput.publishedOn, originalEntry.publishedOn),
772
+ createdBy: getIdentity(rawInput.createdBy, originalEntry.createdBy),
773
+ modifiedBy: getIdentity(rawInput.modifiedBy, getSecurityIdentity()),
774
+ ownedBy: getIdentity(rawInput.ownedBy, originalEntry.ownedBy),
767
775
  values,
768
776
  meta,
769
777
  status: transformEntryStatus(originalEntry.status)
770
778
  });
771
- const folderId = (_inputData$wbyAco_loc2 = inputData.wbyAco_location) === null || _inputData$wbyAco_loc2 === void 0 ? void 0 : _inputData$wbyAco_loc2.folderId;
779
+ const folderId = (_rawInput$wbyAco_loca2 = rawInput.wbyAco_location) === null || _rawInput$wbyAco_loca2 === void 0 ? void 0 : _rawInput$wbyAco_loca2.folderId;
772
780
  if (folderId) {
773
781
  entry.location = {
774
782
  folderId
@@ -916,8 +924,8 @@ const createContentEntryCrud = params => {
916
924
  });
917
925
  const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
918
926
  status: STATUS_PUBLISHED,
919
- publishedOn: originalEntry.publishedOn || new Date().toISOString(),
920
- savedOn: new Date().toISOString(),
927
+ publishedOn: (0, _date.getDate)(originalEntry.publishedOn, new Date()),
928
+ savedOn: (0, _date.getDate)(originalEntry.savedOn, new Date()),
921
929
  webinyVersion: context.WEBINY_VERSION,
922
930
  values
923
931
  });
@@ -1164,7 +1172,7 @@ const createContentEntryCrud = params => {
1164
1172
  entry
1165
1173
  });
1166
1174
  };
1167
- const publishEntry = async (model, id) => {
1175
+ const publishEntry = async (model, id, options) => {
1168
1176
  await entriesPermissions.ensure({
1169
1177
  pw: "p"
1170
1178
  });
@@ -1188,15 +1196,34 @@ const createContentEntryCrud = params => {
1188
1196
  entry: originalEntry
1189
1197
  });
1190
1198
  const currentDate = new Date().toISOString();
1199
+ /**
1200
+ * The existing functionality is to set the publishedOn date to the current date.
1201
+ * Users can now choose to skip updating the publishedOn date - unless it is not set.
1202
+ *
1203
+ * Same logic goes for the savedOn date.
1204
+ */
1205
+ const {
1206
+ updatePublishedOn = true,
1207
+ updateSavedOn = true
1208
+ } = options || {};
1209
+ let publishedOn = originalEntry.publishedOn;
1210
+ if (updatePublishedOn || !publishedOn) {
1211
+ publishedOn = currentDate;
1212
+ }
1213
+ let savedOn = originalEntry.savedOn;
1214
+ if (updateSavedOn || !savedOn) {
1215
+ savedOn = currentDate;
1216
+ }
1191
1217
  const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
1192
1218
  status: STATUS_PUBLISHED,
1193
1219
  locked: true,
1194
- savedOn: currentDate,
1195
- publishedOn: currentDate
1220
+ savedOn,
1221
+ publishedOn
1196
1222
  });
1197
1223
  let storageEntry = null;
1198
1224
  try {
1199
1225
  await onEntryBeforePublish.publish({
1226
+ original: originalEntry,
1200
1227
  entry,
1201
1228
  model
1202
1229
  });
@@ -1206,6 +1233,7 @@ const createContentEntryCrud = params => {
1206
1233
  storageEntry
1207
1234
  });
1208
1235
  await onEntryAfterPublish.publish({
1236
+ original: originalEntry,
1209
1237
  entry,
1210
1238
  storageEntry: result,
1211
1239
  model
@@ -1213,6 +1241,7 @@ const createContentEntryCrud = params => {
1213
1241
  return entry;
1214
1242
  } catch (ex) {
1215
1243
  await onEntryPublishError.publish({
1244
+ original: originalEntry,
1216
1245
  entry,
1217
1246
  model,
1218
1247
  error: ex
@@ -1299,7 +1328,7 @@ const createContentEntryCrud = params => {
1299
1328
  * Or if searching for the owner set that value - in the case that user can see other entries than their own.
1300
1329
  */
1301
1330
  if (await entriesPermissions.canAccessOnlyOwnRecords()) {
1302
- where.ownedBy = getIdentity().id;
1331
+ where.ownedBy = getSecurityIdentity().id;
1303
1332
  }
1304
1333
 
1305
1334
  /**
@@ -1498,9 +1527,9 @@ const createContentEntryCrud = params => {
1498
1527
  return deleteMultipleEntries(model, ids);
1499
1528
  });
1500
1529
  },
1501
- async publishEntry(model, id) {
1530
+ async publishEntry(model, id, options) {
1502
1531
  return context.benchmark.measure("headlessCms.crud.entries.publishEntry", async () => {
1503
- return publishEntry(model, id);
1532
+ return publishEntry(model, id, options);
1504
1533
  });
1505
1534
  },
1506
1535
  async unpublishEntry(model, id) {