salix/modules/zone/back/methods/zone/getZoneClosing.js

61 lines
2.0 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('getZoneClosing', {
description: 'Get zone events filtered for date and prioritized by type',
accepts: [
{
arg: 'zoneIds',
type: ['number'],
description: 'The zone ids',
required: true
},
{
arg: 'date',
type: 'date',
description: 'The calendar date',
}
],
returns: {
type: 'object',
root: true
},
http: {
path: `/getZoneClosing`,
verb: 'POST'
}
});
Self.getZoneClosing = async(zoneIds, date, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
query = `
SELECT *
FROM (
SELECT
DISTINCT z.id,
z.name,
am.name agencyModeName,
IFNULL(ze.hour, z.hour) as hour,
IFNULL(ze.price, z.price) as price
FROM vn.zone z
JOIN vn.agencyMode am ON am.id = z.agencyModeFk
LEFT JOIN vn.zoneEvent ze ON ze.zoneFk = z.id
WHERE ((
ze.type = 'day'
AND ze.dated = ?
) OR (
ze.type != 'day'
AND ze.weekDays & (1 << WEEKDAY(?))
AND (ze.started IS NULL OR ? >= ze.started)
AND (ze.ended IS NULL OR ? <= ze.ended)
))
AND z.id IN (?)
ORDER BY type='day' DESC, type='range' DESC, type='indefinitely' DESC) z
GROUP BY z.id`;
return Self.rawSql(query, [date, date, date, date, zoneIds], myOptions);
};
};