WIP: fix: refs #7917 fix methods, tests, acls #3085
|
@ -0,0 +1,6 @@
|
||||||
|
INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId)
|
||||||
|
VALUES
|
||||||
|
('Route', 'getTickets', 'READ', 'ALLOW', 'ROLE', 'supplier'),
|
||||||
|
|||||||
|
('AgencyTerm', 'filter', 'READ', 'ALLOW', 'ROLE', 'supplier'),
|
||||||
|
('Route', 'summary', 'READ', 'ALLOW', 'ROLE', 'supplier');
|
||||||
|
|
|
@ -173,9 +173,7 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
filter = mergeFilters(ctx.args.filter, {where});
|
filter = mergeFilters(ctx.args.filter, {where});
|
||||||
const userId = ctx.req.accessToken.userId;
|
const supplier = await Self.app.models.Supplier.isSupplier(ctx);
|
||||||
const client = await Self.app.models.Client.findById(userId, myOptions);
|
|
||||||
const supplier = await Self.app.models.Supplier.findOne({where: {nif: client.fi}}, myOptions);
|
|
||||||
if (supplier) {
|
if (supplier) {
|
||||||
if (!filter.where) filter.where = {};
|
if (!filter.where) filter.where = {};
|
||||||
filter.where[`e.supplierFk`] = supplier.id;
|
filter.where[`e.supplierFk`] = supplier.id;
|
||||||
|
|
|
@ -74,6 +74,12 @@ module.exports = Self => {
|
||||||
|
|
||||||
filter = mergeFilters(filter, {where});
|
filter = mergeFilters(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();
|
const date = Date.vnNew();
|
||||||
date.setHours(0, 0, 0, 0);
|
date.setHours(0, 0, 0, 0);
|
||||||
const stmts = [];
|
const stmts = [];
|
||||||
|
|
|
@ -2,9 +2,17 @@ const app = require('vn-loopback/server/server');
|
||||||
const models = require('vn-loopback/server/server').models;
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
|
||||||
describe('AgencyTerm filter()', () => {
|
describe('AgencyTerm filter()', () => {
|
||||||
const authUserId = 9;
|
|
||||||
const today = Date.vnNew();
|
const today = Date.vnNew();
|
||||||
today.setHours(2, 0, 0, 0);
|
today.setHours(2, 0, 0, 0);
|
||||||
|
let ctx = beforeAll.getCtx();
|
||||||
|
beforeAll(async() => {
|
||||||
|
ctx = {
|
||||||
|
req: {
|
||||||
|
accessToken: {},
|
||||||
|
headers: {origin: 'http://localhost'},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
it('should return all results matching the filter', async() => {
|
it('should return all results matching the filter', async() => {
|
||||||
const tx = await models.AgencyTerm.beginTransaction({});
|
const tx = await models.AgencyTerm.beginTransaction({});
|
||||||
|
@ -12,7 +20,6 @@ describe('AgencyTerm filter()', () => {
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
const filter = {};
|
const filter = {};
|
||||||
const ctx = {req: {accessToken: {userId: authUserId}}};
|
|
||||||
|
|
||||||
const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
|
const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
|
||||||
const firstAgencyTerm = agencyTerms[0];
|
const firstAgencyTerm = agencyTerms[0];
|
||||||
|
@ -28,26 +35,33 @@ describe('AgencyTerm filter()', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return results matching "search" searching by integer', async() => {
|
it('should return results matching "search" searching by integer', async() => {
|
||||||
let ctx = {
|
// ctx = {
|
||||||
args: {
|
// args: {
|
||||||
search: 1,
|
// search: 1,
|
||||||
}
|
// }
|
||||||
|
// };
|
||||||
|
const filter = {
|
||||||
|
order: 'isActive ASC, name',
|
||||||
|
search: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = await app.models.AgencyTerm.filter(ctx);
|
let result = await app.models.AgencyTerm.filter(ctx, filter);
|
||||||
|
|
||||||
expect(result.length).toEqual(1);
|
expect(result.length).toEqual(1);
|
||||||
expect(result[0].routeFk).toEqual(1);
|
expect(result[0].routeFk).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return results matching "search" searching by string', async() => {
|
it('should return results matching "search" searching by string', async() => {
|
||||||
let ctx = {
|
// ctx = {
|
||||||
args: {
|
// args: {
|
||||||
search: 'Plants SL',
|
// search: 'Plants SL',
|
||||||
}
|
// }
|
||||||
|
// };
|
||||||
|
const filter = {
|
||||||
|
search: 'Plants SL'
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = await app.models.AgencyTerm.filter(ctx);
|
let result = await app.models.AgencyTerm.filter(ctx, filter);
|
||||||
|
|
||||||
expect(result.length).toEqual(2);
|
expect(result.length).toEqual(2);
|
||||||
});
|
});
|
||||||
|
@ -63,13 +77,6 @@ describe('AgencyTerm filter()', () => {
|
||||||
const to = Date.vnNew();
|
const to = Date.vnNew();
|
||||||
to.setHours(23, 59, 59, 999);
|
to.setHours(23, 59, 59, 999);
|
||||||
|
|
||||||
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);
|
||||||
|
@ -82,26 +89,31 @@ describe('AgencyTerm filter()', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return results matching "agencyModeFk"', async() => {
|
it('should return results matching "agencyModeFk"', async() => {
|
||||||
let ctx = {
|
// ctx = {
|
||||||
args: {
|
// args: {
|
||||||
agencyModeFk: 1,
|
// agencyModeFk: 1,
|
||||||
}
|
// }
|
||||||
|
// };
|
||||||
|
const filter = {
|
||||||
|
agencyModeFk: 1,
|
||||||
};
|
};
|
||||||
|
let result = await app.models.AgencyTerm.filter(ctx, filter);
|
||||||
let result = await app.models.AgencyTerm.filter(ctx);
|
|
||||||
|
|
||||||
expect(result.length).toEqual(1);
|
expect(result.length).toEqual(1);
|
||||||
expect(result[0].routeFk).toEqual(1);
|
expect(result[0].routeFk).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return results matching "agencyFk"', async() => {
|
it('should return results matching "agencyFk"', async() => {
|
||||||
let ctx = {
|
// ctx = {
|
||||||
args: {
|
// args: {
|
||||||
agencyFk: 2,
|
// agencyFk: 2,
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
|
||||||
let result = await app.models.AgencyTerm.filter(ctx);
|
const filter = {
|
||||||
|
agencyFk: 2
|
||||||
|
};
|
||||||
|
let result = await app.models.AgencyTerm.filter(ctx, filter);
|
||||||
|
|
||||||
expect(result.length).toEqual(1);
|
expect(result.length).toEqual(1);
|
||||||
expect(result[0].routeFk).toEqual(2);
|
expect(result[0].routeFk).toEqual(2);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
const {ParameterizedSQL} = require('loopback-connector');
|
const {ParameterizedSQL} = require('loopback-connector');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('getTickets', {
|
Self.remoteMethodCtx('getTickets', {
|
||||||
description: 'Find all instances of the model matched by filter from the data source.',
|
description: 'Find all instances of the model matched by filter from the data source.',
|
||||||
accessType: 'READ',
|
accessType: 'READ',
|
||||||
accepts: [
|
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 conn = Self.dataSource.connector;
|
||||||
|
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
|
@ -77,7 +77,9 @@ module.exports = Self => {
|
||||||
LEFT JOIN ticketObservation tob2 ON tob2.ticketFk = t.id
|
LEFT JOIN ticketObservation tob2 ON tob2.ticketFk = t.id
|
||||||
AND tob2.observationTypeFk = ot2.id
|
AND tob2.observationTypeFk = ot2.id
|
||||||
LEFT JOIN address a ON a.id = t.addressFk
|
LEFT JOIN address a ON a.id = t.addressFk
|
||||||
LEFT JOIN agencyMode am ON am.id = t.agencyModeFk
|
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 account.user u ON u.id = r.workerFk
|
||||||
LEFT JOIN vehicle v ON v.id = r.vehicleFk
|
LEFT JOIN vehicle v ON v.id = r.vehicleFk
|
||||||
LEFT JOIN workerMedia wm ON wm.workerFk = c.salesPersonFk`
|
LEFT JOIN workerMedia wm ON wm.workerFk = c.salesPersonFk`
|
||||||
|
@ -85,6 +87,10 @@ module.exports = Self => {
|
||||||
|
|
||||||
if (!filter.where) filter.where = {};
|
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;
|
const where = filter.where;
|
||||||
where['r.id'] = filter.id;
|
where['r.id'] = filter.id;
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ const app = require('vn-loopback/server/server');
|
||||||
describe('route getTickets()', () => {
|
describe('route getTickets()', () => {
|
||||||
it('should return the tickets for a given route', async() => {
|
it('should return the tickets for a given route', async() => {
|
||||||
const filter = {id: 2};
|
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);
|
expect(result.length).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,35 +1,51 @@
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
describe('route summary()', () => {
|
describe('route summary()', () => {
|
||||||
|
let ctx = beforeAll.getCtx();
|
||||||
|
beforeAll(async() => {
|
||||||
|
ctx = {
|
||||||
|
req: {
|
||||||
|
accessToken: {},
|
||||||
|
headers: {origin: 'http://localhost'},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
it('should return a summary object containing data from one route', async() => {
|
it('should return a summary object containing data from one route', async() => {
|
||||||
const result = await app.models.Route.summary(1);
|
const filter = {
|
||||||
|
id: 1
|
||||||
|
};
|
||||||
|
const result = await app.models.Route.summary(ctx, filter);
|
||||||
|
|
||||||
expect(result.route.id).toEqual(1);
|
expect(result.route.id).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return a summary object containing it's agency`, async() => {
|
it(`should return a summary object containing it's agency`, async() => {
|
||||||
const result = await app.models.Route.summary(1);
|
const filter = {
|
||||||
|
id: 1
|
||||||
|
};
|
||||||
|
const result = await app.models.Route.summary(ctx, filter);
|
||||||
const agency = result.route.agencyMode();
|
const agency = result.route.agencyMode();
|
||||||
|
|
||||||
expect(agency.name).toEqual('inhouse pickup');
|
expect(agency.name).toEqual('inhouse pickup');
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return a summary object containing it's vehicle`, async() => {
|
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, filter);
|
||||||
const vehicle = result.route.vehicle();
|
const vehicle = result.route.vehicle();
|
||||||
|
|
||||||
expect(vehicle.numberPlate).toEqual('3333-BAT');
|
expect(vehicle.numberPlate).toEqual('3333-BAT');
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return a summary object containing it's worker`, async() => {
|
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, filter);
|
||||||
const worker = result.route.worker().user();
|
const worker = result.route.worker().user();
|
||||||
|
|
||||||
expect(worker.name).toEqual('delivery');
|
expect(worker.name).toEqual('delivery');
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return a summary object containing data from the tickets`, async() => {
|
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, filter);
|
||||||
|
|
||||||
expect(result.tickets.length).toEqual(1);
|
expect(result.tickets.length).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('summary', {
|
Self.remoteMethodCtx('summary', {
|
||||||
description: 'Returns the information of a route showed in the route summary',
|
description: 'Returns the information of a route showed in the route summary',
|
||||||
accessType: 'READ',
|
accessType: 'READ',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
|
@ -19,7 +19,7 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.summary = async id => {
|
Self.summary = async(ctx, id) => {
|
||||||
let summary = {};
|
let summary = {};
|
||||||
|
|
||||||
let filter = {
|
let filter = {
|
||||||
|
@ -53,7 +53,7 @@ module.exports = Self => {
|
||||||
};
|
};
|
||||||
|
|
||||||
summary.route = await Self.app.models.Route.findOne(filter);
|
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;
|
return summary;
|
||||||
};
|
};
|
||||||
|
|
|
@ -163,4 +163,10 @@ module.exports = Self => {
|
||||||
if ((nameChanged) && !isAlpha(name))
|
if ((nameChanged) && !isAlpha(name))
|
||||||
throw new UserError('The social name has an invalid format');
|
throw new UserError('The social name has an invalid format');
|
||||||
});
|
});
|
||||||
|
|
||||||
carlossa marked this conversation as resolved
jgallego
commented
aci es mes sencill que pases el userId directament aci es mes sencill que pases el userId directament
|
|||||||
|
Self.isSupplier = async(userId, options = {}) => {
|
||||||
|
const client = await Self.app.models.Client.findById(userId, options);
|
||||||
|
const supplier = await Self.app.models.Supplier.findOne({where: {nif: client.fi}}, options);
|
||||||
|
return supplier;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,19 +35,19 @@
|
||||||
},
|
},
|
||||||
"relations": {
|
"relations": {
|
||||||
"supplierAgencyTerm": {
|
"supplierAgencyTerm": {
|
||||||
"type": "hasOne",
|
"type": "hasOne",
|
||||||
"model": "SupplierAgencyTerm",
|
"model": "SupplierAgencyTerm",
|
||||||
"foreignKey": "agencyFk"
|
"foreignKey": "agencyFk"
|
||||||
},
|
},
|
||||||
"warehouse": {
|
"warehouse": {
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"model": "Warehouse",
|
"model": "Warehouse",
|
||||||
"foreignKey": "warehouseFk"
|
"foreignKey": "warehouseFk"
|
||||||
},
|
},
|
||||||
"workCenter": {
|
"workCenter": {
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"model": "WorkCenter",
|
"model": "WorkCenter",
|
||||||
"foreignKey": "workCenterFk"
|
"foreignKey": "workCenterFk"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
confirmar con jbreso que rol tienen los autonomos que van a usar esto
Lo van a ver usuarios delivery