From 650e5b86264bb460757a705ab7aad4b2bd7232c9 Mon Sep 17 00:00:00 2001 From: Gerard Date: Wed, 27 Mar 2019 08:56:25 +0100 Subject: [PATCH] #1257 route.log --- db/install/changes/00-routeLog.sql | 21 +++++++++ modules/route/back/model-config.json | 3 ++ modules/route/back/models/route-log.json | 58 ++++++++++++++++++++++++ modules/route/back/models/route.json | 5 +- modules/route/front/index.js | 1 + modules/route/front/log/index.html | 9 ++++ modules/route/front/log/index.js | 53 ++++++++++++++++++++++ modules/route/front/log/index.spec.js | 37 +++++++++++++++ modules/route/front/routes.json | 12 ++++- modules/ticket/back/models/ticket.json | 5 +- 10 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 db/install/changes/00-routeLog.sql create mode 100644 modules/route/back/models/route-log.json create mode 100644 modules/route/front/log/index.html create mode 100644 modules/route/front/log/index.js create mode 100644 modules/route/front/log/index.spec.js diff --git a/db/install/changes/00-routeLog.sql b/db/install/changes/00-routeLog.sql new file mode 100644 index 000000000..b6430fcb6 --- /dev/null +++ b/db/install/changes/00-routeLog.sql @@ -0,0 +1,21 @@ +DROP TABLE IF EXISTS `vn`.`routeLog`; + +CREATE TABLE `vn`.`routeLog` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `originFk` int(10) unsigned NOT NULL, + `userFk` int(10) unsigned DEFAULT NULL, + `action` set('insert','update','delete') COLLATE utf8_unicode_ci NOT NULL, + `creationDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `description` text CHARACTER SET utf8, + `changedModel` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, + `oldInstance` text COLLATE utf8_unicode_ci, + `newInstance` text COLLATE utf8_unicode_ci, + `changedModelId` int(11) DEFAULT NULL, + `changedModelValue` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `originFk` (`originFk`), + KEY `userFk` (`userFk`), + CONSTRAINT `routeLog_ibfk_1` FOREIGN KEY (`originFk`) REFERENCES `vn2008`.`Rutas` (`Id_Ruta`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `routeLog_ibfk_2` FOREIGN KEY (`userFk`) REFERENCES `account`.`user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + diff --git a/modules/route/back/model-config.json b/modules/route/back/model-config.json index 6e667134a..e326c57df 100644 --- a/modules/route/back/model-config.json +++ b/modules/route/back/model-config.json @@ -4,5 +4,8 @@ }, "Vehicle": { "dataSource": "vn" + }, + "RouteLog": { + "dataSource": "vn" } } diff --git a/modules/route/back/models/route-log.json b/modules/route/back/models/route-log.json new file mode 100644 index 000000000..abaeeb722 --- /dev/null +++ b/modules/route/back/models/route-log.json @@ -0,0 +1,58 @@ +{ + "name": "RouteLog", + "base": "VnModel", + "options": { + "mysql": { + "table": "routeLog" + } + }, + "properties": { + "id": { + "id": true, + "type": "Number", + "forceId": false + }, + "originFk": { + "type": "Number", + "required": true + }, + "userFk": { + "type": "Number" + }, + "action": { + "type": "String", + "required": true + }, + "changedModel": { + "type": "String" + }, + "oldInstance": { + "type": "Object" + }, + "newInstance": { + "type": "Object" + }, + "creationDate": { + "type": "Date" + }, + "changedModelId": { + "type": "Number" + }, + "changedModelValue": { + "type": "String" + }, + "description": { + "type": "String" + } + }, + "relations": { + "user": { + "type": "belongsTo", + "model": "Account", + "foreignKey": "userFk" + } + }, + "scope": { + "order": ["creationDate DESC", "id DESC"] + } +} diff --git a/modules/route/back/models/route.json b/modules/route/back/models/route.json index 8930d314c..f3e4dc946 100644 --- a/modules/route/back/models/route.json +++ b/modules/route/back/models/route.json @@ -1,6 +1,9 @@ { "name": "Route", - "base": "VnModel", + "base": "Loggable", + "log": { + "model":"RouteLog" + }, "options": { "mysql": { "table": "route" diff --git a/modules/route/front/index.js b/modules/route/front/index.js index 33df6dfca..af8a0b69b 100644 --- a/modules/route/front/index.js +++ b/modules/route/front/index.js @@ -7,3 +7,4 @@ import './summary'; import './card'; import './create'; import './basic-data'; +import './log'; diff --git a/modules/route/front/log/index.html b/modules/route/front/log/index.html new file mode 100644 index 000000000..925ea1d14 --- /dev/null +++ b/modules/route/front/log/index.html @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/modules/route/front/log/index.js b/modules/route/front/log/index.js new file mode 100644 index 000000000..85358bd72 --- /dev/null +++ b/modules/route/front/log/index.js @@ -0,0 +1,53 @@ +import ngModule from '../module'; + +class Controller { + constructor($scope, $stateParams) { + this.$scope = $scope; + this.$stateParams = $stateParams; + this.filter = { + include: [{ + relation: 'user', + scope: { + fields: ['name'], + }, + }], + }; + } + + get logs() { + return this._logs; + } + + set logs(value) { + this._logs = value; + + if (this.logs) { + this.logs.forEach(log => { + log.oldProperties = this.getInstance(log.oldInstance); + log.newProperties = this.getInstance(log.newInstance); + }); + } + } + + getInstance(instance) { + let validDate = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z)?$/; + const properties = []; + if (typeof instance == 'object' && instance != null) { + Object.keys(instance).forEach(property => { + if (validDate.test(instance[property])) + instance[property] = new Date(instance[property]).toLocaleString('es-ES'); + + properties.push({key: property, value: instance[property]}); + }); + return properties; + } + return null; + } +} + +Controller.$inject = ['$scope', '$stateParams']; + +ngModule.component('vnRouteLog', { + template: require('./index.html'), + controller: Controller, +}); diff --git a/modules/route/front/log/index.spec.js b/modules/route/front/log/index.spec.js new file mode 100644 index 000000000..f8b6f8272 --- /dev/null +++ b/modules/route/front/log/index.spec.js @@ -0,0 +1,37 @@ +import './index'; + +describe('Route', () => { + describe('Component vnRouteLog', () => { + let $componentController; + let $scope; + let controller; + + beforeEach(ngModule('route')); + + beforeEach(angular.mock.inject((_$componentController_, $rootScope) => { + $componentController = _$componentController_; + $scope = $rootScope.$new(); + controller = $componentController('vnRouteLog', {$scope: $scope}); + controller.$scope.model = {data: [{newInstance: {id: 1}, oldInstance: {id: 2}}]}; + })); + + describe('logs setter', () => { + it('should call the function getInstance() twice', () => { + spyOn(controller, 'getInstance'); + controller.logs = [{newInstance: {id: 1}, oldInstance: {id: 2}}]; + + expect(controller.getInstance.calls.count()).toBe(2); + expect(controller.getInstance).toHaveBeenCalledWith({id: 1}); + expect(controller.getInstance).toHaveBeenCalledWith({id: 2}); + }); + }); + + describe('getInstance(instance)', () => { + it('should transform the object given in to an array', () => { + let newInstance = controller.getInstance(controller.$scope.model.data[0].newInstance); + + expect(newInstance).toEqual([{key: 'id', value: 1}]); + }); + }); + }); +}); diff --git a/modules/route/front/routes.json b/modules/route/front/routes.json index 521ccd9be..9aae910e3 100644 --- a/modules/route/front/routes.json +++ b/modules/route/front/routes.json @@ -5,7 +5,9 @@ "validations" : true, "dependencies": ["client", "worker", "ticket"], "menu": [ - {"state": "route.card.basicData", "icon": "settings"}], + {"state": "route.card.basicData", "icon": "settings"}, + {"state": "route.card.log", "icon": "history"} + ], "routes": [ { "url": "/route", @@ -51,6 +53,12 @@ "route": "$ctrl.route" }, "acl": ["delivery"] - } + }, { + "url" : "/log", + "state": "route.card.log", + "component": "vn-route-log", + "description": "Log", + "acl": ["delivery"] + } ] } \ No newline at end of file diff --git a/modules/ticket/back/models/ticket.json b/modules/ticket/back/models/ticket.json index c60b98364..6c88e370b 100644 --- a/modules/ticket/back/models/ticket.json +++ b/modules/ticket/back/models/ticket.json @@ -1,6 +1,9 @@ { "name": "Ticket", - "base": "VnModel", + "base": "Loggable", + "log": { + "model":"TicketLog" + }, "options": { "mysql": { "table": "ticket"