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;
|
|
|
|
|
2022-05-04 08:28:17 +00:00
|
|
|
const clientConfig = await models.ClientConfig.findOne();
|
2022-05-02 09:18:39 +00:00
|
|
|
const maxCreditRows = clientConfig.maxCreditRows;
|
2022-05-04 08:28:17 +00:00
|
|
|
|
2022-05-02 09:18:39 +00:00
|
|
|
const clientCredit = await models.ClientCredit.find({
|
|
|
|
where: {clientFk: instance.clientFk},
|
2022-05-04 08:26:13 +00:00
|
|
|
order: 'created DESC',
|
|
|
|
limit: maxCreditRows
|
2022-05-04 05:46:47 +00:00
|
|
|
}, ctx.options);
|
|
|
|
|
2022-05-04 08:26:13 +00:00
|
|
|
const lastCredit = clientCredit[maxCreditRows - 1];
|
|
|
|
if (lastCredit) {
|
2022-05-02 09:18:39 +00:00
|
|
|
await models.ClientCredit.destroyAll({
|
2022-05-04 08:26:13 +00:00
|
|
|
clientFk: instance.clientFk,
|
|
|
|
created: {lt: lastCredit.created}
|
2022-05-04 05:46:47 +00:00
|
|
|
}, ctx.options);
|
2022-05-02 09:18:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|