@intuitionrobotics/live-docs
Version:
118 lines • 6.05 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiveDocsModule = exports.LiveDocsModule_Class = exports.CollectionName_LiveDocs = void 0;
/*
* Permissions management system, define access level for each of
* your server apis, and restrict users by giving them access levels
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ts_common_1 = require("@intuitionrobotics/ts-common");
const backend_1 = require("@intuitionrobotics/firebase/backend");
const backend_2 = require("@intuitionrobotics/thunderstorm/backend");
exports.CollectionName_LiveDocs = "live-docs";
class LiveDocsModule_Class extends ts_common_1.Module {
constructor() {
super();
}
init() {
this.setDefaultConfig({ projectId: process.env.GCLOUD_PROJECT || "" });
const firestore = backend_1.FirebaseModule.createAdminSession(this.config.projectId).getFirestore();
this.livedocs = firestore.getCollection(exports.CollectionName_LiveDocs, ["key"]);
}
changeHistory(audit, key, change) {
return __awaiter(this, void 0, void 0, function* () {
yield this.livedocs.runInTransaction((transaction) => __awaiter(this, void 0, void 0, function* () {
const docsHistory = yield transaction.queryUnique(this.livedocs, { where: { key } });
if (!docsHistory)
throw new ts_common_1.BadImplementationException(`Cannot change history of an non-existing doc with key: ${key}`);
switch (change) {
case "redo":
if (docsHistory.index === 0)
throw new backend_2.ApiException(402, "Nothing to redo anymore");
docsHistory.index--;
break;
case "undo":
if (docsHistory.index === docsHistory.docs.length - 1)
throw new backend_2.ApiException(402, "Nothing to undo anymore");
docsHistory.index++;
break;
}
docsHistory._audit = audit;
yield transaction.upsert(this.livedocs, docsHistory);
}));
});
}
updateLiveDoc(audit, document) {
return __awaiter(this, void 0, void 0, function* () {
const liveDocHistory = yield this.getLiveDocHistory(document.key);
const docDB = Object.assign(Object.assign({}, document), { _audit: audit });
if (!liveDocHistory.index)
liveDocHistory.index = 0;
if (!liveDocHistory.docs) {
this.logDebug("no history array.. creating a new one");
liveDocHistory.docs = [];
}
if (liveDocHistory.index > liveDocHistory.docs.length) {
liveDocHistory.index = liveDocHistory.docs.length - 1;
if (liveDocHistory.index < 0)
liveDocHistory.index = 0;
}
if (liveDocHistory.index > 0) {
this.logDebug(`Rewriting history, current index ${liveDocHistory.index}`);
// this.logDebug(`Rewriting history, current history ${JSON.stringify(liveDocHistory.docs, null, 2)}`);
liveDocHistory.docs.splice(0, liveDocHistory.index);
}
if (liveDocHistory.docs.length === 0 || liveDocHistory.docs[0].document !== docDB.document)
ts_common_1.addItemToArrayAtIndex(liveDocHistory.docs, docDB, 0);
if (liveDocHistory.docs.length > 30)
ts_common_1.removeItemFromArray(liveDocHistory.docs, liveDocHistory.docs[liveDocHistory.docs.length - 1]);
yield this.livedocs.upsert(liveDocHistory);
});
}
getLiveDocHistory(docKey) {
return __awaiter(this, void 0, void 0, function* () {
const docFromDB = yield this.livedocs.queryUnique({ where: { key: docKey } });
return docFromDB || { docs: [], key: docKey, index: 0 };
});
}
getLiveDoc(key) {
return __awaiter(this, void 0, void 0, function* () {
const liveDocHistory = yield this.getLiveDocHistory(key);
let liveDoc = {
document: ""
};
if (liveDocHistory.docs && liveDocHistory.docs.length > 0 && liveDocHistory.docs[liveDocHistory.index]) {
this.logDebug(`Getting live doc from index: ${liveDocHistory.index}`);
liveDoc = liveDocHistory.docs[liveDocHistory.index];
//@ts-ignore
delete liveDoc.key;
}
return liveDoc;
});
}
}
exports.LiveDocsModule_Class = LiveDocsModule_Class;
exports.LiveDocsModule = new LiveDocsModule_Class();
//# sourceMappingURL=LiveDocsModule.js.map