UNPKG

@nu-art/bug-report

Version:
89 lines (88 loc) 4.18 kB
"use strict"; /* * Permissions management system, define access level for each of * your server apis, and restrict users by giving them access levels * * Copyright (C) 2020 Adam van der Kruk aka TacB0sS * * 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. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModuleBE_BugReport = exports.ModuleBE_BugReport_Class = void 0; const ts_common_1 = require("@nu-art/ts-common"); const backend_1 = require("@nu-art/firebase/backend"); const __1 = require("../.."); const backend_2 = require("@nu-art/thunderstorm/backend"); const backend_3 = require("@nu-art/user-account/backend"); const jszip_1 = __importDefault(require("jszip")); class ModuleBE_BugReport_Class extends ts_common_1.Module { constructor() { super(); this.ticketCreatorApis = []; this.sendBugReport = async (body) => { return await exports.ModuleBE_BugReport.saveFile(body, backend_3.MemKey_AccountId.get()); }; this.saveLog = async (report, id) => { var _a; const zip = new jszip_1.default(); report.log.forEach((message, i) => zip.file(`${report.name}_${(0, ts_common_1.padNumber)(i, 2)}.txt`, message)); const buffer = await zip.generateAsync({ type: 'nodebuffer' }); const bucket = await this.storage.getOrCreateBucket((_a = this.config) === null || _a === void 0 ? void 0 : _a.bucket); const fileName = `${id}-${report.name}.zip`; const file = await bucket.getFile(fileName); await file.write(buffer); return { path: `https://storage.cloud.google.com/${file.file.metadata.bucket}/${file.file.metadata.name}`, name: fileName }; }; this.saveFile = async (bugReport, email) => { var _a; const _id = (0, ts_common_1.generateHex)(16); const logs = await Promise.all(bugReport.reports.map(report => this.saveLog(report, _id))); const now = (0, ts_common_1.currentTimeMillis)(); const instance = { _id, __created: now, __updated: now, subject: bugReport.subject, description: bugReport.description, reports: logs, _audit: (0, ts_common_1.auditBy)(email || 'bug-report'), }; if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.bucket) instance.bucket = this.config.bucket; const tickets = await Promise.all(this.ticketCreatorApis.map(api => api(bugReport, logs, email))); instance.tickets = (0, ts_common_1.filterInstances)(tickets); await this.bugReport.insert(instance); return instance.tickets; }; } init() { const sessionAdmin = backend_1.ModuleBE_Firebase.createAdminSession(); const firestore = sessionAdmin.getFirestore(); this.bugReport = firestore.getCollection('bug-report', ['_id']); this.storage = sessionAdmin.getStorage(); (0, backend_2.addRoutes)([ (0, backend_2.createBodyServerApi)(__1.ApiDef_BugReport.v1.sendBugReport, (body) => this.sendBugReport(body)), ]); } addTicketCreator(ticketCreator) { (0, ts_common_1.addItemToArray)(this.ticketCreatorApis, ticketCreator); } } exports.ModuleBE_BugReport_Class = ModuleBE_BugReport_Class; exports.ModuleBE_BugReport = new ModuleBE_BugReport_Class();