Added method to send message to salesPerson on credit classification.

This commit is contained in:
Joan Sanchez 2018-04-20 15:16:03 +02:00
parent de40c400b4
commit 12e67b7b60
9 changed files with 162 additions and 6 deletions

View File

@ -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();

View File

@ -28,7 +28,7 @@
}
},
"relations": {
"client": {
"customer": {
"type": "belongsTo",
"model": "Client",
"foreignKey": "client"

View File

@ -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);
});
};

View File

@ -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;
};
};

View File

@ -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();
});
});
});

View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/message/send.js')(Self);
}

View File

@ -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"
}
}
}

View File

@ -20,6 +20,10 @@
"name": {
"type": "string",
"required": true
},
"userFk": {
"type" : "Number",
"required": true
}
},
"relations": {

View File

@ -101,5 +101,8 @@
},
"Producer": {
"dataSource": "vn"
},
"Message": {
"dataSource": "vn"
}
}