refactor: create endpoint
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-06-13 14:33:22 +02:00
parent dd65f4a468
commit c26b09ead3
4 changed files with 80 additions and 32 deletions

View File

@ -0,0 +1,65 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('editExclusionGeo', {
description: 'Edit the geos excluded from a zone',
accepts: [
{
arg: 'exclusionsGeo',
type: 'any',
description: 'The exclusions by geo'
},
{
arg: 'geoIds',
type: 'any',
description: 'The geos id'
}
],
returns: {
type: 'object',
root: true
},
http: {
path: `/editExclusionGeo`,
verb: 'POST'
}
});
Self.editExclusionGeo = async(exclusionsGeo, 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`);
let actualPosition = 0;
let geoId;
for (let exclusionGeo of exclusionsGeo) {
if (geoIds[actualPosition]) geoId = geoIds[actualPosition].id;
else geoId = null;
if (geoId && geoId == exclusionGeo.geoFk) {
const params = {
id: exclusionGeo.id,
zoneExclusionFk: exclusionGeo.zoneExclusionFk,
geoFk: geoId
};
await models.ZoneExclusionGeo.upsert(params, myOptions);
actualPosition++;
} else
await models.ZoneExclusionGeo.destroyById(exclusionGeo.id, myOptions);
}
while (actualPosition <= geoIds.length - 1) {
const params = {
zoneExclusionFk: exclusionsGeo[0].zoneExclusionFk,
geoFk: geoIds[actualPosition].id
};
await models.ZoneExclusionGeo.create(params, myOptions);
actualPosition++;
}
return true;
};
};

View File

@ -3,7 +3,7 @@ const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('exclusionGeo', {
description: 'Exclude a zoneGeo from a zone',
description: 'Exclude a geo from a zone',
accepts: [
{
arg: 'zoneFk',

View File

@ -9,6 +9,7 @@ module.exports = Self => {
require('../methods/zone/includingExpired')(Self);
require('../methods/zone/getZoneClosing')(Self);
require('../methods/zone/exclusionGeo')(Self);
require('../methods/zone/editExclusionGeo')(Self);
Self.validatesPresenceOf('agencyModeFk', {
message: `Agency cannot be blank`

View File

@ -227,44 +227,26 @@ class Controller extends Section {
const excludeSelected = this.excludeSelected;
let req;
const geoIds = [];
for (let zoneGeo of this.checked) {
geoIds.push({
id: zoneGeo.id
});
}
const params = {
zoneFk: parseInt(this.$params.id),
date: excludeSelected.dated,
geoIds: geoIds
};
if (this.isNew)
if (this.isNew) {
const params = {
zoneFk: parseInt(this.$params.id),
date: excludeSelected.dated,
geoIds: geoIds
};
req = this.$http.post(`Zones/exclusionGeo`, params);
else {
let actualPosition = 0;
let geoId;
for (let exclusionGeo of this.exclusionsGeo) {
if (geoIds[actualPosition]) geoId = geoIds[actualPosition].id;
else geoId = null;
if (geoId && geoId == exclusionGeo.geoFk) {
const params = {
zoneExclusionFk: exclusionGeo.zoneExclusionFk,
geoFk: geoId
};
req = this.$http.put(`ZoneExclusionGeos/${exclusionGeo.id}`, params);
actualPosition++;
} else
req = this.$http.delete(`ZoneExclusionGeos/${exclusionGeo.id}`);
}
while (actualPosition <= geoIds.length - 1) {
const params = {
zoneExclusionFk: excludeSelected.zoneExclusionFk,
geoFk: geoIds[actualPosition].id
};
req = this.$http.post(`ZoneExclusionGeos`, params);
actualPosition++;
}
} else {
const params = {
exclusionsGeo: this.exclusionsGeo,
geoIds: geoIds
};
req = this.$http.post(`Zones/editExclusionGeo`, params);
}
return req.then(() => {
this.refresh();