@webiny/api-headless-cms 5.33.5 → 5.34.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.
- package/context.js +3 -7
- package/context.js.map +1 -1
- package/crud/contentEntry/afterDelete.d.ts +2 -2
- package/crud/contentEntry/afterDelete.js +2 -2
- package/crud/contentEntry/afterDelete.js.map +1 -1
- package/crud/contentEntry/beforeCreate.d.ts +2 -2
- package/crud/contentEntry/beforeCreate.js +2 -2
- package/crud/contentEntry/beforeCreate.js.map +1 -1
- package/crud/contentEntry/beforeUpdate.d.ts +2 -2
- package/crud/contentEntry/beforeUpdate.js +2 -2
- package/crud/contentEntry/beforeUpdate.js.map +1 -1
- package/crud/contentEntry/entryDataValidation.js +3 -7
- package/crud/contentEntry/entryDataValidation.js.map +1 -1
- package/crud/contentEntry/markLockedFields.js +10 -11
- package/crud/contentEntry/markLockedFields.js.map +1 -1
- package/crud/contentEntry/referenceFieldsMapping.js +11 -13
- package/crud/contentEntry/referenceFieldsMapping.js.map +1 -1
- package/crud/contentEntry/searchableFields.d.ts +9 -0
- package/crud/contentEntry/searchableFields.js +83 -0
- package/crud/contentEntry/searchableFields.js.map +1 -0
- package/crud/contentEntry.crud.d.ts +0 -2
- package/crud/contentEntry.crud.js +249 -269
- package/crud/contentEntry.crud.js.map +1 -1
- package/crud/contentModel/afterCreate.d.ts +3 -3
- package/crud/contentModel/afterCreate.js +5 -5
- package/crud/contentModel/afterCreate.js.map +1 -1
- package/crud/contentModel/afterCreateFrom.d.ts +3 -3
- package/crud/contentModel/afterCreateFrom.js +5 -5
- package/crud/contentModel/afterCreateFrom.js.map +1 -1
- package/crud/contentModel/afterDelete.d.ts +3 -3
- package/crud/contentModel/afterDelete.js +5 -5
- package/crud/contentModel/afterDelete.js.map +1 -1
- package/crud/contentModel/afterUpdate.d.ts +3 -3
- package/crud/contentModel/afterUpdate.js +5 -5
- package/crud/contentModel/afterUpdate.js.map +1 -1
- package/crud/contentModel/beforeCreate.d.ts +4 -4
- package/crud/contentModel/beforeCreate.js +10 -10
- package/crud/contentModel/beforeCreate.js.map +1 -1
- package/crud/contentModel/beforeDelete.d.ts +3 -3
- package/crud/contentModel/beforeDelete.js +6 -6
- package/crud/contentModel/beforeDelete.js.map +1 -1
- package/crud/contentModel/beforeUpdate.d.ts +3 -3
- package/crud/contentModel/beforeUpdate.js +5 -5
- package/crud/contentModel/beforeUpdate.js.map +1 -1
- package/crud/contentModel/createFieldStorageId.js +5 -1
- package/crud/contentModel/createFieldStorageId.js.map +1 -1
- package/crud/contentModel/models.js +12 -0
- package/crud/contentModel/models.js.map +1 -1
- package/crud/contentModel/validateModel.js +1 -1
- package/crud/contentModel/validateModelFields.js +42 -34
- package/crud/contentModel/validateModelFields.js.map +1 -1
- package/crud/contentModel.crud.js +222 -169
- package/crud/contentModel.crud.js.map +1 -1
- package/crud/contentModelGroup/beforeCreate.d.ts +2 -2
- package/crud/contentModelGroup/beforeCreate.js +3 -3
- package/crud/contentModelGroup/beforeCreate.js.map +1 -1
- package/crud/contentModelGroup/beforeDelete.d.ts +2 -2
- package/crud/contentModelGroup/beforeDelete.js +3 -3
- package/crud/contentModelGroup/beforeDelete.js.map +1 -1
- package/crud/contentModelGroup/beforeUpdate.d.ts +2 -2
- package/crud/contentModelGroup/beforeUpdate.js +2 -2
- package/crud/contentModelGroup/beforeUpdate.js.map +1 -1
- package/crud/contentModelGroup.crud.js +87 -37
- package/crud/contentModelGroup.crud.js.map +1 -1
- package/crud/index.js +3 -7
- package/crud/index.js.map +1 -1
- package/crud/settings.crud.js +3 -8
- package/crud/settings.crud.js.map +1 -1
- package/crud/system.crud.js +63 -51
- package/crud/system.crud.js.map +1 -1
- package/fieldConverters/CmsModelDefaultFieldConverterPlugin.js +1 -1
- package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.d.ts +11 -0
- package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js +239 -0
- package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js.map +1 -0
- package/fieldConverters/CmsModelObjectFieldConverterPlugin.js +17 -19
- package/fieldConverters/CmsModelObjectFieldConverterPlugin.js.map +1 -1
- package/fieldConverters/index.d.ts +2 -1
- package/fieldConverters/index.js +3 -1
- package/fieldConverters/index.js.map +1 -1
- package/graphql/graphQLHandlerFactory.js +21 -17
- package/graphql/graphQLHandlerFactory.js.map +1 -1
- package/graphql/index.d.ts +1 -1
- package/graphql/schema/baseContentSchema.js +4 -8
- package/graphql/schema/baseContentSchema.js.map +1 -1
- package/graphql/schema/contentEntries.js +1 -1
- package/graphql/schema/contentModels.js +18 -1
- package/graphql/schema/contentModels.js.map +1 -1
- package/graphql/schema/createFieldResolvers.d.ts +1 -1
- package/graphql/schema/createFieldResolvers.js +12 -14
- package/graphql/schema/createFieldResolvers.js.map +1 -1
- package/graphql/schema/createManageResolvers.js +4 -18
- package/graphql/schema/createManageResolvers.js.map +1 -1
- package/graphql/schema/createManageSDL.js +7 -10
- package/graphql/schema/createManageSDL.js.map +1 -1
- package/graphql/schema/createPreviewResolvers.js +3 -7
- package/graphql/schema/createPreviewResolvers.js.map +1 -1
- package/graphql/schema/createReadResolvers.js +3 -7
- package/graphql/schema/createReadResolvers.js.map +1 -1
- package/graphql/schema/createReadSDL.js +5 -3
- package/graphql/schema/createReadSDL.js.map +1 -1
- package/graphql/schema/resolvers/manage/resolveGet.js +5 -9
- package/graphql/schema/resolvers/manage/resolveGet.js.map +1 -1
- package/graphql/schema/resolvers/preview/resolveGet.js +3 -7
- package/graphql/schema/resolvers/preview/resolveGet.js.map +1 -1
- package/graphql/schema/resolvers/read/resolveGet.js +3 -7
- package/graphql/schema/resolvers/read/resolveGet.js.map +1 -1
- package/graphqlFields/dynamicZone/dynamicZoneField.d.ts +2 -0
- package/graphqlFields/dynamicZone/dynamicZoneField.js +234 -0
- package/graphqlFields/dynamicZone/dynamicZoneField.js.map +1 -0
- package/graphqlFields/dynamicZone/dynamicZoneStorage.d.ts +3 -0
- package/graphqlFields/dynamicZone/dynamicZoneStorage.js +79 -0
- package/graphqlFields/dynamicZone/dynamicZoneStorage.js.map +1 -0
- package/graphqlFields/dynamicZone/index.d.ts +2 -0
- package/graphqlFields/dynamicZone/index.js +21 -0
- package/graphqlFields/dynamicZone/index.js.map +1 -0
- package/graphqlFields/index.d.ts +1 -1
- package/graphqlFields/index.js +3 -1
- package/graphqlFields/index.js.map +1 -1
- package/graphqlFields/number.js +4 -0
- package/graphqlFields/number.js.map +1 -1
- package/graphqlFields/object.js +123 -73
- package/graphqlFields/object.js.map +1 -1
- package/graphqlFields/ref.js +23 -23
- package/graphqlFields/ref.js.map +1 -1
- package/index.d.ts +2 -2
- package/index.js +3 -1
- package/index.js.map +1 -1
- package/modelManager/DefaultCmsModelManager.d.ts +7 -7
- package/modelManager/DefaultCmsModelManager.js +1 -1
- package/package.json +27 -28
- package/parameters/header.js +1 -1
- package/parameters/manual.js +7 -8
- package/parameters/manual.js.map +1 -1
- package/parameters/path.js +1 -1
- package/plugins/CmsGroupPlugin.js +1 -1
- package/plugins/CmsModelFieldConverterPlugin.d.ts +2 -2
- package/plugins/CmsModelFieldConverterPlugin.js +1 -1
- package/plugins/CmsModelFieldConverterPlugin.js.map +1 -1
- package/plugins/CmsModelPlugin.d.ts +6 -2
- package/plugins/CmsModelPlugin.js +18 -13
- package/plugins/CmsModelPlugin.js.map +1 -1
- package/plugins/CmsParametersPlugin.js +1 -1
- package/plugins/StorageTransformPlugin.d.ts +11 -11
- package/plugins/StorageTransformPlugin.js +1 -1
- package/plugins/StorageTransformPlugin.js.map +1 -1
- package/storage/object.js +7 -8
- package/storage/object.js.map +1 -1
- package/types.d.ts +436 -164
- package/types.js +50 -9
- package/types.js.map +1 -1
- package/upgrades/5.33.0/index.js +5 -9
- package/upgrades/5.33.0/index.js.map +1 -1
- package/utils/converters/Converter.js +1 -1
- package/utils/converters/ConverterCollection.js +10 -8
- package/utils/converters/ConverterCollection.js.map +1 -1
- package/utils/converters/valueKeyStorageConverter.js +3 -7
- package/utils/converters/valueKeyStorageConverter.js.map +1 -1
- package/utils/createTypeFromFields.d.ts +15 -0
- package/utils/createTypeFromFields.js +75 -0
- package/utils/createTypeFromFields.js.map +1 -0
- package/utils/createTypeName.js +4 -2
- package/utils/createTypeName.js.map +1 -1
- package/utils/entryStorage.js +17 -18
- package/utils/entryStorage.js.map +1 -1
- package/utils/filterModelFields.js +5 -9
- package/utils/filterModelFields.js.map +1 -1
- package/utils/getBaseFieldType.d.ts +4 -0
- package/utils/getBaseFieldType.js +12 -0
- package/utils/getBaseFieldType.js.map +1 -0
- package/utils/permissions.js +1 -5
- package/utils/permissions.js.map +1 -1
- package/utils/pluralizedTypeName.js +1 -1
- package/utils/renderFields.js +3 -1
- package/utils/renderFields.js.map +1 -1
- package/utils/renderGetFilterFields.js +8 -3
- package/utils/renderGetFilterFields.js.map +1 -1
- package/utils/renderInputFields.js +3 -1
- package/utils/renderInputFields.js.map +1 -1
- package/utils/renderListFilterFields.js +12 -2
- package/utils/renderListFilterFields.js.map +1 -1
- package/utils/renderSortEnum.js +3 -1
- package/utils/renderSortEnum.js.map +1 -1
- package/utils/toSlug.js +1 -1
- package/validators/dynamicZone.d.ts +2 -0
- package/validators/dynamicZone.js +24 -0
- package/validators/dynamicZone.js.map +1 -0
- package/validators/index.js +3 -1
- package/validators/index.js.map +1 -1
- package/validators/unique.js +1 -1
- package/graphql/schema/resolvers/manage/resolveRequestChanges.d.ts +0 -7
- package/graphql/schema/resolvers/manage/resolveRequestChanges.js +0 -21
- package/graphql/schema/resolvers/manage/resolveRequestChanges.js.map +0 -1
- package/graphql/schema/resolvers/manage/resolveRequestReview.d.ts +0 -7
- package/graphql/schema/resolvers/manage/resolveRequestReview.js +0 -21
- package/graphql/schema/resolvers/manage/resolveRequestReview.js.map +0 -1
package/context.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
4
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.createContextPlugin = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
11
|
|
|
12
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
13
|
|
|
@@ -15,10 +15,6 @@ var _api = require("@webiny/api");
|
|
|
15
15
|
|
|
16
16
|
var _CmsParametersPlugin = require("./plugins/CmsParametersPlugin");
|
|
17
17
|
|
|
18
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
19
|
-
|
|
20
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
21
|
-
|
|
22
18
|
const getParameters = async context => {
|
|
23
19
|
const plugins = context.plugins.byType(_CmsParametersPlugin.CmsParametersPlugin.type);
|
|
24
20
|
|
|
@@ -50,7 +46,7 @@ const createContextPlugin = () => {
|
|
|
50
46
|
return systemLocale;
|
|
51
47
|
};
|
|
52
48
|
|
|
53
|
-
context.cms =
|
|
49
|
+
context.cms = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, context.cms || {}), {}, {
|
|
54
50
|
type,
|
|
55
51
|
locale,
|
|
56
52
|
getLocale,
|
package/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getParameters","context","plugins","byType","CmsParametersPlugin","type","plugin","result","WebinyError","createContextPlugin","ContextPlugin","locale","getLocale","systemLocale","i18n","cms","READ","PREVIEW","MANAGE"],"sources":["context.ts"],"sourcesContent":["import { CmsContext } from \"~/types\";\nimport WebinyError from \"@webiny/error\";\nimport { ContextPlugin } from \"@webiny/api\";\nimport { CmsParametersPlugin, CmsParametersPluginResponse } from \"./plugins/CmsParametersPlugin\";\n\nconst getParameters = async (context: CmsContext): Promise<CmsParametersPluginResponse> => {\n const plugins = context.plugins.byType<CmsParametersPlugin>(CmsParametersPlugin.type);\n\n for (const plugin of plugins) {\n const result = await plugin.getParameters(context);\n if (result !== null) {\n return result;\n }\n }\n throw new WebinyError(\n \"Could not determine locale and/or type of the CMS.\",\n \"CMS_LOCALE_AND_TYPE_ERROR\"\n );\n};\n\nexport const createContextPlugin = () => {\n return new ContextPlugin<CmsContext>(async context => {\n const { type, locale } = await getParameters(context);\n\n const getLocale = () => {\n const systemLocale = context.i18n.getLocale(locale);\n if (!systemLocale) {\n throw new WebinyError(`There is no locale \"${locale}\" in the system.`);\n }\n return systemLocale;\n };\n\n context.cms = {\n ...(context.cms || {}),\n type,\n locale,\n getLocale,\n READ: type === \"read\",\n PREVIEW: type === \"preview\",\n MANAGE: type === \"manage\"\n };\n });\n};\n"],"mappings":";;;;;;;;;;;AACA;;AACA;;AACA
|
|
1
|
+
{"version":3,"names":["getParameters","context","plugins","byType","CmsParametersPlugin","type","plugin","result","WebinyError","createContextPlugin","ContextPlugin","locale","getLocale","systemLocale","i18n","cms","READ","PREVIEW","MANAGE"],"sources":["context.ts"],"sourcesContent":["import { CmsContext } from \"~/types\";\nimport WebinyError from \"@webiny/error\";\nimport { ContextPlugin } from \"@webiny/api\";\nimport { CmsParametersPlugin, CmsParametersPluginResponse } from \"./plugins/CmsParametersPlugin\";\n\nconst getParameters = async (context: CmsContext): Promise<CmsParametersPluginResponse> => {\n const plugins = context.plugins.byType<CmsParametersPlugin>(CmsParametersPlugin.type);\n\n for (const plugin of plugins) {\n const result = await plugin.getParameters(context);\n if (result !== null) {\n return result;\n }\n }\n throw new WebinyError(\n \"Could not determine locale and/or type of the CMS.\",\n \"CMS_LOCALE_AND_TYPE_ERROR\"\n );\n};\n\nexport const createContextPlugin = () => {\n return new ContextPlugin<CmsContext>(async context => {\n const { type, locale } = await getParameters(context);\n\n const getLocale = () => {\n const systemLocale = context.i18n.getLocale(locale);\n if (!systemLocale) {\n throw new WebinyError(`There is no locale \"${locale}\" in the system.`);\n }\n return systemLocale;\n };\n\n context.cms = {\n ...(context.cms || {}),\n type,\n locale,\n getLocale,\n READ: type === \"read\",\n PREVIEW: type === \"preview\",\n MANAGE: type === \"manage\"\n };\n });\n};\n"],"mappings":";;;;;;;;;;;AACA;;AACA;;AACA;;AAEA,MAAMA,aAAa,GAAG,MAAOC,OAAP,IAAqE;EACvF,MAAMC,OAAO,GAAGD,OAAO,CAACC,OAAR,CAAgBC,MAAhB,CAA4CC,wCAAA,CAAoBC,IAAhE,CAAhB;;EAEA,KAAK,MAAMC,MAAX,IAAqBJ,OAArB,EAA8B;IAC1B,MAAMK,MAAM,GAAG,MAAMD,MAAM,CAACN,aAAP,CAAqBC,OAArB,CAArB;;IACA,IAAIM,MAAM,KAAK,IAAf,EAAqB;MACjB,OAAOA,MAAP;IACH;EACJ;;EACD,MAAM,IAAIC,cAAJ,CACF,oDADE,EAEF,2BAFE,CAAN;AAIH,CAbD;;AAeO,MAAMC,mBAAmB,GAAG,MAAM;EACrC,OAAO,IAAIC,kBAAJ,CAA8B,MAAMT,OAAN,IAAiB;IAClD,MAAM;MAAEI,IAAF;MAAQM;IAAR,IAAmB,MAAMX,aAAa,CAACC,OAAD,CAA5C;;IAEA,MAAMW,SAAS,GAAG,MAAM;MACpB,MAAMC,YAAY,GAAGZ,OAAO,CAACa,IAAR,CAAaF,SAAb,CAAuBD,MAAvB,CAArB;;MACA,IAAI,CAACE,YAAL,EAAmB;QACf,MAAM,IAAIL,cAAJ,CAAiB,uBAAsBG,MAAO,kBAA9C,CAAN;MACH;;MACD,OAAOE,YAAP;IACH,CAND;;IAQAZ,OAAO,CAACc,GAAR,+DACQd,OAAO,CAACc,GAAR,IAAe,EADvB;MAEIV,IAFJ;MAGIM,MAHJ;MAIIC,SAJJ;MAKII,IAAI,EAAEX,IAAI,KAAK,MALnB;MAMIY,OAAO,EAAEZ,IAAI,KAAK,SANtB;MAOIa,MAAM,EAAEb,IAAI,KAAK;IAPrB;EASH,CApBM,CAAP;AAqBH,CAtBM"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Topic } from "@webiny/pubsub/types";
|
|
2
|
-
import {
|
|
2
|
+
import { OnEntryAfterDeleteTopicParams, CmsContext } from "../../types";
|
|
3
3
|
interface AssignAfterEntryDeleteParams {
|
|
4
4
|
context: CmsContext;
|
|
5
|
-
|
|
5
|
+
onEntryAfterDelete: Topic<OnEntryAfterDeleteTopicParams>;
|
|
6
6
|
}
|
|
7
7
|
export declare const assignAfterEntryDelete: (params: AssignAfterEntryDeleteParams) => void;
|
|
8
8
|
export {};
|
|
@@ -10,9 +10,9 @@ var _markLockedFields = require("./markLockedFields");
|
|
|
10
10
|
const assignAfterEntryDelete = params => {
|
|
11
11
|
const {
|
|
12
12
|
context,
|
|
13
|
-
|
|
13
|
+
onEntryAfterDelete
|
|
14
14
|
} = params;
|
|
15
|
-
|
|
15
|
+
onEntryAfterDelete.subscribe(async params => {
|
|
16
16
|
const {
|
|
17
17
|
entry,
|
|
18
18
|
model
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["assignAfterEntryDelete","params","context","
|
|
1
|
+
{"version":3,"names":["assignAfterEntryDelete","params","context","onEntryAfterDelete","subscribe","entry","model","items","cms","storageOperations","entries","list","where","entryId_not","entryId","latest","limit","length","markUnlockedFields"],"sources":["afterDelete.ts"],"sourcesContent":["import { Topic } from \"@webiny/pubsub/types\";\nimport { OnEntryAfterDeleteTopicParams, CmsContext } from \"~/types\";\nimport { markUnlockedFields } from \"./markLockedFields\";\n\ninterface AssignAfterEntryDeleteParams {\n context: CmsContext;\n onEntryAfterDelete: Topic<OnEntryAfterDeleteTopicParams>;\n}\nexport const assignAfterEntryDelete = (params: AssignAfterEntryDeleteParams) => {\n const { context, onEntryAfterDelete } = params;\n\n onEntryAfterDelete.subscribe(async params => {\n const { entry, model } = params;\n\n const { items } = await context.cms.storageOperations.entries.list(model, {\n where: {\n entryId_not: entry.entryId,\n latest: true\n },\n limit: 1\n });\n if (items.length > 0) {\n return;\n }\n await markUnlockedFields({\n context,\n model\n });\n });\n};\n"],"mappings":";;;;;;;AAEA;;AAMO,MAAMA,sBAAsB,GAAIC,MAAD,IAA0C;EAC5E,MAAM;IAAEC,OAAF;IAAWC;EAAX,IAAkCF,MAAxC;EAEAE,kBAAkB,CAACC,SAAnB,CAA6B,MAAMH,MAAN,IAAgB;IACzC,MAAM;MAAEI,KAAF;MAASC;IAAT,IAAmBL,MAAzB;IAEA,MAAM;MAAEM;IAAF,IAAY,MAAML,OAAO,CAACM,GAAR,CAAYC,iBAAZ,CAA8BC,OAA9B,CAAsCC,IAAtC,CAA2CL,KAA3C,EAAkD;MACtEM,KAAK,EAAE;QACHC,WAAW,EAAER,KAAK,CAACS,OADhB;QAEHC,MAAM,EAAE;MAFL,CAD+D;MAKtEC,KAAK,EAAE;IAL+D,CAAlD,CAAxB;;IAOA,IAAIT,KAAK,CAACU,MAAN,GAAe,CAAnB,EAAsB;MAClB;IACH;;IACD,MAAM,IAAAC,oCAAA,EAAmB;MACrBhB,OADqB;MAErBI;IAFqB,CAAnB,CAAN;EAIH,CAjBD;AAkBH,CArBM"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Topic } from "@webiny/pubsub/types";
|
|
2
|
-
import {
|
|
2
|
+
import { OnEntryBeforeCreateTopicParams, CmsContext } from "../../types";
|
|
3
3
|
interface AssignBeforeEntryCreateParams {
|
|
4
4
|
context: CmsContext;
|
|
5
|
-
|
|
5
|
+
onEntryBeforeCreate: Topic<OnEntryBeforeCreateTopicParams>;
|
|
6
6
|
}
|
|
7
7
|
export declare const assignBeforeEntryCreate: (params: AssignBeforeEntryCreateParams) => void;
|
|
8
8
|
export {};
|
|
@@ -10,9 +10,9 @@ var _markLockedFields = require("./markLockedFields");
|
|
|
10
10
|
const assignBeforeEntryCreate = params => {
|
|
11
11
|
const {
|
|
12
12
|
context,
|
|
13
|
-
|
|
13
|
+
onEntryBeforeCreate
|
|
14
14
|
} = params;
|
|
15
|
-
|
|
15
|
+
onEntryBeforeCreate.subscribe(async params => {
|
|
16
16
|
const {
|
|
17
17
|
entry,
|
|
18
18
|
model
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["assignBeforeEntryCreate","params","context","
|
|
1
|
+
{"version":3,"names":["assignBeforeEntryCreate","params","context","onEntryBeforeCreate","subscribe","entry","model","markLockedFields"],"sources":["beforeCreate.ts"],"sourcesContent":["import { Topic } from \"@webiny/pubsub/types\";\nimport { OnEntryBeforeCreateTopicParams, CmsContext } from \"~/types\";\nimport { markLockedFields } from \"./markLockedFields\";\n\ninterface AssignBeforeEntryCreateParams {\n context: CmsContext;\n onEntryBeforeCreate: Topic<OnEntryBeforeCreateTopicParams>;\n}\nexport const assignBeforeEntryCreate = (params: AssignBeforeEntryCreateParams) => {\n const { context, onEntryBeforeCreate } = params;\n\n onEntryBeforeCreate.subscribe(async params => {\n const { entry, model } = params;\n\n await markLockedFields({\n model,\n entry,\n context\n });\n });\n};\n"],"mappings":";;;;;;;AAEA;;AAMO,MAAMA,uBAAuB,GAAIC,MAAD,IAA2C;EAC9E,MAAM;IAAEC,OAAF;IAAWC;EAAX,IAAmCF,MAAzC;EAEAE,mBAAmB,CAACC,SAApB,CAA8B,MAAMH,MAAN,IAAgB;IAC1C,MAAM;MAAEI,KAAF;MAASC;IAAT,IAAmBL,MAAzB;IAEA,MAAM,IAAAM,kCAAA,EAAiB;MACnBD,KADmB;MAEnBD,KAFmB;MAGnBH;IAHmB,CAAjB,CAAN;EAKH,CARD;AASH,CAZM"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Topic } from "@webiny/pubsub/types";
|
|
2
|
-
import {
|
|
2
|
+
import { OnEntryBeforeUpdateTopicParams, CmsContext } from "../../types";
|
|
3
3
|
interface AssignBeforeEntryUpdateParams {
|
|
4
4
|
context: CmsContext;
|
|
5
|
-
|
|
5
|
+
onEntryBeforeUpdate: Topic<OnEntryBeforeUpdateTopicParams>;
|
|
6
6
|
}
|
|
7
7
|
export declare const assignBeforeEntryUpdate: (params: AssignBeforeEntryUpdateParams) => void;
|
|
8
8
|
export {};
|
|
@@ -10,9 +10,9 @@ var _markLockedFields = require("./markLockedFields");
|
|
|
10
10
|
const assignBeforeEntryUpdate = params => {
|
|
11
11
|
const {
|
|
12
12
|
context,
|
|
13
|
-
|
|
13
|
+
onEntryBeforeUpdate
|
|
14
14
|
} = params;
|
|
15
|
-
|
|
15
|
+
onEntryBeforeUpdate.subscribe(async params => {
|
|
16
16
|
const {
|
|
17
17
|
entry,
|
|
18
18
|
model
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["assignBeforeEntryUpdate","params","context","
|
|
1
|
+
{"version":3,"names":["assignBeforeEntryUpdate","params","context","onEntryBeforeUpdate","subscribe","entry","model","markLockedFields"],"sources":["beforeUpdate.ts"],"sourcesContent":["import { Topic } from \"@webiny/pubsub/types\";\nimport { OnEntryBeforeUpdateTopicParams, CmsContext } from \"~/types\";\nimport { markLockedFields } from \"./markLockedFields\";\n\ninterface AssignBeforeEntryUpdateParams {\n context: CmsContext;\n onEntryBeforeUpdate: Topic<OnEntryBeforeUpdateTopicParams>;\n}\nexport const assignBeforeEntryUpdate = (params: AssignBeforeEntryUpdateParams) => {\n const { context, onEntryBeforeUpdate } = params;\n\n onEntryBeforeUpdate.subscribe(async params => {\n const { entry, model } = params;\n\n await markLockedFields({\n model,\n entry,\n context\n });\n });\n};\n"],"mappings":";;;;;;;AAEA;;AAMO,MAAMA,uBAAuB,GAAIC,MAAD,IAA2C;EAC9E,MAAM;IAAEC,OAAF;IAAWC;EAAX,IAAmCF,MAAzC;EAEAE,mBAAmB,CAACC,SAApB,CAA8B,MAAMH,MAAN,IAAgB;IAC1C,MAAM;MAAEI,KAAF;MAASC;IAAT,IAAmBL,MAAzB;IAEA,MAAM,IAAAM,kCAAA,EAAiB;MACnBD,KADmB;MAEnBD,KAFmB;MAGnBH;IAHmB,CAAjB,CAAN;EAKH,CARD;AASH,CAZM"}
|
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
4
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.validateModelEntryData = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
11
|
|
|
12
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
13
|
|
|
14
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
17
|
-
|
|
18
14
|
const validateValue = async (args, fieldValidators, value) => {
|
|
19
15
|
if (!fieldValidators) {
|
|
20
16
|
return null;
|
|
@@ -180,7 +176,7 @@ const validateModelEntryData = async params => {
|
|
|
180
176
|
model,
|
|
181
177
|
validatorList,
|
|
182
178
|
field,
|
|
183
|
-
data:
|
|
179
|
+
data: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (entry === null || entry === void 0 ? void 0 : entry.values) || {}), data),
|
|
184
180
|
context,
|
|
185
181
|
entry
|
|
186
182
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["validateValue","args","fieldValidators","value","validatorList","context","field","model","entry","fieldValidator","name","validations","length","validate","result","validator","message","ex","validatePredefinedValue","enabled","values","predefinedValues","Array","isArray","predefinedValue","runFieldMultipleValuesValidations","data","fieldId","valuesError","listValidation","valueError","validation","predefinedValueError","runFieldValueValidations","error","execValidation","multipleValues","validateModelEntryData","params","plugins","byType","reduce","acc","plugin","push","invalidFields","fields","storageId","WebinyError"],"sources":["entryDataValidation.ts"],"sourcesContent":["import {\n CmsModel,\n CmsModelField,\n CmsModelFieldValidation,\n CmsContext,\n CmsModelFieldValidatorPlugin,\n CmsModelFieldValidatorValidateParams,\n CmsEntry\n} from \"~/types\";\nimport WebinyError from \"@webiny/error\";\n\ntype PluginValidationCallable = (params: CmsModelFieldValidatorValidateParams) => Promise<boolean>;\ntype PluginValidationList = Record<string, PluginValidationCallable[]>;\ntype InputData = Record<string, any>;\n\ninterface ValidateArgs {\n validatorList: PluginValidationList;\n field: CmsModelField;\n model: CmsModel;\n data: InputData;\n context: CmsContext;\n entry?: CmsEntry;\n}\n\ntype PossibleValue = boolean | number | string | null | undefined;\n\nconst validateValue = async (\n args: ValidateArgs,\n fieldValidators: CmsModelFieldValidation[],\n value: PossibleValue | PossibleValue[]\n): Promise<string | null> => {\n if (!fieldValidators) {\n return null;\n }\n\n const { validatorList, context, field, model, entry } = args;\n try {\n for (const fieldValidator of fieldValidators) {\n const name = fieldValidator.name;\n const validations = validatorList[name];\n if (!validations || validations.length === 0) {\n return `There are no \"${name}\" validators defined.`;\n }\n for (const validate of validations) {\n const result = await validate({\n value,\n context,\n validator: fieldValidator,\n field,\n model,\n entry\n });\n if (!result) {\n return fieldValidator.message;\n }\n }\n }\n } catch (ex) {\n return ex.message;\n }\n\n return null;\n};\n\nconst validatePredefinedValue = (field: CmsModelField, value: any | any[]): string | null => {\n const { enabled = false, values: predefinedValues = [] } = field.predefinedValues || {};\n if (!enabled) {\n return null;\n } else if (Array.isArray(predefinedValues) === false || predefinedValues.length === 0) {\n return \"Missing predefined values to validate against.\";\n }\n for (const predefinedValue of predefinedValues) {\n if (predefinedValue.value == value) {\n return null;\n }\n }\n return \"Value sent does not match any of the available predefined values.\";\n};\n/**\n * When multiple values is selected we must run validations on the array containing the values\n * And then on each value in the array\n */\nconst runFieldMultipleValuesValidations = async (args: ValidateArgs): Promise<string | null> => {\n const { field, data } = args;\n const values = data[field.fieldId] || [];\n if (Array.isArray(values) === false) {\n return `Value of the field \"${field.fieldId}\" is not an array.`;\n }\n const valuesError = await validateValue(args, field.listValidation || [], values);\n if (valuesError) {\n return valuesError;\n }\n for (const value of values) {\n const valueError = await validateValue(args, field.validation || [], value);\n if (valueError) {\n return valueError;\n }\n const predefinedValueError = validatePredefinedValue(field, value);\n if (predefinedValueError) {\n return predefinedValueError;\n }\n }\n return null;\n};\n/**\n * Runs validation on given value.\n */\nconst runFieldValueValidations = async (args: ValidateArgs): Promise<string | null> => {\n const { data, field } = args;\n const value = data[field.fieldId];\n const error = await validateValue(args, field.validation || [], value);\n if (error) {\n return error;\n }\n return validatePredefinedValue(field, value);\n};\n\nconst execValidation = async (args: ValidateArgs): Promise<string | null> => {\n if (args.field.multipleValues) {\n return await runFieldMultipleValuesValidations(args);\n }\n return await runFieldValueValidations(args);\n};\n\nexport interface ValidateModelEntryDataParams {\n context: CmsContext;\n model: CmsModel;\n data: InputData;\n entry?: CmsEntry;\n}\nexport const validateModelEntryData = async (params: ValidateModelEntryDataParams) => {\n const { context, model, entry, data } = params;\n /**\n * To later simplify searching for the validations we map them to a name.\n * @see CmsModelFieldValidatorPlugin.validator.validate\n */\n const validatorList: PluginValidationList = context.plugins\n .byType<CmsModelFieldValidatorPlugin>(\"cms-model-field-validator\")\n .reduce((acc, plugin) => {\n const name = plugin.validator.name;\n if (!acc[name]) {\n acc[name] = [];\n }\n acc[name].push(plugin.validator.validate);\n\n return acc;\n }, {} as PluginValidationList);\n\n /**\n * Loop through model fields and validate the corresponding data.\n * Run validation only if the field has validation configured.\n */\n const invalidFields = [];\n for (const field of model.fields) {\n const error = await execValidation({\n model,\n validatorList,\n field,\n data: {\n ...(entry?.values || {}),\n ...data\n },\n context,\n entry\n });\n if (!error) {\n continue;\n }\n invalidFields.push({\n fieldId: field.fieldId,\n storageId: field.storageId,\n error\n });\n }\n\n if (invalidFields.length > 0) {\n throw new WebinyError(\"Validation failed.\", \"VALIDATION_FAILED\", invalidFields);\n }\n};\n"],"mappings":";;;;;;;;;;;AASA;;;;;;AAiBA,MAAMA,aAAa,GAAG,OAClBC,IADkB,EAElBC,eAFkB,EAGlBC,KAHkB,KAIO;EACzB,IAAI,CAACD,eAAL,EAAsB;IAClB,OAAO,IAAP;EACH;;EAED,MAAM;IAAEE,aAAF;IAAiBC,OAAjB;IAA0BC,KAA1B;IAAiCC,KAAjC;IAAwCC;EAAxC,IAAkDP,IAAxD;;EACA,IAAI;IACA,KAAK,MAAMQ,cAAX,IAA6BP,eAA7B,EAA8C;MAC1C,MAAMQ,IAAI,GAAGD,cAAc,CAACC,IAA5B;MACA,MAAMC,WAAW,GAAGP,aAAa,CAACM,IAAD,CAAjC;;MACA,IAAI,CAACC,WAAD,IAAgBA,WAAW,CAACC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ED,IAAIW,aAAa,CAACjC,MAAd,GAAuB,CAA3B,EAA8B;IAC1B,MAAM,IAAIoC,cAAJ,CAAgB,oBAAhB,EAAsC,mBAAtC,EAA2DH,aAA3D,CAAN;EACH;AACJ,CAhDM"}
|
|
1
|
+
{"version":3,"names":["validateValue","args","fieldValidators","value","validatorList","context","field","model","entry","fieldValidator","name","validations","length","validate","result","validator","message","ex","validatePredefinedValue","enabled","values","predefinedValues","Array","isArray","predefinedValue","runFieldMultipleValuesValidations","data","fieldId","valuesError","listValidation","valueError","validation","predefinedValueError","runFieldValueValidations","error","execValidation","multipleValues","validateModelEntryData","params","plugins","byType","reduce","acc","plugin","push","invalidFields","fields","storageId","WebinyError"],"sources":["entryDataValidation.ts"],"sourcesContent":["import {\n CmsModel,\n CmsModelField,\n CmsModelFieldValidation,\n CmsContext,\n CmsModelFieldValidatorPlugin,\n CmsModelFieldValidatorValidateParams,\n CmsEntry\n} from \"~/types\";\nimport WebinyError from \"@webiny/error\";\n\ntype PluginValidationCallable = (params: CmsModelFieldValidatorValidateParams) => Promise<boolean>;\ntype PluginValidationList = Record<string, PluginValidationCallable[]>;\ntype InputData = Record<string, any>;\n\ninterface ValidateArgs {\n validatorList: PluginValidationList;\n field: CmsModelField;\n model: CmsModel;\n data: InputData;\n context: CmsContext;\n entry?: CmsEntry;\n}\n\ntype PossibleValue = boolean | number | string | null | undefined;\n\nconst validateValue = async (\n args: ValidateArgs,\n fieldValidators: CmsModelFieldValidation[],\n value: PossibleValue | PossibleValue[]\n): Promise<string | null> => {\n if (!fieldValidators) {\n return null;\n }\n\n const { validatorList, context, field, model, entry } = args;\n try {\n for (const fieldValidator of fieldValidators) {\n const name = fieldValidator.name;\n const validations = validatorList[name];\n if (!validations || validations.length === 0) {\n return `There are no \"${name}\" validators defined.`;\n }\n for (const validate of validations) {\n const result = await validate({\n value,\n context,\n validator: fieldValidator,\n field,\n model,\n entry\n });\n if (!result) {\n return fieldValidator.message;\n }\n }\n }\n } catch (ex) {\n return ex.message;\n }\n\n return null;\n};\n\nconst validatePredefinedValue = (field: CmsModelField, value: any | any[]): string | null => {\n const { enabled = false, values: predefinedValues = [] } = field.predefinedValues || {};\n if (!enabled) {\n return null;\n } else if (Array.isArray(predefinedValues) === false || predefinedValues.length === 0) {\n return \"Missing predefined values to validate against.\";\n }\n for (const predefinedValue of predefinedValues) {\n if (predefinedValue.value == value) {\n return null;\n }\n }\n return \"Value sent does not match any of the available predefined values.\";\n};\n/**\n * When multiple values is selected we must run validations on the array containing the values\n * And then on each value in the array\n */\nconst runFieldMultipleValuesValidations = async (args: ValidateArgs): Promise<string | null> => {\n const { field, data } = args;\n const values = data[field.fieldId] || [];\n if (Array.isArray(values) === false) {\n return `Value of the field \"${field.fieldId}\" is not an array.`;\n }\n const valuesError = await validateValue(args, field.listValidation || [], values);\n if (valuesError) {\n return valuesError;\n }\n for (const value of values) {\n const valueError = await validateValue(args, field.validation || [], value);\n if (valueError) {\n return valueError;\n }\n const predefinedValueError = validatePredefinedValue(field, value);\n if (predefinedValueError) {\n return predefinedValueError;\n }\n }\n return null;\n};\n/**\n * Runs validation on given value.\n */\nconst runFieldValueValidations = async (args: ValidateArgs): Promise<string | null> => {\n const { data, field } = args;\n const value = data[field.fieldId];\n const error = await validateValue(args, field.validation || [], value);\n if (error) {\n return error;\n }\n return validatePredefinedValue(field, value);\n};\n\nconst execValidation = async (args: ValidateArgs): Promise<string | null> => {\n if (args.field.multipleValues) {\n return await runFieldMultipleValuesValidations(args);\n }\n return await runFieldValueValidations(args);\n};\n\nexport interface ValidateModelEntryDataParams {\n context: CmsContext;\n model: CmsModel;\n data: InputData;\n entry?: CmsEntry;\n}\nexport const validateModelEntryData = async (params: ValidateModelEntryDataParams) => {\n const { context, model, entry, data } = params;\n /**\n * To later simplify searching for the validations we map them to a name.\n * @see CmsModelFieldValidatorPlugin.validator.validate\n */\n const validatorList: PluginValidationList = context.plugins\n .byType<CmsModelFieldValidatorPlugin>(\"cms-model-field-validator\")\n .reduce((acc, plugin) => {\n const name = plugin.validator.name;\n if (!acc[name]) {\n acc[name] = [];\n }\n acc[name].push(plugin.validator.validate);\n\n return acc;\n }, {} as PluginValidationList);\n\n /**\n * Loop through model fields and validate the corresponding data.\n * Run validation only if the field has validation configured.\n */\n const invalidFields = [];\n for (const field of model.fields) {\n const error = await execValidation({\n model,\n validatorList,\n field,\n data: {\n ...(entry?.values || {}),\n ...data\n },\n context,\n entry\n });\n if (!error) {\n continue;\n }\n invalidFields.push({\n fieldId: field.fieldId,\n storageId: field.storageId,\n error\n });\n }\n\n if (invalidFields.length > 0) {\n throw new WebinyError(\"Validation failed.\", \"VALIDATION_FAILED\", invalidFields);\n }\n};\n"],"mappings":";;;;;;;;;;;AASA;;AAiBA,MAAMA,aAAa,GAAG,OAClBC,IADkB,EAElBC,eAFkB,EAGlBC,KAHkB,KAIO;EACzB,IAAI,CAACD,eAAL,EAAsB;IAClB,OAAO,IAAP;EACH;;EAED,MAAM;IAAEE,aAAF;IAAiBC,OAAjB;IAA0BC,KAA1B;IAAiCC,KAAjC;IAAwCC;EAAxC,IAAkDP,IAAxD;;EACA,IAAI;IACA,KAAK,MAAMQ,cAAX,IAA6BP,eAA7B,EAA8C;MAC1C,MAAMQ,IAAI,GAAGD,cAAc,CAACC,IAA5B;MACA,MAAMC,WAAW,GAAGP,aAAa,CAACM,IAAD,CAAjC;;MACA,IAAI,CAACC,WAAD,IAAgBA,WAAW,CAACC,MAAZ,KAAuB,CAA3C,EAA8C;QAC1C,OAAQ,iBAAgBF,IAAK,uBAA7B;MACH;;MACD,KAAK,MAAMG,QAAX,IAAuBF,WAAvB,EAAoC;QAChC,MAAMG,MAAM,GAAG,MAAMD,QAAQ,CAAC;UAC1BV,KAD0B;UAE1BE,OAF0B;UAG1BU,SAAS,EAAEN,cAHe;UAI1BH,KAJ0B;UAK1BC,KAL0B;UAM1BC;QAN0B,CAAD,CAA7B;;QAQA,IAAI,CAACM,MAAL,EAAa;UACT,OAAOL,cAAc,CAACO,O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KAAiB;IACrB,MAAMjC,IAAI,GAAGiC,MAAM,CAAC5B,SAAP,CAAiBL,IAA9B;;IACA,IAAI,CAACgC,GAAG,CAAChC,IAAD,CAAR,EAAgB;MACZgC,GAAG,CAAChC,IAAD,CAAH,GAAY,EAAZ;IACH;;IACDgC,GAAG,CAAChC,IAAD,CAAH,CAAUkC,IAAV,CAAeD,MAAM,CAAC5B,SAAP,CAAiBF,QAAhC;IAEA,OAAO6B,GAAP;EACH,CAVuC,EAUrC,EAVqC,CAA5C;EAYA;AACJ;AACA;AACA;;EACI,MAAMG,aAAa,GAAG,EAAtB;;EACA,KAAK,MAAMvC,KAAX,IAAoBC,KAAK,CAACuC,MAA1B,EAAkC;IAC9B,MAAMZ,KAAK,GAAG,MAAMC,cAAc,CAAC;MAC/B5B,KAD+B;MAE/BH,aAF+B;MAG/BE,KAH+B;MAI/BoB,IAAI,8DACI,CAAAlB,KAAK,SAAL,IAAAA,KAAK,WAAL,YAAAA,KAAK,CAAEY,MAAP,KAAiB,EADrB,GAEGM,IAFH,CAJ2B;MAQ/BrB,OAR+B;MAS/BG;IAT+B,CAAD,CAAlC;;IAWA,IAAI,CAAC0B,KAAL,EAAY;MACR;IACH;;IACDW,aAAa,CAACD,IAAd,CAAmB;MACfjB,OAAO,EAAErB,KAAK,CAACqB,OADA;MAEfoB,SAAS,EAAEzC,KAAK,CAACyC,SAFF;MAGfb;IAHe,CAAnB;EAKH;;EAED,IAAIW,aAAa,CAACjC,MAAd,GAAuB,CAA3B,EAA8B;IAC1B,MAAM,IAAIoC,cAAJ,CAAgB,oBAAhB,EAAsC,mBAAtC,EAA2DH,aAA3D,CAAN;EACH;AACJ,CAhDM"}
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
4
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.markUnlockedFields = exports.markLockedFields = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
11
|
|
|
12
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
13
|
|
|
14
14
|
var _CmsModelPlugin = require("../../plugins/CmsModelPlugin");
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
16
|
+
var _getBaseFieldType = require("../../utils/getBaseFieldType");
|
|
19
17
|
|
|
20
18
|
const markLockedFields = async params => {
|
|
21
19
|
const {
|
|
@@ -37,6 +35,7 @@ const markLockedFields = async params => {
|
|
|
37
35
|
const lockedFields = [];
|
|
38
36
|
|
|
39
37
|
for (const field of model.fields) {
|
|
38
|
+
const baseType = (0, _getBaseFieldType.getBaseFieldType)(field);
|
|
40
39
|
const alreadyLocked = existingLockedFields.some(lockedField => lockedField.fieldId === field.storageId);
|
|
41
40
|
|
|
42
41
|
if (alreadyLocked) {
|
|
@@ -44,7 +43,7 @@ const markLockedFields = async params => {
|
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
let lockedFieldData = {};
|
|
47
|
-
const lockedFieldPlugins = cmsLockedFieldPlugins.filter(pl => pl.fieldType ===
|
|
46
|
+
const lockedFieldPlugins = cmsLockedFieldPlugins.filter(pl => pl.fieldType === baseType);
|
|
48
47
|
|
|
49
48
|
for (const plugin of lockedFieldPlugins) {
|
|
50
49
|
if (typeof plugin.getLockedFieldData !== "function") {
|
|
@@ -54,13 +53,13 @@ const markLockedFields = async params => {
|
|
|
54
53
|
const data = plugin.getLockedFieldData({
|
|
55
54
|
field
|
|
56
55
|
});
|
|
57
|
-
lockedFieldData =
|
|
56
|
+
lockedFieldData = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, lockedFieldData), data);
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
lockedFields.push(
|
|
59
|
+
lockedFields.push((0, _objectSpread2.default)({
|
|
61
60
|
fieldId: field.storageId,
|
|
62
61
|
multipleValues: !!field.multipleValues,
|
|
63
|
-
type:
|
|
62
|
+
type: baseType
|
|
64
63
|
}, lockedFieldData));
|
|
65
64
|
} // no need to update anything if no locked fields were added
|
|
66
65
|
|
|
@@ -74,7 +73,7 @@ const markLockedFields = async params => {
|
|
|
74
73
|
try {
|
|
75
74
|
await context.cms.updateModelDirect({
|
|
76
75
|
original: model,
|
|
77
|
-
model:
|
|
76
|
+
model: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, model), {}, {
|
|
78
77
|
lockedFields: newLockedFields
|
|
79
78
|
})
|
|
80
79
|
});
|
|
@@ -104,7 +103,7 @@ const markUnlockedFields = async params => {
|
|
|
104
103
|
try {
|
|
105
104
|
await context.cms.updateModelDirect({
|
|
106
105
|
original: model,
|
|
107
|
-
model:
|
|
106
|
+
model: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, model), {}, {
|
|
108
107
|
lockedFields: []
|
|
109
108
|
})
|
|
110
109
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["markLockedFields","params","model","context","plugins","byType","CmsModelPlugin","type","find","plugin","contentModel","modelId","cmsLockedFieldPlugins","existingLockedFields","lockedFields","field","fields","alreadyLocked","some","lockedField","fieldId","storageId","lockedFieldData","lockedFieldPlugins","filter","pl","fieldType","getLockedFieldData","data","push","multipleValues","length","newLockedFields","concat","cms","updateModelDirect","original","ex","WebinyError","markUnlockedFields"],"sources":["markLockedFields.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { CmsEntry, CmsModel, CmsContext, CmsModelLockedFieldPlugin, LockedField } from \"~/types\";\nimport { CmsModelPlugin } from \"~/plugins/CmsModelPlugin\";\n\ninterface MarkLockedFieldsParams {\n model: CmsModel;\n entry: CmsEntry;\n context: CmsContext;\n}\nexport const markLockedFields = async (params: MarkLockedFieldsParams): Promise<void> => {\n const { model, context } = params;\n /**\n * If the model is registered via a plugin, we don't need do process anything.\n */\n const plugins = context.plugins.byType<CmsModelPlugin>(CmsModelPlugin.type);\n if (plugins.find(plugin => plugin.contentModel.modelId === model.modelId)) {\n return;\n }\n\n const cmsLockedFieldPlugins =\n context.plugins.byType<CmsModelLockedFieldPlugin>(\"cms-model-locked-field\");\n\n const existingLockedFields = model.lockedFields || [];\n const lockedFields: LockedField[] = [];\n for (const field of model.fields) {\n const alreadyLocked = existingLockedFields.some(\n lockedField => lockedField.fieldId === field.storageId\n );\n if (alreadyLocked) {\n continue;\n }\n\n let lockedFieldData = {};\n\n const lockedFieldPlugins = cmsLockedFieldPlugins.filter(pl => pl.fieldType ===
|
|
1
|
+
{"version":3,"names":["markLockedFields","params","model","context","plugins","byType","CmsModelPlugin","type","find","plugin","contentModel","modelId","cmsLockedFieldPlugins","existingLockedFields","lockedFields","field","fields","baseType","getBaseFieldType","alreadyLocked","some","lockedField","fieldId","storageId","lockedFieldData","lockedFieldPlugins","filter","pl","fieldType","getLockedFieldData","data","push","multipleValues","length","newLockedFields","concat","cms","updateModelDirect","original","ex","WebinyError","markUnlockedFields"],"sources":["markLockedFields.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { CmsEntry, CmsModel, CmsContext, CmsModelLockedFieldPlugin, LockedField } from \"~/types\";\nimport { CmsModelPlugin } from \"~/plugins/CmsModelPlugin\";\nimport { getBaseFieldType } from \"~/utils/getBaseFieldType\";\n\ninterface MarkLockedFieldsParams {\n model: CmsModel;\n entry: CmsEntry;\n context: CmsContext;\n}\nexport const markLockedFields = async (params: MarkLockedFieldsParams): Promise<void> => {\n const { model, context } = params;\n /**\n * If the model is registered via a plugin, we don't need do process anything.\n */\n const plugins = context.plugins.byType<CmsModelPlugin>(CmsModelPlugin.type);\n if (plugins.find(plugin => plugin.contentModel.modelId === model.modelId)) {\n return;\n }\n\n const cmsLockedFieldPlugins =\n context.plugins.byType<CmsModelLockedFieldPlugin>(\"cms-model-locked-field\");\n\n const existingLockedFields = model.lockedFields || [];\n const lockedFields: LockedField[] = [];\n for (const field of model.fields) {\n const baseType = getBaseFieldType(field);\n const alreadyLocked = existingLockedFields.some(\n lockedField => lockedField.fieldId === field.storageId\n );\n if (alreadyLocked) {\n continue;\n }\n\n let lockedFieldData = {};\n\n const lockedFieldPlugins = cmsLockedFieldPlugins.filter(pl => pl.fieldType === baseType);\n for (const plugin of lockedFieldPlugins) {\n if (typeof plugin.getLockedFieldData !== \"function\") {\n continue;\n }\n const data = plugin.getLockedFieldData({\n field\n });\n lockedFieldData = { ...lockedFieldData, ...data };\n }\n\n lockedFields.push({\n fieldId: field.storageId,\n multipleValues: !!field.multipleValues,\n type: baseType,\n ...lockedFieldData\n });\n }\n // no need to update anything if no locked fields were added\n if (lockedFields.length === 0) {\n return;\n }\n\n const newLockedFields = existingLockedFields.concat(lockedFields);\n\n try {\n await context.cms.updateModelDirect({\n original: model,\n model: {\n ...model,\n lockedFields: newLockedFields\n }\n });\n model.lockedFields = newLockedFields;\n } catch (ex) {\n throw new WebinyError(\n `Could not update model \"${model.modelId}\" with new locked fields.`,\n \"MODEL_LOCKED_FIELDS_UPDATE_FAILED\",\n ex\n );\n }\n};\n\nexport interface MarkFieldsUnlockedParams {\n context: CmsContext;\n model: CmsModel;\n}\nexport const markUnlockedFields = async (params: MarkFieldsUnlockedParams) => {\n const { context, model } = params;\n /**\n * If the model is registered via a plugin, we don't need do process anything.\n */\n const plugins = context.plugins.byType<CmsModelPlugin>(CmsModelPlugin.type);\n if (plugins.find(plugin => plugin.contentModel.modelId === model.modelId)) {\n return;\n }\n\n try {\n await context.cms.updateModelDirect({\n original: model,\n model: {\n ...model,\n lockedFields: []\n }\n });\n model.lockedFields = [];\n } catch (ex) {\n throw new WebinyError(\n `Could not update model \"${model.modelId}\" with unlocked fields.`,\n \"MODEL_UNLOCKED_FIELDS_UPDATE_FAILED\",\n ex\n );\n }\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AAEA;;AACA;;AAOO,MAAMA,gBAAgB,GAAG,MAAOC,MAAP,IAAyD;EACrF,MAAM;IAAEC,KAAF;IAASC;EAAT,IAAqBF,MAA3B;EACA;AACJ;AACA;;EACI,MAAMG,OAAO,GAAGD,OAAO,CAACC,OAAR,CAAgBC,MAAhB,CAAuCC,8BAAA,CAAeC,IAAtD,CAAhB;;EACA,IAAIH,OAAO,CAACI,IAAR,CAAaC,MAAM,IAAIA,MAAM,CAACC,YAAP,CAAoBC,OAApB,KAAgCT,KAAK,CAACS,OAA7D,CAAJ,EAA2E;IACvE;EACH;;EAED,MAAMC,qBAAqB,GACvBT,OAAO,CAACC,OAAR,CAAgBC,MAAhB,CAAkD,wBAAlD,CADJ;EAGA,MAAMQ,oBAAoB,GAAGX,KAAK,CAACY,YAAN,IAAsB,EAAnD;EACA,MAAMA,YAA2B,GAAG,EAApC;;EACA,KAAK,MAAMC,KAAX,IAAoBb,KAAK,CAACc,MAA1B,EAAkC;IAC9B,MAAMC,QAAQ,GAAG,IAAAC,kCAAA,EAAiBH,KAAjB,CAAjB;IACA,MAAMI,aAAa,GAAGN,oBAAoB,CAACO,IAArB,CAClBC,WAAW,IAAIA,WAAW,CAACC,OAAZ,KAAwBP,KAAK,CAACQ,SAD3B,CAAtB;;IAGA,IAAIJ,aAAJ,EAAmB;MACf;IACH;;IAED,IAAIK,eAAe,GAAG,EAAtB;IAEA,MAAMC,kBAAkB,GAAGb,qBAAqB,CAACc,MAAtB,CAA6BC,EAAE,IAAIA,EAAE,CAAC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,CAAaC,MAAM,IAAIA,MAAM,CAACC,YAAP,CAAoBC,OAApB,KAAgCT,KAAK,CAACS,OAA7D,CAAJ,EAA2E;IACvE;EACH;;EAED,IAAI;IACA,MAAMR,OAAO,CAACiC,GAAR,CAAYC,iBAAZ,CAA8B;MAChCC,QAAQ,EAAEpC,KADsB;MAEhCA,KAAK,8DACEA,KADF;QAEDY,YAAY,EAAE;MAFb;IAF2B,CAA9B,CAAN;IAOAZ,KAAK,CAACY,YAAN,GAAqB,EAArB;EACH,CATD,CASE,OAAOyB,EAAP,EAAW;IACT,MAAM,IAAIC,cAAJ,CACD,2BAA0BtC,KAAK,CAACS,OAAQ,yBADvC,EAEF,qCAFE,EAGF4B,EAHE,CAAN;EAKH;AACJ,CA1BM"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
4
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.referenceFieldsMapping = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
11
|
|
|
12
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
13
|
|
|
@@ -15,9 +15,7 @@ var _dotProp = _interopRequireDefault(require("dot-prop"));
|
|
|
15
15
|
|
|
16
16
|
var _utils = require("@webiny/utils");
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
18
|
+
var _getBaseFieldType = require("../../utils/getBaseFieldType");
|
|
21
19
|
|
|
22
20
|
const buildReferenceFieldPaths = params => {
|
|
23
21
|
const {
|
|
@@ -27,13 +25,15 @@ const buildReferenceFieldPaths = params => {
|
|
|
27
25
|
} = params;
|
|
28
26
|
const parentPaths = [...initialParentPaths];
|
|
29
27
|
const isMultipleValues = Array.isArray(input);
|
|
30
|
-
return fields.filter(field => ["object", "ref"].includes(
|
|
28
|
+
return fields.filter(field => ["object", "ref"].includes((0, _getBaseFieldType.getBaseFieldType)(field))).reduce((collection, field) => {
|
|
31
29
|
var _field$settings2;
|
|
32
30
|
|
|
33
31
|
/**
|
|
34
32
|
* First we check the ref field
|
|
35
33
|
*/
|
|
36
|
-
|
|
34
|
+
const baseType = (0, _getBaseFieldType.getBaseFieldType)(field);
|
|
35
|
+
|
|
36
|
+
if (baseType === "ref") {
|
|
37
37
|
const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(".")}.` : "";
|
|
38
38
|
|
|
39
39
|
if (field.multipleValues) {
|
|
@@ -143,9 +143,7 @@ const referenceFieldsMapping = async params => {
|
|
|
143
143
|
input,
|
|
144
144
|
validateEntries = false
|
|
145
145
|
} = params;
|
|
146
|
-
|
|
147
|
-
let output = _objectSpread({}, input);
|
|
148
|
-
|
|
146
|
+
let output = (0, _objectSpread2.default)({}, input);
|
|
149
147
|
const referenceFieldPaths = buildReferenceFieldPaths({
|
|
150
148
|
fields: model.fields,
|
|
151
149
|
input,
|
|
@@ -199,7 +197,7 @@ const referenceFieldsMapping = async params => {
|
|
|
199
197
|
const models = (await context.cms.listModels()).filter(model => {
|
|
200
198
|
const entries = referencesByModel[model.modelId];
|
|
201
199
|
|
|
202
|
-
if (Array.isArray(entries)
|
|
200
|
+
if (!Array.isArray(entries) || entries.length === 0) {
|
|
203
201
|
return false;
|
|
204
202
|
}
|
|
205
203
|
|
|
@@ -242,7 +240,7 @@ const referenceFieldsMapping = async params => {
|
|
|
242
240
|
for (const id of entries) {
|
|
243
241
|
if (records[id]) {
|
|
244
242
|
continue;
|
|
245
|
-
} else if (validateEntries
|
|
243
|
+
} else if (validateEntries) {
|
|
246
244
|
throw new _error.default(`Missing referenced entry with id "${id}" in model "${modelId}".`, "ENTRY_NOT_FOUND", {
|
|
247
245
|
id,
|
|
248
246
|
model: modelId
|
|
@@ -269,7 +267,7 @@ const referenceFieldsMapping = async params => {
|
|
|
269
267
|
const paths = pathsByReferenceId[id];
|
|
270
268
|
|
|
271
269
|
if (!entry) {
|
|
272
|
-
if (validateEntries
|
|
270
|
+
if (validateEntries) {
|
|
273
271
|
throw new _error.default("Missing entry in records.", "ENTRY_ERROR", {
|
|
274
272
|
id,
|
|
275
273
|
paths
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["buildReferenceFieldPaths","params","fields","parentPaths","initialParentPaths","input","isMultipleValues","Array","isArray","filter","field","includes","type","reduce","collection","parentPathsValue","length","join","multipleValues","inputValue","dotProp","get","fieldId","key","path","push","objFieldPath","objFieldInputValue","result","settings","concat","results","getReferenceFieldValue","ref","id","modelId","entryId","trim","referenceFieldsMapping","context","model","validateEntries","output","referenceFieldPaths","referencesByModel","pathsByReferenceId","Object","keys","models","cms","listModels","entries","promises","map","getEntriesByIds","Promise","all","records","entry","WebinyError","parseIdentifier","paths","set"],"sources":["referenceFieldsMapping.ts"],"sourcesContent":["import { CmsContext, CmsModel, CmsModelField } from \"~/types\";\nimport WebinyError from \"@webiny/error\";\nimport dotProp from \"dot-prop\";\nimport { parseIdentifier } from \"@webiny/utils\";\n\ninterface CmsRefEntry {\n id: string;\n entryId: string;\n modelId: string;\n}\n\ninterface ReferenceObject {\n id: string;\n modelId: string;\n}\n\ninterface Params {\n context: CmsContext;\n model: CmsModel;\n input: Record<string, ReferenceObject | ReferenceObject[]>;\n validateEntries?: boolean;\n}\n\ninterface BuildReferenceFieldPaths {\n fields: CmsModelField[];\n parentPaths: string[];\n input: Record<string, any>;\n}\n\nconst buildReferenceFieldPaths = (params: BuildReferenceFieldPaths): string[] => {\n const { fields, parentPaths: initialParentPaths, input } = params;\n\n const parentPaths = [...initialParentPaths];\n\n const isMultipleValues = Array.isArray(input);\n\n return fields\n .filter(field => [\"object\", \"ref\"].includes(field.type))\n .reduce((collection, field) => {\n /**\n * First we check the ref field\n */\n if (field.type === \"ref\") {\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n if (field.multipleValues) {\n const inputValue = dotProp.get(input, `${field.fieldId}`, []);\n if (Array.isArray(inputValue) === false) {\n return collection;\n }\n for (const key in inputValue) {\n const path = `${parentPathsValue}${field.fieldId}.${key}`;\n collection.push(path);\n }\n return collection;\n }\n\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n collection.push(`${parentPathsValue}${field.fieldId}`);\n\n return collection;\n }\n /**\n * Then we move onto the object field\n */\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n /**\n * This is if received input is array. We need to map key with fieldId at this point.\n */\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n const objFieldPath = `${field.fieldId}`;\n const objFieldInputValue = dotProp.get(input, objFieldPath, []);\n\n /**\n * If field is multiple values one, we need to go through the input and use the existing keys.\n */\n if (field.multipleValues) {\n if (Array.isArray(objFieldInputValue) === false) {\n return collection;\n }\n for (const key in objFieldInputValue) {\n const result = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue[key],\n parentPaths: parentPaths.concat([field.fieldId, key])\n });\n collection.push(...result);\n }\n\n return collection;\n }\n\n /**\n * Single value reference field.\n */\n const results = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue,\n parentPaths: parentPaths.concat([field.fieldId])\n });\n\n return collection.concat(results);\n }, [] as string[]);\n};\n\nconst getReferenceFieldValue = (ref: any): { id: string | null; modelId: string | null } => {\n if (!ref) {\n return {\n id: null,\n modelId: null\n };\n }\n return {\n id: (ref.id || ref.entryId || \"\").trim() || null,\n modelId: (ref.modelId || \"\").trim() || null\n };\n};\n\nexport const referenceFieldsMapping = async (params: Params): Promise<Record<string, any>> => {\n const { context, model, input, validateEntries = false } = params;\n\n let output: Record<string, any> = {\n ...input\n };\n\n const referenceFieldPaths = buildReferenceFieldPaths({\n fields: model.fields,\n input,\n parentPaths: []\n });\n if (referenceFieldPaths.length === 0) {\n return output;\n }\n\n const referencesByModel: Record<string, string[]> = {};\n const pathsByReferenceId: Record<string, string[]> = {};\n\n for (const path of referenceFieldPaths) {\n const ref = dotProp.get(output, path) as ReferenceObject | any;\n\n const { id, modelId } = getReferenceFieldValue(ref);\n\n if (!id || !modelId) {\n continue;\n }\n if (!referencesByModel[modelId]) {\n referencesByModel[modelId] = [];\n }\n referencesByModel[modelId].push(id);\n if (!pathsByReferenceId[id]) {\n pathsByReferenceId[id] = [];\n }\n pathsByReferenceId[id].push(path);\n }\n\n /**\n * Again, no point in going further.\n */\n if (Object.keys(referencesByModel).length === 0) {\n return output;\n }\n /**\n * Load all models and use only those that are used in reference.\n */\n const models = (await context.cms.listModels()).filter(model => {\n const entries = referencesByModel[model.modelId];\n if (Array.isArray(entries) === false || entries.length === 0) {\n return false;\n }\n return true;\n });\n /**\n * Check for any model existence, just in case.\n */\n if (models.length === 0) {\n return output;\n }\n\n /**\n * Load all the entries by their ID\n */\n const promises = models.map(model => {\n return context.cms.getEntriesByIds(model, referencesByModel[model.modelId]);\n });\n\n const results = await Promise.all(promises);\n\n const records: Record<string, CmsRefEntry> = results.reduce((collection, entries) => {\n for (const entry of entries) {\n collection[entry.id] = {\n id: entry.id,\n entryId: entry.entryId,\n modelId: entry.modelId\n };\n }\n return collection;\n }, {} as Record<string, CmsRefEntry>);\n /**\n * Verify that all referenced entries actually exist.\n */\n for (const modelId in referencesByModel) {\n const entries = referencesByModel[modelId];\n for (const id of entries) {\n if (records[id]) {\n continue;\n } else if (validateEntries === true) {\n throw new WebinyError(\n `Missing referenced entry with id \"${id}\" in model \"${modelId}\".`,\n \"ENTRY_NOT_FOUND\",\n {\n id,\n model: modelId\n }\n );\n }\n const { id: entryId } = parseIdentifier(id);\n records[id] = {\n id,\n entryId,\n modelId\n };\n }\n }\n\n /**\n * In the end, assign the entryId, id and model values to the output.\n */\n for (const id in pathsByReferenceId) {\n const entry = records[id];\n const paths = pathsByReferenceId[id];\n if (!entry) {\n if (validateEntries === true) {\n throw new WebinyError(\"Missing entry in records.\", \"ENTRY_ERROR\", {\n id,\n paths\n });\n }\n continue;\n }\n for (const path of paths) {\n output = dotProp.set(output, path, {\n id: entry.id,\n entryId: entry.entryId,\n modelId: entry.modelId\n });\n }\n }\n\n return output;\n};\n"],"mappings":";;;;;;;;;;;AACA;;AACA;;AACA;;;;;;AA0BA,MAAMA,wBAAwB,GAAIC,MAAD,IAAgD;EAC7E,MAAM;IAAEC,MAAF;IAAUC,WAAW,EAAEC,kBAAvB;IAA2CC;EAA3C,IAAqDJ,MAA3D;EAEA,MAAME,WAAW,GAAG,CAAC,GAAGC,kBAAJ,CAApB;EAEA,MAAME,gBAAgB,GAAGC,KAAK,CAACC,OAAN,CAAcH,KAAd,CAAzB;EAEA,OAAOH,MAAM,CACRO,MADE,CACKC,KAAK,IAAI,CAAC,QAAD,EAAW,KAAX,EAAkBC,QAAlB,CAA2BD,KAAK,CAACE,IAAjC,CADd,EAEFC,MAFE,CAEK,CAACC,UAAD,EAAaJ,KAAb,KAAuB;IAAA;;IAC3B;AACZ;AACA;IACY,IAAIA,KAAK,CAACE,IAAN,KAAe,KAAnB,EAA0B;MACtB,MAAMG,gBAAgB,GAAGZ,WAAW,CAACa,MAAZ,GAAqB,CAArB,GAA0B,GAA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AmBqC,KAAnB,EAA0B;MACtBnB,MAAM,GAAGtB,gBAAA,CAAQ0C,GAAR,CAAYpB,MAAZ,EAAoBlB,IAApB,EAA0B;QAC/BU,EAAE,EAAEwB,KAAK,CAACxB,EADqB;QAE/BE,OAAO,EAAEsB,KAAK,CAACtB,OAFgB;QAG/BD,OAAO,EAAEuB,KAAK,CAACvB;MAHgB,CAA1B,CAAT;IAKH;EACJ;;EAED,OAAOO,MAAP;AACH,CAnIM"}
|
|
1
|
+
{"version":3,"names":["buildReferenceFieldPaths","params","fields","parentPaths","initialParentPaths","input","isMultipleValues","Array","isArray","filter","field","includes","getBaseFieldType","reduce","collection","baseType","parentPathsValue","length","join","multipleValues","inputValue","dotProp","get","fieldId","key","path","push","objFieldPath","objFieldInputValue","result","settings","concat","results","getReferenceFieldValue","ref","id","modelId","entryId","trim","referenceFieldsMapping","context","model","validateEntries","output","referenceFieldPaths","referencesByModel","pathsByReferenceId","Object","keys","models","cms","listModels","entries","promises","map","getEntriesByIds","Promise","all","records","entry","WebinyError","parseIdentifier","paths","set"],"sources":["referenceFieldsMapping.ts"],"sourcesContent":["import { CmsContext, CmsModel, CmsModelField } from \"~/types\";\nimport WebinyError from \"@webiny/error\";\nimport dotProp from \"dot-prop\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport { getBaseFieldType } from \"~/utils/getBaseFieldType\";\n\ninterface CmsRefEntry {\n id: string;\n entryId: string;\n modelId: string;\n}\n\ninterface ReferenceObject {\n id: string;\n modelId: string;\n}\n\ninterface Params {\n context: CmsContext;\n model: CmsModel;\n input: Record<string, ReferenceObject | ReferenceObject[]>;\n validateEntries?: boolean;\n}\n\ninterface BuildReferenceFieldPaths {\n fields: CmsModelField[];\n parentPaths: string[];\n input: Record<string, any>;\n}\n\nconst buildReferenceFieldPaths = (params: BuildReferenceFieldPaths): string[] => {\n const { fields, parentPaths: initialParentPaths, input } = params;\n\n const parentPaths = [...initialParentPaths];\n\n const isMultipleValues = Array.isArray(input);\n\n return fields\n .filter(field => [\"object\", \"ref\"].includes(getBaseFieldType(field)))\n .reduce((collection, field) => {\n /**\n * First we check the ref field\n */\n const baseType = getBaseFieldType(field);\n if (baseType === \"ref\") {\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n if (field.multipleValues) {\n const inputValue = dotProp.get(input, `${field.fieldId}`, []);\n if (Array.isArray(inputValue) === false) {\n return collection;\n }\n for (const key in inputValue) {\n const path = `${parentPathsValue}${field.fieldId}.${key}`;\n collection.push(path);\n }\n return collection;\n }\n\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n collection.push(`${parentPathsValue}${field.fieldId}`);\n\n return collection;\n }\n /**\n * Then we move onto the object field\n */\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n /**\n * This is if received input is array. We need to map key with fieldId at this point.\n */\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n const objFieldPath = `${field.fieldId}`;\n const objFieldInputValue = dotProp.get(input, objFieldPath, []);\n\n /**\n * If field is multiple values one, we need to go through the input and use the existing keys.\n */\n if (field.multipleValues) {\n if (Array.isArray(objFieldInputValue) === false) {\n return collection;\n }\n for (const key in objFieldInputValue) {\n const result = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue[key],\n parentPaths: parentPaths.concat([field.fieldId, key])\n });\n collection.push(...result);\n }\n\n return collection;\n }\n\n /**\n * Single value reference field.\n */\n const results = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue,\n parentPaths: parentPaths.concat([field.fieldId])\n });\n\n return collection.concat(results);\n }, [] as string[]);\n};\n\nconst getReferenceFieldValue = (ref: any): { id: string | null; modelId: string | null } => {\n if (!ref) {\n return {\n id: null,\n modelId: null\n };\n }\n return {\n id: (ref.id || ref.entryId || \"\").trim() || null,\n modelId: (ref.modelId || \"\").trim() || null\n };\n};\n\nexport const referenceFieldsMapping = async (params: Params): Promise<Record<string, any>> => {\n const { context, model, input, validateEntries = false } = params;\n\n let output: Record<string, any> = {\n ...input\n };\n\n const referenceFieldPaths = buildReferenceFieldPaths({\n fields: model.fields,\n input,\n parentPaths: []\n });\n if (referenceFieldPaths.length === 0) {\n return output;\n }\n\n const referencesByModel: Record<string, string[]> = {};\n const pathsByReferenceId: Record<string, string[]> = {};\n\n for (const path of referenceFieldPaths) {\n const ref = dotProp.get(output, path) as ReferenceObject | any;\n\n const { id, modelId } = getReferenceFieldValue(ref);\n\n if (!id || !modelId) {\n continue;\n }\n if (!referencesByModel[modelId]) {\n referencesByModel[modelId] = [];\n }\n referencesByModel[modelId].push(id);\n if (!pathsByReferenceId[id]) {\n pathsByReferenceId[id] = [];\n }\n pathsByReferenceId[id].push(path);\n }\n\n /**\n * Again, no point in going further.\n */\n if (Object.keys(referencesByModel).length === 0) {\n return output;\n }\n /**\n * Load all models and use only those that are used in reference.\n */\n const models = (await context.cms.listModels()).filter(model => {\n const entries = referencesByModel[model.modelId];\n if (!Array.isArray(entries) || entries.length === 0) {\n return false;\n }\n return true;\n });\n /**\n * Check for any model existence, just in case.\n */\n if (models.length === 0) {\n return output;\n }\n\n /**\n * Load all the entries by their ID\n */\n const promises = models.map(model => {\n return context.cms.getEntriesByIds(model, referencesByModel[model.modelId]);\n });\n\n const results = await Promise.all(promises);\n\n const records: Record<string, CmsRefEntry> = results.reduce((collection, entries) => {\n for (const entry of entries) {\n collection[entry.id] = {\n id: entry.id,\n entryId: entry.entryId,\n modelId: entry.modelId\n };\n }\n return collection;\n }, {} as Record<string, CmsRefEntry>);\n /**\n * Verify that all referenced entries actually exist.\n */\n for (const modelId in referencesByModel) {\n const entries = referencesByModel[modelId];\n for (const id of entries) {\n if (records[id]) {\n continue;\n } else if (validateEntries) {\n throw new WebinyError(\n `Missing referenced entry with id \"${id}\" in model \"${modelId}\".`,\n \"ENTRY_NOT_FOUND\",\n {\n id,\n model: modelId\n }\n );\n }\n const { id: entryId } = parseIdentifier(id);\n records[id] = {\n id,\n entryId,\n modelId\n };\n }\n }\n\n /**\n * In the end, assign the entryId, id and model values to the output.\n */\n for (const id in pathsByReferenceId) {\n const entry = records[id];\n const paths = pathsByReferenceId[id];\n if (!entry) {\n if (validateEntries) {\n throw new WebinyError(\"Missing entry in records.\", \"ENTRY_ERROR\", {\n id,\n paths\n });\n }\n continue;\n }\n for (const path of paths) {\n output = dotProp.set(output, path, {\n id: entry.id,\n entryId: entry.entryId,\n modelId: entry.modelId\n });\n }\n }\n\n return output;\n};\n"],"mappings":";;;;;;;;;;;AACA;;AACA;;AACA;;AACA;;AA0BA,MAAMA,wBAAwB,GAAIC,MAAD,IAAgD;EAC7E,MAAM;IAAEC,MAAF;IAAUC,WAAW,EAAEC,kBAAvB;IAA2CC;EAA3C,IAAqDJ,MAA3D;EAEA,MAAME,WAAW,GAAG,CAAC,GAAGC,kBAAJ,CAApB;EAEA,MAAME,gBAAgB,GAAGC,KAAK,CAACC,OAAN,CAAcH,KAAd,CAAzB;EAEA,OAAOH,MAAM,CACRO,MADE,CACKC,KAAK,IAAI,CAAC,QAAD,EAAW,KAAX,EAAkBC,QAAlB,CAA2B,IAAAC,kCAAA,EAAiBF,KAAjB,CAA3B,CADd,EAEFG,MAFE,CAEK,CAACC,UAAD,EAAaJ,KAAb,KAAuB;IAAA;;IAC3B;AACZ;AACA;IACY,MAAMK,QAAQ,GAAG,IAAAH,kCAAA,EAAiBF,KAAjB,CAAjB;;IACA,IAAIK,QAAQ,KAAK,KAAjB,EAAwB;MACpB,MAAMC,gBAAgB,GAAGb,WAAW,CAACc,MAAZ,GAAqB,CAArB,GAA0B,GAAEd,WAAW,CAACe,IAAZ,CAAiB,GAAjB,CAAsB,GAAlD,GAAuD,EAAhF;;MACA,IAAIR,KAAK,CAACS,cAAV,EAA0B;QACtB,MAAMC,UAAU,GAAG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AAEsB,KAAK,CAACtB,OAFgB;QAG/BD,OAAO,EAAEuB,KAAK,CAACvB;MAHgB,CAA1B,CAAT;IAKH;EACJ;;EAED,OAAOO,MAAP;AACH,CAnIM"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CmsModelField } from "../../types";
|
|
2
|
+
import { PluginsContainer } from "@webiny/plugins";
|
|
3
|
+
interface Params {
|
|
4
|
+
input: string[];
|
|
5
|
+
fields: CmsModelField[];
|
|
6
|
+
plugins: PluginsContainer;
|
|
7
|
+
}
|
|
8
|
+
export declare const getSearchableFields: (params: Params) => string[];
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getSearchableFields = void 0;
|
|
7
|
+
|
|
8
|
+
const buildSearchableFieldList = params => {
|
|
9
|
+
const {
|
|
10
|
+
input,
|
|
11
|
+
plugins,
|
|
12
|
+
fields,
|
|
13
|
+
parents
|
|
14
|
+
} = params;
|
|
15
|
+
return fields.reduce((result, field) => {
|
|
16
|
+
var _field$settings;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* We need to check if the field is full text searchable, and for that we need a plugin for the field type.
|
|
20
|
+
*/
|
|
21
|
+
const plugin = plugins[field.type];
|
|
22
|
+
|
|
23
|
+
if (!plugin) {
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* There is a possibility that searchable fields exist in nested object field, so check that as well.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
const childFields = ((_field$settings = field.settings) === null || _field$settings === void 0 ? void 0 : _field$settings.fields) || [];
|
|
32
|
+
|
|
33
|
+
if (childFields.length > 0) {
|
|
34
|
+
/**
|
|
35
|
+
* So we build a list of searchable child fields and push it into the main result set.
|
|
36
|
+
*/
|
|
37
|
+
const childResults = buildSearchableFieldList({
|
|
38
|
+
fields: childFields,
|
|
39
|
+
parents: [...parents, field.fieldId],
|
|
40
|
+
plugins,
|
|
41
|
+
input
|
|
42
|
+
});
|
|
43
|
+
result.push(...childResults);
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* If not searchable, continue further.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if (!plugin.fullTextSearch) {
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Combine all parent paths with the current one and push it.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const path = [...parents, field.fieldId].join(".");
|
|
60
|
+
result.push(path);
|
|
61
|
+
return result;
|
|
62
|
+
}, []);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const getSearchableFields = params => {
|
|
66
|
+
const {
|
|
67
|
+
plugins,
|
|
68
|
+
input,
|
|
69
|
+
fields
|
|
70
|
+
} = params;
|
|
71
|
+
const fieldPluginMap = plugins.byType("cms-model-field-to-graphql").reduce((collection, field) => {
|
|
72
|
+
collection[field.fieldType] = field;
|
|
73
|
+
return collection;
|
|
74
|
+
}, {});
|
|
75
|
+
return buildSearchableFieldList({
|
|
76
|
+
fields,
|
|
77
|
+
input,
|
|
78
|
+
plugins: fieldPluginMap,
|
|
79
|
+
parents: []
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
exports.getSearchableFields = getSearchableFields;
|