refactor(client-credit)
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-05-04 10:26:13 +02:00
parent 800b4984ff
commit 1cc199e26b
1 changed files with 6 additions and 10 deletions

View File

@ -7,21 +7,17 @@ module.exports = Self => {
where: {id: 1} 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
}, ctx.options); }, ctx.options);
if (clientCredit.length > maxCreditRows) { const lastCredit = clientCredit[maxCreditRows - 1];
const creditToDestroy = []; if (lastCredit) {
for (const [index, credit] of clientCredit.entries()) {
if (index >= maxCreditRows)
creditToDestroy.push(credit.id);
}
await models.ClientCredit.destroyAll({ await models.ClientCredit.destroyAll({
id: {inq: creditToDestroy} clientFk: instance.clientFk,
created: {lt: lastCredit.created}
}, ctx.options); }, ctx.options);
} }
}); });