Skip to content

Commit

Permalink
[bugfix] merge arrays list borrowers (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Vieira authored Jul 4, 2023
1 parent 0a89298 commit 1e3cff2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/core/src/services/microcredit/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import { config } from '../../..';
import { WhereOptions, literal, Op, Order } from 'sequelize';
import { MicroCreditApplications } from '../../interfaces/microCredit/applications';
import { utils } from '@impactmarket/core';
import { getUserRoles } from '../../subgraph/queries/user';
import { AppUser } from '../../interfaces/app/appUser';
import { MicroCreditBorrowers } from '../../interfaces/microCredit/borrowers';

function mergeArrays(arr1: any[], arr2: any[], key: string) {
function mergeArrays(arr1: any[], arr2: any[], key: string, mergeIfUndefined = true) {
const map = new Map(arr1.map(item => [item[key], item]));
arr2.forEach(item => {
map.has(item[key]) ? Object.assign(map.get(item[key]), item) : map.set(item[key], item);
if (map.has(item[key])) {
if (mergeIfUndefined) {
Object.assign(map.get(item[key]), item);
}
} else if (mergeIfUndefined) {
map.set(item[key], item);
}
});
return Array.from(map.values());
}
Expand Down Expand Up @@ -78,9 +83,10 @@ export default class MicroCreditList {
let usersToFilter: AppUser[] | undefined = undefined;
let order: Order | undefined;
let where: WhereOptions<MicroCreditBorrowers> | undefined;
const isOrderByBackendData = query.orderBy && query.orderBy.indexOf('performance') !== -1;

// build up database queries based on query params
if (query.orderBy && query.orderBy.indexOf('performance') !== -1) {
if (query.orderBy && isOrderByBackendData) {
order = [[literal('performance'), query.orderBy.indexOf('asc') !== -1 ? 'ASC' : 'DESC']];
}
if (query.filter === 'ontrack') {
Expand Down Expand Up @@ -152,7 +158,9 @@ export default class MicroCreditList {
// merge borrowers loans and profile
return {
count: rawBorrowers.count,
rows: mergeArrays(borrowers, usersToFilter, 'address')
rows: isOrderByBackendData
? mergeArrays(usersToFilter, borrowers, 'address', false)
: mergeArrays(borrowers, usersToFilter, 'address', false)
};
};

Expand Down Expand Up @@ -521,10 +529,7 @@ export default class MicroCreditList {
* description: pending, submitted, in-review, approved, rejected
*
*/
public getUserForm = async (
userId: number,
status?: string
) => {
public getUserForm = async (userId: number, status?: string) => {
return await models.microCreditForm.findOne({
where: {
userId,
Expand Down

0 comments on commit 1e3cff2

Please sign in to comment.