From afb01ed021d61a00b9c36b05e212a18e08ed8c2e Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 23 Dec 2024 11:55:48 +0100 Subject: [PATCH] fix: refs #7917 fix route filter freeLancer --- db/dump/fixtures.before.sql | 14 +++- .../11393-redCamellia/00-firstScript.sql | 17 +++++ modules/route/back/methods/route/filter.js | 9 +++ .../back/methods/route/specs/filter.spec.js | 70 ++++++++++++++++--- 4 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 db/versions/11393-redCamellia/00-firstScript.sql diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 97ed0ae47..6a425cf09 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -4038,7 +4038,7 @@ INSERT IGNORE INTO vn.saySimpleConfig (url, defaultChannel) INSERT INTO vn.workerIrpf (workerFk,spouseNif, geographicMobilityDate) 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), (2, '2001-01-01', 1.0531), (2, '2001-02-01', 7.6347); @@ -4049,3 +4049,15 @@ INSERT IGNORE INTO vn.osrmConfig (id,url,tolerance) INSERT IGNORE INTO vn.inventoryConfig SET id = 1, 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; diff --git a/db/versions/11393-redCamellia/00-firstScript.sql b/db/versions/11393-redCamellia/00-firstScript.sql new file mode 100644 index 000000000..6e7499702 --- /dev/null +++ b/db/versions/11393-redCamellia/00-firstScript.sql @@ -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'); + diff --git a/modules/route/back/methods/route/filter.js b/modules/route/back/methods/route/filter.js index 70bf7ba67..d4e127a92 100644 --- a/modules/route/back/methods/route/filter.js +++ b/modules/route/back/methods/route/filter.js @@ -87,6 +87,8 @@ module.exports = Self => { Self.filter = async(ctx, filter) => { let conn = Self.dataSource.connector; + const userId = ctx.req.accessToken.userId; + const models = Self.app.models; let where = buildFilter(ctx.args, (param, value) => { switch (param) { @@ -110,6 +112,13 @@ module.exports = Self => { 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 stmt; diff --git a/modules/route/back/methods/route/specs/filter.spec.js b/modules/route/back/methods/route/specs/filter.spec.js index 18c0ca04f..1f21bfa74 100644 --- a/modules/route/back/methods/route/specs/filter.spec.js +++ b/modules/route/back/methods/route/specs/filter.spec.js @@ -1,21 +1,39 @@ const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models; -describe('Route filter()', () => { +fdescribe('Route filter()', () => { const today = Date.vnNew(); today.setHours(2, 0, 0, 0); it('should return the routes matching "search"', async() => { const ctx = { 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); - expect(result.length).toEqual(1); - expect(result[0].id).toEqual(1); + expect(result.length).toEqual(3); }); it('should return results matching "from" and "to"', async() => { @@ -32,12 +50,16 @@ describe('Route filter()', () => { args: { from: from, to: to + }, + req: { + accessToken: { + userId: 9 + } } }; - const results = await models.Route.filter(ctx, options); - expect(results.length).toBe(7); + expect(results.length).toBe(9); await tx.rollback(); } catch (e) { @@ -50,6 +72,11 @@ describe('Route filter()', () => { const ctx = { args: { m3: 0.1, + }, + req: { + accessToken: { + userId: 9 + } } }; @@ -62,6 +89,11 @@ describe('Route filter()', () => { const ctx = { args: { description: 'third route', + }, + req: { + accessToken: { + userId: 9 + } } }; @@ -74,24 +106,33 @@ describe('Route filter()', () => { const ctx = { args: { workerFk: 56, + }, + req: { + accessToken: { + userId: 9 + } } }; - 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() => { const ctx = { args: { warehouseFk: 1, + }, + req: { + accessToken: { + userId: 9 + } } }; let result = await app.models.Route.filter(ctx); - expect(result.length).toEqual(7); + expect(result.length).toEqual(9); ctx.args.warehouseFk = 2; @@ -104,9 +145,13 @@ describe('Route filter()', () => { const ctx = { args: { vehicleFk: 2, + }, + req: { + accessToken: { + userId: 9 + } } }; - const result = await app.models.Route.filter(ctx); expect(result.length).toEqual(1); @@ -116,6 +161,11 @@ describe('Route filter()', () => { const ctx = { args: { agencyModeFk: 7, + }, + req: { + accessToken: { + userId: 9 + } } };