salix/modules/client/back/models/client-credit.js

31 lines
982 B
JavaScript
Raw Normal View History

module.exports = Self => {
Self.observe('after save', async ctx => {
const instance = ctx.instance;
const models = Self.app.models;
const clientConfig = await models.ClientConfig.findOne({
where: {id: 1}
});
const maxCreditRows = clientConfig.maxCreditRows;
const clientCredit = await models.ClientCredit.find({
where: {clientFk: instance.clientFk},
order: 'created DESC'
});
if (clientCredit.length > maxCreditRows) {
const creditToDestroy = [];
for (const [index, credit] of clientCredit.entries()) {
2022-05-03 13:17:34 +00:00
console.log(index);
if (index >= maxCreditRows) {
console.log(index, 'BORRAR');
creditToDestroy.push(credit.id);
2022-05-03 13:17:34 +00:00
}
}
2022-05-03 13:17:34 +00:00
await models.ClientCredit.destroyAll({
id: {inq: creditToDestroy}
});
}
});
};