feat(client_credit): remove de old rows
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
57b75dc10e
commit
b40b8d931d
|
@ -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;
|
|
@ -342,9 +342,9 @@ INSERT INTO `vn`.`clientManaCache`(`clientFk`, `mana`, `dated`)
|
|||
(1103, 0, 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
|
||||
(200);
|
||||
(200, 10);
|
||||
|
||||
INSERT INTO `vn`.`address`(`id`, `nickname`, `street`, `city`, `postalCode`, `provinceFk`, `phone`, `mobile`, `isActive`, `clientFk`, `agencyModeFk`, `longitude`, `latitude`, `isEqualizated`, `isDefaultAddress`)
|
||||
VALUES
|
||||
|
|
|
@ -14,6 +14,9 @@
|
|||
"Client": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"ClientConfig": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"ClientContact": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue