Compare commits

..

No commits in common. "8077-sumDefaulterBackFix" and "dev" have entirely different histories.

2 changed files with 27 additions and 58 deletions

View File

@ -21,7 +21,7 @@ module.exports = Self => {
} }
], ],
returns: { returns: {
type: 'object', type: ['object'],
root: true root: true
}, },
http: { http: {
@ -41,28 +41,23 @@ module.exports = Self => {
switch (param) { switch (param) {
case 'search': case 'search':
return {or: [ return {or: [
{'c.id': value}, {'d.clientFk': value},
{'c.name': {like: `%${value}%`}} {'d.clientName': {like: `%${value}%`}}
]}; ]};
} }
}); });
const date = Date.vnNew(); filter = mergeFilters(ctx.args.filter, {where});
date.setHours(0, 0, 0, 0);
filter = mergeFilters({where: {'d.created': date, 'd.amount': {gt: 0}}}, ctx.args.filter);
filter = mergeFilters(filter, {where});
const stmts = []; const stmts = [];
let stmt = new ParameterizedSQL( const date = Date.vnNew();
`CREATE OR REPLACE TEMPORARY TABLE tmp.defaulters date.setHours(0, 0, 0, 0);
WITH clientObservations AS const stmt = new ParameterizedSQL(
(SELECT clientFk,text, created, workerFk `SELECT *
FROM vn.clientObservation FROM (
GROUP BY clientFk SELECT
ORDER BY created DESC DISTINCT c.id clientFk,
)SELECT c.id clientFk,
c.name clientName, c.name clientName,
c.salesPersonFk, c.salesPersonFk,
c.businessTypeFk = 'worker' isWorker, c.businessTypeFk = 'worker' isWorker,
@ -85,43 +80,36 @@ module.exports = Self => {
JOIN client c ON c.id = d.clientFk JOIN client c ON c.id = d.clientFk
JOIN country cn ON cn.id = c.countryFk JOIN country cn ON cn.id = c.countryFk
JOIN payMethod pm ON pm.id = c.payMethodFk JOIN payMethod pm ON pm.id = c.payMethodFk
LEFT JOIN clientObservations co ON co.clientFk = c.id LEFT JOIN clientObservation co ON co.clientFk = c.id
LEFT JOIN account.user u ON u.id = c.salesPersonFk LEFT JOIN account.user u ON u.id = c.salesPersonFk
LEFT JOIN account.user uw ON uw.id = co.workerFk LEFT JOIN account.user uw ON uw.id = co.workerFk
LEFT JOIN ( LEFT JOIN (
SELECT r1.started, r1.clientFk, r1.finished SELECT r1.started, r1.clientFk, r1.finished
FROM recovery r1 FROM recovery r1
JOIN ( JOIN (
SELECT MAX(started) maxStarted, clientFk SELECT MAX(started) AS maxStarted, clientFk
FROM recovery FROM recovery
GROUP BY clientFk GROUP BY clientFk
) r2 ON r1.clientFk = r2.clientFk ) r2 ON r1.clientFk = r2.clientFk
AND r1.started = r2.maxStarted AND r1.started = r2.maxStarted
WHERE r1.finished
GROUP BY r1.clientFk
) r ON r.clientFk = c.id ) r ON r.clientFk = c.id
LEFT JOIN workerDepartment wd ON wd.workerFk = u.id LEFT JOIN workerDepartment wd ON wd.workerFk = u.id
LEFT JOIN department dp ON dp.id = wd.departmentFk`); LEFT JOIN department dp ON dp.id = wd.departmentFk
WHERE
d.created = ?
AND d.amount > 0
ORDER BY co.created DESC) d`
, [date]);
stmt.merge(conn.makeWhere(filter.where)); stmt.merge(conn.makeWhere(filter.where));
stmts.push(stmt); stmt.merge(`GROUP BY d.clientFk`);
stmt = new ParameterizedSQL(`
SELECT SUM(amount) amount
FROM tmp.defaulters
`);
stmts.push(stmt);
stmt = new ParameterizedSQL(`
SELECT *
FROM tmp.defaulters
`);
stmt.merge(conn.makeOrderBy(filter.order)); stmt.merge(conn.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter));
const itemsIndex = stmts.push(stmt) - 1; const itemsIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions); const result = await conn.executeStmt(sql, myOptions);
return {defaulters: result[itemsIndex], amount: result[itemsIndex - 1][0].amount}; return itemsIndex === 0 ? result : result[itemsIndex];
}; };
}; };

View File

@ -11,10 +11,10 @@ describe('defaulter filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}}; const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}};
const result = await models.Defaulter.filter(ctx, null, options); const result = await models.Defaulter.filter(ctx, null, options);
const firstRow = result.defaulters[0]; const firstRow = result[0];
expect(firstRow.clientFk).toEqual(1101); expect(firstRow.clientFk).toEqual(1101);
expect(result.defaulters.length).toEqual(5); expect(result.length).toEqual(5);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
@ -31,7 +31,7 @@ describe('defaulter filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 1101}}; const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 1101}};
const result = await models.Defaulter.filter(ctx, null, options); const result = await models.Defaulter.filter(ctx, null, options);
const firstRow = result.defaulters[0]; const firstRow = result[0];
expect(firstRow.clientFk).toEqual(1101); expect(firstRow.clientFk).toEqual(1101);
@ -50,7 +50,7 @@ describe('defaulter filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 'Petter Parker'}}; const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 'Petter Parker'}};
const result = await models.Defaulter.filter(ctx, null, options); const result = await models.Defaulter.filter(ctx, null, options);
const firstRow = result.defaulters[0]; const firstRow = result[0];
expect(firstRow.clientName).toEqual('Petter Parker'); expect(firstRow.clientName).toEqual('Petter Parker');
@ -60,23 +60,4 @@ describe('defaulter filter()', () => {
throw e; throw e;
} }
}); });
it('should return the defaulter the sum of every defaulters', async() => {
const tx = await models.Defaulter.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 1101}};
const {defaulters, amount} = await models.Defaulter.filter(ctx, null, options);
const total = defaulters.reduce((total, row) => total + row.amount, 0);
expect(total).toEqual(amount);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
}); });