189 lines
5.0 KiB
JavaScript
189 lines
5.0 KiB
JavaScript
const db = require('../db/models');
|
|
const Form_submissionsDBApi = require('../db/api/form_submissions');
|
|
const Activity_feed_itemsDBApi = require('../db/api/activity_feed_items');
|
|
const processFile = require("../middlewares/upload");
|
|
const ValidationError = require('./notifications/errors/validation');
|
|
const csv = require('csv-parser');
|
|
const stream = require('stream');
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = class Form_submissionsService {
|
|
static async create(data, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
try {
|
|
const submission = await Form_submissionsDBApi.create(
|
|
data,
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await Activity_feed_itemsDBApi.create(
|
|
{
|
|
item_type: 'submission_created',
|
|
title: 'Form Submission',
|
|
summary: `New form submission was received.`,
|
|
occurred_at: new Date(),
|
|
link_path: `/form_submissions/form_submissions-view/?id=${submission.id}`,
|
|
tenant: submission.tenantId || currentUser.tenantId,
|
|
actor_user: currentUser.id,
|
|
organizations: submission.organizationsId || currentUser.organizationsId,
|
|
},
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
}
|
|
);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async bulkImport(req, res) {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
await processFile(req, res);
|
|
const bufferStream = new stream.PassThrough();
|
|
const results = [];
|
|
|
|
await bufferStream.end(Buffer.from(req.file.buffer, "utf-8")); // convert Buffer to Stream
|
|
|
|
await new Promise((resolve, reject) => {
|
|
bufferStream
|
|
.pipe(csv())
|
|
.on('data', (data) => results.push(data))
|
|
.on('end', async () => {
|
|
console.log('CSV results', results);
|
|
resolve();
|
|
})
|
|
.on('error', (error) => reject(error));
|
|
})
|
|
|
|
const submissions = await Form_submissionsDBApi.bulkImport(results, {
|
|
transaction,
|
|
ignoreDuplicates: true,
|
|
validate: true,
|
|
currentUser: req.currentUser
|
|
});
|
|
|
|
for (const submission of submissions) {
|
|
await Activity_feed_itemsDBApi.create(
|
|
{
|
|
item_type: 'submission_created',
|
|
title: 'Form Submission (Bulk)',
|
|
summary: `New form submission was imported.`,
|
|
occurred_at: new Date(),
|
|
link_path: `/form_submissions/form_submissions-view/?id=${submission.id}`,
|
|
tenant: submission.tenantId || req.currentUser.tenantId,
|
|
actor_user: req.currentUser.id,
|
|
organizations: submission.organizationsId || req.currentUser.organizationsId,
|
|
},
|
|
{
|
|
currentUser: req.currentUser,
|
|
transaction,
|
|
}
|
|
);
|
|
}
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async update(data, id, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
try {
|
|
let form_submissions = await Form_submissionsDBApi.findBy(
|
|
{id},
|
|
{transaction},
|
|
);
|
|
|
|
if (!form_submissions) {
|
|
throw new ValidationError(
|
|
'form_submissionsNotFound',
|
|
);
|
|
}
|
|
|
|
const updatedForm_submissions = await Form_submissionsDBApi.update(
|
|
id,
|
|
data,
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await Activity_feed_itemsDBApi.create(
|
|
{
|
|
item_type: 'submission_updated',
|
|
title: 'Form Submission Updated',
|
|
summary: `Form submission was updated.`,
|
|
occurred_at: new Date(),
|
|
link_path: `/form_submissions/form_submissions-view/?id=${updatedForm_submissions.id}`,
|
|
tenant: updatedForm_submissions.tenantId || currentUser.tenantId,
|
|
actor_user: currentUser.id,
|
|
organizations: updatedForm_submissions.organizationsId || currentUser.organizationsId,
|
|
},
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
}
|
|
);
|
|
|
|
await transaction.commit();
|
|
return updatedForm_submissions;
|
|
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async deleteByIds(ids, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
await Form_submissionsDBApi.deleteByIds(ids, {
|
|
currentUser,
|
|
transaction,
|
|
});
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async remove(id, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
await Form_submissionsDBApi.remove(
|
|
id,
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
|
|
};
|