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

24 lines
766 B
JavaScript
Raw Normal View History

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();
const maxCreditRows = clientConfig.maxCreditRows;
2022-05-04 08:28:17 +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) {
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);
}
});
};