WIP: feat: refs #7119 add VehicleState model with data source configuration #3328

Draft
jorgep wants to merge 20 commits from 7119-createVehicle into dev
10 changed files with 386 additions and 20 deletions

View File

@ -849,14 +849,14 @@ INSERT INTO `vn`.`deliveryPoint` (`id`, `name`, `ubication`)
VALUES
(1, 'Gotham','1007 Mountain Drive, Gotham');
INSERT INTO `vn`.`vehicle`(`id`, `numberPlate`, `tradeMark`, `model`, `companyFk`, `warehouseFk`, `description`, `m3`, `isActive`, `deliveryPointFk`)
INSERT INTO `vn`.`vehicle`(`id`, `numberPlate`, `tradeMark`, `model`, `companyFk`, `warehouseFk`, `description`, `m3`, `isActive`, `deliveryPointFk`, `chassis`, `leasing`, `supplierFk`, `fuelTypeFk`, `bankPolicyFk`)
VALUES
(1, '3333-BAT', 'WAYNE INDUSTRIES', 'BATMOBILE', 442, 1, 'The ultimate war machine', 50, 1, 1),
(2, '1111-IMK', 'STARK INDUSTRIES', 'MARK-III', 442, 1, 'Iron-Man Heavy Armor MARK-III', 18, 1, 1),
(3, '2222-IMK', 'STARK INDUSTRIES', 'MARK-VI', 442, 1, 'Iron-Man Heavy Armor MARK-VI', 16, 1, 1),
(4, '3333-IMK', 'STARK INDUSTRIES', 'MARK-VII', 442, 1, 'Iron-Man Heavy Armor MARK-VII', 14, 1, 1),
(5, '4444-IMK', 'STARK INDUSTRIES', 'MARK-XLII', 442, 1, 'Iron-Man Heavy Armor MARK-XLII', 13, 1, 1),
(6, '5555-IMK', 'STARK INDUSTRIES', 'MARK-XLV', 442, 1, 'Iron-Man Heavy Armor MARK-XLV', 12, 0, 1);
(1, '3333-BAT', 'WAYNE INDUSTRIES', 'BATMOBILE', 442, 1, 'The ultimate war machine', 50, 1, 1, 'XCSC133C60', 'Wayne leasing', 1, 1, 1),
(2, '1111-IMK', 'STARK INDUSTRIES', 'MARK-III', 442, 1, 'Iron-Man Heavy Armor MARK-III', 18, 1, 1, '', '', 2, 2, 2),
(3, '2222-IMK', 'STARK INDUSTRIES', 'MARK-VI', 442, 1, 'Iron-Man Heavy Armor MARK-VI', 16, 1, 1, '', '', 442, 2, null),
(4, '3333-IMK', 'STARK INDUSTRIES', 'MARK-VII', 442, 1, 'Iron-Man Heavy Armor MARK-VII', 14, 1, 1, '', '', 442, 3, null),
(5, '4444-IMK', 'STARK INDUSTRIES', 'MARK-XLII', 442, 1, 'Iron-Man Heavy Armor MARK-XLII', 13, 1, 1, '', '', 442, 4, null),
(6, '5555-IMK', 'STARK INDUSTRIES', 'MARK-XLV', 442, 1, 'Iron-Man Heavy Armor MARK-XLV', 12, 0, 1, '', '', 442, 5, null);
INSERT INTO `vn`.`config`(`id`, `mdbServer`, `fakeEmail`, `defaultersMaxAmount`, `inventoried`)
VALUES
@ -4056,3 +4056,47 @@ INSERT IGNORE INTO vn.osrmConfig (id,url,tolerance)
INSERT IGNORE INTO vn.inventoryConfig
SET id = 1,
supplierFk = 4;
INSERT INTO vn.vehicleState (state, hasToNotify)
VALUES
('Operativo', NULL),
('Prestado', NULL),
('Robado', NULL),
('Taller', NULL),
('Targeta SOLRED', NULL),
('Via T SOLRED', NULL),
('ITV', NULL);
INSERT INTO vn.vehicleEvent (started, finished, vehicleStateFk, description, vehicleFk, userFk, notified)
VALUES
('2000-12-01', '2000-12-02', 4, 'cambio de aceite', 5, 103, NULL),
('2000-12-15', '2000-12-18', 2, 'viaje fin de curso', 5, 103, NULL),
('2000-12-20', '2001-01-01', 3, 'llaves puestas', 2, 103, NULL);
INSERT INTO vn.fuelType (id, name, code)
VALUES
(1, 'gasoil', 'gasoil'),
(2, 'gas', 'gas'),
(3, 'adblue', 'adblue'),
(4, 'gasolina', 'gasolina'),
(5, 'gasoil-frigo', 'gasoil-frigo'),
(6, 'electrico', 'electric');
INSERT INTO vn.bankPolicy (id, `ref`, amount, committedFee, nonCommittedFee, annualFee, started, ended, accountingFk, companyFk, supplierFk, description, hasGuarantee, dmsFk, notaryFk, currencyFk, amortizationTypeFk, periodicityTypeFk, insuranceExpired)
VALUES
(1, '11112222', 500000.0, 0.028, 0.0, 0.001, '2001-01-01', '2001-02-01', 1, 442, NULL, NULL, 0, NULL, NULL, 1, NULL, NULL, NULL),
(2, '33334444', 100000.0, 0.017, 0.0, 0.0, '2001-01-01', '2001-02-01', 1, 2, NULL, NULL, 0, NULL, NULL, 2, NULL, NULL, NULL);
INSERT INTO vn.ppe (id, amortization, firstAmortizated, lastAmortizated, finished, value, planFk, groupFk, account, endowment, elementAccount, nature, location, discharged, cause, isInvestmentAsset, workerFk, companyFk, description, isDone)
VALUES
(1, 0.00, '2001-01-01', NULL, NULL, 700.95, 16, 4, '3456000000', '4320000000', '12345', 'INMOVILIZADO', 'V02', NULL, NULL, 0, NULL, 442, 'UTILLAJE LASER ROTATIVO AUTONIVELANTE 500M', NULL),
(2, 0.00, '2001-01-01', NULL, NULL, 400.00, 16, 4, '5678000000', '1230000000', '67891', 'INMOVILIZADO', 'V02', NULL, NULL, 0, NULL, 442, 'UTILLAJE BALANZA Z100 150KILOS', NULL);
INSERT IGNORE INTO vn.vehicleType (id, name)
VALUES (1,'vehículo empresa'),
(2, 'furgoneta'),
(3, 'cabeza tractora'),
(4, 'remolque');

View File

@ -0,0 +1,36 @@
USE vn;
INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId)
VALUES ('Vehicle', 'filter', 'READ', 'ALLOW', 'ROLE', 'administrative'),
('Vehicle', 'filter', 'READ', 'ALLOW', 'ROLE', 'delivery'),
('Vehicle', 'find', 'READ', 'ALLOW', 'ROLE', 'administrative'),
('Vehicle', 'find', 'READ', 'ALLOW', 'ROLE', 'delivery'),
('Vehicle', 'findById', 'READ', 'ALLOW', 'ROLE', 'administrative'),
('Vehicle', 'updateAttributes', 'WRITE', 'ALLOW', 'ROLE', 'administrative'),
('Vehicle', 'updateAttributes', 'WRITE', 'ALLOW', 'ROLE', 'delivery'),
('BankPolicy', 'find', 'READ', 'ALLOW', 'ROLE', 'administrative'),
('BankPolicy', 'find', 'READ', 'ALLOW', 'ROLE', 'delivery'),
('VehicleState', 'find', 'READ', 'ALLOW', 'ROLE', 'administrative'),
('VehicleState', 'find', 'READ', 'ALLOW', 'ROLE', 'delivery'),
('Ppe', 'find', 'READ', 'ALLOW', 'ROLE', 'administrative' ),
('Ppe', 'find', 'READ', 'ALLOW', 'ROLE', 'delivery' ),
('Vehicle', 'deleteById', 'WRITE', 'ALLOW', 'ROLE', 'administrative'),
('Vehicle', 'deleteById', 'WRITE', 'ALLOW', 'ROLE', 'delivery'),
('Vehicle', '__get__active', 'READ', 'ALLOW', 'ROLE', 'employee');
ALTER TABLE vehicle ADD COLUMN importCooler decimal(10,2) DEFAULT NULL;
CREATE TABLE IF NOT EXISTS vehicleType (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(45) NOT NULL
);
INSERT IGNORE INTO vehicleType (id, name)
VALUES (1,'vehículo empresa'),
(2, 'furgoneta'),
(3, 'cabeza tractora'),
(4, 'remolque');
ALTER TABLE vehicle ADD COLUMN vehicleTypeFk INT(11) DEFAULT 1;
ALTER TABLE vehicle ADD CONSTRAINT fk_vehicle_vehicleType FOREIGN KEY (vehicleTypeFk) REFERENCES vehicleType(id);

View File

@ -0,0 +1,135 @@
const {ParameterizedSQL} = require('loopback-connector');
const {buildFilter, mergeFilters} = require('vn-loopback/util/filter');
module.exports = Self => {
Self.remoteMethodCtx('filter', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'object',
description: 'Filter defining where, order, skip and limit - must be a JSON-encoded string',
http: {source: 'query'}
}, {
arg: 'search',
type: 'string',
description: 'Searchs the vehicle by id or numberPlate',
http: {source: 'query'}
}, {
arg: 'id',
type: 'number'
}, {
arg: 'description',
type: 'string'
}, {
arg: 'companyFk',
type: 'number'
}, {
arg: 'tradeMark',
type: 'string'
}, {
arg: 'numberPlate',
type: 'string'
}, {
arg: 'warehouseFk',
type: 'number'
}, {
arg: 'chassis',
type: 'string'
}, {
arg: 'leasing',
type: 'string'
}, {
arg: 'countryCodeFk',
type: 'string'
}, {
arg: 'isKmTruckRate',
type: 'boolean'
}, {
arg: 'vehicleStateFk',
type: 'number'
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/filter`,
verb: `GET`
}
});
Self.filter = async(ctx, filter, options) => {
const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object') Object.assign(myOptions, options);
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
return {or: [{'v.id': value}, {numberPlate: {like: `%${value}%`}}]};
case 'id':
return {id: value};
case 'description':
return {description: {like: `%${value}%`}};
case 'companyFk':
return {companyFk: value};
case 'tradeMark':
return {tradeMark: {like: `%${value}%`}};
case 'numberPlate':
return {numberPlate: {like: `%${value}%`}};
case 'warehouseFk':
return {warehouseFk: value};
case 'chassis':
return {chassis: {like: `%${value}%`}};
case 'leasing':
return {leasing: {like: `%${value}%`}};
case 'countryCodeFk':
return {countryCodeFk: value};
case 'isKmTruckRate':
return {isKmTruckRate: value};
case 'vehicleStateFk':
return {vehicleStateFk: value};
}
}) || {};
const myFilter = mergeFilters(filter, {where});
const stmt = new ParameterizedSQL(`
SELECT v.id,
v.numberPlate,
v.tradeMark,
v.model,
v.m3,
v.description,
v.isActive,
v.countryCodeFk,
v.chassis,
v.leasing,
v.isKmTruckRate,
w.name as warehouse,
c.code as company,
sub.state
FROM vehicle v
LEFT JOIN warehouse w ON w.id = v.warehouseFk
LEFT JOIN company c ON c.id = v.companyFk
LEFT JOIN (
SELECT e.vehicleFk,
e.vehicleStateFk,
s.state,
ROW_NUMBER() OVER (PARTITION BY e.vehicleFk ORDER BY e.started DESC) rn
FROM vehicleEvent e
LEFT JOIN vehicleState s ON e.vehicleStateFk = s.id
) sub ON sub.vehicleFk = v.id AND sub.rn = 1
`);
const sqlWhere = conn.makeWhere(myFilter.where);
stmt.merge(sqlWhere);
stmt.merge(conn.makePagination(myFilter));
const sql = ParameterizedSQL.join([stmt], ';');
return conn.executeStmt(sql, myOptions);
};
};

View File

@ -5,12 +5,21 @@
"AgencyTermConfig": {
"dataSource": "vn"
},
"BankPolicy": {
"dataSource": "vn"
},
"Cmr": {
"dataSource": "vn"
},
"DeliveryPoint": {
"dataSource": "vn"
},
"FuelType": {
"dataSource": "vn"
},
"Ppe": {
"dataSource": "vn"
},
"RoadmapAddress": {
"dataSource": "vn"
},
@ -35,6 +44,9 @@
"Vehicle": {
"dataSource": "vn"
},
"VehicleState": {
"dataSource": "vn"
},
"RoutesMonitor": {
"dataSource": "vn"
}

View File

@ -0,0 +1,18 @@
{
"name": "BankPolicy",
"base": "VnModel",
"options": {
"mysql": {
"table": "bankPolicy"
}
},
"properties": {
"id": {
"type": "number",
"id": true
},
"ref": {
"type": "string"
}
}
}

View File

@ -0,0 +1,30 @@
{
"name": "FuelType",
"base": "VnModel",
"options": {
"mysql": {
"table": "fuelType"
}
},
"properties": {
"id": {
"type": "number",
"id": true,
"description": "Identifier"
},
"name": {
"type": "string"
},
"code": {
"type": "string"
}
},
"acls": [
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
]
}

View File

@ -0,0 +1,15 @@
{
"name": "Ppe",
"base": "VnModel",
"options": {
"mysql": {
"table": "ppe"
}
},
"properties": {
"id": {
"type": "number",
"id": true
}
}
}

View File

@ -0,0 +1,21 @@
{
"name": "VehicleState",
"base": "VnModel",
"options": {
"mysql": {
"table": "vehicleState"
}
},
"properties": {
"id": {
"type": "number",
"id": true
},
"state": {
"type": "string"
},
"hasToNotify": {
"type": "number"
}
}
}

View File

@ -1,3 +1,4 @@
module.exports = Self => {
require('../methods/vehicle/sorted')(Self);
require('../methods/vehicle/filter')(Self);
};

View File

@ -29,6 +29,30 @@
},
"isActive": {
"type": "number"
},
"countryCodeFk": {
"type": "string"
},
"chassis": {
"type": "string"
},
"leasing": {
"type": "string"
},
"isKmTruckRate": {
"type": "number"
},
"fuelTypeFk": {
"type": "number"
},
"import": {
"type": "number"
},
"vin": {
"type": "string"
},
"ppeFk": {
"type": "number"
}
},
"relations": {
@ -46,21 +70,51 @@
"type": "belongsTo",
"model": "DeliveryPoint",
"foreignKey": "deliveryPointFk"
},
"event": {
"type": "hasMany",
"model": "VehicleEvent",
"foreignKey": "vehicleFk",
"property": "id"
},
"supplier": {
"type": "belongsTo",
"model": "Supplier",
"foreignKey": "supplierFk"
},
"supplierCooler": {
"type": "belongsTo",
"model": "Supplier",
"foreignKey": "supplierCoolerFk"
},
"bankPolicy": {
"type": "belongsTo",
"model": "BankPolicy",
"foreignKey": "bankPolicyFk"
},
"fuelType": {
"type": "belongsTo",
"model": "FuelType",
"foreignKey": "fuelTypeFk"
},
"ppe": {
"type": "hasOne",
"model": "Ppe",
"foreignKey": "id",
"property": "ppeFk"
}
},
"scope": {
"scopes": {
"active": {
"fields": [
"id",
"numberPlate"
],
"where": {
"isActive": {
"neq": false
}
}
},
"acls": [
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
]
}
}