UNPKG

tanam

Version:

Pluggable CMS for Firebase

134 lines 5.91 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 documentService = require("./document.service"); const systemNotificationService = require("./system-message.service"); const siteCollection = () => admin.firestore().collection('tanam').doc(process.env.GCLOUD_PROJECT); function _normalizeData(data) { const normalizedData = Object.assign({}, data); for (const key in normalizedData) { const val = normalizedData[key]; if (val && val.toDate) { // Applies to Firestore timestamps normalizedData[key] = val.toDate(); } else if (val === undefined) { normalizedData[key] = null; } } return normalizedData; } function queryPageContext(documentTypeId, queryOpts = {}) { return __awaiter(this, void 0, void 0, function* () { console.log(`[queryPageContext] ${documentTypeId}, query=${JSON.stringify(queryOpts)}`); const orderByField = queryOpts.orderBy && queryOpts.orderBy.field || 'updated'; const sortOrder = queryOpts.orderBy && queryOpts.orderBy.sortOrder || 'desc'; const limit = queryOpts.limit || 20; console.log(`[queryPageContext] effective query ${JSON.stringify({ orderByField, sortOrder, limit })}`); let querySnap; try { querySnap = yield siteCollection() .collection('documents') .where('status', '==', 'published') .where('documentType', '==', documentTypeId) .orderBy(orderByField, sortOrder) .limit(limit) .get(); } catch (err) { console.error(`[queryPageContext] ${JSON.stringify(err)}`); const details = err.details; if (details.indexOf('firestore/indexes?create')) { yield systemNotificationService.reportMissingIndex(details); } else { yield systemNotificationService.reportUnknownError(details); } return []; } console.log(`[queryPageContext] num results: ${querySnap.docs.length}`); const result = []; for (const doc of querySnap.docs) { console.log(`[queryPageContext] ${JSON.stringify(doc.data())}`); result.push(yield _toContext(doc.data())); } console.log(`[queryPageContextResult] ${JSON.stringify(result)}`); return result; }); } exports.queryPageContext = queryPageContext; function getPageContextById(docId) { return __awaiter(this, void 0, void 0, function* () { console.log(`[getPageContextById] ${JSON.stringify(docId)}`); const doc = yield documentService.getDocumentById(docId); return !!doc ? _toContext(doc) : null; }); } exports.getPageContextById = getPageContextById; function getPageContextByUrl(url) { return __awaiter(this, void 0, void 0, function* () { console.log(`[getPageContextByUrl] ${JSON.stringify(url)}`); const documents = yield documentService.getDocumentByUrl(url || '/'); console.log(`[getPageContextByUrl] Number of query results: ${documents.length}`); if (documents.length === 0) { const page404 = yield documentService.getDocument404(); return _toContext(page404[0]); } return _toContext(documents[0]); }); } exports.getPageContextByUrl = getPageContextByUrl; function _toContext(document) { return __awaiter(this, void 0, void 0, function* () { if (!document) { return null; } // Run update operation in parallel while doing preparing the context data const updatePromise = siteCollection() .collection('documents').doc(document.id) .update({ dependencies: [], rendered: admin.firestore.FieldValue.serverTimestamp(), }); const siteInfo = (yield siteCollection().get()).data(); const siteContext = { domain: siteInfo.primaryDomain, analytics: siteInfo.analytics, url: `https://${siteInfo.primaryDomain}`, theme: siteInfo.theme, title: siteInfo.title, }; const normalizedDoc = _normalizeData(document); const documentContext = { id: normalizedDoc.id, documentType: normalizedDoc.documentType, data: _normalizeData(normalizedDoc.data), title: normalizedDoc.title, standalone: normalizedDoc.standalone, url: normalizedDoc.standalone ? normalizedDoc.url : null, permalink: normalizedDoc.standalone ? `${siteContext.url}${normalizedDoc.url}` : null, canonicalUrl: normalizedDoc.canonicalUrl, revision: normalizedDoc.revision, status: normalizedDoc.status, tags: normalizedDoc.tags, created: normalizedDoc.created, updated: normalizedDoc.updated, published: normalizedDoc.published, }; // Wait until update operation finish yield updatePromise; return { document: documentContext, site: siteContext, }; }); } //# sourceMappingURL=page-context.service.js.map