tanam
Version:
Pluggable CMS for Firebase
62 lines • 2.98 kB
JavaScript
;
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 crypto_js_1 = require("crypto-js");
const admin = require("firebase-admin");
const siteCollection = () => admin.firestore().collection('tanam').doc(process.env.GCLOUD_PROJECT);
function createNoficication(type, title, message) {
const id = crypto_js_1.SHA1(type + title + message).toString().toLowerCase();
const docRef = siteCollection().collection('notifications').doc(id);
return admin.firestore().runTransaction((trx) => __awaiter(this, void 0, void 0, function* () {
const trxNotifDoc = yield trx.get(docRef);
if (trxNotifDoc.exists) {
trx.update(docRef, {
updated: admin.firestore.FieldValue.serverTimestamp(),
numOccurances: admin.firestore.FieldValue.increment(1),
isRead: false,
});
}
else {
trx.set(docRef, {
id: id,
type: 'error',
title: title,
message: message,
isRead: false,
numOccurances: 0,
updated: admin.firestore.FieldValue.serverTimestamp(),
created: admin.firestore.FieldValue.serverTimestamp(),
});
}
}));
}
/**
* Creates a system message about missing index.
*
* Use it like this:
* 1. Run a query that requires an index within a try/catch
* 2. Extract the message from the exception `exception.details`
* 3. Pass that messaage details string into this function
*
* The messaage details will contain a link similar to this:
* `https://console.firebase.google.com/project/<project id>/database/firestore/indexes?create_composite=<base64 encoded data>`
*
* @param message Error message from Firebase SDK exception
*/
function reportMissingIndex(message) {
const createIndexUrl = message.match(/(https:\/\/.*)$/)[1];
return createNoficication('error', 'Missing database index', `One of your templates is missing a database index. You can create it by <a target="_blank" href="${createIndexUrl}">clicking here</a>.`);
}
exports.reportMissingIndex = reportMissingIndex;
function reportUnknownError(message) {
return createNoficication('error', 'Unknown error', message);
}
exports.reportUnknownError = reportUnknownError;
//# sourceMappingURL=system-message.service.js.map