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',
|
2018-04-20 13:16:03 +00:00
|
|
|
accepts: [{
|
2018-03-28 13:07:48 +00:00
|
|
|
arg: 'data',
|
|
|
|
type: 'object',
|
|
|
|
http: {source: 'body'}
|
2018-04-20 13:16:03 +00:00
|
|
|
}, {
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-28 09:26:54 +00:00
|
|
|
Self.createWithInsurance = async(data, ctx) => {
|
2018-03-28 13:07:48 +00:00
|
|
|
let transaction = await Self.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
let classificationSchema = {client: data.clientFk, started: data.started};
|
|
|
|
let newClassification = await Self.create(classificationSchema, {transaction});
|
2018-04-20 13:16:03 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-04-20 13:16:03 +00:00
|
|
|
let newCreditInsurance = await CreditInsurance.create(insuranceSchema, {transaction});
|
2018-03-28 13:07:48 +00:00
|
|
|
await transaction.commit();
|
2018-04-20 13:16:03 +00:00
|
|
|
await CreditInsurance.messageSend(newCreditInsurance, ctx.req.accessToken);
|
|
|
|
|
2018-03-28 13:07:48 +00:00
|
|
|
return newClassification;
|
|
|
|
} catch (e) {
|
|
|
|
transaction.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|