From d83995e64e94cac3e561ddacde8dc09a71e7d624 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Fri, 17 Dec 2021 10:41:59 +0100 Subject: [PATCH] filter worker id refactor --- .../order/back/methods/order-row/removes.js | 2 +- modules/order/back/methods/order/filter.js | 36 +++++++++---------- .../back/methods/order/updateBasicData.js | 1 + 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/modules/order/back/methods/order-row/removes.js b/modules/order/back/methods/order-row/removes.js index 7f35ec69d..614761aed 100644 --- a/modules/order/back/methods/order-row/removes.js +++ b/modules/order/back/methods/order-row/removes.js @@ -21,7 +21,7 @@ module.exports = Self => { } }); - Self.removes = async params => { + Self.removes = async(params, options) => { const myOptions = {}; let tx; diff --git a/modules/order/back/methods/order/filter.js b/modules/order/back/methods/order/filter.js index 8481ce3ba..27cacdd62 100644 --- a/modules/order/back/methods/order/filter.js +++ b/modules/order/back/methods/order/filter.js @@ -78,29 +78,22 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - let worker = await Self.app.models.Worker.findOne({ - where: {userFk: ctx.req.accessToken.userId}, - include: [ - {relation: 'collegues'} - ] - }, myOptions); - const args = ctx.args; - let teamIds = []; - if (worker.collegues().length && args.myTeam) { - worker.collegues().forEach(collegue => { - teamIds.push(collegue.collegueFk); - }); - } - - if (worker.collegues().length === 0 && args.myTeam) { - worker = await Self.app.models.Worker.findOne({ - fields: ['id'], - where: {userFk: ctx.req.accessToken.userId} + // Apply filter by team + const teamMembersId = []; + if (args.myTeam != null) { + const worker = await models.Worker.findById(userId, { + include: { + relation: 'collegues' + } }, myOptions); + const collegues = worker.collegues() || []; + for (let collegue of collegues) + teamMembersId.push(collegue.collegueFk); - teamIds = [worker && worker.id]; + if (teamMembersId.length == 0) + teamMembersId.push(userId); } if (args && args.myTeam) @@ -131,7 +124,10 @@ module.exports = Self => { case 'isConfirmed': return {'o.confirmed': value ? 1 : 0}; case 'myTeam': - return {'c.salesPersonFk': {inq: teamIds}}; + if (value) + return {'c.salesPersonFk': {inq: teamMembersId}}; + else + return {'c.salesPersonFk': {nin: teamMembersId}}; case 'showEmpty': return {'o.total': {neq: value}}; case 'id': diff --git a/modules/order/back/methods/order/updateBasicData.js b/modules/order/back/methods/order/updateBasicData.js index ae5e5b74a..5104db87f 100644 --- a/modules/order/back/methods/order/updateBasicData.js +++ b/modules/order/back/methods/order/updateBasicData.js @@ -43,6 +43,7 @@ module.exports = Self => { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } + try { const order = await models.Order.findById(id, null, myOptions); const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions);