@tsed/formio
Version:
Formio package for Ts.ED framework
49 lines (48 loc) • 1.99 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { getValue } from "@tsed/core";
import { Inject } from "@tsed/di";
import async from "async";
import { Alter } from "../decorators/alter.js";
import { FormioDatabase } from "../services/FormioDatabase.js";
let AlterTemplateExportSteps = class AlterTemplateExportSteps {
transform(queue, template, map, options) {
queue.push(async.apply(this.exportSubmissions.bind(this), template, map, options));
return queue;
}
async exportSubmissions(template, map, options, next) {
const [mapper, submissions] = await Promise.all([
this.database.getFormioMapper(),
this.database.submissionModel.find({
deleted: { $eq: null }
})
]);
template.submissions = await this.mapSubmissions(submissions, mapper);
next(null, template);
}
mapSubmissions(submissions, mapper) {
return submissions
.map((submission) => submission.toObject())
.reduce((acc, { _id, created, updated, modified, __v, owner, roles, form, metadata, ...submission }) => {
const machineName = mapper.mapToExport(form.toString());
const key = machineName.replace("$machineName:", "");
acc[key] = getValue(acc, key, []);
acc[key].push({
...submission,
created: created && created.toString(),
modified: modified && modified.toString(),
data: mapper.mapToExport(submission.data),
roles: mapper.mapToExport(roles).filter(Boolean),
form: mapper.mapToExport(form)
});
return acc;
}, {});
}
};
__decorate([
Inject(),
__metadata("design:type", FormioDatabase)
], AlterTemplateExportSteps.prototype, "database", void 0);
AlterTemplateExportSteps = __decorate([
Alter("templateExportSteps")
], AlterTemplateExportSteps);
export { AlterTemplateExportSteps };