From 5acef1fcc0ea544941d642b0dcce17ac3bea2e44 Mon Sep 17 00:00:00 2001 From: jcasado Date: Thu, 4 Apr 2024 13:53:23 +0200 Subject: [PATCH 1/6] refs #6641 add button my team --- modules/claim/front/search-panel/index.html | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/claim/front/search-panel/index.html b/modules/claim/front/search-panel/index.html index fbc527d60..6465eb621 100644 --- a/modules/claim/front/search-panel/index.html +++ b/modules/claim/front/search-panel/index.html @@ -73,5 +73,12 @@ + + + From 1c6d9139261ebf507251194077880bfd0a080947 Mon Sep 17 00:00:00 2001 From: jcasado Date: Thu, 4 Apr 2024 14:28:39 +0200 Subject: [PATCH 2/6] refs #6641 addFilterCollegues --- modules/claim/back/methods/claim/filter.js | 29 ++++++++++++++++++- .../back/methods/claim/specs/filter.spec.js | 19 ++++++++++++ modules/claim/front/search-panel/index.html | 8 ++--- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index 2daee6413..b1d1cb3a7 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -79,7 +79,12 @@ module.exports = Self => { type: 'number', description: 'The claimResponsible id', http: {source: 'query'} - } + }, + { + arg: 'myTeam', + type: 'boolean', + description: `Team partners` + }, ], returns: { type: ['object'], @@ -92,6 +97,7 @@ module.exports = Self => { }); Self.filter = async(ctx, filter, options) => { + const userId = ctx.req.accessToken.userId; const models = Self.app.models; const conn = Self.dataSource.connector; const args = ctx.args; @@ -121,6 +127,22 @@ module.exports = Self => { claimIdsByClaimResponsibleFk = claims.map(claim => claim.claimFk); } + // 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); + + if (teamMembersId.length == 0) + teamMembersId.push(userId); + } + const where = buildFilter(args, (param, value) => { switch (param) { case 'search': @@ -152,6 +174,11 @@ module.exports = Self => { to.setHours(23, 59, 59, 999); return {'cl.created': {between: [value, to]}}; + case 'myTeam': + if (value) + return {'cl.workerFk': {inq: teamMembersId}}; + else + return {'cl.workerFk': {nin: teamMembersId}}; } }); diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index 49e258505..a25ec8d27 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -97,4 +97,23 @@ describe('claim filter()', () => { throw e; } }); + + it('should now return the tickets from the worker team', async() => { + const tx = await models.Claim.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toBeGreaterThan(20); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); diff --git a/modules/claim/front/search-panel/index.html b/modules/claim/front/search-panel/index.html index 6465eb621..260f86801 100644 --- a/modules/claim/front/search-panel/index.html +++ b/modules/claim/front/search-panel/index.html @@ -70,15 +70,15 @@ label="Responsible"> - - - - + + + + From 84e6e7f7bf740cd2b551d43bbd3d988be32834f2 Mon Sep 17 00:00:00 2001 From: jcasado Date: Thu, 4 Apr 2024 14:53:26 +0200 Subject: [PATCH 3/6] refs #6641 test fixtures --- db/dump/fixtures.before.sql | 10 +++++++--- modules/claim/back/methods/claim/filter.js | 15 ++++++++++++--- .../claim/back/methods/claim/specs/filter.spec.js | 9 +++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 1dad68b2c..f874c8aee 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -1827,9 +1827,9 @@ INSERT INTO `vn`.`claimState`(`id`, `code`, `description`, `roleFk`, `priority`, INSERT INTO `vn`.`claim`(`id`, `ticketCreated`, `claimStateFk`, `clientFk`, `workerFk`, `responsibility`, `isChargedToMana`, `created`, `packages`, `ticketFk`) VALUES - (1, util.VN_CURDATE(), 1, 1101, 18, 3, 0, util.VN_CURDATE(), 0, 11), + (1, util.VN_CURDATE(), 1, 1101, 19, 3, 0, util.VN_CURDATE(), 0, 11), (2, util.VN_CURDATE(), 4, 1101, 18, 3, 0, util.VN_CURDATE(), 1, 16), - (3, util.VN_CURDATE(), 3, 1101, 18, 1, 1, util.VN_CURDATE(), 5, 7), + (3, util.VN_CURDATE(), 3, 1101, 19, 1, 1, util.VN_CURDATE(), 5, 7), (4, util.VN_CURDATE(), 3, 1104, 18, 5, 0, util.VN_CURDATE(), 10, 8); INSERT INTO `vn`.`claimObservation` (`claimFk`, `workerFk`, `text`, `created`) @@ -3745,4 +3745,8 @@ INSERT INTO vn.ticketLog (originFk,userFk,`action`,creationDate,changedModel,new INSERT INTO `vn`.`supplierDms`(`supplierFk`, `dmsFk`, `editorFk`) VALUES - (1, 10, 9); \ No newline at end of file + (1, 10, 9); + +INSERT INTO vn.workerTeam +(id, team, workerFk) +VALUES(8, 1, 19); diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index b1d1cb3a7..21d6ff80f 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -4,10 +4,15 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { - Self.remoteMethodCtx('filter', { + Self.remoteMethod('filter', { description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [ + { + arg: 'ctx', + type: 'object', + http: {source: 'context'} + }, { arg: 'filter', type: 'object', @@ -97,7 +102,11 @@ module.exports = Self => { }); Self.filter = async(ctx, filter, options) => { - const userId = ctx.req.accessToken.userId; + const userId = ctx?.req?.accessToken?.userId; + console.log('ctx', ctx); + console.log('ctx.req', ctx.req); + console.log('ctx.req.accessToken', ctx.req.accessToken); + console.log('ctx.req.accessToken.userId', ctx.req.accessToken.userId); const models = Self.app.models; const conn = Self.dataSource.connector; const args = ctx.args; @@ -143,7 +152,7 @@ module.exports = Self => { teamMembersId.push(userId); } - const where = buildFilter(args, (param, value) => { + const where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'search': return /^\d+$/.test(value) diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index a25ec8d27..677015c4d 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -1,6 +1,7 @@ const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; -describe('claim filter()', () => { +fdescribe('claim filter()', () => { it('should return 1 result filtering by id', async() => { const tx = await app.models.Claim.beginTransaction({}); @@ -104,11 +105,11 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; + const ctx = {req: {accessToken: {userId: 9}}, args: {myTeam: true}}; const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + const result = await models.Claim.filter(ctx, filter, options); - expect(result.length).toBeGreaterThan(20); + expect(result.length).toEqual(2); await tx.rollback(); } catch (e) { From 26c74a3e71b321bb0c5d35768dcb47581a95860f Mon Sep 17 00:00:00 2001 From: jcasado Date: Mon, 8 Apr 2024 13:03:15 +0200 Subject: [PATCH 4/6] refs #6641 fix test --- modules/claim/back/methods/claim/filter.js | 6 +---- .../back/methods/claim/specs/filter.spec.js | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index 21d6ff80f..56d4fdfed 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -102,11 +102,7 @@ module.exports = Self => { }); Self.filter = async(ctx, filter, options) => { - const userId = ctx?.req?.accessToken?.userId; - console.log('ctx', ctx); - console.log('ctx.req', ctx.req); - console.log('ctx.req.accessToken', ctx.req.accessToken); - console.log('ctx.req.accessToken.userId', ctx.req.accessToken.userId); + const userId = ctx.req.accessToken.userId; const models = Self.app.models; const conn = Self.dataSource.connector; const args = ctx.args; diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index 677015c4d..08e680e97 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -1,14 +1,24 @@ const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models; -fdescribe('claim filter()', () => { +describe('claim filter()', () => { + let ctx; + beforeEach(() => { + ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'http://localhost'} + } + }; + }); + it('should return 1 result filtering by id', async() => { const tx = await app.models.Claim.beginTransaction({}); try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({args: {filter: {}, search: 1}}, null, options); + const result = await app.models.Claim.filter({...ctx, args: {filter: {}, search: 1}}, null, options); expect(result.length).toEqual(1); expect(result[0].id).toEqual(1); @@ -26,7 +36,7 @@ fdescribe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({args: {filter: {}, search: 'Tony Stark'}}, null, options); + const result = await app.models.Claim.filter({...ctx, args: {filter: {}, search: 'Tony Stark'}}, null, options); expect(result.length).toEqual(1); expect(result[0].id).toEqual(4); @@ -44,7 +54,7 @@ fdescribe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({args: {filter: {}, workerFk: 18}}, null, options); + const result = await app.models.Claim.filter({...ctx, args: {filter: {}, workerFk: 18}}, null, options); expect(result.length).toEqual(4); expect(result[0].id).toEqual(1); @@ -65,7 +75,7 @@ fdescribe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({args: {filter: {}, itemFk: 2}}, null, options); + const result = await app.models.Claim.filter({...ctx, args: {filter: {}, itemFk: 2}}, null, options); expect(result.length).toEqual(3); expect(result[0].id).toEqual(1); @@ -85,7 +95,7 @@ fdescribe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({args: {filter: {}, claimResponsibleFk: 7}}, null, options); + const result = await app.models.Claim.filter({...ctx, args: {filter: {}, claimResponsibleFk: 7}}, null, options); expect(result.length).toEqual(3); expect(result[0].id).toEqual(2); @@ -99,15 +109,14 @@ fdescribe('claim filter()', () => { } }); - it('should now return the tickets from the worker team', async() => { + it('should now return claims from the worker team', async() => { const tx = await models.Claim.beginTransaction({}); try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 9}}, args: {myTeam: true}}; const filter = {}; - const result = await models.Claim.filter(ctx, filter, options); + const result = await models.Claim.filter({...ctx, args: {filter: {}, itemFk: null, myTeam: true}}, filter, options); expect(result.length).toEqual(2); From 7d925bac87ef0a9b2b14a613e3e89f3147813728 Mon Sep 17 00:00:00 2001 From: jcasado Date: Wed, 10 Apr 2024 13:16:41 +0200 Subject: [PATCH 5/6] refs #6641 fix PR changes --- db/dump/fixtures.before.sql | 6 +++--- modules/claim/back/methods/claim/filter.js | 7 +------ .../back/methods/claim/specs/filter.spec.js | 21 ++++++++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index f874c8aee..cd7c15dca 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -3747,6 +3747,6 @@ INSERT INTO `vn`.`supplierDms`(`supplierFk`, `dmsFk`, `editorFk`) VALUES (1, 10, 9); -INSERT INTO vn.workerTeam -(id, team, workerFk) -VALUES(8, 1, 19); +INSERT INTO vn.workerTeam(id, team, workerFk) + VALUES + (8, 1, 19); diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index 56d4fdfed..f60b6572e 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -4,15 +4,10 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { - Self.remoteMethod('filter', { + Self.remoteMethodCtx('filter', { description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [ - { - arg: 'ctx', - type: 'object', - http: {source: 'context'} - }, { arg: 'filter', type: 'object', diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index 08e680e97..acaa3c527 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -1,7 +1,7 @@ const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models; -describe('claim filter()', () => { +fdescribe('claim filter()', () => { let ctx; beforeEach(() => { ctx = { @@ -18,7 +18,8 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({...ctx, args: {filter: {}, search: 1}}, null, options); + ctx.args = {search: 1}; + const result = await app.models.Claim.filter(ctx, null, options); expect(result.length).toEqual(1); expect(result[0].id).toEqual(1); @@ -36,7 +37,8 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({...ctx, args: {filter: {}, search: 'Tony Stark'}}, null, options); + ctx.args = {search: 'Tony Stark'}; + const result = await app.models.Claim.filter(ctx, null, options); expect(result.length).toEqual(1); expect(result[0].id).toEqual(4); @@ -54,7 +56,8 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({...ctx, args: {filter: {}, workerFk: 18}}, null, options); + ctx.args = {workerFk: 18}; + const result = await app.models.Claim.filter(ctx, null, options); expect(result.length).toEqual(4); expect(result[0].id).toEqual(1); @@ -75,7 +78,8 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({...ctx, args: {filter: {}, itemFk: 2}}, null, options); + ctx.args = {itemFk: 2}; + const result = await app.models.Claim.filter(ctx, null, options); expect(result.length).toEqual(3); expect(result[0].id).toEqual(1); @@ -95,7 +99,8 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({...ctx, args: {filter: {}, claimResponsibleFk: 7}}, null, options); + ctx.args = {claimResponsibleFk: 7}; + const result = await app.models.Claim.filter(ctx, null, options); expect(result.length).toEqual(3); expect(result[0].id).toEqual(2); @@ -115,8 +120,8 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const filter = {}; - const result = await models.Claim.filter({...ctx, args: {filter: {}, itemFk: null, myTeam: true}}, filter, options); + ctx.args = {itemFk: null, myTeam: true}; + const result = await app.models.Claim.filter(ctx, null, options); expect(result.length).toEqual(2); From 0cf1ced7476685e3cea1ae0c3cc1ef9f9fe38db3 Mon Sep 17 00:00:00 2001 From: jcasado Date: Wed, 10 Apr 2024 13:17:43 +0200 Subject: [PATCH 6/6] remove focus --- modules/claim/back/methods/claim/specs/filter.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index acaa3c527..872f49aa3 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -1,7 +1,7 @@ const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models; -fdescribe('claim filter()', () => { +describe('claim filter()', () => { let ctx; beforeEach(() => { ctx = {