fix: refs #7917 fix methods, tests, acls #3085

Closed
carlossa wants to merge 35 commits from 7917-freelancerRoute into dev
9 changed files with 84 additions and 132 deletions

View File

@ -4049,3 +4049,16 @@ 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;

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');

View File

@ -1,109 +0,0 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('AgencyTerm filter()', () => {
const authUserId = 9;
const today = Date.vnNew();
today.setHours(2, 0, 0, 0);
it('should return all results matching the filter', async() => {
const tx = await models.AgencyTerm.beginTransaction({});
try {
const options = {transaction: tx};
const filter = {};
const ctx = {req: {accessToken: {userId: authUserId}}};
const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
const firstAgencyTerm = agencyTerms[0];
expect(firstAgencyTerm.routeFk).toEqual(1);
expect(agencyTerms.length).toEqual(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "search" searching by integer', async() => {
let ctx = {
args: {
search: 1,
}
};
let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
});
it('should return results matching "search" searching by string', async() => {
let ctx = {
args: {
search: 'Plants SL',
}
};
let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(2);
});
it('should return results matching "from" and "to"', async() => {
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
try {
const from = Date.vnNew();
from.setHours(0, 0, 0, 0);
const to = Date.vnNew();
to.setHours(23, 59, 59, 999);
const ctx = {
args: {
from: from,
to: to
}
};
const results = await models.AgencyTerm.filter(ctx, options);
expect(results.length).toBe(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "agencyModeFk"', async() => {
let ctx = {
args: {
agencyModeFk: 1,
}
};
let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
});
it('should return results matching "agencyFk"', async() => {
let ctx = {
args: {
agencyFk: 2,
}
};
let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(2);
});
});

View File

@ -86,6 +86,8 @@ module.exports = Self => {
});
Self.filter = async(ctx, filter) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
let conn = Self.dataSource.connector;
let where = buildFilter(ctx.args, (param, value) => {
@ -109,7 +111,12 @@ 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;

View File

@ -2,7 +2,7 @@
const {ParameterizedSQL} = require('loopback-connector');
module.exports = Self => {
Self.remoteMethod('getTickets', {
Self.remoteMethodCtx('getTickets', {
Review

els test de back han de verificar les 3 opcions,
un supplier no treballador sols veu lo seu
un treballador veu tot
uno que no es res, no veu res

els test de back han de verificar les 3 opcions, un supplier no treballador sols veu lo seu un treballador veu tot uno que no es res, no veu res
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
@ -28,6 +28,7 @@ module.exports = Self => {
const myOptions = {};
console.log('filter.where', filter.where);
if (typeof options == 'object')
Object.assign(myOptions, options);

View File

@ -1,10 +1,30 @@
const app = require('vn-loopback/server/server');
describe('route getTickets()', () => {
fdescribe('route getTickets()', () => {
const ctx = beforeAll.getCtx();
it('should return the tickets for a given route', async() => {
const filter = {id: 2};
let result = await app.models.Route.getTickets(filter);
let result = await app.models.Route.getTickets(ctx, filter);
expect(result.length).toEqual(1);
});
it('should return only routes belonging to the supplier', async() => {
const filter = {}; // No especificamos un filtro específico, se debería aplicar el de supplier automáticamente
// Mock para simular que el usuario es un supplier específico
spyOn(app.models.Supplier, 'isSupplier').and.returnValue({
id: 1101 // ID del proveedor ficticio
});
let result = await app.models.Route.getTickets(ctx, filter);
console.log('ctx: ', ctx);
// Verifica que todas las rutas pertenecen al proveedor simulado
expect(result.length).toBeGreaterThan(0);
// result.forEach(route => {
// expect(route.supplierFk).toEqual(123); // Asegúrate de comparar con el campo adecuado
// });
});
});

View File

@ -1,35 +1,38 @@
const app = require('vn-loopback/server/server');
describe('route summary()', () => {
const id = 1;
const ctx = beforeAll.getCtx();
it('should return a summary object containing data from one route', async() => {
const result = await app.models.Route.summary(1);
const result = await app.models.Route.summary(ctx, id);
expect(result.route.id).toEqual(1);
});
it(`should return a summary object containing it's agency`, async() => {
const result = await app.models.Route.summary(1);
const result = await app.models.Route.summary(ctx, id);
const agency = result.route.agencyMode();
expect(agency.name).toEqual('inhouse pickup');
});
it(`should return a summary object containing it's vehicle`, async() => {
const result = await app.models.Route.summary(1);
const result = await app.models.Route.summary(ctx, id);
const vehicle = result.route.vehicle();
expect(vehicle.numberPlate).toEqual('3333-BAT');
});
it(`should return a summary object containing it's worker`, async() => {
const result = await app.models.Route.summary(1);
const result = await app.models.Route.summary(ctx, id);
const worker = result.route.worker().user();
expect(worker.name).toEqual('delivery');
});
it(`should return a summary object containing data from the tickets`, async() => {
const result = await app.models.Route.summary(2);
const result = await app.models.Route.summary(ctx, 2);
expect(result.tickets.length).toEqual(1);
});

View File

@ -1,5 +1,5 @@
module.exports = Self => {
Self.remoteMethod('summary', {
Self.remoteMethodCtx('summary', {
description: 'Returns the information of a route showed in the route summary',
accessType: 'READ',
accepts: [{
@ -19,7 +19,7 @@ module.exports = Self => {
}
});
Self.summary = async id => {
Self.summary = async(ctx, id) => {
let summary = {};
let filter = {