Merge pull request 'zone delete' (#285) from 2244-zone_descriptor into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Bernat Exposito 2020-06-02 09:18:24 +00:00
commit fac64c45c0
9 changed files with 135 additions and 7 deletions

View File

@ -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;

View File

@ -42,6 +42,9 @@
},
"priority": {
"type": "Number"
},
"zoneFk": {
"type": "Number"
}
},
"relations": {

View File

@ -76,7 +76,7 @@
</vn-check>
</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.hb}}</vn-td>
<vn-td shrink>{{entry.freightValue | currency: 'EUR': 2}}</vn-td>

View File

@ -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;
}
};
};

View File

@ -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);
});
});

View File

@ -4,6 +4,7 @@ module.exports = Self => {
require('../methods/zone/getEvents')(Self);
require('../methods/zone/toggleIsIncluded')(Self);
require('../methods/zone/getUpcomingDeliveries')(Self);
require('../methods/zone/deleteZone')(Self);
Self.validatesPresenceOf('agencyModeFk', {
message: `Agency cannot be blank`

View File

@ -3,10 +3,16 @@
description="$ctrl.zone.name">
<slot-menu>
<vn-item class="vn-item"
ng-click="deleteZone.show()"
ng-click="$ctrl.onDelete()"
translate>
Delete
</vn-item>
<vn-item
ng-click="clone.show()"
name="cloneZone"
translate>
Clone
</vn-item>
</slot-menu>
<slot-body>
<div class="attributes">
@ -39,7 +45,12 @@
</vn-descriptor-content>
<vn-confirm
vn-id="deleteZone"
on-accept="$ctrl.onDeleteAccept()"
question="Are you sure you want to delete this zone?"
on-accept="$ctrl.deleteZone()"
message="This zone will be removed">
</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>

View File

@ -10,9 +10,33 @@ class Controller extends Descriptor {
this.entity = value;
}
onDeleteAccept() {
return this.$http.delete(`Zones/${this.id}`)
.then(() => this.$state.go('zone.index'));
onDelete() {
const $t = this.$translate.instant;
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}));
}
}

View File

@ -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