feat(client_credit): remove de old rows
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-05-02 11:18:39 +02:00
parent 57b75dc10e
commit b40b8d931d
6 changed files with 103 additions and 2 deletions

View File

@ -0,0 +1,5 @@
ALTER TABLE `vn`.`clientConfig` ADD `maxCreditRows` int(11) NULL COMMENT 'Máximo número de registros a mantener en la tabla clientCredit';
UPDATE `vn`.`clientConfig`
SET `maxCreditRows` = 10
WHERE `id` = 1;

View File

@ -342,9 +342,9 @@ INSERT INTO `vn`.`clientManaCache`(`clientFk`, `mana`, `dated`)
(1103, 0, DATE_ADD(CURDATE(), INTERVAL -1 MONTH)), (1103, 0, DATE_ADD(CURDATE(), INTERVAL -1 MONTH)),
(1104, -30, DATE_ADD(CURDATE(), INTERVAL -1 MONTH)); (1104, -30, DATE_ADD(CURDATE(), INTERVAL -1 MONTH));
INSERT INTO `vn`.`clientConfig`(`riskTolerance`) INSERT INTO `vn`.`clientConfig`(`riskTolerance`, `maxCreditRows`)
VALUES VALUES
(200); (200, 10);
INSERT INTO `vn`.`address`(`id`, `nickname`, `street`, `city`, `postalCode`, `provinceFk`, `phone`, `mobile`, `isActive`, `clientFk`, `agencyModeFk`, `longitude`, `latitude`, `isEqualizated`, `isDefaultAddress`) INSERT INTO `vn`.`address`(`id`, `nickname`, `street`, `city`, `postalCode`, `provinceFk`, `phone`, `mobile`, `isActive`, `clientFk`, `agencyModeFk`, `longitude`, `latitude`, `isEqualizated`, `isDefaultAddress`)
VALUES VALUES

View File

@ -14,6 +14,9 @@
"Client": { "Client": {
"dataSource": "vn" "dataSource": "vn"
}, },
"ClientConfig": {
"dataSource": "vn"
},
"ClientContact": { "ClientContact": {
"dataSource": "vn" "dataSource": "vn"
}, },

View File

@ -0,0 +1,22 @@
{
"name": "ClientConfig",
"base": "VnModel",
"options": {
"mysql": {
"table": "clientConfig"
}
},
"properties": {
"id": {
"type": "Number",
"id": true,
"description": "Identifier"
},
"riskTolerance": {
"type": "Number"
},
"maxCreditRows": {
"type": "Number"
}
}
}

View File

@ -0,0 +1,27 @@
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()) {
if (index >= maxCreditRows)
creditToDestroy.push(credit.id);
}
await models.ClientCredit.destroyAll({
id: {inq: creditToDestroy}
});
}
});
};

View File

@ -0,0 +1,44 @@
const models = require('vn-loopback/server/server').models;
describe('Client Credit', () => {
const instance = {id: 1101, name: 'Bruce Banner'};
describe('after save', () => {
it('should delete old rows of clientCredit', async() => {
const tx = await models.ClientCredit.beginTransaction({});
const clientConfig = await models.ClientConfig.findOne({
where: {id: 1}
});
let rowsBefore;
let rowsAfter;
try {
const options = {transaction: tx};
const context = {options};
rowsBefore = await models.ClientCredit.find({
where: {clientFk: instance.id}
}, options);
const salesAssistant = await models.Account.findOne({
where: {name: 'salesAssistant'}
}, options);
context.options.accessToken = {userId: salesAssistant.id};
await models.Client.changeCredit(context, instance, {credit: 350});
rowsAfter = await models.ClientCredit.find({
where: {clientFk: instance.id}
}, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(rowsBefore.length).toEqual(11);
expect(rowsAfter.length).toEqual(clientConfig.maxCreditRows);
});
});
});