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

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-05-16 05:17:29 +00:00
2022-06-09 12:59:17 +00:00
const UserError = require('vn-loopback/util/user-error');
2022-05-16 05:17:29 +00:00
module.exports = Self => {
Self.remoteMethod('exclusionGeo', {
2022-06-13 12:33:22 +00:00
description: 'Exclude a geo from a zone',
2022-05-16 05:17:29 +00:00
accepts: [
{
arg: 'zoneFk',
type: 'number',
description: 'The zone id'
},
{
arg: 'date',
type: 'date',
description: 'The date to exclude'
},
{
arg: 'geoIds',
type: ['number'],
2022-05-16 05:17:29 +00:00
description: 'The geos id'
}
],
returns: {
type: 'object',
root: true
},
http: {
path: `/exclusionGeo`,
verb: 'POST'
}
});
2022-06-09 12:59:17 +00:00
Self.exclusionGeo = async(zoneFk, date, geoIds, options) => {
2022-05-16 05:17:29 +00:00
const models = Self.app.models;
2022-06-09 12:59:17 +00:00
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2022-06-10 06:15:54 +00:00
if (!geoIds[0]) throw new UserError(`You must select a location`);
2022-05-16 05:17:29 +00:00
const newZoneExclusion = await models.ZoneExclusion.create({
zoneFk: zoneFk,
dated: date
2022-06-09 12:59:17 +00:00
}, myOptions);
const promises = [];
2022-05-16 05:17:29 +00:00
for (const geoId of geoIds) {
2022-06-09 12:59:17 +00:00
const newZoneExclusionGeo = await models.ZoneExclusionGeo.create({
2022-05-16 05:17:29 +00:00
zoneExclusionFk: newZoneExclusion.id,
geoFk: geoId
2022-06-09 12:59:17 +00:00
}, myOptions);
promises.push(newZoneExclusionGeo);
2022-05-16 05:17:29 +00:00
}
2022-06-09 12:59:17 +00:00
const newZoneExclusionGeos = await Promise.all(promises);
return newZoneExclusionGeos;
2022-05-16 05:17:29 +00:00
};
};