From 8ede30398b1577814a2d292ceb5779c5c13baba5 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Thu, 24 Dec 2020 11:31:20 +0100 Subject: [PATCH] clone feature for routes index --- modules/route/back/methods/route/clone.js | 56 +++++++++++++++++++ .../back/methods/route/specs/clone.spec.js | 31 ++++++++++ modules/route/back/models/route.js | 1 + modules/route/front/basic-data/locale/es.yml | 2 + modules/route/front/index/index.html | 36 ++++++++++-- modules/route/front/index/index.js | 16 ++++++ modules/route/front/index/index.spec.js | 10 ++++ modules/route/front/index/locale/es.yml | 5 +- 8 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 modules/route/back/methods/route/clone.js create mode 100644 modules/route/back/methods/route/specs/clone.spec.js diff --git a/modules/route/back/methods/route/clone.js b/modules/route/back/methods/route/clone.js new file mode 100644 index 000000000..ece232ccf --- /dev/null +++ b/modules/route/back/methods/route/clone.js @@ -0,0 +1,56 @@ +module.exports = Self => { + Self.remoteMethod('clone', { + description: 'Clones the selected routes', + accessType: 'WRITE', + accepts: [ + { + arg: 'ids', + type: ['number'], + required: true, + description: 'The routes ids to clone' + }, + { + arg: 'started', + type: 'date', + required: true, + description: 'The started date for all routes' + } + ], + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/clone`, + verb: 'POST' + } + }); + + Self.clone = async(ids, started) => { + const tx = await Self.beginTransaction({}); + try { + const options = {transaction: tx}; + const originalRoutes = await Self.find({ + where: {id: {inq: ids}}, + fields: ['workerFk', 'agencyModeFk', 'vehicleFk', 'description'] + }, options); + + if (ids.length != originalRoutes.length) + throw new Error(`The amount of routes found don't match`); + + const routes = originalRoutes.map(route => { + route.started = started; + route.created = new Date(); + return route; + }); + + const clones = await Self.create(routes, options); + + await tx.commit(); + return clones; + } catch (e) { + await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/route/back/methods/route/specs/clone.spec.js b/modules/route/back/methods/route/specs/clone.spec.js new file mode 100644 index 000000000..c0f5f04f1 --- /dev/null +++ b/modules/route/back/methods/route/specs/clone.spec.js @@ -0,0 +1,31 @@ +const app = require('vn-loopback/server/server'); + +describe('route clone()', () => { + const startDate = new Date(); + it('should throw an error if the amount of ids pased to the clone function do no match the database', async() => { + const ids = [996, 997, 998, 999]; + + let error; + + try { + await app.models.Route.clone(ids, startDate); + } catch (e) { + error = e; + } + + expect(error).toBeDefined(); + expect(error.message).toEqual(`The amount of routes found don't match`); + }); + + it('should clone two routes', async() => { + const ids = [1, 2]; + + const clones = await app.models.Route.clone(ids, startDate); + + expect(clones.length).toEqual(2); + + // restores + for (const clone of clones) + await app.models.Route.destroyById(clone.id); + }); +}); diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index 6320a888c..4423131bf 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -6,6 +6,7 @@ module.exports = Self => { require('../methods/route/updateVolume')(Self); require('../methods/route/getDeliveryPoint')(Self); require('../methods/route/insertTicket')(Self); + require('../methods/route/clone')(Self); Self.validate('kmStart', validateDistance, { message: 'Distance must be lesser than 1000' diff --git a/modules/route/front/basic-data/locale/es.yml b/modules/route/front/basic-data/locale/es.yml index f0414b5b1..a98f20215 100644 --- a/modules/route/front/basic-data/locale/es.yml +++ b/modules/route/front/basic-data/locale/es.yml @@ -3,3 +3,5 @@ Date started: Fecha inicio Km start: Km de inicio Km end: Km de fin Description: Descripción +Hour started: Hora inicio +Hour finished: Hora fin diff --git a/modules/route/front/index/index.html b/modules/route/front/index/index.html index d71bcbc48..7bbadefe4 100644 --- a/modules/route/front/index/index.html +++ b/modules/route/front/index/index.html @@ -44,7 +44,7 @@ {{::route.agencyName | dashIfEmpty}} {{::route.vehiclePlateNumber | dashIfEmpty}} - {{::route.created | date:'dd/MM/yyyy' | dashIfEmpty}} + {{::route.created | dashIfEmpty | date:'dd/MM/yyyy'}} {{::route.m3 | dashIfEmpty}} {{::route.description | dashIfEmpty}} @@ -59,19 +59,26 @@ + + - -
+ + - -
\ No newline at end of file + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/route/front/index/index.js b/modules/route/front/index/index.js index 4400bdbb1..2dc767f4c 100644 --- a/modules/route/front/index/index.js +++ b/modules/route/front/index/index.js @@ -40,6 +40,22 @@ export default class Controller extends Section { }); } + openClonationDialog() { + this.startedDate = new Date(); + this.$.clonationDialog.show(); + } + + cloneSelectedRoutes() { + const routesIds = []; + for (let route of this.checked) + routesIds.push(route.id); + + return this.$http.post('Routes/clone', {ids: routesIds, started: this.startedDate}).then(() => { + this.$.model.refresh(); + this.vnApp.showSuccess(this.$t('Data saved!')); + }); + } + onDrop($event) { const target = $event.target; const droppable = target.closest(this.droppableElement); diff --git a/modules/route/front/index/index.spec.js b/modules/route/front/index/index.spec.js index b66ecaf00..8346f924c 100644 --- a/modules/route/front/index/index.spec.js +++ b/modules/route/front/index/index.spec.js @@ -60,6 +60,16 @@ describe('Component vnRouteIndex', () => { }); }); + describe('cloneSelectedRoutes()', () => { + it('should perform an http request to Routes/clone', () => { + controller.startedDate = new Date(); + + $httpBackend.expect('POST', 'Routes/clone').respond(); + controller.cloneSelectedRoutes(); + $httpBackend.flush(); + }); + }); + describe('onDrop()', () => { it('should call the insert method when dragging a ticket number', () => { jest.spyOn(controller, 'insert'); diff --git a/modules/route/front/index/locale/es.yml b/modules/route/front/index/locale/es.yml index 0c09b21ee..40cd5f2b5 100644 --- a/modules/route/front/index/locale/es.yml +++ b/modules/route/front/index/locale/es.yml @@ -1,2 +1,5 @@ Vehicle: Vehículo -Download selected routes as PDF: Descargar rutas seleccionadas como PDF \ No newline at end of file +Download selected routes as PDF: Descargar rutas seleccionadas como PDF +Clone selected routes: Clonar rutas seleccionadas +Select the starting date: Seleccione fecha de inicio +Starting date: Fecha de inicio \ No newline at end of file