87 lines
2.3 KiB
JavaScript
87 lines
2.3 KiB
JavaScript
module.exports = function(Self) {
|
|
Self.remoteMethod('getCard', {
|
|
description: 'Get client basic data and debt',
|
|
accepts: {
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The client id',
|
|
http: {source: 'path'}
|
|
},
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
verb: 'GET',
|
|
path: '/:id/getCard'
|
|
}
|
|
});
|
|
|
|
Self.getCard = async(id, options) => {
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const client = await Self.findOne({
|
|
where: {
|
|
id: id
|
|
},
|
|
include: [
|
|
{
|
|
relation: 'contactChannel',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
},
|
|
{
|
|
relation: 'province',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
},
|
|
{
|
|
relation: 'salesPersonUser',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
},
|
|
{
|
|
relation: 'country',
|
|
scope: {
|
|
fields: ['id', 'country']
|
|
}
|
|
},
|
|
{
|
|
relation: 'payMethod',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
},
|
|
{
|
|
relation: 'account',
|
|
scope: {
|
|
fields: ['id', 'name', 'email', 'active']
|
|
}
|
|
},
|
|
{
|
|
relation: 'supplier',
|
|
scope: {
|
|
fields: ['id', 'nif']
|
|
}
|
|
}
|
|
]
|
|
}, myOptions);
|
|
|
|
const date = new Date();
|
|
date.setHours(0, 0, 0, 0);
|
|
const query = `SELECT vn.clientGetDebt(?, ?) AS debt`;
|
|
const data = await Self.rawSql(query, [id, date], myOptions);
|
|
|
|
client.debt = data[0].debt;
|
|
|
|
return client;
|
|
};
|
|
};
|