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

51 lines
1.5 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) => {
let tx = await Self.beginTransaction({});
2018-03-28 13:07:48 +00:00
try {
let options = {transaction: tx};
2018-03-28 13:07:48 +00:00
let classificationSchema = {client: data.clientFk, started: data.started};
let newClassification = await Self.create(classificationSchema, options);
let CreditInsurance = Self.app.models.CreditInsurance;
2018-03-28 13:07:48 +00:00
let insuranceSchema = {
creditClassification: newClassification.id,
credit: data.credit,
grade: data.grade
};
let newCreditInsurance = await CreditInsurance.create(insuranceSchema, options);
await tx.commit();
await CreditInsurance.messageSend(newCreditInsurance, ctx.req.accessToken);
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;
}
};
};