From e3015a655917c69cfcfff66c11356db7141fe1bd Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 4 Sep 2024 14:52:53 +0200 Subject: [PATCH 1/5] feat: refs #7524 no apply limit --- loopback/common/models/vn-model.js | 10 +++++++--- modules/item/back/methods/item/getBalance.js | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/loopback/common/models/vn-model.js b/loopback/common/models/vn-model.js index a11bed11d..269ed673d 100644 --- a/loopback/common/models/vn-model.js +++ b/loopback/common/models/vn-model.js @@ -28,19 +28,19 @@ module.exports = function(Self) { }); this.beforeRemote('**', async ctx => { - if (!this.hasFilter(ctx)) return; + if (this.hasNoLimit(ctx) || !this.hasFilter(ctx)) return; const defaultLimit = this.app.orm.selectLimit; const filter = ctx.args.filter || {limit: defaultLimit}; - if (filter.limit > defaultLimit) { + if (!filter.limit || filter.limit > defaultLimit) { filter.limit = defaultLimit; ctx.args.filter = filter; } }); this.afterRemote('**', async ctx => { - if (!this.hasFilter(ctx)) return; + if (this.hasNoLimit(ctx) || !this.hasFilter(ctx)) return; const {result} = ctx; const length = Array.isArray(result) ? result.length : result ? 1 : 0; @@ -351,6 +351,10 @@ module.exports = function(Self) { hasFilter(ctx) { return ctx.req.method.toUpperCase() === 'GET' && ctx.method.accepts.some(x => x.arg === 'filter' && x.type.toLowerCase() === 'object'); + }, + + hasNoLimit(ctx) { + return ctx.method.accepts.some(x => x.arg.toLowerCase() === 'nolimit') && ctx.args.noLimit; } }); diff --git a/modules/item/back/methods/item/getBalance.js b/modules/item/back/methods/item/getBalance.js index 207f8020f..1a4c7999d 100644 --- a/modules/item/back/methods/item/getBalance.js +++ b/modules/item/back/methods/item/getBalance.js @@ -8,6 +8,10 @@ module.exports = Self => { required: true, description: 'Filter defining where and paginated data', http: {source: 'query'} + }, { + arg: 'noLimit', + type: 'Boolean', + required: false, }], returns: { type: ['Object'], @@ -19,7 +23,7 @@ module.exports = Self => { } }); - Self.getBalance = async(ctx, filter, options) => { + Self.getBalance = async(ctx, filter, noLimit, options) => { const myOptions = {userId: ctx.req.accessToken.userId}; if (typeof options == 'object') -- 2.40.1 From d82f9b2cd6e2f946ff37621e4226e4bdf91ac35b Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 4 Sep 2024 14:55:59 +0200 Subject: [PATCH 2/5] chore: refs #7524 fix test --- modules/item/back/methods/item/specs/getBalance.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/item/back/methods/item/specs/getBalance.spec.js b/modules/item/back/methods/item/specs/getBalance.spec.js index 95de3cc50..cef206411 100644 --- a/modules/item/back/methods/item/specs/getBalance.spec.js +++ b/modules/item/back/methods/item/specs/getBalance.spec.js @@ -23,7 +23,7 @@ describe('item getBalance()', () => { date: null } }; - const results = await models.Item.getBalance(ctx, filter, options); + const results = await models.Item.getBalance(ctx, filter, true, options); const result = results.find(element => element.clientType == 'loses'); @@ -57,8 +57,8 @@ describe('item getBalance()', () => { } }; - const firstItemBalance = await models.Item.getBalance(ctx, firstFilter, options); - const secondItemBalance = await models.Item.getBalance(ctx, secondFilter, options); + const firstItemBalance = await models.Item.getBalance(ctx, firstFilter, true, options); + const secondItemBalance = await models.Item.getBalance(ctx, secondFilter, true, options); expect(firstItemBalance[9].claimFk).toEqual(null); expect(secondItemBalance[7].claimFk).toEqual(1); -- 2.40.1 From d98f0a24936e1bf9b4091f006c10926a9eaa4d0a Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 10 Sep 2024 13:15:58 +0200 Subject: [PATCH 3/5] chore: refs #7524 rollback --- modules/item/back/methods/item/getBalance.js | 9 +++------ modules/item/back/methods/item/specs/getBalance.spec.js | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/modules/item/back/methods/item/getBalance.js b/modules/item/back/methods/item/getBalance.js index 1a4c7999d..c835cd56f 100644 --- a/modules/item/back/methods/item/getBalance.js +++ b/modules/item/back/methods/item/getBalance.js @@ -8,10 +8,6 @@ module.exports = Self => { required: true, description: 'Filter defining where and paginated data', http: {source: 'query'} - }, { - arg: 'noLimit', - type: 'Boolean', - required: false, }], returns: { type: ['Object'], @@ -20,10 +16,11 @@ module.exports = Self => { http: { path: `/getBalance`, verb: 'GET' - } + }, + noLimit: true }); - Self.getBalance = async(ctx, filter, noLimit, options) => { + Self.getBalance = async(ctx, filter, options) => { const myOptions = {userId: ctx.req.accessToken.userId}; if (typeof options == 'object') diff --git a/modules/item/back/methods/item/specs/getBalance.spec.js b/modules/item/back/methods/item/specs/getBalance.spec.js index cef206411..95de3cc50 100644 --- a/modules/item/back/methods/item/specs/getBalance.spec.js +++ b/modules/item/back/methods/item/specs/getBalance.spec.js @@ -23,7 +23,7 @@ describe('item getBalance()', () => { date: null } }; - const results = await models.Item.getBalance(ctx, filter, true, options); + const results = await models.Item.getBalance(ctx, filter, options); const result = results.find(element => element.clientType == 'loses'); @@ -57,8 +57,8 @@ describe('item getBalance()', () => { } }; - const firstItemBalance = await models.Item.getBalance(ctx, firstFilter, true, options); - const secondItemBalance = await models.Item.getBalance(ctx, secondFilter, true, options); + const firstItemBalance = await models.Item.getBalance(ctx, firstFilter, options); + const secondItemBalance = await models.Item.getBalance(ctx, secondFilter, options); expect(firstItemBalance[9].claimFk).toEqual(null); expect(secondItemBalance[7].claimFk).toEqual(1); -- 2.40.1 From 1ff1a97f29e9638fddabb1bbaa6fc3d539d815d4 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 10 Sep 2024 13:21:56 +0200 Subject: [PATCH 4/5] feat: refs #7524 check noLimit method key --- loopback/common/models/vn-model.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/loopback/common/models/vn-model.js b/loopback/common/models/vn-model.js index 269ed673d..596ec0967 100644 --- a/loopback/common/models/vn-model.js +++ b/loopback/common/models/vn-model.js @@ -28,7 +28,7 @@ module.exports = function(Self) { }); this.beforeRemote('**', async ctx => { - if (this.hasNoLimit(ctx) || !this.hasFilter(ctx)) return; + if (ctx.method.noLimit || !this.hasFilter(ctx)) return; const defaultLimit = this.app.orm.selectLimit; const filter = ctx.args.filter || {limit: defaultLimit}; @@ -40,7 +40,7 @@ module.exports = function(Self) { }); this.afterRemote('**', async ctx => { - if (this.hasNoLimit(ctx) || !this.hasFilter(ctx)) return; + if (ctx.method.noLimit || !this.hasFilter(ctx)) return; const {result} = ctx; const length = Array.isArray(result) ? result.length : result ? 1 : 0; @@ -352,10 +352,5 @@ module.exports = function(Self) { return ctx.req.method.toUpperCase() === 'GET' && ctx.method.accepts.some(x => x.arg === 'filter' && x.type.toLowerCase() === 'object'); }, - - hasNoLimit(ctx) { - return ctx.method.accepts.some(x => x.arg.toLowerCase() === 'nolimit') && ctx.args.noLimit; - } - }); }; -- 2.40.1 From e3b51c81f9a4bd67cffa95e7e5ced1c80971c08a Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 10 Sep 2024 16:37:46 +0200 Subject: [PATCH 5/5] refactor: refs #7524 wrap it up in a fn --- loopback/common/models/vn-model.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/loopback/common/models/vn-model.js b/loopback/common/models/vn-model.js index 596ec0967..e24653d13 100644 --- a/loopback/common/models/vn-model.js +++ b/loopback/common/models/vn-model.js @@ -28,7 +28,7 @@ module.exports = function(Self) { }); this.beforeRemote('**', async ctx => { - if (ctx.method.noLimit || !this.hasFilter(ctx)) return; + if (!this.hasFilter(ctx)) return; const defaultLimit = this.app.orm.selectLimit; const filter = ctx.args.filter || {limit: defaultLimit}; @@ -40,7 +40,7 @@ module.exports = function(Self) { }); this.afterRemote('**', async ctx => { - if (ctx.method.noLimit || !this.hasFilter(ctx)) return; + if (!this.hasFilter(ctx)) return; const {result} = ctx; const length = Array.isArray(result) ? result.length : result ? 1 : 0; @@ -349,8 +349,10 @@ module.exports = function(Self) { }, hasFilter(ctx) { - return ctx.req.method.toUpperCase() === 'GET' && - ctx.method.accepts.some(x => x.arg === 'filter' && x.type.toLowerCase() === 'object'); + const {method, req} = ctx; + if (method.noLimit) return false; + return req.method.toUpperCase() === 'GET' && + method.accepts.some(x => x.arg === 'filter' && x.type.toLowerCase() === 'object'); }, }); }; -- 2.40.1