salix/modules/item/back/methods/item-shelving/hasItemOlder.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

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