56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('updateExclusionGeo', {
|
|
description: 'Update the geos excluded from a zone',
|
|
accepts: [
|
|
{
|
|
arg: 'zoneExclusionFk',
|
|
type: 'number',
|
|
description: 'The zoneExclusion id'
|
|
},
|
|
{
|
|
arg: 'geoIds',
|
|
type: ['number'],
|
|
description: 'The geos id'
|
|
}
|
|
|
|
],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/updateExclusionGeo`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.updateExclusionGeo = async(zoneExclusionFk, 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`);
|
|
|
|
await models.ZoneExclusionGeo.destroyAll({
|
|
zoneExclusionFk: zoneExclusionFk
|
|
}, myOptions);
|
|
|
|
const promises = [];
|
|
|
|
for (const geoId of geoIds) {
|
|
const params = {
|
|
zoneExclusionFk: zoneExclusionFk,
|
|
geoFk: geoId
|
|
};
|
|
const deletedZoneExclusionGeos = models.ZoneExclusionGeo.create(params, myOptions);
|
|
promises.push(deletedZoneExclusionGeos);
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
};
|
|
};
|