Merge branch '3409-clientCredit_maxRows' of https://gitea.verdnatura.es/verdnatura/salix into 3409-clientCredit_maxRows
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-05-09 13:19:38 +02:00
commit a00728384f
2 changed files with 13 additions and 20 deletions

View File

@ -3,28 +3,21 @@ module.exports = Self => {
const instance = ctx.instance; const instance = ctx.instance;
const models = Self.app.models; const models = Self.app.models;
const clientConfig = await models.ClientConfig.findOne({ const clientConfig = await models.ClientConfig.findOne();
where: {id: 1}
});
const maxCreditRows = clientConfig.maxCreditRows; const maxCreditRows = clientConfig.maxCreditRows;
const clientCredit = await models.ClientCredit.find({ const clientCredit = await models.ClientCredit.find({
where: {clientFk: instance.clientFk}, where: {clientFk: instance.clientFk},
order: 'created DESC' order: 'created DESC',
}); limit: maxCreditRows
if (clientCredit.length > maxCreditRows) { }, ctx.options);
const creditToDestroy = [];
for (const [index, credit] of clientCredit.entries()) {
console.log(index);
if (index >= maxCreditRows) {
console.log(index, 'BORRAR');
creditToDestroy.push(credit.id);
}
}
const lastCredit = clientCredit[maxCreditRows - 1];
if (lastCredit) {
await models.ClientCredit.destroyAll({ await models.ClientCredit.destroyAll({
id: {inq: creditToDestroy} clientFk: instance.clientFk,
}); created: {lt: lastCredit.created}
}, ctx.options);
} }
}); });
}; };

View File

@ -16,8 +16,8 @@ describe('Client Credit', () => {
const ctx = {options}; const ctx = {options};
const salesAssistant = await models.Account.findOne({ const salesAssistant = await models.Account.findOne({
where: {name: 'salesAssistant'} where: {name: 'salesAssistant'}
}, options); }, ctx.options);
ctx.options.accessToken = {userId: salesAssistant.id};
await models.ClientCredit.create({ await models.ClientCredit.create({
amount: 355, amount: 355,
clientFk: instance.id, clientFk: instance.id,
@ -25,8 +25,8 @@ describe('Client Credit', () => {
}, ctx.options); }, ctx.options);
rowsAfter = await models.ClientCredit.find({ rowsAfter = await models.ClientCredit.find({
where: {clientFk: instance.id} where: {clientFk: instance.id},
}, options); }, ctx.options);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {