module.exports = Self => { Self.remoteMethod('getStockBought', { description: 'Returns the stock bought for a given date', accessType: 'READ', accepts: [{ arg: 'workerFk', type: 'number', description: 'The id for a buyer', }, { arg: 'dated', type: 'date', description: 'The date to filter', } ], returns: { type: ['object'], root: true }, http: { path: `/getStockBought`, verb: 'GET' } }); Self.getStockBought = async(workerFk, dated = Date.vnNew()) => { const models = Self.app.models; const today = Date.vnNew(); dated.setHours(0, 0, 0, 0); today.setHours(0, 0, 0, 0); await models.StockBought.rawSql(`CALL vn.stockBought_calculate(?)`, [dated]); const filter = { where: {dated}, include: [ { fields: ['workerFk', 'reserve', 'bought'], relation: 'worker', scope: { include: [ { relation: 'user', scope: { fields: ['id', 'name'] } } ] } } ] }; if (workerFk) filter.where.workerFk = workerFk; return models.StockBought.find(filter); }; };