2022-05-02 09:18:39 +00:00
|
|
|
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}
|
|
|
|
});
|
2022-05-12 06:06:30 +00:00
|
|
|
|
|
|
|
let rowsBefore;
|
2022-05-02 09:18:39 +00:00
|
|
|
let rowsAfter;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2023-01-23 14:24:00 +00:00
|
|
|
const salesAssistant = await models.VnUser.findOne({
|
2022-05-02 09:18:39 +00:00
|
|
|
where: {name: 'salesAssistant'}
|
2022-05-12 06:06:30 +00:00
|
|
|
}, options);
|
|
|
|
|
|
|
|
rowsBefore = await models.ClientCredit.find({
|
|
|
|
where: {clientFk: instance.id},
|
2022-05-12 07:47:47 +00:00
|
|
|
order: 'created DESC',
|
2022-05-12 06:06:30 +00:00
|
|
|
}, options);
|
2022-05-04 05:46:47 +00:00
|
|
|
|
2022-05-03 13:17:34 +00:00
|
|
|
await models.ClientCredit.create({
|
|
|
|
amount: 355,
|
|
|
|
clientFk: instance.id,
|
|
|
|
workerFk: salesAssistant.id
|
2022-05-12 06:06:30 +00:00
|
|
|
}, options);
|
2022-05-02 09:18:39 +00:00
|
|
|
|
|
|
|
rowsAfter = await models.ClientCredit.find({
|
2022-05-04 05:46:47 +00:00
|
|
|
where: {clientFk: instance.id},
|
2022-05-12 07:47:47 +00:00
|
|
|
order: 'created DESC',
|
2022-05-12 06:06:30 +00:00
|
|
|
}, options);
|
2022-05-02 09:18:39 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2022-05-12 07:47:47 +00:00
|
|
|
const FirstRowsBefore = rowsBefore[0];
|
|
|
|
const FirstRowsAfter = rowsAfter[0];
|
|
|
|
|
|
|
|
expect(FirstRowsAfter.id).toBeGreaterThan(FirstRowsBefore.id);
|
|
|
|
|
2022-05-12 06:06:30 +00:00
|
|
|
expect(rowsBefore.length).toBeGreaterThanOrEqual(clientConfig.maxCreditRows);
|
2022-05-02 09:18:39 +00:00
|
|
|
expect(rowsAfter.length).toEqual(clientConfig.maxCreditRows);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|