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

65 lines
1.6 KiB
JavaScript

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