diff --git a/services/client/common/methods/creditClassification/createWithInsurance.js b/services/client/common/methods/creditClassification/createWithInsurance.js index d22dd9113..1d4a0abe6 100644 --- a/services/client/common/methods/creditClassification/createWithInsurance.js +++ b/services/client/common/methods/creditClassification/createWithInsurance.js @@ -1,11 +1,17 @@ module.exports = function(Self) { Self.remoteMethod('createWithInsurance', { description: 'Creates both classification and one insurance', - accepts: { + accepts: [{ arg: 'data', type: 'object', http: {source: 'body'} - }, + }, { + arg: 'context', + type: 'object', + http: function(ctx) { + return ctx; + } + }], returns: { root: true, type: 'boolean' @@ -16,21 +22,23 @@ module.exports = function(Self) { } }); - Self.createWithInsurance = async data => { + Self.createWithInsurance = async (data, ctx) => { let transaction = await Self.beginTransaction({}); try { let classificationSchema = {client: data.clientFk, started: data.started}; let newClassification = await Self.create(classificationSchema, {transaction}); + let CreditInsurance = Self.app.models.CreditInsurance; let insuranceSchema = { creditClassification: newClassification.id, credit: data.credit, grade: data.grade }; - Self.app.models.CreditInsurance.create(insuranceSchema, {transaction}); - + let newCreditInsurance = await CreditInsurance.create(insuranceSchema, {transaction}); await transaction.commit(); + await CreditInsurance.messageSend(newCreditInsurance, ctx.req.accessToken); + return newClassification; } catch (e) { transaction.rollback(); diff --git a/services/client/common/models/credit-classification.json b/services/client/common/models/credit-classification.json index 74edb6640..51bda330a 100644 --- a/services/client/common/models/credit-classification.json +++ b/services/client/common/models/credit-classification.json @@ -28,7 +28,7 @@ } }, "relations": { - "client": { + "customer": { "type": "belongsTo", "model": "Client", "foreignKey": "client" diff --git a/services/client/common/models/credit-insurance.js b/services/client/common/models/credit-insurance.js index 155d4de44..63fe8bfaa 100644 --- a/services/client/common/models/credit-insurance.js +++ b/services/client/common/models/credit-insurance.js @@ -19,4 +19,51 @@ module.exports = function(Self) { message: 'The grade must be an integer greater than or equal to zero', allowNull: true }); + + Self.messageSend = async function(data, accessToken) { + let filter = { + include: { + relation: 'classification', + scope: { + fields: ['client'], + include: { + relation: 'customer', + scope: { + fields: ['name', 'salesPersonFk'], + include: { + relation: 'salesPerson', + scope: { + fields: 'userFk', + include: { + relation: 'user', + scope: { + fields: ['name'] + } + } + } + } + } + } + } + } + }; + + let ctx = {req: {accessToken: accessToken}}; + let insurance = await Self.findById(data.id, filter); + let customer = insurance.classification().customer(); + let salesPerson = customer.salesPerson().user().name; + let grade = data.grade ? `(Grado ${data.grade})` : '(Sin grado)'; + let message = { + message: `He cambiado el crédito asegurado del cliente "${customer.name}" a ${data.credit} € ${grade}` + }; + + Self.app.models.Message.send(salesPerson, message, ctx); + }; + + // Update from transaction misses ctx accessToken. + // Fixed passing accessToken from method messageSend() + Self.observe('after save', async function(ctx) { + if (ctx.options.accessToken) + await Self.messageSend(ctx.instance, ctx.options.accessToken); + }); }; diff --git a/services/loopback/common/methods/message/send.js b/services/loopback/common/methods/message/send.js new file mode 100644 index 000000000..0a918a1d2 --- /dev/null +++ b/services/loopback/common/methods/message/send.js @@ -0,0 +1,40 @@ +module.exports = Self => { + Self.remoteMethod('send', { + description: 'Send message to user', + accessType: 'WRITE', + accepts: [{ + arg: 'recipient', + type: 'string', + required: true, + description: 'The user/alias name', + http: {source: 'path'} + }, { + arg: 'data', + type: 'object', + required: true, + description: 'Message data', + http: {source: 'body'} + }, { + arg: 'context', + type: 'object', + http: function(ctx) { + return ctx; + } + }], + returns: { + type: 'boolean', + root: true + }, + http: { + path: `/:recipient/send`, + verb: 'post' + } + }); + + Self.send = async (recipient, data, ctx) => { + let query = `SELECT vn.messageSendWithUser(?, ?, ?) AS sent`; + let [result] = await Self.rawSql(query, [ctx.req.accessToken.userId, recipient, data.message]); + + return result; + }; +}; diff --git a/services/loopback/common/methods/message/specs/send.spec.js b/services/loopback/common/methods/message/specs/send.spec.js new file mode 100644 index 000000000..ad5ae54c1 --- /dev/null +++ b/services/loopback/common/methods/message/specs/send.spec.js @@ -0,0 +1,12 @@ +const app = require(`${servicesDir}/client/server/server`); + +describe('message send()', () => { + it('should call the send method and return the response', done => { + let ctx = {req: {accessToken: {userId: 1}}}; + app.models.Message.send('salesPerson', {message: 'I changed something'}, ctx) + .then(response => { + expect(response.sent).toEqual(1); + done(); + }); + }); +}); diff --git a/services/loopback/common/models/message.js b/services/loopback/common/models/message.js new file mode 100644 index 000000000..ecebfb5d3 --- /dev/null +++ b/services/loopback/common/models/message.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/message/send.js')(Self); +} \ No newline at end of file diff --git a/services/loopback/common/models/message.json b/services/loopback/common/models/message.json new file mode 100644 index 000000000..2a855c907 --- /dev/null +++ b/services/loopback/common/models/message.json @@ -0,0 +1,39 @@ +{ + "name": "Message", + "base": "VnModel", + "options": { + "mysql": { + "table": "message" + } + }, + "properties": { + "id": { + "type": "Number", + "id": true, + "description": "Identifier" + }, + "sender": { + "type": "String", + "required": true + }, + "recipient": { + "type": "String", + "required": true + }, + "message": { + "type": "String" + } + }, + "relations": { + "remitter": { + "type": "belongsTo", + "model": "User", + "foreignKey": "sender" + }, + "receptor": { + "type": "belongsTo", + "model": "User", + "foreignKey": "recipient" + } + } +} \ No newline at end of file diff --git a/services/loopback/common/models/worker.json b/services/loopback/common/models/worker.json index f8399be8d..81807e6f6 100644 --- a/services/loopback/common/models/worker.json +++ b/services/loopback/common/models/worker.json @@ -20,6 +20,10 @@ "name": { "type": "string", "required": true + }, + "userFk": { + "type" : "Number", + "required": true } }, "relations": { diff --git a/services/loopback/server/model-config.json b/services/loopback/server/model-config.json index a46e46ea0..f97479309 100644 --- a/services/loopback/server/model-config.json +++ b/services/loopback/server/model-config.json @@ -101,5 +101,8 @@ }, "Producer": { "dataSource": "vn" + }, + "Message": { + "dataSource": "vn" } }