module.exports = Self => { Self.remoteMethod('getListItemNewer', { description: 'Get boolean if any or specific item of the shelving has older created in another shelving or parking', accessType: 'READ', accepts: [{ arg: 'shelvingFk', type: 'string', required: true, description: 'Shelving code' }, { arg: 'parking', type: 'string', required: true, description: 'Parking code' }, ], returns: { type: 'Array', root: true }, http: { path: `/getListItemNewer`, verb: 'GET' } }); Self.getListItemNewer = async(shelvingFk, parking, options) => { const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const [isParkingToReview] = await Self.rawSql(` SELECT COUNT(p.id) parkingToReview FROM vn.parking p JOIN vn.sector s ON s.id = p.sectorFk JOIN vn.productionConfig pc WHERE p.code = ? AND s.code = pc.sectorToCode;`, [parking], myOptions); if (isParkingToReview['parkingToReview'] < 1) return []; const result = await Self.rawSql(` WITH tItemShelving AS( SELECT is2.itemFk, is2.created, p.sectorFK, is2.id FROM vn.itemShelving is2 JOIN vn.shelving sh ON sh.code = is2.shelvingFk JOIN vn.parking p ON p.id = sh.parkingFk JOIN vn.sector s ON s.id = p.sectorFk JOIN vn.productionConfig pc WHERE is2.shelvingFk = ? AND s.code = pc.sectorFromCode ), tItemInSector AS ( SELECT is2.itemFk, is2.created, is2.shelvingFk FROM vn.itemShelving is2 JOIN vn.shelving sh ON sh.code = is2.shelvingFk JOIN vn.parking p ON p.id = sh.parkingFk JOIN vn.sector s ON s.id = p.sectorFk JOIN vn.productionConfig pc WHERE is2.shelvingFk <> ? AND s.code = pc.sectorFromCode) SELECT ti.itemFK, tis.shelvingFk FROM tItemShelving ti JOIN tItemInSector tis ON tis.itemFk = ti.itemFk JOIN vn.productionConfig pc WHERE ti.created + INTERVAL pc.itemOlderReviewHours HOUR < tis.created ;`, [shelvingFk, shelvingFk], myOptions); return result; }; };