Skip to content

Commit

Permalink
fix list communities (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Pedro da Silva authored Jan 11, 2023
1 parent 0d2d81f commit 8d10e42
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions packages/api/src/routes/v2/community/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ export default (route: Router): void => {
* type: string
* required: false
* description: especify fields to return
* - in: query
* name: state
* schema:
* type: string
* enum: [base, ubi]
* required: false
* description: community state
* responses:
* "200":
* description: OK
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/services/ubi/community/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class CommunityDetailsService {
estimatedDuration: community.metrics?.length
? community.metrics[0].estimatedDuration
: 0,
communityId,
};
}

Expand Down
39 changes: 33 additions & 6 deletions packages/core/src/services/ubi/community/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class CommunityListService {
status?: 'valid' | 'pending';
review?: 'pending' | 'claimed' | 'declined' | 'accepted' | 'accepted';
ambassadorAddress?: string;
state?: string | string[];
}): Promise<{ count: number; rows: CommunityAttributes[] }> {
let extendedWhere: WhereOptions<CommunityAttributes> = {};
const orderOption: OrderItem[] = [];
Expand Down Expand Up @@ -441,7 +442,7 @@ export class CommunityListService {
// remove empty elements
communitiesId = communitiesId.filter(Number);

let states: ({
const states: ({
communityId: number;
claims: number;
claimed: string;
Expand All @@ -450,12 +451,38 @@ export class CommunityListService {
contributed: string;
contributors: number;
managers: number;
} | null)[];
} | null)[] = [];
// TODO: review validation
if (returnState) {
const promises = communitiesId.map((id) =>
this.communityDetailsService.getBaseState(id!)
);
states = await Promise.all(promises);
const batch = config.cronJobBatchSize;
for (let i = 0; ; i = i + batch) {
const communities = communitiesId.slice(i, i + batch);
const promises = communities.map(async (id) => {
let baseState: any = {},
ubiState: any = {};
baseState = await this.communityDetailsService.getBaseState(
id!
);
if (
!!query.state &&
(query.state === 'ubi' || query.state.indexOf('ubi') !== -1)
) {
ubiState = await this.communityDetailsService.getUbiState(
id!
);
}

return {
...baseState,
...ubiState,
} as any;
});
const result = await Promise.all(promises);
states.push(...result);
if (i + batch > communitiesId.length) {
break;
}
}
}

//formate response
Expand Down

0 comments on commit 8d10e42

Please sign in to comment.