Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 2283_e2e_delete_ticket_with_stowaway

This commit is contained in:
Carlos Jimenez Ruiz 2020-06-10 16:24:17 +02:00
commit 538ae75b89
2 changed files with 36 additions and 23 deletions

View File

@ -100,34 +100,27 @@ module.exports = Self => {
}); });
Self.filter = async(ctx, filter) => { Self.filter = async(ctx, filter) => {
const userId = ctx.req.accessToken.userId;
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const models = Self.app.models;
const args = ctx.args; const args = ctx.args;
let worker = await Self.app.models.Worker.findOne({ // Apply filter by team
where: {userFk: ctx.req.accessToken.userId}, const teamMembersId = [];
include: [ if (args.myTeam != null) {
{relation: 'collegues'} const worker = await models.Worker.findById(userId, {
] include: {
}); relation: 'collegues'
}
let teamIds = [];
if (worker.collegues().length && args.myTeam) {
worker.collegues().forEach(collegue => {
teamIds.push(collegue.collegueFk);
}); });
} const collegues = worker.collegues() || [];
collegues.forEach(collegue => {
if (args.mine || (worker.collegues().length === 0 && args.myTeam)) { teamMembersId.push(collegue.collegueFk);
worker = await Self.app.models.Worker.findOne({
fields: ['id'],
where: {userFk: ctx.req.accessToken.userId}
}); });
teamIds = [worker && worker.id];
}
if (ctx.args && (args.mine || args.myTeam)) if (teamMembersId.length == 0)
args.teamIds = teamIds; teamMembersId.push(userId);
}
if (ctx.args && args.to) { if (ctx.args && args.to) {
const dateTo = args.to; const dateTo = args.to;
@ -156,7 +149,11 @@ module.exports = Self => {
return {'ts.stateFk': value}; return {'ts.stateFk': value};
case 'mine': case 'mine':
case 'myTeam': case 'myTeam':
return {'c.salesPersonFk': {inq: teamIds}}; if (value)
return {'c.salesPersonFk': {inq: teamMembersId}};
else
return {'c.salesPersonFk': {nin: teamMembersId}};
case 'alertLevel': case 'alertLevel':
return {'ts.alertLevel': value}; return {'ts.alertLevel': value};
case 'pending': case 'pending':

View File

@ -71,4 +71,20 @@ describe('ticket filter()', () => {
expect(secondRow.state).toEqual('Entregado'); expect(secondRow.state).toEqual('Entregado');
expect(thirdRow.state).toEqual('Entregado'); expect(thirdRow.state).toEqual('Entregado');
}); });
it('should return the tickets from the worker team', async() => {
const ctx = {req: {accessToken: {userId: 9}}, args: {myTeam: true}};
const filter = {};
const result = await app.models.Ticket.filter(ctx, filter);
expect(result.length).toEqual(17);
});
it('should return the tickets that are not from the worker team', async() => {
const ctx = {req: {accessToken: {userId: 9}}, args: {myTeam: false}};
const filter = {};
const result = await app.models.Ticket.filter(ctx, filter);
expect(result.length).toEqual(7);
});
}); });