Merge branch 'dev' of http://git.verdnatura.es/salix into dev

This commit is contained in:
Carlos Jimenez 2018-04-20 16:26:06 +02:00
commit 03c61bb30f
13 changed files with 164 additions and 40 deletions

View File

@ -1,5 +1,4 @@
<mg-ajax path="/client/api/InvoiceOuts/filter" options="vnIndexNonAuto"></mg-ajax> <mg-ajax path="/client/api/InvoiceOuts/filter" options="vnIndexNonAuto"></mg-ajax>
<mg-ajax path="/client/api/InvoiceOuts/{{edit.params.id}}/sumAmount" options="mgEdit"></mg-ajax>
<vn-vertical> <vn-vertical>
<vn-card pad-large> <vn-card pad-large>
<vn-vertical> <vn-vertical>
@ -31,10 +30,7 @@
No results No results
</vn-one> </vn-one>
</vn-vertical> </vn-vertical>
<vn-horizontal vn-one class="list list-footer"> <vn-horizontal vn-one class="list list-footer"></vn-horizontal>
<vn-three></vn-three>
<vn-one>{{edit.model.sumAmount | currency:'€':2}}</vn-one>
</vn-horizontal>
</vn-vertical> </vn-vertical>
<vn-paging margin-large-top vn-one index="index" total="index.model.count"></vn-paging> <vn-paging margin-large-top vn-one index="index" total="index.model.count"></vn-paging>
</vn-vertical> </vn-vertical>

View File

@ -1,11 +1,17 @@
module.exports = function(Self) { module.exports = function(Self) {
Self.remoteMethod('createWithInsurance', { Self.remoteMethod('createWithInsurance', {
description: 'Creates both classification and one insurance', description: 'Creates both classification and one insurance',
accepts: { accepts: [{
arg: 'data', arg: 'data',
type: 'object', type: 'object',
http: {source: 'body'} http: {source: 'body'}
}, }, {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}],
returns: { returns: {
root: true, root: true,
type: 'boolean' type: 'boolean'
@ -16,21 +22,23 @@ module.exports = function(Self) {
} }
}); });
Self.createWithInsurance = async data => { Self.createWithInsurance = async (data, ctx) => {
let transaction = await Self.beginTransaction({}); let transaction = await Self.beginTransaction({});
try { try {
let classificationSchema = {client: data.clientFk, started: data.started}; let classificationSchema = {client: data.clientFk, started: data.started};
let newClassification = await Self.create(classificationSchema, {transaction}); let newClassification = await Self.create(classificationSchema, {transaction});
let CreditInsurance = Self.app.models.CreditInsurance;
let insuranceSchema = { let insuranceSchema = {
creditClassification: newClassification.id, creditClassification: newClassification.id,
credit: data.credit, credit: data.credit,
grade: data.grade grade: data.grade
}; };
Self.app.models.CreditInsurance.create(insuranceSchema, {transaction}); let newCreditInsurance = await CreditInsurance.create(insuranceSchema, {transaction});
await transaction.commit(); await transaction.commit();
await CreditInsurance.messageSend(newCreditInsurance, ctx.req.accessToken);
return newClassification; return newClassification;
} catch (e) { } catch (e) {
transaction.rollback(); transaction.rollback();

View File

@ -8,7 +8,7 @@ module.exports = Self => {
}, },
skip: (params.page - 1) * params.size, skip: (params.page - 1) * params.size,
limit: params.size, limit: params.size,
order: params.order, order: params.order
}; };
} }
}; };

View File

@ -1,27 +0,0 @@
module.exports = Self => {
Self.remoteMethod('sumAmount', {
description: 'Returns the sum of invoices amount for a client',
accessType: 'READ',
accepts: [{
arg: 'id',
description: 'The client Id',
type: 'number',
required: true,
http: {source: 'path'}
}],
returns: {
arg: 'sumAmount',
type: 'number'
},
http: {
path: `/:id/sumAmount`,
verb: 'GET'
}
});
Self.sumAmount = async id => {
let query = `SELECT SUM(amount) AS sumAmount FROM vn.invoiceOut WHERE clientFk = ?`;
let result = await Self.rawSql(query, [id]);
return result[0].sumAmount;
};
};

View File

@ -28,7 +28,7 @@
} }
}, },
"relations": { "relations": {
"client": { "customer": {
"type": "belongsTo", "type": "belongsTo",
"model": "Client", "model": "Client",
"foreignKey": "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', message: 'The grade must be an integer greater than or equal to zero',
allowNull: true 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

@ -1,4 +1,3 @@
module.exports = function(Self) { module.exports = function(Self) {
require('../methods/invoiceOut/filter.js')(Self); require('../methods/invoiceOut/filter.js')(Self);
require('../methods/invoiceOut/sumAmount.js')(Self);
}; };

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": { "name": {
"type": "string", "type": "string",
"required": true "required": true
},
"userFk": {
"type" : "Number",
"required": true
} }
}, },
"relations": { "relations": {

View File

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