From 800b4984fff124d0419e0b8f1915b7dc7f7e334d Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 4 May 2022 07:46:47 +0200 Subject: [PATCH 1/3] fix(test): use ctx.options --- modules/client/back/models/client-credit.js | 10 ++++------ modules/client/back/models/specs/clientCredit.spec.js | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/modules/client/back/models/client-credit.js b/modules/client/back/models/client-credit.js index 96ac5a22a..7070dd6ae 100644 --- a/modules/client/back/models/client-credit.js +++ b/modules/client/back/models/client-credit.js @@ -11,20 +11,18 @@ module.exports = Self => { const clientCredit = await models.ClientCredit.find({ where: {clientFk: instance.clientFk}, order: 'created DESC' - }); + }, ctx.options); + if (clientCredit.length > maxCreditRows) { const creditToDestroy = []; for (const [index, credit] of clientCredit.entries()) { - console.log(index); - if (index >= maxCreditRows) { - console.log(index, 'BORRAR'); + if (index >= maxCreditRows) creditToDestroy.push(credit.id); - } } await models.ClientCredit.destroyAll({ id: {inq: creditToDestroy} - }); + }, ctx.options); } }); }; diff --git a/modules/client/back/models/specs/clientCredit.spec.js b/modules/client/back/models/specs/clientCredit.spec.js index 4d4c2bdee..7a42b7531 100644 --- a/modules/client/back/models/specs/clientCredit.spec.js +++ b/modules/client/back/models/specs/clientCredit.spec.js @@ -16,8 +16,8 @@ describe('Client Credit', () => { const ctx = {options}; const salesAssistant = await models.Account.findOne({ where: {name: 'salesAssistant'} - }, options); - ctx.options.accessToken = {userId: salesAssistant.id}; + }, ctx.options); + await models.ClientCredit.create({ amount: 355, clientFk: instance.id, @@ -25,8 +25,8 @@ describe('Client Credit', () => { }, ctx.options); rowsAfter = await models.ClientCredit.find({ - where: {clientFk: instance.id} - }, options); + where: {clientFk: instance.id}, + }, ctx.options); await tx.rollback(); } catch (e) { From 1cc199e26bdc79f01fba874300a73ce52fd37ed2 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 4 May 2022 10:26:13 +0200 Subject: [PATCH 2/3] refactor(client-credit) --- modules/client/back/models/client-credit.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/modules/client/back/models/client-credit.js b/modules/client/back/models/client-credit.js index 7070dd6ae..dedc9491a 100644 --- a/modules/client/back/models/client-credit.js +++ b/modules/client/back/models/client-credit.js @@ -7,21 +7,17 @@ module.exports = Self => { where: {id: 1} }); const maxCreditRows = clientConfig.maxCreditRows; - const clientCredit = await models.ClientCredit.find({ where: {clientFk: instance.clientFk}, - order: 'created DESC' + order: 'created DESC', + limit: maxCreditRows }, ctx.options); - if (clientCredit.length > maxCreditRows) { - const creditToDestroy = []; - for (const [index, credit] of clientCredit.entries()) { - if (index >= maxCreditRows) - creditToDestroy.push(credit.id); - } - + const lastCredit = clientCredit[maxCreditRows - 1]; + if (lastCredit) { await models.ClientCredit.destroyAll({ - id: {inq: creditToDestroy} + clientFk: instance.clientFk, + created: {lt: lastCredit.created} }, ctx.options); } }); From 0cae852dea9d91f89188bd3fb47e9d48bc6b2ca0 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 4 May 2022 10:28:17 +0200 Subject: [PATCH 3/3] refactor --- modules/client/back/models/client-credit.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/client/back/models/client-credit.js b/modules/client/back/models/client-credit.js index dedc9491a..01fe8214a 100644 --- a/modules/client/back/models/client-credit.js +++ b/modules/client/back/models/client-credit.js @@ -3,10 +3,9 @@ module.exports = Self => { const instance = ctx.instance; const models = Self.app.models; - const clientConfig = await models.ClientConfig.findOne({ - where: {id: 1} - }); + const clientConfig = await models.ClientConfig.findOne(); const maxCreditRows = clientConfig.maxCreditRows; + const clientCredit = await models.ClientCredit.find({ where: {clientFk: instance.clientFk}, order: 'created DESC',