This commit is contained in:
Gerard 2019-03-27 08:56:25 +01:00
parent b24a6a6255
commit 650e5b8626
10 changed files with 200 additions and 4 deletions

View File

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

View File

@ -4,5 +4,8 @@
}, },
"Vehicle": { "Vehicle": {
"dataSource": "vn" "dataSource": "vn"
},
"RouteLog": {
"dataSource": "vn"
} }
} }

View File

@ -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"]
}
}

View File

@ -1,6 +1,9 @@
{ {
"name": "Route", "name": "Route",
"base": "VnModel", "base": "Loggable",
"log": {
"model":"RouteLog"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "route" "table": "route"

View File

@ -7,3 +7,4 @@ import './summary';
import './card'; import './card';
import './create'; import './create';
import './basic-data'; import './basic-data';
import './log';

View File

@ -0,0 +1,9 @@
<vn-crud-model
vn-id="model"
url="/api/RouteLogs"
link="{originFk: $ctrl.$stateParams.id}"
filter="$ctrl.filter"
limit="20"
data="$ctrl.logs" auto-load="false">
</vn-crud-model>
<vn-log model="model"></vn-log>

View File

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

View File

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

View File

@ -5,7 +5,9 @@
"validations" : true, "validations" : true,
"dependencies": ["client", "worker", "ticket"], "dependencies": ["client", "worker", "ticket"],
"menu": [ "menu": [
{"state": "route.card.basicData", "icon": "settings"}], {"state": "route.card.basicData", "icon": "settings"},
{"state": "route.card.log", "icon": "history"}
],
"routes": [ "routes": [
{ {
"url": "/route", "url": "/route",
@ -51,6 +53,12 @@
"route": "$ctrl.route" "route": "$ctrl.route"
}, },
"acl": ["delivery"] "acl": ["delivery"]
}, {
"url" : "/log",
"state": "route.card.log",
"component": "vn-route-log",
"description": "Log",
"acl": ["delivery"]
} }
] ]
} }

View File

@ -1,6 +1,9 @@
{ {
"name": "Ticket", "name": "Ticket",
"base": "VnModel", "base": "Loggable",
"log": {
"model":"TicketLog"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "ticket" "table": "ticket"