UNPKG

@tsed/formio

Version:
137 lines (136 loc) 4.24 kB
import { __decorate, __metadata } from "tslib"; import { isArray, isObject, isString, toMap as tMap } from "@tsed/core"; import { Inject, Injectable } from "@tsed/di"; import omit from "lodash/omit.js"; import { FormioMapper } from "../builder/FormioMapper.js"; import { isMongoId } from "../utils/isMongoId.js"; import { FormioService } from "./FormioService.js"; function toMap(list) { return tMap(list, (o) => [o._id.toString(), `$machineName:${o.name || o.machineName}`]); } let FormioDatabase = class FormioDatabase { get models() { return this.formio.mongoose.models; } get roleModel() { return this.models.role; } get formModel() { return this.models.form; } get actionModel() { return this.models.action; } get submissionModel() { return this.models.submission; } get tokenModel() { return this.models.token; } get actionItemModel() { return this.models.actionItem; } async getFormioMapper() { const [roles, forms, actions] = await Promise.all([ this.roleModel.find({ deleted: { $eq: null } }), this.formModel.find({ deleted: { $eq: null } }), this.actionModel.find({ deleted: { $eq: null } }) ]); return new FormioMapper({ forms: toMap(forms), actions: toMap(actions), roles: toMap(roles) }); } async hasForms() { return (await this.formModel.countDocuments()) > 0; } async hasForm(name) { return !!(await this.formModel.countDocuments({ name: { $eq: name } })); } getForm(nameOrId) { return this.formModel .findOne({ deleted: { $eq: null }, ...(isMongoId(nameOrId) ? { _id: nameOrId } : { name: { $eq: nameOrId } }) }) .lean() .exec(); } /** * Import form previously exported by export tool. * This method transform alias to a real mongoose _id * @param form * @param onCreate */ async importFormIfNotExists(form, onCreate) { if (!(await this.hasForm(form.name))) { const createForm = await this.importForm(form); onCreate && (await onCreate(createForm)); } return this.getForm(form.name); } /** * Import form previously exported by export tool. * This method transform alias to a real mongoose _id * @param form */ async importForm(form) { const mapper = await this.getFormioMapper(); return this.saveForm(mapper.mapToImport(form)); } saveForm(form) { form = new this.formModel(omit(form, ["__v"])); return this.formModel.findOneAndUpdate({ _id: form._id }, form, { upsert: true, new: true }); } getSubmissions(query = {}) { return this.submissionModel.find({ ...query, deleted: { $eq: null } }); } /** * Import submission previously exported by export tool. * This method transform alias to a real mongoose _id * @param submission */ async importSubmission(submission) { const mapper = await this.getFormioMapper(); return this.saveSubmission(mapper.mapToImport(submission)); } saveSubmission(submission) { submission = new this.submissionModel(omit(submission, ["__v"])); return this.submissionModel.findOneAndUpdate({ _id: submission._id }, submission, { new: true, upsert: true }); } idToBson(form) { if (isArray(form)) { return { $in: form.map(this.formio.util.idToBson) }; } else if (isObject(form)) { return this.formio.util.idToBson(form._id); } else if (isString(form)) { return this.formio.util.idToBson(form); } } }; __decorate([ Inject(), __metadata("design:type", FormioService) ], FormioDatabase.prototype, "formio", void 0); FormioDatabase = __decorate([ Injectable() ], FormioDatabase); export { FormioDatabase };