const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('hasItemOlder', { description: 'Get boolean if any or specific item of the shelving has older created in another shelving or parking', accessType: 'READ', accepts: [{ arg: 'shelvingFkIn', type: 'string', required: true, description: 'Shelving code' }, { arg: 'parking', type: 'string', description: 'Parking code' }, { arg: 'shelvingFkOut', type: 'string', description: 'Shelving code' }, { arg: 'itemFk', type: 'integer', description: 'Item id' }], returns: { type: 'boolean', root: true }, http: { path: `/hasItemOlder`, verb: 'GET' } }); Self.hasItemOlder = async(shelvingFkIn, parking, shelvingFkOut, itemFk, options) => { if (!parking && !shelvingFkOut) throw new UserError('Missing data: parking or shelving'); const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const result = await Self.rawSql(` SELECT COUNT(ish.id) countItemOlder FROM vn.itemShelving ish JOIN ( SELECT ish.itemFk, created,shelvingFk FROM vn.itemShelving ish JOIN vn.shelving s ON ish.shelvingFk = s.code WHERE ish.shelvingFk = ? )sub ON sub.itemFK = ish.itemFk JOIN vn.shelving s ON s.code = ish.shelvingFk JOIN vn.parking p ON p.id = s.parkingFk WHERE sub.created > ish.created AND (p.code <> ? OR ? IS NULL) AND (ish.shelvingFk <> ? OR ? IS NULL) AND (ish.itemFk <> ? OR ? IS NULL)`, [shelvingFkIn, parking, parking, shelvingFkOut, shelvingFkOut, itemFk, itemFk], myOptions); return result[0]['countItemOlder'] > 0; }; };