87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('uploadFile', {
|
|
description: 'Upload and attach a file',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'The entry id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'warehouseId',
|
|
type: 'number',
|
|
description: 'The warehouse id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'companyId',
|
|
type: 'number',
|
|
description: 'The company id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'dmsTypeId',
|
|
type: 'number',
|
|
description: 'The dms type id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'reference',
|
|
type: 'string',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'description',
|
|
type: 'string',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'hasFile',
|
|
type: 'boolean',
|
|
description: 'True if has an attached file',
|
|
required: true
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/uploadFile`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.uploadFile = async(ctx, id, options) => {
|
|
const {Dms, EntryDms} = Self.app.models;
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
|
|
|
|
const promises = uploadedFiles.map(dms => EntryDms.create({
|
|
entryFk: id,
|
|
dmsFk: dms.id
|
|
}, myOptions));
|
|
await Promise.all(promises);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return uploadedFiles;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|