51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethod('createFromSales', {
|
|
description: 'Create a claim',
|
|
accessType: '',
|
|
accepts: [{
|
|
arg: 'claim',
|
|
type: 'object',
|
|
required: true,
|
|
description: ' newTicketFk, clientFk, ticketCreated',
|
|
http: {source: 'body'}
|
|
}, {
|
|
arg: 'sales',
|
|
type: 'object',
|
|
required: true,
|
|
description: '[sales IDs]',
|
|
http: {source: 'body'}
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/createFromSales`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.createFromSales = async params => {
|
|
let model = Self.app.models;
|
|
let transaction = await Self.beginTransaction({});
|
|
try {
|
|
let newClaim = await Self.create(params.claim, {transaction});
|
|
let promises = [];
|
|
for (let i = 0; i < params.sales.length; i++) {
|
|
promises.push(model.ClaimBeginning.create(
|
|
{saleFk: params.sales[i].id,
|
|
claimFk: newClaim.id,
|
|
quantity: params.sales[i].quantity},
|
|
{transaction}));
|
|
}
|
|
await Promise.all(promises);
|
|
await transaction.commit();
|
|
|
|
return newClaim;
|
|
} catch (e) {
|
|
transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|