UNPKG

tanam

Version:

Pluggable CMS for Firebase

117 lines 5.79 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const admin = require("firebase-admin"); const functions = require("firebase-functions"); const taskService = require("../services/task.service"); exports.onUpdateActiveTheme = functions.firestore.document('tanam/{siteId}').onUpdate((change, context) => __awaiter(this, void 0, void 0, function* () { const siteId = context.params.siteId; const siteInfoBefore = change.before.data(); const siteInfoAfter = change.after.data(); if (siteInfoBefore.theme === siteInfoAfter.theme) { console.log(`Active theme is unchanged. Nothing to do.`); return null; } const promises = []; const oldAssetFiles = yield admin.firestore() .collection('tanam').doc(siteId) .collection('themes').doc(siteInfoBefore.theme) .collection('assets') .get(); console.log(`Clearing cache for ${oldAssetFiles.docs.length} assets in previous theme (${siteInfoBefore.theme}).`); for (const doc of oldAssetFiles.docs) { const file = doc.data(); promises.push(taskService.deleteCache(siteId, `/_/theme/${encodeURIComponent(file.title)}`)); } const currentAssetFiles = yield admin.firestore() .collection('tanam').doc(siteId) .collection('themes').doc(siteInfoAfter.theme) .collection('assets') .get(); console.log(`Heating cache for ${currentAssetFiles.docs.length} assets in current theme (${siteInfoAfter.theme}).`); for (const doc of currentAssetFiles.docs) { const file = doc.data(); promises.push(taskService.createCache(siteId, `/_/theme/${encodeURIComponent(file.title)}`)); } const publishedDocs = yield admin.firestore() .collection('tanam').doc(siteId) .collection('documents') .where('status', '==', 'published') .get(); console.log(`Updating cache for ${publishedDocs.docs.length} published documents.`); for (const doc of publishedDocs.docs) { const document = doc.data(); promises.push(taskService.updateCache(siteId, document.url)); } return Promise.all(promises); })); exports.onDeleteThemeDeleteTemplates = functions.firestore.document('tanam/{siteId}/themes/{themeId}').onDelete((snap) => __awaiter(this, void 0, void 0, function* () { console.log(`Deleting all templates for theme ${snap.data().title} (${snap.data().id})`); const templates = yield snap.ref.collection('templates').get(); const promises = []; const batchDeletes = []; for (let i = 0; i < templates.docs.length; i++) { const doc = templates.docs[i]; if ((i % 500) === 0) { batchDeletes.push(admin.firestore().batch()); } const batchNum = batchDeletes.length - 1; console.log(`Batch delete #${batchNum} (${i + 1}/500): ${doc.id}`); batchDeletes[batchNum].delete(doc.ref); } batchDeletes.forEach((batchWrite) => { promises.push(batchWrite.commit()); }); return Promise.all(promises); })); exports.onDeleteThemeDeleteAssets = functions.firestore.document('tanam/{siteId}/themes/{themeId}').onDelete((snap) => __awaiter(this, void 0, void 0, function* () { console.log(`Deleting all assets for theme ${snap.data().title} (${snap.data().id})`); const assets = yield snap.ref.collection('assets').get(); const promises = []; const batchDeletes = []; for (let i = 0; i < assets.docs.length; i++) { const doc = assets.docs[i]; if ((i % 500) === 0) { batchDeletes.push(admin.firestore().batch()); } const batchNum = batchDeletes.length - 1; console.log(`Batch delete #${batchNum} (${i + 1}/500): ${doc.id}`); batchDeletes[batchNum].delete(doc.ref); } batchDeletes.forEach((batchWrite) => { promises.push(batchWrite.commit()); }); return Promise.all(promises); })); exports.onWriteTemplateUpdateCache = functions.firestore.document('tanam/{siteId}/themes/{themeId}/templates/{templateId}').onWrite((change, context) => __awaiter(this, void 0, void 0, function* () { const siteId = context.params.siteId; const themeId = context.params.themeId; const templateId = context.params.themeId; console.log(`Writing to template ${JSON.stringify({ siteId, themeId, templateId })}`); const siteInfo = (yield admin.firestore().collection('tanam').doc(siteId).get()).data(); console.log(`Active theme: ${siteInfo.theme}`); if (siteInfo.theme !== themeId) { console.log(`Writing template in a theme that is not active. No need to refresh cache.`); return null; } const publishedDocs = yield admin.firestore() .collection('tanam').doc(siteId) .collection('documents') .where('status', '==', 'published') .get(); console.log(`Updating cache for ${publishedDocs.docs.length} published documents.`); const promises = []; for (const doc of publishedDocs.docs) { const document = doc.data(); promises.push(taskService.updateCache(siteId, document.url)); } return Promise.all(promises); })); //# sourceMappingURL=theme.js.map