UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

166 lines 6.93 kB
import { __awaiter } from "tslib"; import { ButtonStyle, CmsException } from '../../cms'; import { ResourceNotFoundCmsException } from '../../cms/exceptions'; import { CONTENT_FIELDS, ContentFieldType, } from '../../manage-cms/fields'; import { isOfType } from '../../util/enums'; import { BUTTON, EntryLink, QUICK_REPLY } from './contentful-api'; export class ManageContentfulEntry { constructor(options, manage, environment) { this.options = options; this.manage = manage; this.environment = environment; } updateFields(context, contentId, fields) { return __awaiter(this, void 0, void 0, function* () { const environment = yield this.environment; const oldEntry = yield this.getEntry(environment, contentId); let needUpdate = false; for (const key of Object.keys(fields)) { if (!isOfType(key, ContentFieldType)) { throw new CmsException(`'${key}' is not a valid content field type`); } const field = this.checkOverwrite(context, oldEntry, key, false); if (oldEntry.fields[field.cmsName][context.locale] === fields[key]) { continue; } needUpdate = true; fields[key] = this.convertValueType(key, fields[key]); oldEntry.fields[field.cmsName][context.locale] = fields[key]; } if (!needUpdate) { return oldEntry.fields; } // we could use this.deliver.contentFromEntry & IgnoreFallbackDecorator to convert // the multilocale fields returned by update() yield this.writeEntry(context, oldEntry); return oldEntry.fields; }); } createContent(_context, model, id) { return __awaiter(this, void 0, void 0, function* () { const environment = yield this.environment; try { yield environment.createEntryWithId(model, id, { fields: {}, }); } catch (e) { throw new CmsException(`ERROR while creating content with id: '${id}'`, e); } }); } deleteContent(_context, contentId) { return __awaiter(this, void 0, void 0, function* () { const environment = yield this.environment; try { const oldEntry = yield this.getEntry(environment, contentId); if (oldEntry.isPublished()) yield oldEntry.unpublish(); yield oldEntry.delete(); } catch (e) { throw new CmsException('ERROR while deleting content', e); } }); } checkOverwrite(context, entry, fieldType, failIfMissing) { if (entry.isArchived()) { throw new CmsException(`Cannot update an archived entry`); } const field = CONTENT_FIELDS.get(fieldType); if (!field) { throw new CmsException(`Invalid field type '${fieldType}'`); } if (!context.locale) { // paranoic check throw new Error('Context.locale must be defined'); } if (!(field.cmsName in entry.fields)) { if (!failIfMissing) { entry.fields[field.cmsName] = {}; return field; } const fields = Object.keys(entry.fields); throw new CmsException(`Field '${field.cmsName}' not found in entry of type '${entry.sys.contentType.sys.id}. It only has ${JSON.stringify(fields)}'`); } if (!context.allowOverwrites) { const value = entry.fields[field.cmsName][context.locale]; if (value) { const error = `Cannot overwrite field '${field.cmsName}' of entry '${entry.sys.id}'`; const detail = `(has value '${String(value)}') because ManageContext.allowOverwrites is false`; throw new CmsException(error + detail); } } return field; } copyField(context, contentId, fieldType, fromLocale, onlyIfTargetEmpty) { return __awaiter(this, void 0, void 0, function* () { const environment = yield this.environment; const oldEntry = yield environment.getEntry(contentId.id); const field = this.checkOverwrite(context, oldEntry, fieldType, false); const fieldEntry = oldEntry.fields[field.cmsName]; if (fieldEntry === undefined) { return; } // TODO shouldn't this check be done before checkOverwrite? if (onlyIfTargetEmpty && context.locale in fieldEntry) { return; } fieldEntry[context.locale] = fieldEntry[fromLocale]; yield this.writeEntry(context, oldEntry); }); } writeEntry(context, entry) { return __awaiter(this, void 0, void 0, function* () { if (context.dryRun) { console.log('Not updating due to dryRun mode'); return; } const updated = yield entry.update(); if (!context.preview) { yield updated.publish(); } }); } getEntry(environment, contentId) { return __awaiter(this, void 0, void 0, function* () { try { return yield environment.getEntry(contentId.id); } catch (e) { throw new ResourceNotFoundCmsException(contentId, e); } }); } convertValueType(key, field) { var _a; const valueType = (_a = CONTENT_FIELDS.get(key)) === null || _a === void 0 ? void 0 : _a.valueType; if (valueType === typeof field) return field; if (key === ContentFieldType.BUTTONS_STYLE) { if (field === undefined) return null; return field === ButtonStyle.QUICK_REPLY ? QUICK_REPLY : BUTTON; } if (field === undefined) return field; if (key === ContentFieldType.FOLLOW_UP || key === ContentFieldType.TARGET || key === ContentFieldType.HANDOFF_QUEUE || key === ContentFieldType.ON_FINISH) { return this.getEntryLink(field, 'Entry'); } if (key === ContentFieldType.IMAGE || key === ContentFieldType.PIC) { return this.getEntryLink(field, 'Asset'); } if (key === ContentFieldType.BUTTONS || key === ContentFieldType.ELEMENTS) { const fieldLinks = field.map((id) => this.getEntryLink(id, 'Entry')); return fieldLinks; } return field; } getEntryLink(id, linkType) { return { sys: Object.assign({}, new EntryLink(id, linkType)) }; } } //# sourceMappingURL=manage-entry.js.map