salix/services/loopback/common/methods/client/card.js

77 lines
2.1 KiB
JavaScript

module.exports = function(Self) {
Self.remoteMethod('card', {
description: 'Get client basic data',
accepts: {
arg: 'id',
type: 'number',
required: true,
description: 'The client id',
http: {source: 'path'}
},
returns: {
arg: 'data',
type: 'Object',
root: true
},
http: {
verb: 'GET',
path: '/:id/card'
}
});
Self.card = function(id, cb) {
let filter = {
where: {
id: id
},
include: [
{
relation: 'salesPerson',
scope: {
fields: ['id', 'firstName', 'name']
}
}, {
relation: 'contactChannel',
scope: {
fields: ['id', 'name']
}
}, {
relation: 'province',
scope: {
fields: ['id', 'name']
}
}, {
relation: 'country',
scope: {
fields: ['id', 'country']
}
}, {
relation: 'payMethod',
scope: {
fields: ['id', 'name']
}
}, {
relation: 'account',
scope: {
fields: ['id', 'name', 'active']
}
}
]
};
Self.findOne(filter, function(err, card) {
if (err) return cb(err);
let formatedCard = JSON.parse(JSON.stringify(card));
if (formatedCard.salesPersonFk)
formatedCard.salesPerson = {
id: card.salesPerson().id,
name: `${card.salesPerson().firstName} ${card.salesPerson().name}`
};
cb(null, formatedCard);
});
};
};