2019-01-21 10:45:53 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('toggleIsIncluded', {
|
|
|
|
description: 'Toggle include to delivery',
|
|
|
|
accessType: '',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'zoneFk',
|
|
|
|
type: 'Number',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'geoFk',
|
|
|
|
type: 'Number',
|
|
|
|
required: true,
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/toggleIsIncluded`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.toggleIsIncluded = async(zoneFk, geoFk) => {
|
2019-01-28 11:07:46 +00:00
|
|
|
const models = Self.app.models;
|
2019-01-29 14:04:38 +00:00
|
|
|
const geo = await models.ZoneGeo.findById(geoFk);
|
2019-01-21 10:45:53 +00:00
|
|
|
const isIncluded = await Self.findOne({
|
|
|
|
where: {zoneFk, geoFk}
|
|
|
|
});
|
|
|
|
|
2019-01-28 11:07:46 +00:00
|
|
|
const hasCheckedParents = await Self.rawSql(
|
|
|
|
`SELECT id
|
2019-01-29 14:04:38 +00:00
|
|
|
FROM vn.zoneGeo zt
|
2019-01-28 11:07:46 +00:00
|
|
|
JOIN vn.zoneIncluded zi ON zi.geoFk = zt.id
|
|
|
|
WHERE zt.lft < ? AND zt.rgt > ?`, [geo.lft, geo.rgt]
|
|
|
|
);
|
|
|
|
const hasCheckedChilds = await Self.rawSql(
|
|
|
|
`SELECT id
|
2019-01-29 14:04:38 +00:00
|
|
|
FROM vn.zoneGeo zt
|
2019-01-28 11:07:46 +00:00
|
|
|
JOIN vn.zoneIncluded zi ON zi.geoFk = zt.id
|
|
|
|
WHERE zt.lft > ? AND zt.rgt < ?`, [geo.lft, geo.rgt]
|
|
|
|
);
|
|
|
|
|
|
|
|
const isExcluded = hasCheckedParents.length || hasCheckedChilds.length;
|
|
|
|
|
2019-01-21 10:45:53 +00:00
|
|
|
if (isIncluded)
|
|
|
|
return Self.destroyAll({zoneFk, geoFk});
|
|
|
|
else
|
2019-01-28 11:07:46 +00:00
|
|
|
return Self.upsert({zoneFk, geoFk, isIncluded: isExcluded});
|
2019-01-21 10:45:53 +00:00
|
|
|
};
|
|
|
|
};
|