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

Draft
carlossa wants to merge 12 commits from 7917-freelancerRoute into dev
11 changed files with 171 additions and 185 deletions

View File

@ -0,0 +1,6 @@
INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId)
VALUES
('Route', 'getTickets', 'READ', 'ALLOW', 'ROLE', 'supplier'),
Review

confirmar con jbreso que rol tienen los autonomos que van a usar esto

confirmar con jbreso que rol tienen los autonomos que van a usar esto
Review

Lo van a ver usuarios delivery

Lo van a ver usuarios delivery
('AgencyTerm', 'filter', 'READ', 'ALLOW', 'ROLE', 'supplier'),
('Route', 'summary', 'READ', 'ALLOW', 'ROLE', 'supplier');

View File

@ -173,9 +173,7 @@ module.exports = Self => {
}
});
filter = mergeFilters(ctx.args.filter, {where});
const userId = ctx.req.accessToken.userId;
const client = await Self.app.models.Client.findById(userId, myOptions);
const supplier = await Self.app.models.Supplier.findOne({where: {nif: client.fi}}, myOptions);
const supplier = await Self.app.models.Supplier.isSupplier(ctx);
if (supplier) {
if (!filter.where) filter.where = {};
filter.where[`e.supplierFk`] = supplier.id;

View File

@ -72,7 +72,13 @@ module.exports = Self => {
}
});
filter = mergeFilters(filter, {where});
filter = mergeFilters(ctx.args?.filter ?? {}, {where});
const supplier = await Self.app.models.Supplier.isSupplier(ctx, myOptions);
if (supplier) {
if (!filter.where) filter.where = {};
filter.where[`a.supplierFk`] = supplier.id;
}
const date = Date.vnNew();
date.setHours(0, 0, 0, 0);

View File

@ -1,109 +1,62 @@
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);
const ctx = beforeAll.getCtx();
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);
ctx.args = {};
const agencyTerms = await models.AgencyTerm.filter(ctx);
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,
}
};
const search = 1;
ctx.args = {search};
let result = await app.models.AgencyTerm.filter(ctx);
const [result] = await models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
expect(result.invoiceInFk).toEqual(search);
});
it('should return results matching "search" searching by string', async() => {
let ctx = {
args: {
search: 'Plants SL',
}
};
ctx.args = {search: 'Plants SL'};
let result = await app.models.AgencyTerm.filter(ctx);
let result = await 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
}
};
ctx.args = {from, to};
const results = await models.AgencyTerm.filter(ctx, options);
const results = await models.AgencyTerm.filter(ctx);
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,
}
};
const agencyModeFk = 1;
ctx.args = {agencyModeFk};
let result = await app.models.AgencyTerm.filter(ctx);
const [result] = await models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
expect(result.agencyModeFk).toEqual(agencyModeFk);
});
it('should return results matching "agencyFk"', async() => {
let ctx = {
args: {
agencyFk: 2,
}
};
const agencyFk = 1;
ctx.args = {agencyFk};
const [result] = await models.AgencyTerm.filter(ctx);
let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(2);
expect(result.agencyFk).toEqual(agencyFk);
});
});

View File

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

View File

@ -2,7 +2,7 @@
const {ParameterizedSQL} = require('loopback-connector');
module.exports = Self => {
Self.remoteMethod('getTickets', {
Self.remoteMethodCtx('getTickets', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
@ -23,7 +23,7 @@ module.exports = Self => {
}
});
Self.getTickets = async(filter, options) => {
Self.getTickets = async(ctx, filter, options) => {
const conn = Self.dataSource.connector;
const myOptions = {};
@ -78,6 +78,8 @@ module.exports = Self => {
AND tob2.observationTypeFk = ot2.id
LEFT JOIN address a ON a.id = t.addressFk
LEFT JOIN agencyMode am ON am.id = t.agencyModeFk
LEFT JOIN agency ag ON am.agencyFk = ag.id
LEFT JOIN supplierAgencyTerm sat ON sat.agencyFk = ag.id
LEFT JOIN account.user u ON u.id = r.workerFk
LEFT JOIN vehicle v ON v.id = r.vehicleFk
LEFT JOIN workerMedia wm ON wm.workerFk = c.salesPersonFk`
@ -85,6 +87,10 @@ module.exports = Self => {
if (!filter.where) filter.where = {};
const supplier = await Self.app.models.Supplier.isSupplier(ctx, myOptions);
if (supplier)
filter.where['sat.supplierFk'] = supplier.id;
const where = filter.where;
where['r.id'] = filter.id;

View File

@ -1,9 +1,11 @@
const app = require('vn-loopback/server/server');
describe('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);
});

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 = {
@ -53,7 +53,7 @@ module.exports = Self => {
};
summary.route = await Self.app.models.Route.findOne(filter);
summary.tickets = await Self.app.models.Route.getTickets({id: id, order: 'priority ASC'});
summary.tickets = await Self.app.models.Route.getTickets(ctx, {id, order: 'priority ASC'});
return summary;
};

View File

@ -163,4 +163,16 @@ module.exports = Self => {
if ((nameChanged) && !isAlpha(name))
throw new UserError('The social name has an invalid format');
});
carlossa marked this conversation as resolved
Review

aci es mes sencill que pases el userId directament

aci es mes sencill que pases el userId directament
Self.isSupplier = async(ctx, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const userId = ctx.req.accessToken.userId;
const client = await Self.app.models.Client.findById(userId, null, myOptions);
const supplier = await Self.app.models.Supplier.findOne({where: {nif: client.fi}}, myOptions);
return supplier;
};
};