salix/modules/client/back/methods/credit-classification/createWithInsurance.js

55 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('createWithInsurance', {
description: 'Creates both classification and one insurance',
accepts: [
{
arg: 'data',
type: 'object',
http: {source: 'body'}
}
],
returns: {
root: true,
type: 'boolean'
},
http: {
verb: 'post',
path: '/createWithInsurance'
}
});
Self.createWithInsurance = async(data, options) => {
const models = 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 newClassification = await Self.create({
client: data.clientFk,
started: data.started
}, myOptions);
await models.CreditInsurance.create({
creditClassification: newClassification.id,
credit: data.credit,
grade: data.grade
}, myOptions);
if (tx) await tx.commit();
return newClassification;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};