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

53 lines
1.3 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2018-03-28 13:07:48 +00:00
Self.remoteMethod('createWithInsurance', {
description: 'Creates both classification and one insurance',
accepts: [{
2018-03-28 13:07:48 +00:00
arg: 'data',
type: 'object',
http: {source: 'body'}
}, {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}],
2018-03-28 13:07:48 +00:00
returns: {
root: true,
type: 'boolean'
},
http: {
verb: 'post',
path: '/createWithInsurance'
}
});
Self.createWithInsurance = async(data, ctx) => {
2020-01-23 12:31:07 +00:00
const tx = await Self.beginTransaction({});
const models = Self.app.models;
const $t = ctx.req.__; // $translate
2018-03-28 13:07:48 +00:00
try {
let options = {transaction: tx};
2020-01-23 12:31:07 +00:00
const newClassification = await Self.create({
client: data.clientFk,
started: data.started
}, options);
await models.CreditInsurance.create({
2018-03-28 13:07:48 +00:00
creditClassification: newClassification.id,
credit: data.credit,
grade: data.grade
2020-01-23 12:31:07 +00:00
}, options);
2018-03-28 13:07:48 +00:00
await tx.commit();
2018-03-28 13:07:48 +00:00
return newClassification;
} catch (e) {
await tx.rollback();
2018-03-28 13:07:48 +00:00
throw e;
}
};
};