@webiny/api-headless-cms 0.0.0-unstable.611c5af35e → 0.0.0-unstable.6509387258

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 (246) hide show
  1. package/context.d.ts +5 -2
  2. package/context.js +60 -8
  3. package/context.js.map +1 -1
  4. package/crud/contentEntry/markLockedFields.d.ts +1 -1
  5. package/crud/contentEntry/markLockedFields.js +17 -4
  6. package/crud/contentEntry/markLockedFields.js.map +1 -1
  7. package/crud/contentEntry/referenceFieldsMapping.js +7 -5
  8. package/crud/contentEntry/referenceFieldsMapping.js.map +1 -1
  9. package/crud/contentEntry/searchableFields.d.ts +9 -0
  10. package/crud/contentEntry/searchableFields.js +72 -0
  11. package/crud/contentEntry/searchableFields.js.map +1 -0
  12. package/crud/contentEntry.crud.d.ts +4 -1
  13. package/crud/contentEntry.crud.js +958 -819
  14. package/crud/contentEntry.crud.js.map +1 -1
  15. package/crud/contentModel/beforeCreate.d.ts +2 -3
  16. package/crud/contentModel/beforeCreate.js +43 -81
  17. package/crud/contentModel/beforeCreate.js.map +1 -1
  18. package/crud/contentModel/beforeUpdate.d.ts +2 -4
  19. package/crud/contentModel/beforeUpdate.js +33 -5
  20. package/crud/contentModel/beforeUpdate.js.map +1 -1
  21. package/crud/contentModel/compatibility/modelApiName.d.ts +3 -0
  22. package/crud/contentModel/compatibility/modelApiName.js +24 -0
  23. package/crud/contentModel/compatibility/modelApiName.js.map +1 -0
  24. package/crud/contentModel/createFieldModels.js +6 -0
  25. package/crud/contentModel/createFieldModels.js.map +1 -1
  26. package/crud/contentModel/createFieldStorageId.js +4 -1
  27. package/crud/contentModel/createFieldStorageId.js.map +1 -1
  28. package/crud/contentModel/defaultFields.d.ts +5 -0
  29. package/crud/contentModel/defaultFields.js +58 -0
  30. package/crud/contentModel/defaultFields.js.map +1 -0
  31. package/crud/contentModel/fieldIdValidation.js +5 -0
  32. package/crud/contentModel/fieldIdValidation.js.map +1 -1
  33. package/crud/contentModel/fields/descriptionField.d.ts +2 -0
  34. package/crud/contentModel/fields/descriptionField.js +42 -0
  35. package/crud/contentModel/fields/descriptionField.js.map +1 -0
  36. package/crud/contentModel/fields/imageField.d.ts +2 -0
  37. package/crud/contentModel/fields/imageField.js +46 -0
  38. package/crud/contentModel/fields/imageField.js.map +1 -0
  39. package/crud/contentModel/fields/titleField.d.ts +2 -0
  40. package/crud/contentModel/fields/titleField.js +58 -0
  41. package/crud/contentModel/fields/titleField.js.map +1 -0
  42. package/crud/contentModel/idValidation.js +5 -0
  43. package/crud/contentModel/idValidation.js.map +1 -1
  44. package/crud/contentModel/models.js +20 -1
  45. package/crud/contentModel/models.js.map +1 -1
  46. package/crud/contentModel/systemFields.js.map +1 -1
  47. package/crud/contentModel/validate/endingAllowed.d.ts +6 -0
  48. package/crud/contentModel/validate/endingAllowed.js +26 -0
  49. package/crud/contentModel/validate/endingAllowed.js.map +1 -0
  50. package/crud/contentModel/validate/isModelEndingAllowed.d.ts +6 -0
  51. package/crud/contentModel/validate/isModelEndingAllowed.js +24 -0
  52. package/crud/contentModel/validate/isModelEndingAllowed.js.map +1 -0
  53. package/crud/contentModel/validate/modelId.d.ts +11 -0
  54. package/crud/contentModel/validate/modelId.js +36 -0
  55. package/crud/contentModel/validate/modelId.js.map +1 -0
  56. package/crud/contentModel/validate/pluralApiName.d.ts +7 -0
  57. package/crud/contentModel/validate/pluralApiName.js +24 -0
  58. package/crud/contentModel/validate/pluralApiName.js.map +1 -0
  59. package/crud/contentModel/validate/singularApiName.d.ts +7 -0
  60. package/crud/contentModel/validate/singularApiName.js +24 -0
  61. package/crud/contentModel/validate/singularApiName.js.map +1 -0
  62. package/crud/contentModel/validateModel.d.ts +4 -4
  63. package/crud/contentModel/validateModel.js +6 -3
  64. package/crud/contentModel/validateModel.js.map +1 -1
  65. package/crud/contentModel/validateModelFields.d.ts +4 -4
  66. package/crud/contentModel/validateModelFields.js +123 -88
  67. package/crud/contentModel/validateModelFields.js.map +1 -1
  68. package/crud/contentModel/validation.d.ts +519 -0
  69. package/crud/contentModel/validation.js +145 -0
  70. package/crud/contentModel/validation.js.map +1 -0
  71. package/crud/contentModel.crud.js +369 -273
  72. package/crud/contentModel.crud.js.map +1 -1
  73. package/crud/contentModelGroup/validation.d.ts +30 -0
  74. package/crud/contentModelGroup/validation.js +34 -0
  75. package/crud/contentModelGroup/validation.js.map +1 -0
  76. package/crud/contentModelGroup.crud.js +189 -166
  77. package/crud/contentModelGroup.crud.js.map +1 -1
  78. package/crud/system.crud.js +0 -60
  79. package/crud/system.crud.js.map +1 -1
  80. package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.d.ts +11 -0
  81. package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js +203 -0
  82. package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js.map +1 -0
  83. package/fieldConverters/index.d.ts +2 -1
  84. package/fieldConverters/index.js +2 -1
  85. package/fieldConverters/index.js.map +1 -1
  86. package/graphql/buildSchemaPlugins.d.ts +8 -3
  87. package/graphql/buildSchemaPlugins.js +3 -7
  88. package/graphql/buildSchemaPlugins.js.map +1 -1
  89. package/graphql/createExecutableSchema.d.ts +6 -0
  90. package/graphql/createExecutableSchema.js +29 -0
  91. package/graphql/createExecutableSchema.js.map +1 -0
  92. package/graphql/generateSchema.d.ts +8 -0
  93. package/graphql/generateSchema.js +31 -0
  94. package/graphql/generateSchema.js.map +1 -0
  95. package/graphql/graphQLHandlerFactory.js +104 -48
  96. package/graphql/graphQLHandlerFactory.js.map +1 -1
  97. package/graphql/index.d.ts +1 -3
  98. package/graphql/index.js +2 -39
  99. package/graphql/index.js.map +1 -1
  100. package/graphql/schema/baseContentSchema.d.ts +6 -2
  101. package/graphql/schema/baseContentSchema.js +9 -11
  102. package/graphql/schema/baseContentSchema.js.map +1 -1
  103. package/graphql/schema/baseSchema.d.ts +3 -0
  104. package/graphql/schema/baseSchema.js +53 -0
  105. package/graphql/schema/baseSchema.js.map +1 -0
  106. package/graphql/schema/contentEntries.d.ts +6 -2
  107. package/graphql/schema/contentEntries.js +116 -65
  108. package/graphql/schema/contentEntries.js.map +1 -1
  109. package/graphql/schema/contentModelGroups.d.ts +6 -2
  110. package/graphql/schema/contentModelGroups.js +14 -10
  111. package/graphql/schema/contentModelGroups.js.map +1 -1
  112. package/graphql/schema/contentModels.d.ts +6 -2
  113. package/graphql/schema/contentModels.js +60 -8
  114. package/graphql/schema/contentModels.js.map +1 -1
  115. package/graphql/schema/createFieldResolvers.d.ts +1 -1
  116. package/graphql/schema/createFieldResolvers.js +17 -6
  117. package/graphql/schema/createFieldResolvers.js.map +1 -1
  118. package/graphql/schema/createManageResolvers.d.ts +1 -1
  119. package/graphql/schema/createManageResolvers.js +33 -17
  120. package/graphql/schema/createManageResolvers.js.map +1 -1
  121. package/graphql/schema/createManageSDL.d.ts +3 -0
  122. package/graphql/schema/createManageSDL.js +63 -53
  123. package/graphql/schema/createManageSDL.js.map +1 -1
  124. package/graphql/schema/createPreviewResolvers.js +3 -7
  125. package/graphql/schema/createPreviewResolvers.js.map +1 -1
  126. package/graphql/schema/createReadResolvers.js +8 -7
  127. package/graphql/schema/createReadResolvers.js.map +1 -1
  128. package/graphql/schema/createReadSDL.d.ts +3 -0
  129. package/graphql/schema/createReadSDL.js +33 -25
  130. package/graphql/schema/createReadSDL.js.map +1 -1
  131. package/graphql/schema/resolvers/commonFieldResolvers.d.ts +3 -2
  132. package/graphql/schema/resolvers/commonFieldResolvers.js +2 -1
  133. package/graphql/schema/resolvers/commonFieldResolvers.js.map +1 -1
  134. package/graphql/schema/schemaPlugins.d.ts +8 -3
  135. package/graphql/schema/schemaPlugins.js +60 -48
  136. package/graphql/schema/schemaPlugins.js.map +1 -1
  137. package/graphql/system.js +55 -74
  138. package/graphql/system.js.map +1 -1
  139. package/graphqlFields/dynamicZone/dynamicZoneField.d.ts +2 -0
  140. package/graphqlFields/dynamicZone/dynamicZoneField.js +216 -0
  141. package/graphqlFields/dynamicZone/dynamicZoneField.js.map +1 -0
  142. package/graphqlFields/dynamicZone/dynamicZoneStorage.d.ts +3 -0
  143. package/graphqlFields/dynamicZone/dynamicZoneStorage.js +63 -0
  144. package/graphqlFields/dynamicZone/dynamicZoneStorage.js.map +1 -0
  145. package/graphqlFields/dynamicZone/index.d.ts +2 -0
  146. package/graphqlFields/dynamicZone/index.js +19 -0
  147. package/graphqlFields/dynamicZone/index.js.map +1 -0
  148. package/graphqlFields/index.d.ts +1 -1
  149. package/graphqlFields/index.js +2 -1
  150. package/graphqlFields/index.js.map +1 -1
  151. package/graphqlFields/number.js +4 -0
  152. package/graphqlFields/number.js.map +1 -1
  153. package/graphqlFields/object.js +131 -68
  154. package/graphqlFields/object.js.map +1 -1
  155. package/graphqlFields/ref.js +70 -56
  156. package/graphqlFields/ref.js.map +1 -1
  157. package/index.d.ts +5 -3
  158. package/index.js +24 -9
  159. package/index.js.map +1 -1
  160. package/package.json +27 -28
  161. package/plugins/CmsGraphQLSchemaPlugin.d.ts +5 -0
  162. package/plugins/CmsGraphQLSchemaPlugin.js +12 -0
  163. package/plugins/CmsGraphQLSchemaPlugin.js.map +1 -0
  164. package/plugins/CmsGraphQLSchemaSorterPlugin.d.ts +20 -0
  165. package/plugins/CmsGraphQLSchemaSorterPlugin.js +28 -0
  166. package/plugins/CmsGraphQLSchemaSorterPlugin.js.map +1 -0
  167. package/plugins/CmsModelFieldConverterPlugin.d.ts +2 -2
  168. package/plugins/CmsModelFieldConverterPlugin.js.map +1 -1
  169. package/plugins/CmsModelPlugin.d.ts +19 -3
  170. package/plugins/CmsModelPlugin.js +28 -2
  171. package/plugins/CmsModelPlugin.js.map +1 -1
  172. package/plugins/StorageTransformPlugin.d.ts +11 -11
  173. package/plugins/StorageTransformPlugin.js.map +1 -1
  174. package/plugins/index.d.ts +2 -0
  175. package/plugins/index.js +22 -0
  176. package/plugins/index.js.map +1 -1
  177. package/storage/object.js +4 -2
  178. package/storage/object.js.map +1 -1
  179. package/types.d.ts +318 -82
  180. package/types.js +63 -8
  181. package/types.js.map +1 -1
  182. package/upgrades/5.33.0/index.js +26 -3
  183. package/upgrades/5.33.0/index.js.map +1 -1
  184. package/upgrades/index.js +3 -0
  185. package/upgrades/index.js.map +1 -1
  186. package/utils/converters/ConverterCollection.js +5 -2
  187. package/utils/converters/ConverterCollection.js.map +1 -1
  188. package/utils/converters/valueKeyStorageConverter.d.ts +2 -2
  189. package/utils/converters/valueKeyStorageConverter.js +8 -1
  190. package/utils/converters/valueKeyStorageConverter.js.map +1 -1
  191. package/utils/createTypeFromFields.d.ts +16 -0
  192. package/utils/createTypeFromFields.js +67 -0
  193. package/utils/createTypeFromFields.js.map +1 -0
  194. package/utils/createTypeName.js +2 -1
  195. package/utils/createTypeName.js.map +1 -1
  196. package/utils/entryStorage.js +14 -11
  197. package/utils/entryStorage.js.map +1 -1
  198. package/utils/getBaseFieldType.d.ts +4 -0
  199. package/utils/getBaseFieldType.js +10 -0
  200. package/utils/getBaseFieldType.js.map +1 -0
  201. package/utils/getEntryDescription.d.ts +2 -0
  202. package/utils/getEntryDescription.js +17 -0
  203. package/utils/getEntryDescription.js.map +1 -0
  204. package/utils/getEntryImage.d.ts +2 -0
  205. package/utils/getEntryImage.js +17 -0
  206. package/utils/getEntryImage.js.map +1 -0
  207. package/utils/getSchemaFromFieldPlugins.d.ts +4 -7
  208. package/utils/getSchemaFromFieldPlugins.js +22 -14
  209. package/utils/getSchemaFromFieldPlugins.js.map +1 -1
  210. package/utils/incrementEntryIdVersion.d.ts +5 -0
  211. package/utils/incrementEntryIdVersion.js +29 -0
  212. package/utils/incrementEntryIdVersion.js.map +1 -0
  213. package/utils/ownership.d.ts +3 -3
  214. package/utils/ownership.js.map +1 -1
  215. package/utils/pluralizedTypeName.js +6 -0
  216. package/utils/pluralizedTypeName.js.map +1 -1
  217. package/utils/renderFields.d.ts +2 -1
  218. package/utils/renderFields.js +10 -2
  219. package/utils/renderFields.js.map +1 -1
  220. package/utils/renderGetFilterFields.js +6 -3
  221. package/utils/renderGetFilterFields.js.map +1 -1
  222. package/utils/renderInputFields.d.ts +2 -1
  223. package/utils/renderInputFields.js +6 -1
  224. package/utils/renderInputFields.js.map +1 -1
  225. package/utils/renderListFilterFields.js +4 -2
  226. package/utils/renderListFilterFields.js.map +1 -1
  227. package/utils/renderSortEnum.d.ts +7 -4
  228. package/utils/renderSortEnum.js +21 -4
  229. package/utils/renderSortEnum.js.map +1 -1
  230. package/validators/dynamicZone.d.ts +2 -0
  231. package/validators/dynamicZone.js +20 -0
  232. package/validators/dynamicZone.js.map +1 -0
  233. package/validators/index.js +2 -1
  234. package/validators/index.js.map +1 -1
  235. package/crud/index.d.ts +0 -6
  236. package/crud/index.js +0 -69
  237. package/crud/index.js.map +0 -1
  238. package/graphql/schema/resolvers/manage/resolveRequestChanges.d.ts +0 -7
  239. package/graphql/schema/resolvers/manage/resolveRequestChanges.js +0 -21
  240. package/graphql/schema/resolvers/manage/resolveRequestChanges.js.map +0 -1
  241. package/graphql/schema/resolvers/manage/resolveRequestReview.d.ts +0 -7
  242. package/graphql/schema/resolvers/manage/resolveRequestReview.js +0 -21
  243. package/graphql/schema/resolvers/manage/resolveRequestReview.js.map +0 -1
  244. package/utils/filterModelFields.d.ts +0 -16
  245. package/utils/filterModelFields.js +0 -71
  246. package/utils/filterModelFields.js.map +0 -1
@@ -6,22 +6,23 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.createContentEntryCrud = exports.STATUS_UNPUBLISHED = exports.STATUS_PUBLISHED = exports.STATUS_DRAFT = void 0;
8
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9
+ var _merge = _interopRequireDefault(require("lodash/merge"));
9
10
  var _mdbid = _interopRequireDefault(require("mdbid"));
11
+ var _error = _interopRequireDefault(require("@webiny/error"));
10
12
  var _handlerGraphql = require("@webiny/handler-graphql");
11
13
  var _entryDataValidation = require("./contentEntry/entryDataValidation");
12
- var _error = _interopRequireDefault(require("@webiny/error"));
13
14
  var _pubsub = require("@webiny/pubsub");
14
15
  var _beforeCreate = require("./contentEntry/beforeCreate");
15
16
  var _beforeUpdate = require("./contentEntry/beforeUpdate");
16
17
  var _utils = require("@webiny/utils");
17
18
  var _afterDelete = require("./contentEntry/afterDelete");
18
19
  var _referenceFieldsMapping = require("./contentEntry/referenceFieldsMapping");
19
- var _merge = _interopRequireDefault(require("lodash/merge"));
20
20
  var _permissions = require("../utils/permissions");
21
21
  var _access = require("../utils/access");
22
22
  var _ownership = require("../utils/ownership");
23
23
  var _entryStorage = require("../utils/entryStorage");
24
24
  var _valueKeyStorageConverter = require("../utils/converters/valueKeyStorageConverter");
25
+ var _searchableFields = require("./contentEntry/searchableFields");
25
26
  /**
26
27
  * Package mdbid does not have types.
27
28
  */
@@ -130,16 +131,19 @@ const mapAndCleanUpdatedInputData = (model, input) => {
130
131
  */
131
132
  const createEntryMeta = (input, original) => {
132
133
  const meta = (0, _merge.default)(original || {}, input || {});
133
- for (const key in meta) {
134
- if (meta[key] !== undefined || meta[key] !== null) {
135
- continue;
134
+ return (0, _utils.removeUndefinedValues)((0, _utils.removeNullValues)(meta));
135
+ };
136
+ const createEntryId = input => {
137
+ let entryId = (0, _mdbid.default)();
138
+ if (input.id) {
139
+ if (input.id.match(/^([a-zA-Z0-9])([a-zA-Z0-9\-]+)([a-zA-Z0-9])$/) === null) {
140
+ throw new _error.default("The provided ID is not valid. It must be a string which can be A-Z, a-z, 0-9, - and it cannot start or end with a -.", "INVALID_ID", {
141
+ id: input.id
142
+ });
136
143
  }
137
- delete meta[key];
144
+ entryId = input.id;
138
145
  }
139
- return meta;
140
- };
141
- const createEntryId = version => {
142
- const entryId = (0, _mdbid.default)();
146
+ const version = 1;
143
147
  return {
144
148
  entryId,
145
149
  version,
@@ -168,31 +172,6 @@ const increaseEntryIdVersion = id => {
168
172
  })
169
173
  };
170
174
  };
171
- const getSearchableFields = params => {
172
- const {
173
- plugins,
174
- model,
175
- fields
176
- } = params;
177
- const fieldPluginMap = plugins.byType("cms-model-field-to-graphql").reduce((collection, field) => {
178
- collection[field.fieldType] = field;
179
- return collection;
180
- }, {});
181
- return model.fields.filter(field => {
182
- if (!field.fieldId) {
183
- return false;
184
- }
185
- const plugin = fieldPluginMap[field.type];
186
- if (!plugin) {
187
- return false;
188
- } else if (!plugin.fullTextSearch) {
189
- return false;
190
- } else if (!fields || fields.length === 0) {
191
- return true;
192
- }
193
- return fields.includes(field.fieldId);
194
- }).map(field => field.fieldId);
195
- };
196
175
  const allowedEntryStatus = ["draft", "published", "unpublished"];
197
176
  const transformEntryStatus = status => {
198
177
  return allowedEntryStatus.includes(status) ? status : "draft";
@@ -202,51 +181,85 @@ const createContentEntryCrud = params => {
202
181
  storageOperations,
203
182
  context,
204
183
  getIdentity,
205
- getTenant
184
+ getTenant,
185
+ getLocale
206
186
  } = params;
207
187
  const {
208
188
  plugins
209
189
  } = context;
190
+ const getCreatedBy = () => {
191
+ const identity = getIdentity();
192
+ return {
193
+ id: identity.id,
194
+ displayName: identity.displayName,
195
+ type: identity.type
196
+ };
197
+ };
210
198
 
211
- // create
199
+ /**
200
+ * Create
201
+ */
212
202
  const onEntryBeforeCreate = (0, _pubsub.createTopic)("cms.onEntryBeforeCreate");
213
203
  const onEntryAfterCreate = (0, _pubsub.createTopic)("cms.onEntryAfterCreate");
214
204
  const onEntryCreateError = (0, _pubsub.createTopic)("cms.onEntryCreateError");
215
205
 
216
- // create revision
206
+ /**
207
+ * Create new revision
208
+ */
217
209
  const onEntryBeforeCreateRevision = (0, _pubsub.createTopic)("cms.onEntryBeforeCreateRevision");
218
210
  const onEntryRevisionAfterCreate = (0, _pubsub.createTopic)("cms.onEntryRevisionAfterCreate");
219
211
  const onEntryCreateRevisionError = (0, _pubsub.createTopic)("cms.onEntryCreateRevisionError");
220
212
 
221
- // update
213
+ /**
214
+ * Update
215
+ */
222
216
  const onEntryBeforeUpdate = (0, _pubsub.createTopic)("cms.onEntryBeforeUpdate");
223
217
  const onEntryAfterUpdate = (0, _pubsub.createTopic)("cms.onEntryAfterUpdate");
224
218
  const onEntryUpdateError = (0, _pubsub.createTopic)("cms.onEntryUpdateError");
225
219
 
226
- // publish
220
+ /**
221
+ * Publish
222
+ */
227
223
  const onEntryBeforePublish = (0, _pubsub.createTopic)("cms.onEntryBeforePublish");
228
- const onEntryAfterPublish = (0, _pubsub.createTopic)("cms.onEntryAfterPublic");
224
+ const onEntryAfterPublish = (0, _pubsub.createTopic)("cms.onEntryAfterPublish");
229
225
  const onEntryPublishError = (0, _pubsub.createTopic)("cms.onEntryPublishError");
230
226
 
231
- // unpublish
227
+ /**
228
+ * Republish
229
+ */
230
+ const onEntryBeforeRepublish = (0, _pubsub.createTopic)("cms.onEntryBeforeRepublish");
231
+ const onEntryAfterRepublish = (0, _pubsub.createTopic)("cms.onEntryAfterRepublish");
232
+ const onEntryRepublishError = (0, _pubsub.createTopic)("cms.onEntryRepublishError");
233
+
234
+ /**
235
+ * Unpublish
236
+ */
232
237
  const onEntryBeforeUnpublish = (0, _pubsub.createTopic)("cms.onEntryBeforeUnpublish");
233
238
  const onEntryAfterUnpublish = (0, _pubsub.createTopic)("cms.onEntryAfterUnpublish");
234
239
  const onEntryUnpublishError = (0, _pubsub.createTopic)("cms.onEntryUnpublishError");
235
240
 
236
- // delete
241
+ /**
242
+ * Delete
243
+ */
237
244
  const onEntryBeforeDelete = (0, _pubsub.createTopic)("cms.onEntryBeforeDelete");
238
245
  const onEntryAfterDelete = (0, _pubsub.createTopic)("cms.onEntryAfterDelete");
239
246
  const onEntryDeleteError = (0, _pubsub.createTopic)("cms.onEntryDeleteError");
240
247
 
241
- // delete revision
248
+ /**
249
+ * Delete revision
250
+ */
242
251
  const onEntryRevisionBeforeDelete = (0, _pubsub.createTopic)("cms.onEntryRevisionBeforeDelete");
243
252
  const onEntryRevisionAfterDelete = (0, _pubsub.createTopic)("cms.onEntryRevisionAfterDelete");
244
253
  const onEntryRevisionDeleteError = (0, _pubsub.createTopic)("cms.onEntryRevisionDeleteError");
245
254
 
246
- // get
255
+ /**
256
+ * Get entry
257
+ */
247
258
  const onEntryBeforeGet = (0, _pubsub.createTopic)("cms.onEntryBeforeGet");
248
259
 
249
- // list
260
+ /**
261
+ * List entries
262
+ */
250
263
  const onEntryBeforeList = (0, _pubsub.createTopic)("cms.onEntryBeforeList");
251
264
 
252
265
  /**
@@ -271,38 +284,74 @@ const createContentEntryCrud = params => {
271
284
  /**
272
285
  * A helper to delete the entire entry.
273
286
  */
274
- const deleteEntry = async params => {
275
- const {
276
- model,
277
- entry
278
- } = params;
279
- try {
280
- await onEntryBeforeDelete.publish({
281
- entry,
282
- model
283
- });
284
- await storageOperations.entries.delete(model, {
285
- entry
286
- });
287
- await onEntryAfterDelete.publish({
288
- entry,
289
- model
290
- });
291
- } catch (ex) {
292
- await onEntryDeleteError.publish({
293
- entry,
287
+ const deleteEntryHelper = async params => {
288
+ return context.benchmark.measure("headlessCms.crud.entries.deleteEntry", async () => {
289
+ const {
294
290
  model,
295
- error: ex
296
- });
297
- throw new _error.default(ex.message || "Could not delete entry.", ex.code || "DELETE_ERROR", {
298
291
  entry
299
- });
300
- }
292
+ } = params;
293
+ try {
294
+ await onEntryBeforeDelete.publish({
295
+ entry,
296
+ model
297
+ });
298
+ await storageOperations.entries.delete(model, {
299
+ entry
300
+ });
301
+ await onEntryAfterDelete.publish({
302
+ entry,
303
+ model
304
+ });
305
+ } catch (ex) {
306
+ await onEntryDeleteError.publish({
307
+ entry,
308
+ model,
309
+ error: ex
310
+ });
311
+ throw new _error.default(ex.message || "Could not delete entry.", ex.code || "DELETE_ERROR", {
312
+ entry
313
+ });
314
+ }
315
+ });
301
316
  };
302
317
  /**
303
318
  * A helper to get entries by revision IDs
304
319
  */
305
320
  const getEntriesByIds = async (initialModel, ids) => {
321
+ return context.benchmark.measure("headlessCms.crud.entries.getEntriesByIds", async () => {
322
+ const permission = await checkEntryPermissions({
323
+ rwd: "r"
324
+ });
325
+ await (0, _access.checkModelAccess)(context, initialModel);
326
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
327
+ model: initialModel,
328
+ plugins
329
+ });
330
+ const entries = await storageOperations.entries.getByIds(model, {
331
+ ids
332
+ });
333
+ return entries.filter(entry => (0, _ownership.validateOwnership)(context, permission, entry));
334
+ });
335
+ };
336
+ const getEntryById = async (initialModel, id) => {
337
+ const where = {
338
+ id
339
+ };
340
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
341
+ model: initialModel,
342
+ plugins
343
+ });
344
+ await onEntryBeforeGet.publish({
345
+ where,
346
+ model
347
+ });
348
+ const [entry] = await getEntriesByIds(model, [id]);
349
+ if (!entry) {
350
+ throw new _handlerGraphql.NotFoundError(`Entry by ID "${id}" not found.`);
351
+ }
352
+ return entry;
353
+ };
354
+ const getPublishedEntriesByIds = async (initialModel, ids) => {
306
355
  const permission = await checkEntryPermissions({
307
356
  rwd: "r"
308
357
  });
@@ -311,239 +360,852 @@ const createContentEntryCrud = params => {
311
360
  model: initialModel,
312
361
  plugins
313
362
  });
314
- const entries = await storageOperations.entries.getByIds(model, {
363
+ const entries = await storageOperations.entries.getPublishedByIds(model, {
315
364
  ids
316
365
  });
317
366
  return entries.filter(entry => (0, _ownership.validateOwnership)(context, permission, entry));
318
367
  };
319
- return {
320
- /**
321
- * Deprecated - will be removed in 5.35.0
322
- */
323
- onBeforeEntryCreate: onEntryBeforeCreate,
324
- onAfterEntryCreate: onEntryAfterCreate,
325
- onBeforeEntryCreateRevision: onEntryBeforeCreateRevision,
326
- onAfterEntryCreateRevision: onEntryRevisionAfterCreate,
327
- onBeforeEntryUpdate: onEntryBeforeUpdate,
328
- onAfterEntryUpdate: onEntryAfterUpdate,
329
- onBeforeEntryDelete: onEntryBeforeDelete,
330
- onAfterEntryDelete: onEntryAfterDelete,
331
- onBeforeEntryDeleteRevision: onEntryRevisionBeforeDelete,
332
- onAfterEntryDeleteRevision: onEntryRevisionAfterDelete,
333
- onBeforeEntryPublish: onEntryBeforePublish,
334
- onAfterEntryPublish: onEntryAfterPublish,
335
- onBeforeEntryUnpublish: onEntryBeforeUnpublish,
336
- onAfterEntryUnpublish: onEntryAfterUnpublish,
337
- onBeforeEntryGet: onEntryBeforeGet,
338
- onBeforeEntryList: onEntryBeforeList,
368
+ const getLatestEntriesByIds = async (initialModel, ids) => {
369
+ const permission = await checkEntryPermissions({
370
+ rwd: "r"
371
+ });
372
+ await (0, _access.checkModelAccess)(context, initialModel);
373
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
374
+ model: initialModel,
375
+ plugins
376
+ });
377
+ const entries = await storageOperations.entries.getLatestByIds(model, {
378
+ ids
379
+ });
380
+ return entries.filter(entry => (0, _ownership.validateOwnership)(context, permission, entry));
381
+ };
382
+ const getEntry = async (initialModel, params) => {
383
+ await checkEntryPermissions({
384
+ rwd: "r"
385
+ });
386
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
387
+ model: initialModel,
388
+ plugins
389
+ });
390
+ const {
391
+ where,
392
+ sort
393
+ } = params;
394
+ await onEntryBeforeGet.publish({
395
+ where,
396
+ model
397
+ });
398
+ const [items] = await listEntries(model, {
399
+ where,
400
+ sort,
401
+ limit: 1
402
+ });
403
+ const item = items.shift();
404
+ if (!item) {
405
+ throw new _handlerGraphql.NotFoundError(`Entry not found!`);
406
+ }
407
+ return item;
408
+ };
409
+ const getEntryRevisions = async (initialModel, entryId) => {
410
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
411
+ model: initialModel,
412
+ plugins
413
+ });
414
+ return storageOperations.entries.getRevisions(model, {
415
+ id: entryId
416
+ });
417
+ };
418
+ const listEntries = async (initialModel, params) => {
419
+ const permission = await checkEntryPermissions({
420
+ rwd: "r"
421
+ });
422
+ await (0, _access.checkModelAccess)(context, initialModel);
423
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
424
+ model: initialModel,
425
+ plugins
426
+ });
427
+ const {
428
+ where: initialWhere,
429
+ limit: initialLimit
430
+ } = params;
431
+ const limit = initialLimit && initialLimit > 0 ? initialLimit : 50;
339
432
  /**
340
- * Released in 5.34.0
433
+ * We always assign tenant and locale because we do not allow one model to have content through multiple tenants.
341
434
  */
342
- onEntryBeforeCreate,
343
- onEntryAfterCreate,
344
- onEntryCreateError,
345
- onEntryRevisionBeforeCreate: onEntryBeforeCreateRevision,
346
- onEntryRevisionAfterCreate,
347
- onEntryRevisionCreateError: onEntryCreateRevisionError,
348
- onEntryBeforeUpdate,
349
- onEntryAfterUpdate,
350
- onEntryUpdateError,
351
- onEntryBeforeDelete,
352
- onEntryAfterDelete,
353
- onEntryDeleteError,
354
- onEntryRevisionBeforeDelete,
355
- onEntryRevisionAfterDelete,
356
- onEntryRevisionDeleteError,
357
- onEntryBeforePublish,
358
- onEntryAfterPublish,
359
- onEntryPublishError,
360
- onEntryBeforeUnpublish,
361
- onEntryAfterUnpublish,
362
- onEntryUnpublishError,
363
- onEntryBeforeGet,
364
- onEntryBeforeList,
435
+ const where = (0, _objectSpread2.default)({}, initialWhere);
365
436
  /**
366
- * Get entries by exact revision IDs from the database.
437
+ * Possibly only get records which are owned by current user.
438
+ * Or if searching for the owner set that value - in the case that user can see other entries than their own.
367
439
  */
368
- getEntriesByIds: getEntriesByIds,
440
+ const ownedBy = permission.own ? getIdentity().id : where.ownedBy;
441
+ if (ownedBy !== undefined) {
442
+ where.ownedBy = ownedBy;
443
+ }
369
444
  /**
370
- * Get a single entry by revision ID from the database.
445
+ * Where must contain either latest or published keys.
446
+ * We cannot list entries without one of those
371
447
  */
372
- async getEntryById(initialModel, id) {
373
- const where = {
374
- id
375
- };
376
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
377
- model: initialModel,
378
- plugins
448
+ if (where.latest && where.published) {
449
+ throw new _error.default("Cannot list entries that are both published and latest.", "LIST_ENTRIES_ERROR", {
450
+ where
451
+ });
452
+ } else if (!where.latest && !where.published) {
453
+ throw new _error.default("Cannot list entries if we do not have latest or published defined.", "LIST_ENTRIES_ERROR", {
454
+ where
379
455
  });
380
- await onEntryBeforeGet.publish({
456
+ }
457
+ const fields = (0, _searchableFields.getSearchableFields)({
458
+ fields: model.fields,
459
+ plugins: context.plugins,
460
+ input: params.fields || []
461
+ });
462
+ try {
463
+ await onEntryBeforeList.publish({
381
464
  where,
382
465
  model
383
466
  });
384
- const [entry] = await getEntriesByIds(model, [id]);
385
- if (!entry) {
386
- throw new _handlerGraphql.NotFoundError(`Entry by ID "${id}" not found.`);
387
- }
388
- return entry;
389
- },
467
+ const {
468
+ hasMoreItems,
469
+ totalCount,
470
+ cursor,
471
+ items
472
+ } = await storageOperations.entries.list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
473
+ limit,
474
+ where,
475
+ fields
476
+ }));
477
+ const meta = {
478
+ hasMoreItems,
479
+ totalCount,
480
+ /**
481
+ * Cursor should be null if there are no more items to load.
482
+ * Just make sure of that, disregarding what is returned from the storageOperations.entries.list method.
483
+ */
484
+ cursor: hasMoreItems ? cursor : null
485
+ };
486
+ return [items, meta];
487
+ } catch (ex) {
488
+ throw new _error.default("Error while fetching entries from storage.", "LIST_ENTRIES_ERROR", {
489
+ params,
490
+ error: {
491
+ message: ex.message,
492
+ code: ex.code,
493
+ data: ex.data
494
+ },
495
+ model,
496
+ fields
497
+ });
498
+ }
499
+ };
500
+ const createEntry = async (initialModel, inputData) => {
501
+ await checkEntryPermissions({
502
+ rwd: "w"
503
+ });
504
+ await (0, _access.checkModelAccess)(context, initialModel);
505
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
506
+ model: initialModel,
507
+ plugins
508
+ });
509
+
390
510
  /**
391
- * Get published revisions by entry IDs.
511
+ * Make sure we only work with fields that are defined in the model.
392
512
  */
393
- async getPublishedEntriesByIds(initialModel, ids) {
394
- const permission = await checkEntryPermissions({
395
- rwd: "r"
396
- });
397
- await (0, _access.checkModelAccess)(context, initialModel);
398
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
399
- model: initialModel,
400
- plugins
401
- });
402
- const entries = await storageOperations.entries.getPublishedByIds(model, {
403
- ids
404
- });
405
- return entries.filter(entry => (0, _ownership.validateOwnership)(context, permission, entry));
406
- },
513
+ const initialInput = mapAndCleanCreateInputData(model, inputData);
514
+ await (0, _entryDataValidation.validateModelEntryData)({
515
+ context,
516
+ model,
517
+ data: initialInput
518
+ });
519
+ const input = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
520
+ context,
521
+ model,
522
+ input: initialInput,
523
+ validateEntries: true
524
+ });
525
+ const locale = getLocale();
526
+ const owner = getCreatedBy();
527
+ const {
528
+ id,
529
+ entryId,
530
+ version
531
+ } = createEntryId(inputData);
407
532
  /**
408
- * Get latest revisions by entry IDs.
533
+ * There is a possibility that user sends an ID in the input, so we will use that one.
534
+ * There is no check if the ID is unique or not, that is up to the user.
409
535
  */
410
- async getLatestEntriesByIds(initialModel, ids) {
411
- const permission = await checkEntryPermissions({
412
- rwd: "r"
536
+ const entry = {
537
+ webinyVersion: context.WEBINY_VERSION,
538
+ tenant: getTenant().id,
539
+ entryId,
540
+ id,
541
+ modelId: model.modelId,
542
+ locale: locale.code,
543
+ createdOn: new Date().toISOString(),
544
+ savedOn: new Date().toISOString(),
545
+ createdBy: owner,
546
+ ownedBy: owner,
547
+ modifiedBy: null,
548
+ version,
549
+ locked: false,
550
+ status: STATUS_DRAFT,
551
+ values: input
552
+ };
553
+ let storageEntry = null;
554
+ try {
555
+ await onEntryBeforeCreate.publish({
556
+ entry,
557
+ input,
558
+ model
413
559
  });
414
- await (0, _access.checkModelAccess)(context, initialModel);
415
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
416
- model: initialModel,
417
- plugins
560
+ storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
561
+ const result = await storageOperations.entries.create(model, {
562
+ entry,
563
+ storageEntry
418
564
  });
419
- const entries = await storageOperations.entries.getLatestByIds(model, {
420
- ids
565
+ await onEntryAfterCreate.publish({
566
+ entry,
567
+ storageEntry: result,
568
+ model,
569
+ input
421
570
  });
422
- return entries.filter(entry => (0, _ownership.validateOwnership)(context, permission, entry));
423
- },
424
- async getEntryRevisions(initialModel, entryId) {
425
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
426
- model: initialModel,
427
- plugins
571
+ return result;
572
+ } catch (ex) {
573
+ await onEntryCreateError.publish({
574
+ error: ex,
575
+ entry,
576
+ model,
577
+ input
428
578
  });
429
- return storageOperations.entries.getRevisions(model, {
430
- id: entryId
579
+ throw new _error.default(ex.message || "Could not create content entry.", ex.code || "CREATE_ENTRY_ERROR", ex.data || {
580
+ error: ex,
581
+ input,
582
+ entry,
583
+ storageEntry
431
584
  });
432
- },
433
- /**
434
- * TODO determine if this method is required at all.
435
- *
436
- * @internal
585
+ }
586
+ };
587
+ const createEntryRevisionFrom = async (initialModel, sourceId, inputData) => {
588
+ const permission = await checkEntryPermissions({
589
+ rwd: "w"
590
+ });
591
+ await (0, _access.checkModelAccess)(context, initialModel);
592
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
593
+ model: initialModel,
594
+ plugins
595
+ });
596
+
597
+ /**
598
+ * Make sure we only work with fields that are defined in the model.
437
599
  */
438
- async getEntry(initialModel, params) {
439
- await checkEntryPermissions({
440
- rwd: "r"
600
+ const input = mapAndCleanUpdatedInputData(model, inputData);
601
+
602
+ /**
603
+ * Entries are identified by a common parent ID + Revision number.
604
+ */
605
+ const {
606
+ id: uniqueId
607
+ } = (0, _utils.parseIdentifier)(sourceId);
608
+ const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
609
+ id: sourceId
610
+ });
611
+ const latestStorageEntry = await storageOperations.entries.getLatestRevisionByEntryId(model, {
612
+ id: uniqueId
613
+ });
614
+ if (!originalStorageEntry) {
615
+ throw new _handlerGraphql.NotFoundError(`Entry "${sourceId}" of model "${model.modelId}" was not found.`);
616
+ }
617
+
618
+ /**
619
+ * We need to convert data from DB to its original form before using it further.
620
+ */
621
+ const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
622
+ const initialValues = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry.values), input);
623
+ await (0, _entryDataValidation.validateModelEntryData)({
624
+ context,
625
+ model,
626
+ data: initialValues,
627
+ entry: originalEntry
628
+ });
629
+ const values = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
630
+ context,
631
+ model,
632
+ input: initialValues,
633
+ validateEntries: false
634
+ });
635
+ (0, _ownership.checkOwnership)(context, permission, originalEntry);
636
+ const identity = getIdentity();
637
+ const latestId = latestStorageEntry ? latestStorageEntry.id : sourceId;
638
+ const {
639
+ id,
640
+ version: nextVersion
641
+ } = increaseEntryIdVersion(latestId);
642
+ const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
643
+ id,
644
+ version: nextVersion,
645
+ savedOn: new Date().toISOString(),
646
+ createdOn: new Date().toISOString(),
647
+ createdBy: {
648
+ id: identity.id,
649
+ displayName: identity.displayName,
650
+ type: identity.type
651
+ },
652
+ modifiedBy: null,
653
+ locked: false,
654
+ publishedOn: undefined,
655
+ status: STATUS_DRAFT,
656
+ values
657
+ });
658
+ let storageEntry = null;
659
+ try {
660
+ await onEntryBeforeCreateRevision.publish({
661
+ input,
662
+ entry,
663
+ original: originalEntry,
664
+ model
441
665
  });
442
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
443
- model: initialModel,
444
- plugins
666
+ storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
667
+ const result = await storageOperations.entries.createRevisionFrom(model, {
668
+ entry,
669
+ storageEntry
445
670
  });
446
- const {
447
- where,
448
- sort
449
- } = params;
450
- await onEntryBeforeGet.publish({
451
- where,
671
+ await onEntryRevisionAfterCreate.publish({
672
+ input,
673
+ entry,
674
+ model,
675
+ original: originalEntry,
676
+ storageEntry: result
677
+ });
678
+ return result;
679
+ } catch (ex) {
680
+ await onEntryCreateRevisionError.publish({
681
+ entry,
682
+ original: originalEntry,
683
+ model,
684
+ input,
685
+ error: ex
686
+ });
687
+ throw new _error.default(ex.message || "Could not create entry from existing one.", ex.code || "CREATE_FROM_REVISION_ERROR", {
688
+ error: ex,
689
+ entry,
690
+ storageEntry,
691
+ originalEntry,
692
+ originalStorageEntry
693
+ });
694
+ }
695
+ };
696
+ const updateEntry = async (initialModel, id, inputData, metaInput) => {
697
+ const permission = await checkEntryPermissions({
698
+ rwd: "w"
699
+ });
700
+ await (0, _access.checkModelAccess)(context, initialModel);
701
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
702
+ model: initialModel,
703
+ plugins
704
+ });
705
+
706
+ /**
707
+ * Make sure we only work with fields that are defined in the model.
708
+ */
709
+ const input = mapAndCleanUpdatedInputData(model, inputData);
710
+
711
+ /**
712
+ * The entry we are going to update.
713
+ */
714
+ const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
715
+ id
716
+ });
717
+ if (!originalStorageEntry) {
718
+ throw new _handlerGraphql.NotFoundError(`Entry "${id}" of model "${model.modelId}" was not found.`);
719
+ }
720
+ if (originalStorageEntry.locked) {
721
+ throw new _error.default(`Cannot update entry because it's locked.`, "CONTENT_ENTRY_UPDATE_ERROR");
722
+ }
723
+ const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
724
+ await (0, _entryDataValidation.validateModelEntryData)({
725
+ context,
726
+ model,
727
+ data: input,
728
+ entry: originalEntry
729
+ });
730
+ (0, _ownership.checkOwnership)(context, permission, originalEntry);
731
+ const initialValues = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry.values), input);
732
+ const values = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
733
+ context,
734
+ model,
735
+ input: initialValues,
736
+ validateEntries: false
737
+ });
738
+ /**
739
+ * If users wants to remove a key from meta values, they need to send meta key with the null value.
740
+ */
741
+ const meta = createEntryMeta(metaInput, originalEntry.meta);
742
+ /**
743
+ * We always send the full entry to the hooks and storage operations update.
744
+ */
745
+ const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
746
+ savedOn: new Date().toISOString(),
747
+ modifiedBy: getCreatedBy(),
748
+ values,
749
+ meta,
750
+ status: transformEntryStatus(originalEntry.status)
751
+ });
752
+ let storageEntry = null;
753
+ try {
754
+ await onEntryBeforeUpdate.publish({
755
+ entry,
756
+ model,
757
+ input,
758
+ original: originalEntry
759
+ });
760
+ storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
761
+ const result = await storageOperations.entries.update(model, {
762
+ entry,
763
+ storageEntry
764
+ });
765
+ await onEntryAfterUpdate.publish({
766
+ entry,
767
+ storageEntry: result,
768
+ model,
769
+ input,
770
+ original: originalEntry
771
+ });
772
+ return result;
773
+ } catch (ex) {
774
+ await onEntryUpdateError.publish({
775
+ entry,
776
+ model,
777
+ input,
778
+ error: ex
779
+ });
780
+ throw new _error.default(ex.message || "Could not update existing entry.", ex.code || "UPDATE_ERROR", {
781
+ error: ex,
782
+ entry,
783
+ storageEntry,
784
+ originalEntry,
785
+ input
786
+ });
787
+ }
788
+ };
789
+ const republishEntry = async (initialModel, id) => {
790
+ await checkEntryPermissions({
791
+ rwd: "w"
792
+ });
793
+ await (0, _access.checkModelAccess)(context, initialModel);
794
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
795
+ model: initialModel,
796
+ plugins
797
+ });
798
+ /**
799
+ * Fetch the entry from the storage.
800
+ */
801
+ const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
802
+ id
803
+ });
804
+ if (!originalStorageEntry) {
805
+ throw new _handlerGraphql.NotFoundError(`Entry "${id}" was not found!`);
806
+ }
807
+ const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
808
+ /**
809
+ * We can only process published entries.
810
+ */
811
+ if (originalEntry.status !== "published") {
812
+ throw new _error.default("Entry with given ID is not published!", "NOT_PUBLISHED_ERROR", {
813
+ id,
814
+ original: originalEntry
815
+ });
816
+ }
817
+ const values = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
818
+ context,
819
+ model,
820
+ input: originalEntry.values,
821
+ validateEntries: false
822
+ });
823
+ const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
824
+ savedOn: new Date().toISOString(),
825
+ webinyVersion: context.WEBINY_VERSION,
826
+ values
827
+ });
828
+ const storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
829
+ /**
830
+ * First we need to update existing entry.
831
+ */
832
+ try {
833
+ await storageOperations.entries.update(model, {
834
+ entry,
835
+ storageEntry
836
+ });
837
+ } catch (ex) {
838
+ throw new _error.default("Could not update existing entry with new data while re-publishing.", "REPUBLISH_UPDATE_ERROR", {
839
+ entry
840
+ });
841
+ }
842
+ /**
843
+ * Then we move onto publishing it again.
844
+ */
845
+ try {
846
+ await onEntryBeforeRepublish.publish({
847
+ entry,
452
848
  model
453
849
  });
454
- const [items] = await this.listEntries(model, {
455
- where,
456
- sort,
457
- limit: 1
850
+ const result = await storageOperations.entries.publish(model, {
851
+ entry,
852
+ storageEntry
853
+ });
854
+ await onEntryAfterRepublish.publish({
855
+ entry,
856
+ model,
857
+ storageEntry
858
+ });
859
+ return result;
860
+ } catch (ex) {
861
+ await onEntryRepublishError.publish({
862
+ entry,
863
+ model,
864
+ error: ex
865
+ });
866
+ throw new _error.default("Could not publish existing entry while re-publishing.", "REPUBLISH_PUBLISH_ERROR", {
867
+ entry
868
+ });
869
+ }
870
+ };
871
+ const deleteEntryRevision = async (initialModel, revisionId) => {
872
+ const permission = await checkEntryPermissions({
873
+ rwd: "d"
874
+ });
875
+ await (0, _access.checkModelAccess)(context, initialModel);
876
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
877
+ model: initialModel,
878
+ plugins
879
+ });
880
+ const {
881
+ id: entryId,
882
+ version
883
+ } = (0, _utils.parseIdentifier)(revisionId);
884
+ const storageEntryToDelete = await storageOperations.entries.getRevisionById(model, {
885
+ id: revisionId
886
+ });
887
+ const latestStorageEntry = await storageOperations.entries.getLatestRevisionByEntryId(model, {
888
+ id: entryId
889
+ });
890
+ const previousStorageEntry = await storageOperations.entries.getPreviousRevision(model, {
891
+ entryId,
892
+ version: version
893
+ });
894
+ if (!storageEntryToDelete) {
895
+ throw new _handlerGraphql.NotFoundError(`Entry "${revisionId}" was not found!`);
896
+ }
897
+ (0, _ownership.checkOwnership)(context, permission, storageEntryToDelete);
898
+ const latestEntryRevisionId = latestStorageEntry ? latestStorageEntry.id : null;
899
+ const entryToDelete = await (0, _entryStorage.entryFromStorageTransform)(context, model, storageEntryToDelete);
900
+ /**
901
+ * If targeted record is the latest entry record and there is no previous one, we need to run full delete with hooks.
902
+ * At this point deleteRevision hooks are not fired.
903
+ */
904
+ if (entryToDelete.id === latestEntryRevisionId && !previousStorageEntry) {
905
+ return await deleteEntryHelper({
906
+ model,
907
+ entry: entryToDelete
908
+ });
909
+ }
910
+ /**
911
+ * If targeted record is latest entry revision, set the previous one as the new latest
912
+ */
913
+ let entryToSetAsLatest = null;
914
+ let storageEntryToSetAsLatest = null;
915
+ if (entryToDelete.id === latestEntryRevisionId && previousStorageEntry) {
916
+ entryToSetAsLatest = await (0, _entryStorage.entryFromStorageTransform)(context, model, previousStorageEntry);
917
+ storageEntryToSetAsLatest = previousStorageEntry;
918
+ }
919
+ try {
920
+ await onEntryRevisionBeforeDelete.publish({
921
+ entry: entryToDelete,
922
+ model
923
+ });
924
+ await storageOperations.entries.deleteRevision(model, {
925
+ entry: entryToDelete,
926
+ storageEntry: storageEntryToDelete,
927
+ latestEntry: entryToSetAsLatest,
928
+ latestStorageEntry: storageEntryToSetAsLatest
929
+ });
930
+ await onEntryRevisionAfterDelete.publish({
931
+ entry: entryToDelete,
932
+ model
933
+ });
934
+ } catch (ex) {
935
+ await onEntryRevisionDeleteError.publish({
936
+ entry: entryToDelete,
937
+ model,
938
+ error: ex
939
+ });
940
+ throw new _error.default(ex.message, ex.code || "DELETE_REVISION_ERROR", {
941
+ error: ex,
942
+ entry: entryToDelete,
943
+ storageEntry: storageEntryToDelete,
944
+ latestEntry: entryToSetAsLatest,
945
+ latestStorageEntry: storageEntryToSetAsLatest
946
+ });
947
+ }
948
+ };
949
+ const deleteEntry = async (initialModel, entryId) => {
950
+ const permission = await checkEntryPermissions({
951
+ rwd: "d"
952
+ });
953
+ await (0, _access.checkModelAccess)(context, initialModel);
954
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
955
+ model: initialModel,
956
+ plugins
957
+ });
958
+ const storageEntry = await storageOperations.entries.getLatestRevisionByEntryId(model, {
959
+ id: entryId
960
+ });
961
+ if (!storageEntry) {
962
+ throw new _handlerGraphql.NotFoundError(`Entry "${entryId}" was not found!`);
963
+ }
964
+ (0, _ownership.checkOwnership)(context, permission, storageEntry);
965
+ const entry = await (0, _entryStorage.entryFromStorageTransform)(context, model, storageEntry);
966
+ return await deleteEntryHelper({
967
+ model,
968
+ entry
969
+ });
970
+ };
971
+ const publishEntry = async (initialModel, id) => {
972
+ const permission = await checkEntryPermissions({
973
+ pw: "p"
974
+ });
975
+ await (0, _access.checkModelAccess)(context, initialModel);
976
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
977
+ model: initialModel,
978
+ plugins
979
+ });
980
+ const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
981
+ id
982
+ });
983
+ if (!originalStorageEntry) {
984
+ throw new _handlerGraphql.NotFoundError(`Entry "${id}" in the model "${model.modelId}" was not found.`);
985
+ }
986
+ (0, _ownership.checkOwnership)(context, permission, originalStorageEntry);
987
+ const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
988
+ const currentDate = new Date().toISOString();
989
+ const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
990
+ status: STATUS_PUBLISHED,
991
+ locked: true,
992
+ savedOn: currentDate,
993
+ publishedOn: currentDate
994
+ });
995
+ let storageEntry = null;
996
+ try {
997
+ await onEntryBeforePublish.publish({
998
+ entry,
999
+ model
1000
+ });
1001
+ storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
1002
+ const result = await storageOperations.entries.publish(model, {
1003
+ entry,
1004
+ storageEntry
1005
+ });
1006
+ await onEntryAfterPublish.publish({
1007
+ entry,
1008
+ storageEntry: result,
1009
+ model
1010
+ });
1011
+ return result;
1012
+ } catch (ex) {
1013
+ await onEntryPublishError.publish({
1014
+ entry,
1015
+ model,
1016
+ error: ex
1017
+ });
1018
+ throw new _error.default(ex.message || "Could not publish entry.", ex.code || "PUBLISH_ERROR", {
1019
+ error: ex,
1020
+ entry,
1021
+ storageEntry,
1022
+ originalEntry,
1023
+ originalStorageEntry
1024
+ });
1025
+ }
1026
+ };
1027
+ const unpublishEntry = async (initialModel, id) => {
1028
+ const permission = await checkEntryPermissions({
1029
+ pw: "u"
1030
+ });
1031
+ const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
1032
+ model: initialModel,
1033
+ plugins
1034
+ });
1035
+ const {
1036
+ id: entryId
1037
+ } = (0, _utils.parseIdentifier)(id);
1038
+ const originalStorageEntry = await storageOperations.entries.getPublishedRevisionByEntryId(model, {
1039
+ id: entryId
1040
+ });
1041
+ if (!originalStorageEntry) {
1042
+ throw new _handlerGraphql.NotFoundError(`Entry "${id}" of model "${model.modelId}" was not found.`);
1043
+ }
1044
+ if (originalStorageEntry.id !== id) {
1045
+ throw new _error.default(`Entry is not published.`, "UNPUBLISH_ERROR", {
1046
+ entry: originalStorageEntry
1047
+ });
1048
+ }
1049
+ (0, _ownership.checkOwnership)(context, permission, originalStorageEntry);
1050
+ const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
1051
+ const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
1052
+ status: STATUS_UNPUBLISHED
1053
+ });
1054
+ let storageEntry = null;
1055
+ try {
1056
+ await onEntryBeforeUnpublish.publish({
1057
+ entry,
1058
+ model
1059
+ });
1060
+ storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
1061
+ const result = await storageOperations.entries.unpublish(model, {
1062
+ entry,
1063
+ storageEntry
1064
+ });
1065
+ await onEntryAfterUnpublish.publish({
1066
+ entry,
1067
+ storageEntry: result,
1068
+ model
1069
+ });
1070
+ return result;
1071
+ } catch (ex) {
1072
+ await onEntryUnpublishError.publish({
1073
+ entry,
1074
+ model,
1075
+ error: ex
1076
+ });
1077
+ throw new _error.default(ex.message || "Could not unpublish entry.", ex.code || "UNPUBLISH_ERROR", {
1078
+ originalEntry,
1079
+ originalStorageEntry,
1080
+ entry,
1081
+ storageEntry
1082
+ });
1083
+ }
1084
+ };
1085
+ return {
1086
+ /**
1087
+ * Deprecated - will be removed in 5.35.0
1088
+ */
1089
+ onBeforeEntryCreate: onEntryBeforeCreate,
1090
+ onAfterEntryCreate: onEntryAfterCreate,
1091
+ onBeforeEntryCreateRevision: onEntryBeforeCreateRevision,
1092
+ onAfterEntryCreateRevision: onEntryRevisionAfterCreate,
1093
+ onBeforeEntryUpdate: onEntryBeforeUpdate,
1094
+ onAfterEntryUpdate: onEntryAfterUpdate,
1095
+ onBeforeEntryDelete: onEntryBeforeDelete,
1096
+ onAfterEntryDelete: onEntryAfterDelete,
1097
+ onBeforeEntryDeleteRevision: onEntryRevisionBeforeDelete,
1098
+ onAfterEntryDeleteRevision: onEntryRevisionAfterDelete,
1099
+ onBeforeEntryPublish: onEntryBeforePublish,
1100
+ onAfterEntryPublish: onEntryAfterPublish,
1101
+ onBeforeEntryUnpublish: onEntryBeforeUnpublish,
1102
+ onAfterEntryUnpublish: onEntryAfterUnpublish,
1103
+ onBeforeEntryGet: onEntryBeforeGet,
1104
+ onBeforeEntryList: onEntryBeforeList,
1105
+ /**
1106
+ * Released in 5.34.0
1107
+ *
1108
+ * Create
1109
+ */
1110
+ onEntryBeforeCreate,
1111
+ onEntryAfterCreate,
1112
+ onEntryCreateError,
1113
+ /**
1114
+ * Create revision
1115
+ */
1116
+ onEntryRevisionBeforeCreate: onEntryBeforeCreateRevision,
1117
+ onEntryRevisionAfterCreate,
1118
+ onEntryRevisionCreateError: onEntryCreateRevisionError,
1119
+ /**
1120
+ * Update
1121
+ */
1122
+ onEntryBeforeUpdate,
1123
+ onEntryAfterUpdate,
1124
+ onEntryUpdateError,
1125
+ /**
1126
+ * Delete whole entry
1127
+ */
1128
+ onEntryBeforeDelete,
1129
+ onEntryAfterDelete,
1130
+ onEntryDeleteError,
1131
+ /**
1132
+ * Delete entry revision
1133
+ */
1134
+ onEntryRevisionBeforeDelete,
1135
+ onEntryRevisionAfterDelete,
1136
+ onEntryRevisionDeleteError,
1137
+ /**
1138
+ * Publish
1139
+ */
1140
+ onEntryBeforePublish,
1141
+ onEntryAfterPublish,
1142
+ onEntryPublishError,
1143
+ /**
1144
+ * Republish
1145
+ */
1146
+ onEntryBeforeRepublish,
1147
+ onEntryAfterRepublish,
1148
+ onEntryRepublishError,
1149
+ /**
1150
+ * Unpublish
1151
+ */
1152
+ onEntryBeforeUnpublish,
1153
+ onEntryAfterUnpublish,
1154
+ onEntryUnpublishError,
1155
+ onEntryBeforeGet,
1156
+ onEntryBeforeList,
1157
+ /**
1158
+ * Get entries by exact revision IDs from the database.
1159
+ */
1160
+ getEntriesByIds,
1161
+ /**
1162
+ * Get a single entry by revision ID from the database.
1163
+ */
1164
+ async getEntryById(model, id) {
1165
+ return context.benchmark.measure("headlessCms.crud.entries.getEntryById", async () => {
1166
+ return getEntryById(model, id);
1167
+ });
1168
+ },
1169
+ /**
1170
+ * Get published revisions by entry IDs.
1171
+ */
1172
+ async getPublishedEntriesByIds(model, ids) {
1173
+ return context.benchmark.measure("headlessCms.crud.entries.getPublishedEntriesByIds", async () => {
1174
+ return getPublishedEntriesByIds(model, ids);
1175
+ });
1176
+ },
1177
+ /**
1178
+ * Get the latest revisions by entry IDs.
1179
+ */
1180
+ async getLatestEntriesByIds(model, ids) {
1181
+ return context.benchmark.measure("headlessCms.crud.entries.getLatestEntriesByIds", async () => {
1182
+ return getLatestEntriesByIds(model, ids);
1183
+ });
1184
+ },
1185
+ async getEntryRevisions(model, entryId) {
1186
+ return context.benchmark.measure("headlessCms.crud.entries.getEntryRevisions", async () => {
1187
+ return getEntryRevisions(model, entryId);
458
1188
  });
459
- if (items.length === 0) {
460
- throw new _handlerGraphql.NotFoundError(`Entry not found!`);
461
- }
462
- return items[0];
463
1189
  },
464
1190
  /**
465
- * @description Should not be used directly. Internal use only!
1191
+ * TODO determine if this method is required at all.
466
1192
  *
467
1193
  * @internal
468
1194
  */
469
- async listEntries(initialModel, params) {
470
- const permission = await checkEntryPermissions({
471
- rwd: "r"
472
- });
473
- await (0, _access.checkModelAccess)(context, initialModel);
474
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
475
- model: initialModel,
476
- plugins
1195
+ async getEntry(model, params) {
1196
+ return context.benchmark.measure("headlessCms.crud.entries.getEntry", async () => {
1197
+ return getEntry(model, params);
477
1198
  });
478
- const {
479
- where: initialWhere
480
- } = params;
481
- /**
482
- * We always assign tenant and locale because we do not allow one model to have content through multiple tenants.
483
- */
484
- const where = (0, _objectSpread2.default)({}, initialWhere);
485
- /**
486
- * Possibly only get records which are owned by current user.
487
- * Or if searching for the owner set that value - in the case that user can see other entries than their own.
488
- */
489
- const ownedBy = permission.own ? getIdentity().id : where.ownedBy;
490
- if (ownedBy !== undefined) {
491
- where.ownedBy = ownedBy;
492
- }
493
- /**
494
- * Where must contain either latest or published keys.
495
- * We cannot list entries without one of those
496
- */
497
- if (where.latest && where.published) {
498
- throw new _error.default("Cannot list entries that are both published and latest.", "LIST_ENTRIES_ERROR", {
499
- where
500
- });
501
- } else if (!where.latest && !where.published) {
502
- throw new _error.default("Cannot list entries if we do not have latest or published defined.", "LIST_ENTRIES_ERROR", {
503
- where
504
- });
505
- }
506
- const fields = getSearchableFields({
507
- model,
508
- plugins: context.plugins,
509
- fields: params.fields || []
1199
+ },
1200
+ /**
1201
+ * @description Should not be used directly. Internal use only!
1202
+ *
1203
+ * @internal
1204
+ */
1205
+ async listEntries(model, params) {
1206
+ return context.benchmark.measure("headlessCms.crud.entries.listEntries", async () => {
1207
+ return listEntries(model, params);
510
1208
  });
511
- try {
512
- await onEntryBeforeList.publish({
513
- where,
514
- model
515
- });
516
- const {
517
- hasMoreItems,
518
- totalCount,
519
- cursor,
520
- items
521
- } = await storageOperations.entries.list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
522
- where,
523
- fields
524
- }));
525
- const meta = {
526
- hasMoreItems,
527
- totalCount,
528
- /**
529
- * Cursor should be null if there are no more items to load.
530
- * Just make sure of that, disregarding what is returned from the storageOperations.entries.list method.
531
- */
532
- cursor: hasMoreItems ? cursor : null
533
- };
534
- return [items, meta];
535
- } catch (ex) {
536
- throw new _error.default("Error while fetching entries from storage.", "LIST_ENTRIES_ERROR", {
537
- params,
538
- error: {
539
- message: ex.message,
540
- code: ex.code,
541
- data: ex.data
542
- },
543
- model,
544
- fields
545
- });
546
- }
547
1209
  },
548
1210
  async listLatestEntries(model, params) {
549
1211
  const where = (params === null || params === void 0 ? void 0 : params.where) || {};
@@ -565,572 +1227,49 @@ const createContentEntryCrud = params => {
565
1227
  })
566
1228
  }));
567
1229
  },
568
- async createEntry(initialModel, inputData) {
569
- await checkEntryPermissions({
570
- rwd: "w"
571
- });
572
- await (0, _access.checkModelAccess)(context, initialModel);
573
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
574
- model: initialModel,
575
- plugins
1230
+ async createEntry(model, input) {
1231
+ return context.benchmark.measure("headlessCms.crud.entries.createEntry", async () => {
1232
+ return createEntry(model, input);
576
1233
  });
577
-
578
- /**
579
- * Make sure we only work with fields that are defined in the model.
580
- */
581
- const initialInput = mapAndCleanCreateInputData(model, inputData);
582
- await (0, _entryDataValidation.validateModelEntryData)({
583
- context,
584
- model,
585
- data: initialInput
586
- });
587
- const input = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
588
- context,
589
- model,
590
- input: initialInput,
591
- validateEntries: true
592
- });
593
- const identity = context.security.getIdentity();
594
- const locale = this.getLocale();
595
- const owner = {
596
- id: identity.id,
597
- displayName: identity.displayName,
598
- type: identity.type
599
- };
600
- const {
601
- id,
602
- entryId,
603
- version
604
- } = createEntryId(1);
605
- const entry = {
606
- webinyVersion: context.WEBINY_VERSION,
607
- tenant: getTenant().id,
608
- entryId,
609
- id,
610
- modelId: model.modelId,
611
- locale: locale.code,
612
- createdOn: new Date().toISOString(),
613
- savedOn: new Date().toISOString(),
614
- createdBy: owner,
615
- ownedBy: owner,
616
- version,
617
- locked: false,
618
- status: STATUS_DRAFT,
619
- values: input
620
- };
621
- let storageEntry = null;
622
- try {
623
- await onEntryBeforeCreate.publish({
624
- entry,
625
- input,
626
- model
627
- });
628
- storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
629
- const result = await storageOperations.entries.create(model, {
630
- entry,
631
- storageEntry
632
- });
633
- await onEntryAfterCreate.publish({
634
- entry,
635
- storageEntry: result,
636
- model,
637
- input
638
- });
639
- return result;
640
- } catch (ex) {
641
- await onEntryCreateError.publish({
642
- error: ex,
643
- entry,
644
- model,
645
- input
646
- });
647
- throw new _error.default(ex.message || "Could not create content entry.", ex.code || "CREATE_ENTRY_ERROR", ex.data || {
648
- error: ex,
649
- input,
650
- entry,
651
- storageEntry
652
- });
653
- }
654
1234
  },
655
- async createEntryRevisionFrom(initialModel, sourceId, inputData) {
656
- const permission = await checkEntryPermissions({
657
- rwd: "w"
658
- });
659
- await (0, _access.checkModelAccess)(context, initialModel);
660
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
661
- model: initialModel,
662
- plugins
663
- });
664
-
665
- /**
666
- * Make sure we only work with fields that are defined in the model.
667
- */
668
- const input = mapAndCleanUpdatedInputData(model, inputData);
669
-
670
- /**
671
- * Entries are identified by a common parent ID + Revision number.
672
- */
673
- const {
674
- id: uniqueId
675
- } = (0, _utils.parseIdentifier)(sourceId);
676
- const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
677
- id: sourceId
678
- });
679
- const latestStorageEntry = await storageOperations.entries.getLatestRevisionByEntryId(model, {
680
- id: uniqueId
1235
+ async createEntryRevisionFrom(model, sourceId, input) {
1236
+ return context.benchmark.measure("headlessCms.crud.entries.createEntryRevisionFrom", async () => {
1237
+ return createEntryRevisionFrom(model, sourceId, input);
681
1238
  });
682
- if (!originalStorageEntry) {
683
- throw new _handlerGraphql.NotFoundError(`Entry "${sourceId}" of model "${model.modelId}" was not found.`);
684
- }
685
-
686
- /**
687
- * We need to convert data from DB to its original form before using it further.
688
- */
689
- const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
690
- const initialValues = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry.values), input);
691
- await (0, _entryDataValidation.validateModelEntryData)({
692
- context,
693
- model,
694
- data: initialValues,
695
- entry: originalEntry
696
- });
697
- const values = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
698
- context,
699
- model,
700
- input: initialValues,
701
- validateEntries: false
702
- });
703
- (0, _ownership.checkOwnership)(context, permission, originalEntry);
704
- const identity = context.security.getIdentity();
705
- const latestId = latestStorageEntry ? latestStorageEntry.id : sourceId;
706
- const {
707
- id,
708
- version: nextVersion
709
- } = increaseEntryIdVersion(latestId);
710
- const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
711
- id,
712
- version: nextVersion,
713
- savedOn: new Date().toISOString(),
714
- createdOn: new Date().toISOString(),
715
- createdBy: {
716
- id: identity.id,
717
- displayName: identity.displayName,
718
- type: identity.type
719
- },
720
- locked: false,
721
- publishedOn: undefined,
722
- status: STATUS_DRAFT,
723
- values
724
- });
725
- let storageEntry = null;
726
- try {
727
- await onEntryBeforeCreateRevision.publish({
728
- input,
729
- entry,
730
- original: originalEntry,
731
- model
732
- });
733
- storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
734
- const result = await storageOperations.entries.createRevisionFrom(model, {
735
- entry,
736
- storageEntry
737
- });
738
- await onEntryRevisionAfterCreate.publish({
739
- input,
740
- entry,
741
- model,
742
- original: originalEntry,
743
- storageEntry: result
744
- });
745
- return result;
746
- } catch (ex) {
747
- await onEntryCreateRevisionError.publish({
748
- entry,
749
- model,
750
- input,
751
- error: ex
752
- });
753
- throw new _error.default(ex.message || "Could not create entry from existing one.", ex.code || "CREATE_FROM_REVISION_ERROR", {
754
- error: ex,
755
- entry,
756
- storageEntry,
757
- originalEntry,
758
- originalStorageEntry
759
- });
760
- }
761
1239
  },
762
- async updateEntry(initialModel, id, inputData, metaInput) {
763
- const permission = await checkEntryPermissions({
764
- rwd: "w"
765
- });
766
- await (0, _access.checkModelAccess)(context, initialModel);
767
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
768
- model: initialModel,
769
- plugins
770
- });
771
-
772
- /**
773
- * Make sure we only work with fields that are defined in the model.
774
- */
775
- const input = mapAndCleanUpdatedInputData(model, inputData);
776
-
777
- /**
778
- * The entry we are going to update.
779
- */
780
- const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
781
- id
782
- });
783
- if (!originalStorageEntry) {
784
- throw new _handlerGraphql.NotFoundError(`Entry "${id}" of model "${model.modelId}" was not found.`);
785
- }
786
- if (originalStorageEntry.locked) {
787
- throw new _error.default(`Cannot update entry because it's locked.`, "CONTENT_ENTRY_UPDATE_ERROR");
788
- }
789
- const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
790
- await (0, _entryDataValidation.validateModelEntryData)({
791
- context,
792
- model,
793
- data: input,
794
- entry: originalEntry
1240
+ async updateEntry(model, id, input, meta) {
1241
+ return context.benchmark.measure("headlessCms.crud.entries.updateEntry", async () => {
1242
+ return updateEntry(model, id, input, meta);
795
1243
  });
796
- (0, _ownership.checkOwnership)(context, permission, originalEntry);
797
- const initialValues = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry.values), input);
798
- const values = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
799
- context,
800
- model,
801
- input: initialValues,
802
- validateEntries: false
803
- });
804
- /**
805
- * If users wants to remove a key from meta values, they need to send meta key with the null value.
806
- */
807
- const meta = createEntryMeta(metaInput, originalEntry.meta);
808
- /**
809
- * We always send the full entry to the hooks and storage operations update.
810
- */
811
- const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
812
- savedOn: new Date().toISOString(),
813
- values,
814
- meta,
815
- status: transformEntryStatus(originalEntry.status)
816
- });
817
- let storageEntry = null;
818
- try {
819
- await onEntryBeforeUpdate.publish({
820
- entry,
821
- model,
822
- input,
823
- original: originalEntry
824
- });
825
- storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
826
- const result = await storageOperations.entries.update(model, {
827
- entry,
828
- storageEntry
829
- });
830
- await onEntryAfterUpdate.publish({
831
- entry,
832
- storageEntry: result,
833
- model,
834
- input,
835
- original: originalEntry
836
- });
837
- return result;
838
- } catch (ex) {
839
- await onEntryUpdateError.publish({
840
- entry,
841
- model,
842
- input,
843
- error: ex
844
- });
845
- throw new _error.default(ex.message || "Could not update existing entry.", ex.code || "UPDATE_ERROR", {
846
- error: ex,
847
- entry,
848
- storageEntry,
849
- originalEntry,
850
- input
851
- });
852
- }
853
1244
  },
854
- async republishEntry(initialModel, id) {
855
- await checkEntryPermissions({
856
- rwd: "w"
857
- });
858
- await (0, _access.checkModelAccess)(context, initialModel);
859
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
860
- model: initialModel,
861
- plugins
862
- });
863
- /**
864
- * Fetch the entry from the storage.
865
- */
866
- const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
867
- id
1245
+ /**
1246
+ * Method used internally. Not documented and should not be used in users systems.
1247
+ * @internal
1248
+ */
1249
+ async republishEntry(model, id) {
1250
+ return context.benchmark.measure("headlessCms.crud.entries.republishEntry", async () => {
1251
+ return republishEntry(model, id);
868
1252
  });
869
- if (!originalStorageEntry) {
870
- throw new _handlerGraphql.NotFoundError(`Entry "${id}" was not found!`);
871
- }
872
- const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
873
- /**
874
- * We can only process published entries.
875
- */
876
- if (originalEntry.status !== "published") {
877
- throw new _error.default("Entry with given ID is not published!", "NOT_PUBLISHED_ERROR", {
878
- id,
879
- original: originalEntry
880
- });
881
- }
882
- const values = await (0, _referenceFieldsMapping.referenceFieldsMapping)({
883
- context,
884
- model,
885
- input: originalEntry.values,
886
- validateEntries: false
887
- });
888
- const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
889
- savedOn: new Date().toISOString(),
890
- webinyVersion: context.WEBINY_VERSION,
891
- values
892
- });
893
- const storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
894
- /**
895
- * First we need to update existing entry.
896
- */
897
- try {
898
- await storageOperations.entries.update(model, {
899
- entry,
900
- storageEntry
901
- });
902
- } catch (ex) {
903
- throw new _error.default("Could not update existing entry with new data while re-publishing.", "REPUBLISH_UPDATE_ERROR", {
904
- entry
905
- });
906
- }
907
- /**
908
- * Then we move onto publishing it again.
909
- */
910
- try {
911
- return await storageOperations.entries.publish(model, {
912
- entry,
913
- storageEntry
914
- });
915
- } catch (ex) {
916
- throw new _error.default("Could not publish existing entry while re-publishing.", "REPUBLISH_PUBLISH_ERROR", {
917
- entry
918
- });
919
- }
920
1253
  },
921
- async deleteEntryRevision(initialModel, revisionId) {
922
- const permission = await checkEntryPermissions({
923
- rwd: "d"
924
- });
925
- await (0, _access.checkModelAccess)(context, initialModel);
926
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
927
- model: initialModel,
928
- plugins
929
- });
930
- const {
931
- id: entryId,
932
- version
933
- } = (0, _utils.parseIdentifier)(revisionId);
934
- const storageEntryToDelete = await storageOperations.entries.getRevisionById(model, {
935
- id: revisionId
936
- });
937
- const latestStorageEntry = await storageOperations.entries.getLatestRevisionByEntryId(model, {
938
- id: entryId
1254
+ async deleteEntryRevision(model, id) {
1255
+ return context.benchmark.measure("headlessCms.crud.entries.deleteEntryRevision", async () => {
1256
+ return deleteEntryRevision(model, id);
939
1257
  });
940
- const previousStorageEntry = await storageOperations.entries.getPreviousRevision(model, {
941
- entryId,
942
- version: version
943
- });
944
- if (!storageEntryToDelete) {
945
- throw new _handlerGraphql.NotFoundError(`Entry "${revisionId}" was not found!`);
946
- }
947
- (0, _ownership.checkOwnership)(context, permission, storageEntryToDelete);
948
- const latestEntryRevisionId = latestStorageEntry ? latestStorageEntry.id : null;
949
- const entryToDelete = await (0, _entryStorage.entryFromStorageTransform)(context, model, storageEntryToDelete);
950
- /**
951
- * If targeted record is the latest entry record and there is no previous one, we need to run full delete with hooks.
952
- * At this point deleteRevision hooks are not fired.
953
- */
954
- if (entryToDelete.id === latestEntryRevisionId && !previousStorageEntry) {
955
- return await deleteEntry({
956
- model,
957
- entry: entryToDelete
958
- });
959
- }
960
- /**
961
- * If targeted record is latest entry revision, set the previous one as the new latest
962
- */
963
- let entryToSetAsLatest = null;
964
- let storageEntryToSetAsLatest = null;
965
- if (entryToDelete.id === latestEntryRevisionId && previousStorageEntry) {
966
- entryToSetAsLatest = await (0, _entryStorage.entryFromStorageTransform)(context, model, previousStorageEntry);
967
- storageEntryToSetAsLatest = previousStorageEntry;
968
- }
969
- try {
970
- await onEntryRevisionBeforeDelete.publish({
971
- entry: entryToDelete,
972
- model
973
- });
974
- await storageOperations.entries.deleteRevision(model, {
975
- entry: entryToDelete,
976
- storageEntry: storageEntryToDelete,
977
- latestEntry: entryToSetAsLatest,
978
- latestStorageEntry: storageEntryToSetAsLatest
979
- });
980
- await onEntryRevisionAfterDelete.publish({
981
- entry: entryToDelete,
982
- model
983
- });
984
- } catch (ex) {
985
- await onEntryRevisionDeleteError.publish({
986
- entry: entryToDelete,
987
- model,
988
- error: ex
989
- });
990
- throw new _error.default(ex.message, ex.code || "DELETE_REVISION_ERROR", {
991
- error: ex,
992
- entry: entryToDelete,
993
- storageEntry: storageEntryToDelete,
994
- latestEntry: entryToSetAsLatest,
995
- latestStorageEntry: storageEntryToSetAsLatest
996
- });
997
- }
998
1258
  },
999
- async deleteEntry(initialModel, entryId) {
1000
- const permission = await checkEntryPermissions({
1001
- rwd: "d"
1002
- });
1003
- await (0, _access.checkModelAccess)(context, initialModel);
1004
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
1005
- model: initialModel,
1006
- plugins
1007
- });
1008
- const storageEntry = await storageOperations.entries.getLatestRevisionByEntryId(model, {
1009
- id: entryId
1010
- });
1011
- if (!storageEntry) {
1012
- throw new _handlerGraphql.NotFoundError(`Entry "${entryId}" was not found!`);
1013
- }
1014
- (0, _ownership.checkOwnership)(context, permission, storageEntry);
1015
- const entry = await (0, _entryStorage.entryFromStorageTransform)(context, model, storageEntry);
1016
- return await deleteEntry({
1017
- model,
1018
- entry
1259
+ async deleteEntry(model, entryId) {
1260
+ return context.benchmark.measure("headlessCms.crud.entries.deleteEntry", async () => {
1261
+ return deleteEntry(model, entryId);
1019
1262
  });
1020
1263
  },
1021
- async publishEntry(initialModel, id) {
1022
- const permission = await checkEntryPermissions({
1023
- pw: "p"
1024
- });
1025
- await (0, _access.checkModelAccess)(context, initialModel);
1026
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
1027
- model: initialModel,
1028
- plugins
1029
- });
1030
- const originalStorageEntry = await storageOperations.entries.getRevisionById(model, {
1031
- id
1264
+ async publishEntry(model, id) {
1265
+ return context.benchmark.measure("headlessCms.crud.entries.publishEntry", async () => {
1266
+ return publishEntry(model, id);
1032
1267
  });
1033
- if (!originalStorageEntry) {
1034
- throw new _handlerGraphql.NotFoundError(`Entry "${id}" in the model "${model.modelId}" was not found.`);
1035
- }
1036
- (0, _ownership.checkOwnership)(context, permission, originalStorageEntry);
1037
- const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
1038
- const currentDate = new Date().toISOString();
1039
- const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
1040
- status: STATUS_PUBLISHED,
1041
- locked: true,
1042
- savedOn: currentDate,
1043
- publishedOn: currentDate
1044
- });
1045
- let storageEntry = null;
1046
- try {
1047
- await onEntryBeforePublish.publish({
1048
- entry,
1049
- model
1050
- });
1051
- storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
1052
- const result = await storageOperations.entries.publish(model, {
1053
- entry,
1054
- storageEntry
1055
- });
1056
- await onEntryAfterPublish.publish({
1057
- entry,
1058
- storageEntry: result,
1059
- model
1060
- });
1061
- return result;
1062
- } catch (ex) {
1063
- await onEntryPublishError.publish({
1064
- entry,
1065
- model,
1066
- error: ex
1067
- });
1068
- throw new _error.default(ex.message || "Could not publish entry.", ex.code || "PUBLISH_ERROR", {
1069
- error: ex,
1070
- entry,
1071
- storageEntry,
1072
- originalEntry,
1073
- originalStorageEntry
1074
- });
1075
- }
1076
1268
  },
1077
- async unpublishEntry(initialModel, id) {
1078
- const permission = await checkEntryPermissions({
1079
- pw: "u"
1080
- });
1081
- const model = (0, _valueKeyStorageConverter.attachCmsModelFieldConverters)({
1082
- model: initialModel,
1083
- plugins
1084
- });
1085
- const {
1086
- id: entryId
1087
- } = (0, _utils.parseIdentifier)(id);
1088
- const originalStorageEntry = await storageOperations.entries.getPublishedRevisionByEntryId(model, {
1089
- id: entryId
1090
- });
1091
- if (!originalStorageEntry) {
1092
- throw new _handlerGraphql.NotFoundError(`Entry "${id}" of model "${model.modelId}" was not found.`);
1093
- }
1094
- if (originalStorageEntry.id !== id) {
1095
- throw new _error.default(`Entry is not published.`, "UNPUBLISH_ERROR", {
1096
- entry: originalStorageEntry
1097
- });
1098
- }
1099
- (0, _ownership.checkOwnership)(context, permission, originalStorageEntry);
1100
- const originalEntry = await (0, _entryStorage.entryFromStorageTransform)(context, model, originalStorageEntry);
1101
- const entry = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originalEntry), {}, {
1102
- status: STATUS_UNPUBLISHED
1269
+ async unpublishEntry(model, id) {
1270
+ return context.benchmark.measure("headlessCms.crud.entries.unpublishEntry", async () => {
1271
+ return unpublishEntry(model, id);
1103
1272
  });
1104
- let storageEntry = null;
1105
- try {
1106
- await onEntryBeforeUnpublish.publish({
1107
- entry,
1108
- model
1109
- });
1110
- storageEntry = await (0, _entryStorage.entryToStorageTransform)(context, model, entry);
1111
- const result = await storageOperations.entries.unpublish(model, {
1112
- entry,
1113
- storageEntry
1114
- });
1115
- await onEntryAfterUnpublish.publish({
1116
- entry,
1117
- storageEntry: result,
1118
- model
1119
- });
1120
- return result;
1121
- } catch (ex) {
1122
- await onEntryUnpublishError.publish({
1123
- entry,
1124
- model,
1125
- error: ex
1126
- });
1127
- throw new _error.default(ex.message || "Could not unpublish entry.", ex.code || "UNPUBLISH_ERROR", {
1128
- originalEntry,
1129
- originalStorageEntry,
1130
- entry,
1131
- storageEntry
1132
- });
1133
- }
1134
1273
  }
1135
1274
  };
1136
1275
  };