UNPKG

contentful-migration

Version:
305 lines 13.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = __importDefault(require("./errors")); const editor_layout_1 = require("../../utils/editor-layout"); const index_1 = require("./index"); const editorLayoutErrors = errors_1.default.editorLayout; const RELATIVE_MOVEMENTS = ['beforeField', 'afterField', 'beforeFieldGroup', 'afterFieldGroup']; const RELATIVE_MOVEMENTS_FIELD_PIVOT = ['beforeField', 'afterField']; const ABSOLUTE_MOVEMENTS = ['toTheTopOfFieldGroup', 'toTheBottomOfFieldGroup']; const VALID_MOVEMENT_DIRECTIONS = [...RELATIVE_MOVEMENTS, ...ABSOLUTE_MOVEMENTS]; class DuplicateCreate { validate(intent, context) { if (!intent.isEditorLayoutCreate()) { return; } if (!context.createdEditorLayouts.has(intent.getContentTypeId())) { return; } return editorLayoutErrors.createEditorLayout.EDITOR_LAYOUT_ALREADY_CREATED(intent.getContentTypeId()); } } class AlreadyExistingCreates { validate(intent, context) { if (!intent.isEditorLayoutCreate()) { return; } if (!context.remoteEditorLayouts.has(intent.getContentTypeId())) { return; } return editorLayoutErrors.createEditorLayout.EDITOR_LAYOUT_ALREADY_EXISTS(intent.getContentTypeId()); } } class InvalidEditorLayoutMethod { validate(intent) { if (intent.isEditorLayoutInvalidMethod()) { return editorLayoutErrors.updateEditorLayout.INVALID_METHOD(intent.getInvalidMethod()); } } } class DuplicateFieldGroupCreate { validate(intent, context) { if (!intent.isFieldGroupCreate()) { return; } if (!context.createdFieldGroups.has(getScopedFieldGroupId(intent))) { return; } return editorLayoutErrors.createFieldGroup.FIELD_GROUP_ALREADY_CREATED(intent.getFieldGroupId(), intent.getContentTypeId()); } } class AlreadyExistingFieldGroupCreates { validate(intent, context) { if (!intent.isFieldGroupCreate()) { return; } if (!context.remoteFieldGroups.has(getScopedFieldGroupId(intent))) { return; } return editorLayoutErrors.createFieldGroup.FIELD_GROUP_ALREADY_EXISTS(intent.getFieldGroupId(), intent.getContentTypeId()); } } class InvalidFieldGroupId { validate(intent) { if (!intent.isFieldGroupCreate() && !intent.isFieldGroupIdChange()) { return; } const fieldGroupId = intent.isFieldGroupIdChange() ? intent.getNewFieldGroupId() : intent.getFieldGroupId(); if (!fieldGroupId.match(/^[a-zA-Z0-9_]+$/) || fieldGroupId.length === 0) { return editorLayoutErrors.createFieldGroup.INVALID_CHARACTER_IN_ID(fieldGroupId, intent.getContentTypeId()); } if (fieldGroupId.match(/^\d/)) { return editorLayoutErrors.createFieldGroup.INVALID_FIRST_CHARACTER_IN_ID(fieldGroupId, intent.getContentTypeId()); } if (fieldGroupId.length > 50) { return editorLayoutErrors.createFieldGroup.ID_TOO_LONG(fieldGroupId, intent.getContentTypeId()); } } } class InvalidFieldGroupName { validate(intent) { if (!intent.isFieldGroupUpdate()) { return; } const name = intent.getFieldGroupProps().name; if (name && name.length > 50) { return editorLayoutErrors.createFieldGroup.NAME_TOO_LONG(intent.getFieldGroupId(), intent.getContentTypeId()); } } } class NonExistingDeletes { validate(intent, context) { if (!intent.isFieldGroupDelete()) { return; } const fieldGroupId = getScopedFieldGroupId(intent); const fieldGroupExists = context.remoteFieldGroups.has(fieldGroupId) || context.createdFieldGroups.has(fieldGroupId) || context.deletedFieldGroups.has(fieldGroupId); if (fieldGroupExists) { return; } return editorLayoutErrors.deleteFieldGroup.FIELD_GROUP_DOES_NOT_EXIST(intent.getFieldGroupId(), intent.getContentTypeId()); } } class InvalidFieldMove { validate(intent, { fields, remoteFieldGroups, createdFieldGroups, deletedFieldGroups }) { var _a, _b; if (intent.getRawType() !== 'contentType/moveFieldInEditorLayout') { return; } const moveIntent = intent; const { moveField: moveFieldError } = editorLayoutErrors; const fieldId = moveIntent.getFieldId(); const direction = moveIntent.getDirection(); const contentTypeId = moveIntent.getContentTypeId(); if (!fieldId) { return moveFieldError.MISSING_FIELD_ID(); } const fieldExists = ((_a = fields.contentTypeFields[contentTypeId]) === null || _a === void 0 ? void 0 : _a.has(fieldId)) && !((_b = fields.recentlyRemoved[contentTypeId]) === null || _b === void 0 ? void 0 : _b.has(fieldId)); if (!fieldExists) { return moveFieldError.FIELD_DOES_NOT_EXIST(fieldId); } if (!VALID_MOVEMENT_DIRECTIONS.includes(direction)) { return moveFieldError.INVALID_DIRECTION(fieldId, direction); } const pivotType = RELATIVE_MOVEMENTS_FIELD_PIVOT.includes(direction) ? 'field' : 'field group'; const pivotId = moveIntent.getPivotId(); if (RELATIVE_MOVEMENTS.includes(direction) && !pivotId) { return moveFieldError.MISSING_PIVOT(fieldId, pivotType); } if (pivotType === 'field' && fieldId === pivotId) { return moveFieldError.SELF_PIVOT(fieldId); } if (pivotId) { const scopedPivotId = `${contentTypeId}.${pivotId}`; const groupWithPivotIdExists = (remoteFieldGroups.has(scopedPivotId) || createdFieldGroups.has(scopedPivotId)) && !deletedFieldGroups.has(scopedPivotId); const fieldWithPivotIdExists = fields.contentTypeFields[contentTypeId].has(pivotId) && !fields.recentlyRemoved[contentTypeId].has(pivotId); const pivotExists = pivotType === 'field group' && groupWithPivotIdExists || pivotType === 'field' && fieldWithPivotIdExists; if (!pivotExists) { const explanation = ABSOLUTE_MOVEMENTS.includes(direction) ? `destination group "${pivotId}" does not exist` : `pivot ${pivotType} "${pivotId}" does not exist`; return moveFieldError.INVALID_PIVOT(fieldId, explanation); } } } } class DuplicateDeletes { validate(intent, context) { if (!intent.isFieldGroupDelete()) { return; } const fieldGroupId = getScopedFieldGroupId(intent); if (!context.deletedFieldGroups.has(fieldGroupId)) { return; } return editorLayoutErrors.deleteFieldGroup.FIELD_GROUP_ALREADY_DELETED(intent.getFieldGroupId(), intent.getContentTypeId()); } } class InvalidFielGroupIdChange { validate(intent, { remoteFieldGroups, createdFieldGroups }) { if (!intent.isFieldGroupIdChange()) { return; } const changeErrors = editorLayoutErrors.changeFieldGroupId; const contentTypeId = intent.getContentTypeId(); const fieldGroupId = intent.getFieldGroupId(); const newFieldGroupId = intent.getNewFieldGroupId(); if (!fieldGroupId) { return changeErrors.MISSING_FIELD_GROUP_ID(); } if (newFieldGroupId === undefined) { return changeErrors.MISSING_NEW_FIELD_GROUP(fieldGroupId); } const scopedFieldGroupId = getScopedFieldGroupId(intent); if (!remoteFieldGroups.has(scopedFieldGroupId) && !createdFieldGroups.has(scopedFieldGroupId)) { return changeErrors.FIELD_GROUP_DOES_NOT_EXIST(fieldGroupId, contentTypeId); } if (fieldGroupId === newFieldGroupId) { return changeErrors.SELF_FIELD_GROUP(fieldGroupId); } const scopedNewFieldGroupId = generateScopedId(contentTypeId, newFieldGroupId); if (remoteFieldGroups.has(scopedNewFieldGroupId) || createdFieldGroups.has(scopedNewFieldGroupId)) { return changeErrors.FIELD_GROUP_CONFLICT(newFieldGroupId, contentTypeId); } } } class InvalidFieldGroupControlChange { validate(intent, { remoteFieldGroups, createdFieldGroups }) { if (!intent.isFieldGroupControlChange()) { return; } const scopedFieldGroupId = getScopedFieldGroupId(intent); if (!remoteFieldGroups.has(scopedFieldGroupId) && !createdFieldGroups.has(scopedFieldGroupId)) { return editorLayoutErrors.changeFieldGroupControl.FIELD_GROUP_DOES_NOT_EXIST(intent.getFieldGroupId()); } } } const checks = [ new DuplicateCreate(), new AlreadyExistingCreates(), new DuplicateFieldGroupCreate(), new AlreadyExistingFieldGroupCreates(), new NonExistingDeletes(), new DuplicateDeletes(), new InvalidFieldMove(), new InvalidFielGroupIdChange(), new InvalidFieldGroupId(), new InvalidFieldGroupName(), new InvalidFieldGroupControlChange(), new InvalidEditorLayoutMethod() ]; function getScopedFieldGroupId(intent) { return generateScopedId(intent.getContentTypeId(), intent.getFieldGroupId()); } function generateScopedId(ctId, id) { return `${ctId}.${id}`; } function default_1(intents, editorInterfaces, fieldsContext, contentTypes) { let remoteFieldGroups = []; const remoteEditorLayouts = new Set(); editorInterfaces.forEach((editorInterfaces, ctId) => { const editorLayout = editorInterfaces.getEditorLayout(); if (editorLayout) { remoteEditorLayouts.add(ctId); remoteFieldGroups = remoteFieldGroups.concat((0, editor_layout_1.collectFieldGroupIds)(editorLayout).map(id => `${ctId}.${id}`)); } }); const toBeCreated = intents.filter((intent) => intent.isFieldGroupCreate()).map(getScopedFieldGroupId); let context = { fields: fieldsContext, remoteEditorLayouts, createdEditorLayouts: new Set(), remoteFieldGroups: new Set(remoteFieldGroups), createdFieldGroups: new Set(), deletedFieldGroups: new Set(), toBeCreatedFieldGroups: new Set(toBeCreated) // all future (in remaining iteration steps) created field groups }; let errors = []; for (const intent of intents) { if (!intent.isAboutEditorLayout()) { continue; } if (intent.isEditorLayoutCreate()) { const contentTypeId = intent.getContentTypeId(); const contentTypeExists = Boolean(contentTypes.find((ct) => ct.id === contentTypeId)); if (!contentTypeExists) { errors.push((0, index_1.invalidActionError)(editorLayoutErrors.updateEditorLayout.CONTENT_TYPE_DOES_NOT_EXIST(contentTypeId), intent)); } } let error; for (const check of checks) { error = check.validate(intent, context); if (error && error.length) { // proceed with next intent break; } } if (error) { const errorList = Array.isArray(error) ? error : [error]; const invalidActions = errorList.map((error) => ({ type: 'InvalidAction', message: error, details: { intent } })); errors = errors.concat(invalidActions); // do not update context continue; } if (intent.isEditorLayoutCreate()) { context.createdEditorLayouts.add(intent.getContentTypeId()); } if (intent.isEditorLayoutDelete()) { context.remoteEditorLayouts.delete(intent.getContentTypeId()); context.createdEditorLayouts.delete(intent.getContentTypeId()); } const fieldGroupId = getScopedFieldGroupId(intent); if (intent.isFieldGroupCreate()) { context.createdFieldGroups.add(fieldGroupId); context.toBeCreatedFieldGroups.delete(fieldGroupId); context.deletedFieldGroups.delete(fieldGroupId); } if (intent.isFieldGroupDelete()) { context.deletedFieldGroups.add(fieldGroupId); context.remoteFieldGroups.delete(fieldGroupId); context.createdFieldGroups.delete(fieldGroupId); } if (intent.isFieldGroupIdChange()) { const newFieldGroupId = generateScopedId(intent.getContentTypeId(), intent.getNewFieldGroupId()); context.remoteFieldGroups.delete(fieldGroupId); context.createdFieldGroups.delete(fieldGroupId); context.createdFieldGroups.add(newFieldGroupId); } } return errors; } exports.default = default_1; //# sourceMappingURL=editor-layout.js.map