fix: refs #7917 fix route filter freeLancer
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Carlos Satorres 2024-12-23 11:55:48 +01:00
parent 549cb7633e
commit afb01ed021
4 changed files with 99 additions and 11 deletions

View File

@ -4038,7 +4038,7 @@ INSERT IGNORE INTO vn.saySimpleConfig (url, defaultChannel)
INSERT INTO vn.workerIrpf (workerFk,spouseNif, geographicMobilityDate) INSERT INTO vn.workerIrpf (workerFk,spouseNif, geographicMobilityDate)
VALUES (1106,'26493101E','2019-09-20'); VALUES (1106,'26493101E','2019-09-20');
INSERT INTO vn.referenceRate (currencyFk, dated, value) INSERT INTO vn.referenceRate (currencyFk, dated, value)
VALUES (2, '2000-12-01', 1.0495), VALUES (2, '2000-12-01', 1.0495),
(2, '2001-01-01', 1.0531), (2, '2001-01-01', 1.0531),
(2, '2001-02-01', 7.6347); (2, '2001-02-01', 7.6347);
@ -4049,3 +4049,15 @@ INSERT IGNORE INTO vn.osrmConfig (id,url,tolerance)
INSERT IGNORE INTO vn.inventoryConfig INSERT IGNORE INTO vn.inventoryConfig
SET id = 1, SET id = 1,
supplierFk = 4; supplierFk = 4;
UPDATE vn.worker
SET isFreelance=1
WHERE firstName='deliveryFreelancer';
INSERT INTO vn.route (workerFk,firstEditorFk,created,vehicleFk,agencyModeFk,`time`,isOk,started,finished,cost,m3,description,zoneFk,priority,invoiceInFk,editorFk,dated)
VALUES (132,100,'2001-01-01 00:00:00.000',1,1,'1899-12-30 12:15:00.000',0,'2001-01-01 00:00:00.000','2001-01-02 00:00:00.000',10.0,1.8,'eighth route',1,0,1,100,'2001-01-01');
INSERT INTO vn.route (workerFk,firstEditorFk,created,vehicleFk,agencyModeFk,`time`,isOk,started,finished,cost,m3,description,zoneFk,priority,invoiceInFk,editorFk,dated)
VALUES (132,100,'2001-01-01 00:00:00.000',1,2,'1899-12-30 13:20:00.000',0,'2001-01-01 00:00:00.000','2001-01-02 00:00:00.000',20.0,0.2,'ninth route',9,0,2,100,'2001-01-01');
UPDATE vn.route
SET workerFk=132
WHERE id=1;

View File

@ -0,0 +1,17 @@
INSERT INTO account.`role` (name,description,hasLogin,created,modified)
VALUES ('deliveryFreelancer','Repartidor autónomo',1,'2024-07-05 10:18:58.000','2024-07-05 10:18:58.000');
-- INSERT INTO account.roleInherit (`role`, inheritsFrom) VALUES(132, 2);
-- CALL account.role_sync();
INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId)
VALUES
('Route', 'getTickets', 'READ', 'ALLOW', 'ROLE', 'deliveryFreelancer'),
('AgencyTerm', 'filter', 'READ', 'ALLOW', 'ROLE', 'deliveryFreelancer'),
('Route', 'summary', 'READ', 'ALLOW', 'ROLE', 'deliveryFreelancer'),
('Route', 'getRouteByAgency', 'WRITE', 'ALLOW', 'ROLE', 'deliveryFreelancer'),
('Route','filter','READ','ALLOW','ROLE','deliveryFreelancer'),
('UserConfig','getUserConfig','*','ALLOW','ROLE','deliveryFreelancer'),
('Route', 'getTickets', 'READ', 'ALLOW', 'ROLE', 'deliveryFreelancer'),
('Route', 'findById', 'READ', 'ALLOW', 'ROLE', 'deliveryFreelancer');

View File

@ -87,6 +87,8 @@ module.exports = Self => {
Self.filter = async(ctx, filter) => { Self.filter = async(ctx, filter) => {
let conn = Self.dataSource.connector; let conn = Self.dataSource.connector;
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
let where = buildFilter(ctx.args, (param, value) => { let where = buildFilter(ctx.args, (param, value) => {
switch (param) { switch (param) {
@ -110,6 +112,13 @@ module.exports = Self => {
filter = mergeFilters(filter, {where}); filter = mergeFilters(filter, {where});
const worker = await models.Worker.findById(userId, {fields: ['isFreelance']});
const getMyRoute = await models.ACL.checkAccessAcl(ctx, 'Route', 'getRouteByAgency', 'WRITE');
if (userId && getMyRoute && worker.isFreelance) {
if (!filter.where) filter.where = {};
filter.where[`workerFk`] = userId;
}
let stmts = []; let stmts = [];
let stmt; let stmt;

View File

@ -1,21 +1,39 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Route filter()', () => { fdescribe('Route filter()', () => {
const today = Date.vnNew(); const today = Date.vnNew();
today.setHours(2, 0, 0, 0); today.setHours(2, 0, 0, 0);
it('should return the routes matching "search"', async() => { it('should return the routes matching "search"', async() => {
const ctx = { const ctx = {
args: { args: {
search: 1, search: 5,
},
req: {
accessToken: {
userId: 9
}
}
};
const result = await app.models.Route.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].id).toEqual(5);
});
it('should return all results matching the filter', async() => {
const ctx = {
req: {
accessToken: {
userId: 132
}
} }
}; };
const result = await app.models.Route.filter(ctx); const result = await app.models.Route.filter(ctx);
expect(result.length).toEqual(1); expect(result.length).toEqual(3);
expect(result[0].id).toEqual(1);
}); });
it('should return results matching "from" and "to"', async() => { it('should return results matching "from" and "to"', async() => {
@ -32,12 +50,16 @@ describe('Route filter()', () => {
args: { args: {
from: from, from: from,
to: to to: to
},
req: {
accessToken: {
userId: 9
}
} }
}; };
const results = await models.Route.filter(ctx, options); const results = await models.Route.filter(ctx, options);
expect(results.length).toBe(7); expect(results.length).toBe(9);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
@ -50,6 +72,11 @@ describe('Route filter()', () => {
const ctx = { const ctx = {
args: { args: {
m3: 0.1, m3: 0.1,
},
req: {
accessToken: {
userId: 9
}
} }
}; };
@ -62,6 +89,11 @@ describe('Route filter()', () => {
const ctx = { const ctx = {
args: { args: {
description: 'third route', description: 'third route',
},
req: {
accessToken: {
userId: 9
}
} }
}; };
@ -74,24 +106,33 @@ describe('Route filter()', () => {
const ctx = { const ctx = {
args: { args: {
workerFk: 56, workerFk: 56,
},
req: {
accessToken: {
userId: 9
}
} }
}; };
const result = await app.models.Route.filter(ctx); const result = await app.models.Route.filter(ctx);
expect(result.length).toEqual(5); expect(result.length).toEqual(4);
}); });
it('should return the routes matching "warehouseFk"', async() => { it('should return the routes matching "warehouseFk"', async() => {
const ctx = { const ctx = {
args: { args: {
warehouseFk: 1, warehouseFk: 1,
},
req: {
accessToken: {
userId: 9
}
} }
}; };
let result = await app.models.Route.filter(ctx); let result = await app.models.Route.filter(ctx);
expect(result.length).toEqual(7); expect(result.length).toEqual(9);
ctx.args.warehouseFk = 2; ctx.args.warehouseFk = 2;
@ -104,9 +145,13 @@ describe('Route filter()', () => {
const ctx = { const ctx = {
args: { args: {
vehicleFk: 2, vehicleFk: 2,
},
req: {
accessToken: {
userId: 9
}
} }
}; };
const result = await app.models.Route.filter(ctx); const result = await app.models.Route.filter(ctx);
expect(result.length).toEqual(1); expect(result.length).toEqual(1);
@ -116,6 +161,11 @@ describe('Route filter()', () => {
const ctx = { const ctx = {
args: { args: {
agencyModeFk: 7, agencyModeFk: 7,
},
req: {
accessToken: {
userId: 9
}
} }
}; };