salix/modules/ticket/back/methods/ticket/uploadFile.js

79 lines
1.9 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('uploadFile', {
description: 'Upload and attach a document',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Number',
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'warehouseId',
type: 'Number',
description: ''
},
{
arg: 'companyId',
type: 'Number',
description: ''
},
{
arg: 'dmsTypeId',
type: 'Number',
description: ''
},
{
arg: 'reference',
type: 'String',
description: ''
},
{
arg: 'description',
type: 'String',
description: ''
},
{
arg: 'hasFile',
type: 'Boolean',
description: ''
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/:id/uploadFile`,
verb: 'POST'
}
});
Self.uploadFile = async(ctx, id) => {
const models = Self.app.models;
const promises = [];
const tx = await Self.beginTransaction({});
try {
const options = {transaction: tx};
const uploadedFiles = await models.Dms.uploadFile(ctx, options);
uploadedFiles.forEach(dms => {
const newTicketDms = models.TicketDms.create({
ticketFk: id,
dmsFk: dms.id
}, options);
promises.push(newTicketDms);
});
const resolvedPromises = await Promise.all(promises);
await tx.commit();
return resolvedPromises;
} catch (err) {
await tx.rollback();
throw err;
}
};
};