41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
module.exports = function(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 => {
|
|
let transaction = await Self.beginTransaction({});
|
|
|
|
try {
|
|
let classificationSchema = {client: data.clientFk, started: data.started};
|
|
let newClassification = await Self.create(classificationSchema, {transaction});
|
|
let insuranceSchema = {
|
|
creditClassification: newClassification.id,
|
|
credit: data.credit,
|
|
grade: data.grade
|
|
};
|
|
|
|
Self.app.models.CreditInsurance.create(insuranceSchema, {transaction});
|
|
|
|
await transaction.commit();
|
|
return newClassification;
|
|
} catch (e) {
|
|
transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|