module.exports = function(Self) { Self.remoteMethodCtx('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(ctx, id, options) => { const myOptions = {userId: ctx.req.accessToken.userId}; 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', 'name'] } }, { relation: 'payMethod', scope: { fields: ['id', 'name'] } }, { relation: 'businessType', scope: { fields: ['description'] } }, { relation: 'account', scope: { fields: ['id', 'name', 'email', 'active'] } }, { relation: 'supplier', scope: { fields: ['id', 'nif'] } } ] }, myOptions); const date = Date.vnNew(); date.setHours(0, 0, 0, 0); const query = `SELECT vn.client_getDebt(?, ?) AS debt`; const data = await Self.rawSql(query, [id, date], myOptions); client.debt = data[0].debt; client.unpaid = await Self.app.models.ClientUnpaid.findById(id, null, myOptions); return client; }; };