2022-05-02 09:18:39 +00:00
|
|
|
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');
|
2022-05-02 09:18:39 +00:00
|
|
|
creditToDestroy.push(credit.id);
|
2022-05-03 13:17:34 +00:00
|
|
|
}
|
2022-05-02 09:18:39 +00:00
|
|
|
}
|
2022-05-03 13:17:34 +00:00
|
|
|
|
2022-05-02 09:18:39 +00:00
|
|
|
await models.ClientCredit.destroyAll({
|
|
|
|
id: {inq: creditToDestroy}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|