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

51 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('toggleIsIncluded', {
description: 'Toggle include to delivery',
accepts: [{
arg: 'id',
type: 'Number',
description: 'The zone id',
http: {source: 'path'},
required: true
},
{
arg: 'geoId',
type: 'Number',
required: true
},
{
arg: 'isIncluded',
type: 'Boolean'
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/toggleIsIncluded`,
verb: 'POST'
}
});
Self.toggleIsIncluded = async(id, geoId, isIncluded, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (isIncluded === undefined)
return models.ZoneIncluded.destroyAll({zoneFk: id, geoFk: geoId}, myOptions);
const zoneIncluded = await models.ZoneIncluded.findOne({where: {zoneFk: id, geoFk: geoId}}, myOptions);
if (zoneIncluded)
return zoneIncluded.updateAttribute('isIncluded', isIncluded, myOptions);
return models.ZoneIncluded.create({
zoneFk: id,
geoFk: geoId,
isIncluded: isIncluded
}, myOptions);
};
};