diff --git a/modules/route/back/methods/agency-term/filter.js b/modules/route/back/methods/agency-term/filter.js index 0bca9ddf7..ee79a04a7 100644 --- a/modules/route/back/methods/agency-term/filter.js +++ b/modules/route/back/methods/agency-term/filter.js @@ -1,4 +1,6 @@ const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { Self.remoteMethodCtx('filter', { @@ -9,7 +11,31 @@ module.exports = Self => { arg: 'filter', type: 'object', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', - http: {source: 'query'} + }, + { + arg: 'search', + type: 'string', + description: `If it's and integer searchs by invoiceInFk, otherwise it searchs by the supplierName`, + }, + { + arg: 'agencyModeFk', + type: 'integer', + description: 'The agency agencyModeFk id', + }, + { + arg: 'agencyFk', + type: 'integer', + description: 'The agencyFk id', + }, + { + arg: 'from', + type: 'date', + description: 'The from date filter', + }, + { + arg: 'to', + type: 'date', + description: 'The to date filter', } ], returns: { @@ -29,6 +55,25 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); + const where = buildFilter(ctx.args, (param, value) => { + switch (param) { + case 'search': + return /^\d+$/.test(value) + ? {'invoiceInFk': {inq: value}} + : {'supplierName': {like: `%${value}%`}}; + case 'agencyModeFk': + return {'agencyModeFk': value}; + case 'agencyFk': + return {'agencyFk': value}; + case 'from': + return {'created': {gte: value}}; + case 'to': + return {'created': {lte: value}}; + } + }); + + filter = mergeFilters(filter, {where}); + const stmts = []; const stmt = new ParameterizedSQL( `SELECT * @@ -61,7 +106,8 @@ module.exports = Self => { ) a` ); - stmt.merge(conn.makeSuffix(filter)); + stmt.merge(conn.makeWhere(filter.where)); + stmt.merge(conn.makePagination(filter)); const agencyTerm = stmts.push(stmt) - 1; const sql = ParameterizedSQL.join(stmts, ';'); diff --git a/modules/route/back/methods/agency-term/specs/filter.spec.js b/modules/route/back/methods/agency-term/specs/filter.spec.js index 69a7f987c..2f00b1de2 100644 --- a/modules/route/back/methods/agency-term/specs/filter.spec.js +++ b/modules/route/back/methods/agency-term/specs/filter.spec.js @@ -1,9 +1,12 @@ +const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models; describe('AgencyTerm filter()', () => { const authUserId = 9; + const today = new Date(); + today.setHours(2, 0, 0, 0); - it('should return all the tickets matching the filter', async() => { + it('should return all results matching the filter', async() => { const tx = await models.AgencyTerm.beginTransaction({}); try { @@ -23,4 +26,84 @@ describe('AgencyTerm filter()', () => { throw e; } }); + + it('should return results matching "search" searching by integer', async() => { + let ctx = { + args: { + search: 1, + } + }; + + let result = await app.models.AgencyTerm.filter(ctx); + + 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', + } + }; + + let result = await app.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 = new Date(); + from.setHours(0, 0, 0, 0); + + const to = new Date(); + to.setHours(23, 59, 59, 999); + + const ctx = { + args: { + from: from, + to: to + } + }; + + const results = await models.AgencyTerm.filter(ctx, options); + + expect(results.length).toBe(3); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should return results matching "agencyModeFk"', async() => { + let ctx = { + args: { + agencyModeFk: 1, + } + }; + + let result = await app.models.AgencyTerm.filter(ctx); + + expect(result.length).toEqual(1); + expect(result[0].routeFk).toEqual(1); + }); + + it('should return results matching "agencyFk"', async() => { + let ctx = { + args: { + agencyFk: 2, + } + }; + + let result = await app.models.AgencyTerm.filter(ctx); + + expect(result.length).toEqual(1); + expect(result[0].routeFk).toEqual(2); + }); }); diff --git a/modules/route/back/methods/route/specs/filter.spec.js b/modules/route/back/methods/route/specs/filter.spec.js index a4742c128..9b3c6edf4 100644 --- a/modules/route/back/methods/route/specs/filter.spec.js +++ b/modules/route/back/methods/route/specs/filter.spec.js @@ -1,4 +1,5 @@ const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('Route filter()', () => { let today = new Date(); @@ -17,29 +18,33 @@ describe('Route filter()', () => { expect(result[0].id).toEqual(1); }); - // #1428 cuadrar formato de horas - xit('should return the routes matching "from"', async() => { - let ctx = { - args: { - from: today, - } - }; + it('should return results matching "from" and "to"', async() => { + const tx = await models.Buy.beginTransaction({}); + const options = {transaction: tx}; - let result = await app.models.Route.filter(ctx); + try { + const from = new Date(); + from.setHours(0, 0, 0, 0); - expect(result.length).toEqual(7); - }); + const to = new Date(); + to.setHours(23, 59, 59, 999); - it('should return the routes matching "to"', async() => { - let ctx = { - args: { - to: today, - } - }; + const ctx = { + args: { + from: from, + to: to + } + }; - let result = await app.models.Route.filter(ctx); + const results = await models.Route.filter(ctx, options); - expect(result.length).toEqual(7); + expect(results.length).toBe(7); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the routes matching "m3"', async() => { diff --git a/modules/route/front/agency-term-search-panel/index.html b/modules/route/front/agency-term-search-panel/index.html new file mode 100644 index 000000000..fec6bf5eb --- /dev/null +++ b/modules/route/front/agency-term-search-panel/index.html @@ -0,0 +1,47 @@ +