Merge pull request 'zone delete' (#285) from 2244-zone_descriptor into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
commit
fac64c45c0
|
@ -0,0 +1,8 @@
|
||||||
|
ALTER TABLE `vn`.`route`
|
||||||
|
DROP FOREIGN KEY `fk_route_1`;
|
||||||
|
ALTER TABLE `vn`.`route`
|
||||||
|
ADD CONSTRAINT `fk_route_1`
|
||||||
|
FOREIGN KEY (`zoneFk`)
|
||||||
|
REFERENCES `vn`.`zone` (`id`)
|
||||||
|
ON DELETE SET NULL
|
||||||
|
ON UPDATE CASCADE;
|
|
@ -42,6 +42,9 @@
|
||||||
},
|
},
|
||||||
"priority": {
|
"priority": {
|
||||||
"type": "Number"
|
"type": "Number"
|
||||||
|
},
|
||||||
|
"zoneFk": {
|
||||||
|
"type": "Number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"relations": {
|
"relations": {
|
||||||
|
|
|
@ -76,7 +76,7 @@
|
||||||
</vn-check>
|
</vn-check>
|
||||||
</vn-td>
|
</vn-td>
|
||||||
<vn-td shrink>{{entry.id}} </vn-td>
|
<vn-td shrink>{{entry.id}} </vn-td>
|
||||||
<vn-td shrink>{{entry.supplierName}}</vn-td>
|
<vn-td expand>{{entry.supplierName}}</vn-td>
|
||||||
<vn-td shrink>{{entry.ref}}</vn-td>
|
<vn-td shrink>{{entry.ref}}</vn-td>
|
||||||
<vn-td shrink>{{entry.hb}}</vn-td>
|
<vn-td shrink>{{entry.hb}}</vn-td>
|
||||||
<vn-td shrink>{{entry.freightValue | currency: 'EUR': 2}}</vn-td>
|
<vn-td shrink>{{entry.freightValue | currency: 'EUR': 2}}</vn-td>
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethod('deleteZone', {
|
||||||
|
description: 'Delete a zone',
|
||||||
|
accessType: 'WRITE',
|
||||||
|
accepts: {
|
||||||
|
arg: 'id',
|
||||||
|
type: 'Number',
|
||||||
|
description: 'The zone id',
|
||||||
|
http: {source: 'path'}
|
||||||
|
},
|
||||||
|
returns: {
|
||||||
|
type: 'Object',
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/:id/deleteZone`,
|
||||||
|
verb: 'POST'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.deleteZone = async id => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const tx = await Self.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const filter = {where: {zoneFk: id}};
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
|
const ticketList = await models.Ticket.find(filter, options);
|
||||||
|
ticketList.forEach(ticket => {
|
||||||
|
promises.push(ticket.updateAttributes({zoneFk: null}, options));
|
||||||
|
});
|
||||||
|
await Promise.all(promises);
|
||||||
|
await models.Zone.destroyById(id, options);
|
||||||
|
await tx.commit();
|
||||||
|
|
||||||
|
return id;
|
||||||
|
} catch (err) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
|
@ -0,0 +1,33 @@
|
||||||
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
|
describe('zone deletezone()', () => {
|
||||||
|
let zoneId = 1;
|
||||||
|
let originalZoneTickets;
|
||||||
|
let originalZone;
|
||||||
|
|
||||||
|
beforeAll(async done => {
|
||||||
|
originalZone = await app.models.Zone.findById(zoneId);
|
||||||
|
originalZoneTickets = await app.models.Ticket.find({where: {zoneFk: zoneId}});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async done => {
|
||||||
|
await originalZone.save();
|
||||||
|
|
||||||
|
originalZoneTickets.forEach(async ticket => {
|
||||||
|
await ticket.updateAttributes({zoneFk: zoneId});
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete a zone and update their tickets', async() => {
|
||||||
|
await app.models.Zone.deleteZone(zoneId);
|
||||||
|
|
||||||
|
let updatedZone = await app.models.Zone.findById(zoneId);
|
||||||
|
let zoneUpdatedTicket = await app.models.Ticket.findById(originalZoneTickets[0].id);
|
||||||
|
|
||||||
|
expect(updatedZone).toBeNull();
|
||||||
|
expect(zoneUpdatedTicket.zoneFk).not.toBe(zoneId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -4,6 +4,7 @@ module.exports = Self => {
|
||||||
require('../methods/zone/getEvents')(Self);
|
require('../methods/zone/getEvents')(Self);
|
||||||
require('../methods/zone/toggleIsIncluded')(Self);
|
require('../methods/zone/toggleIsIncluded')(Self);
|
||||||
require('../methods/zone/getUpcomingDeliveries')(Self);
|
require('../methods/zone/getUpcomingDeliveries')(Self);
|
||||||
|
require('../methods/zone/deleteZone')(Self);
|
||||||
|
|
||||||
Self.validatesPresenceOf('agencyModeFk', {
|
Self.validatesPresenceOf('agencyModeFk', {
|
||||||
message: `Agency cannot be blank`
|
message: `Agency cannot be blank`
|
||||||
|
|
|
@ -3,10 +3,16 @@
|
||||||
description="$ctrl.zone.name">
|
description="$ctrl.zone.name">
|
||||||
<slot-menu>
|
<slot-menu>
|
||||||
<vn-item class="vn-item"
|
<vn-item class="vn-item"
|
||||||
ng-click="deleteZone.show()"
|
ng-click="$ctrl.onDelete()"
|
||||||
translate>
|
translate>
|
||||||
Delete
|
Delete
|
||||||
</vn-item>
|
</vn-item>
|
||||||
|
<vn-item
|
||||||
|
ng-click="clone.show()"
|
||||||
|
name="cloneZone"
|
||||||
|
translate>
|
||||||
|
Clone
|
||||||
|
</vn-item>
|
||||||
</slot-menu>
|
</slot-menu>
|
||||||
<slot-body>
|
<slot-body>
|
||||||
<div class="attributes">
|
<div class="attributes">
|
||||||
|
@ -39,7 +45,12 @@
|
||||||
</vn-descriptor-content>
|
</vn-descriptor-content>
|
||||||
<vn-confirm
|
<vn-confirm
|
||||||
vn-id="deleteZone"
|
vn-id="deleteZone"
|
||||||
on-accept="$ctrl.onDeleteAccept()"
|
on-accept="$ctrl.deleteZone()"
|
||||||
question="Are you sure you want to delete this zone?"
|
|
||||||
message="This zone will be removed">
|
message="This zone will be removed">
|
||||||
</vn-confirm>
|
</vn-confirm>
|
||||||
|
<vn-confirm
|
||||||
|
vn-id="clone"
|
||||||
|
on-accept="$ctrl.onCloneAccept()"
|
||||||
|
question="Do you want to clone this zone?"
|
||||||
|
message="All it's properties will be copied">
|
||||||
|
</vn-confirm>
|
|
@ -10,9 +10,33 @@ class Controller extends Descriptor {
|
||||||
this.entity = value;
|
this.entity = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
onDeleteAccept() {
|
onDelete() {
|
||||||
return this.$http.delete(`Zones/${this.id}`)
|
const $t = this.$translate.instant;
|
||||||
.then(() => this.$state.go('zone.index'));
|
const today = new Date();
|
||||||
|
today.setHours(0, 0, 0, 0);
|
||||||
|
const filter = {where: {zoneFk: this.id, shipped: {gte: today}}};
|
||||||
|
this.$http.get(`Tickets`, {filter}).then(res => {
|
||||||
|
const ticketsAmount = res.data.length;
|
||||||
|
if (ticketsAmount) {
|
||||||
|
const params = {ticketsAmount};
|
||||||
|
console.log('ticketsAmount', res.data);
|
||||||
|
const question = $t('This zone contains tickets', params, null, null, 'sanitizeParameters');
|
||||||
|
this.$.deleteZone.question = question;
|
||||||
|
this.$.deleteZone.show();
|
||||||
|
} else
|
||||||
|
this.deleteZone();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteZone() {
|
||||||
|
return this.$http.post(`Zones/${this.id}/deleteZone`).then(() => {
|
||||||
|
this.$state.go('zone.index');
|
||||||
|
this.vnApp.showSuccess(this.$t('Zone deleted'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
onCloneAccept() {
|
||||||
|
return this.$http.post(`Zones/${this.id}/clone`).
|
||||||
|
then(res => this.$state.go('zone.card.basicData', {id: res.data.id}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
This zone contains tickets: Esta zona contiene {{ticketsAmount}} tickets. ¿Seguro que quieres eliminar esta zona?
|
||||||
|
Do you want to clone this zone?: ¿Quieres clonar esta zona?
|
||||||
|
All it's properties will be copied: Todas sus propiedades serán copiadas
|
||||||
|
Zone deleted: Zona eliminada
|
Loading…
Reference in New Issue