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

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-06-13 12:33:22 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2022-07-08 07:47:11 +00:00
Self.remoteMethod('updateExclusionGeo', {
description: 'Update the geos excluded from a zone',
2022-06-13 12:33:22 +00:00
accepts: [
{
2022-06-17 06:18:23 +00:00
arg: 'zoneExclusionFk',
type: 'number',
description: 'The zoneExclusion id'
2022-06-13 12:33:22 +00:00
},
{
arg: 'geoIds',
type: ['number'],
2022-06-13 12:33:22 +00:00
description: 'The geos id'
}
],
returns: {
type: 'object',
root: true
},
http: {
2022-07-08 07:47:11 +00:00
path: `/updateExclusionGeo`,
2022-06-13 12:33:22 +00:00
verb: 'POST'
}
});
2022-07-08 07:47:11 +00:00
Self.updateExclusionGeo = async(zoneExclusionFk, geoIds, options) => {
2022-06-13 12:33:22 +00:00
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`);
2022-06-17 06:18:23 +00:00
await models.ZoneExclusionGeo.destroyAll({
zoneExclusionFk: zoneExclusionFk
}, myOptions);
const promises = [];
for (const geoId of geoIds) {
2022-06-13 12:33:22 +00:00
const params = {
2022-06-17 06:18:23 +00:00
zoneExclusionFk: zoneExclusionFk,
geoFk: geoId
2022-06-13 12:33:22 +00:00
};
2022-06-17 06:18:23 +00:00
const deletedZoneExclusionGeos = models.ZoneExclusionGeo.create(params, myOptions);
promises.push(deletedZoneExclusionGeos);
2022-06-13 12:33:22 +00:00
}
2022-06-17 06:18:23 +00:00
return Promise.all(promises);
2022-06-13 12:33:22 +00:00
};
};