2018-05-04 09:46:03 +00:00
|
|
|
module.exports = Self => {
|
2018-03-28 13:07:48 +00:00
|
|
|
Self.remoteMethod('createWithInsurance', {
|
|
|
|
description: 'Creates both classification and one insurance',
|
2021-07-08 13:53:30 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'data',
|
|
|
|
type: 'object',
|
|
|
|
http: {source: 'body'}
|
2018-04-20 13:16:03 +00:00
|
|
|
}
|
2021-07-08 13:53:30 +00:00
|
|
|
],
|
2018-03-28 13:07:48 +00:00
|
|
|
returns: {
|
|
|
|
root: true,
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'post',
|
|
|
|
path: '/createWithInsurance'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-08 13:53:30 +00:00
|
|
|
Self.createWithInsurance = async(data, options) => {
|
2020-01-23 12:31:07 +00:00
|
|
|
const models = Self.app.models;
|
2021-07-08 13:53:30 +00:00
|
|
|
let tx;
|
|
|
|
const myOptions = {};
|
2018-03-28 13:07:48 +00:00
|
|
|
|
2021-07-08 13:53:30 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2019-06-12 15:31:45 +00:00
|
|
|
|
2021-07-08 13:53:30 +00:00
|
|
|
try {
|
2020-01-23 12:31:07 +00:00
|
|
|
const newClassification = await Self.create({
|
|
|
|
client: data.clientFk,
|
|
|
|
started: data.started
|
2021-07-08 13:53:30 +00:00
|
|
|
}, myOptions);
|
2020-01-23 12:31:07 +00:00
|
|
|
|
|
|
|
await models.CreditInsurance.create({
|
2018-03-28 13:07:48 +00:00
|
|
|
creditClassification: newClassification.id,
|
|
|
|
credit: data.credit,
|
|
|
|
grade: data.grade
|
2021-07-08 13:53:30 +00:00
|
|
|
}, myOptions);
|
2018-03-28 13:07:48 +00:00
|
|
|
|
2021-07-08 13:53:30 +00:00
|
|
|
if (tx) await tx.commit();
|
2018-04-20 13:16:03 +00:00
|
|
|
|
2018-03-28 13:07:48 +00:00
|
|
|
return newClassification;
|
|
|
|
} catch (e) {
|
2021-07-08 13:53:30 +00:00
|
|
|
if (tx) await tx.rollback();
|
2018-03-28 13:07:48 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|