module.exports = Self => { Self.remoteMethod('createWithInsurance', { description: 'Creates both classification and one insurance', accepts: [{ arg: 'data', type: 'object', http: {source: 'body'} }, { arg: 'context', type: 'object', http: function(ctx) { return ctx; } }], returns: { root: true, type: 'boolean' }, http: { verb: 'post', path: '/createWithInsurance' } }); Self.createWithInsurance = async(data, ctx) => { const tx = await Self.beginTransaction({}); const models = Self.app.models; try { let options = {transaction: tx}; const newClassification = await Self.create({ client: data.clientFk, started: data.started }, options); await models.CreditInsurance.create({ creditClassification: newClassification.id, credit: data.credit, grade: data.grade }, options); await tx.commit(); return newClassification; } catch (e) { await tx.rollback(); throw e; } }; };