UNPKG

@botonic/plugin-contentful

Version:

Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet

71 lines 3.08 kB
import { __awaiter } from "tslib"; import { ContentCallback } from '../cms'; import { getButtons, } from '../cms/visitors/message-visitors'; import { CONTENT_FIELDS, ContentFieldType, getFieldsForContentType, } from './fields'; /** * Deletes fields and the references from other contents that reference the * content through a button. */ export class ContentDeleter { constructor(manageCms, inverseTraverser, context) { this.manageCms = manageCms; this.inverseTraverser = inverseTraverser; this.context = context; } deleteContent(contentId, name) { return __awaiter(this, void 0, void 0, function* () { console.log(`Deleting fields and references to ${contentId.toString()} ${String(name)} for locale ${this.context.locale}`); yield this.deleteFields(contentId); yield this.deleteReferencesTo(contentId); }); } deleteFields(contentId) { return __awaiter(this, void 0, void 0, function* () { const fields = getFieldsForContentType(contentId.model); const newVal = {}; for (const field of fields) { const f = CONTENT_FIELDS.get(field); if (!f.isLocalized) continue; newVal[field] = undefined; } yield this.manageCms.updateFields(this.context, contentId, newVal); }); } deleteReferencesTo(contentId) { return __awaiter(this, void 0, void 0, function* () { if (!this.inverseTraverser.isLoaded()) { yield this.inverseTraverser.load(); } const sourceContents = this.inverseTraverser.getReferencesTo(contentId); for (const source of sourceContents) { const referenceStr = `Button from ${source.toString()} to ${contentId.toString()}`; const originalButtons = getButtons(source); if (originalButtons.filter(b => !(b.callback instanceof ContentCallback)) .length) { console.error(`${referenceStr} cannot be updated because a button contains a payload.` + ' Remove it manually'); continue; } const buttons = originalButtons .filter(b => !b.callback.equals(ContentCallback.ofContentId(contentId))) .map(b => ({ sys: { type: 'Link', linkType: 'Entry', id: b.callback.id, }, })); if (buttons.length == originalButtons.length) { console.error(`${referenceStr} not found`); continue; } // TODO do it in parallel yield this.manageCms.updateFields(this.context, source.contentId, { [ContentFieldType.BUTTONS]: buttons, }); } }); } } //# sourceMappingURL=content-deleter.js.map