Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IPCT1-755] - order by out of funds #278

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 69 additions & 50 deletions packages/core/src/services/ubi/community/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export class CommunityListService {
]
| undefined = undefined;

let funds:
| [
{
id: string;
estimatedFunds: string;
}
]
| undefined = undefined;

let communitiesId: (number | undefined)[];
let contractAddress: string[] = [];

Expand Down Expand Up @@ -183,25 +192,30 @@ export class CommunityListService {
}
case 'out_of_funds': {
// this requires extended
// query.extended = 'true';
// // check if there was another order previously
// if (
// orderOption.length === 0 &&
// !orderBeneficiary.active
// ) {
// const result = await this._getOutOfFunds(
// {
// limit: query.limit,
// offset: query.offset,
// },
// orderType
// );
// // communitiesId = result.map((el) => el.id);
// } else {
// // list communities out of funds after
// orderOutOfFunds.active = true;
// orderOutOfFunds.orderType = orderType;
// }
query.extended = 'true';
// check if there was another order previously
if (
orderOption.length === 0 &&
!orderBeneficiary.active
) {
funds = await this._communityEntities(
'estimatedFunds',
{
status: query.status,
limit: query.limit,
offset: query.offset,
},
extendedWhere,
orderType
);
contractAddress = funds!.map((el) =>
ethers.utils.getAddress(el.id)
);
} else {
// list communities out of funds after
orderOutOfFunds.active = true;
orderOutOfFunds.orderType = orderType;
}
break;
}
case 'newest':
Expand All @@ -217,7 +231,8 @@ export class CommunityListService {
!orderOutOfFunds.active
) {
beneficiariesState =
await this._getBeneficiaryState(
await this._communityEntities(
'beneficiaries',
{
status: query.status,
limit: query.limit,
Expand All @@ -241,7 +256,8 @@ export class CommunityListService {
} else {
// if searching by pending or did not pass the "state" on fields, do not search on the graph
if (query.status !== 'pending' && (!query.fields || query.fields.indexOf('state') !== -1)) {
beneficiariesState = await this._getBeneficiaryState(
beneficiariesState = await this._communityEntities(
'beneficiaries',
{
status: query.status,
limit: query.limit,
Expand Down Expand Up @@ -342,7 +358,8 @@ export class CommunityListService {
}

if (orderBeneficiary.active) {
beneficiariesState = await this._getBeneficiaryState(
beneficiariesState = await this._communityEntities(
'beneficiaries',
{
status: query.status,
limit: query.limit,
Expand All @@ -365,18 +382,30 @@ export class CommunityListService {
});
}

// if (orderOutOfFunds.active) {
// const result = await this._getOutOfFunds(
// {
// limit: query.limit,
// offset: query.offset,
// },
// orderOutOfFunds.orderType,
// communitiesId
// );
// // re-order by out of funds
// communitiesId = result.map((el) => el.id);
// }
if (orderOutOfFunds.active) {
funds = await this._communityEntities(
'estimatedFunds',
{
status: query.status,
limit: query.limit,
offset: query.offset,
},
{},
undefined,
contractAddress
);
contractAddress = funds!.map((el) =>
ethers.utils.getAddress(el.id)
);

// re-order IDs
communitiesId = contractAddress.map((el) => {
const community = communitiesResult.find(
(community) => community.contractAddress === el
);
return community?.id;
});
}

// remove empty elements
communitiesId = communitiesId.filter(Number);
Expand Down Expand Up @@ -450,7 +479,8 @@ export class CommunityListService {
return requestByAddress;
}

private async _getBeneficiaryState(
private async _communityEntities(
orderBy: string,
query: {
status?: string;
limit?: string;
Expand Down Expand Up @@ -484,7 +514,7 @@ export class CommunityListService {

if (contractAddress.length > 0) {
result = await communityEntities(
`orderBy: beneficiaries,
`orderBy: ${orderBy},
orderDirection: ${
orderType ? orderType.toLocaleLowerCase() : 'desc'
},
Expand All @@ -501,11 +531,11 @@ export class CommunityListService {
where: { id_in: [${contractAddress.map(
(el) => `"${el.toLocaleLowerCase()}"`
)}]}`,
`id, beneficiaries`
`id, ${orderBy}`
);
} else {
result = await communityEntities(
`orderBy: beneficiaries,
`orderBy: ${orderBy},
orderDirection: ${
orderType ? orderType.toLocaleLowerCase() : 'desc'
},
Expand All @@ -519,24 +549,13 @@ export class CommunityListService {
? parseInt(query.offset, 10)
: config.defaultOffset
},`,
`id, beneficiaries`
`id, ${orderBy}`
);
}

return result;
}

// private async _getOutOfFunds(
// query: {
// limit?: string;
// offset?: string;
// },
// orderType?: string,
// communitiesId?: number[]
// ): Promise<any> {

// }

private _generateInclude(fields: any): Includeable[] {
const extendedInclude: Includeable[] = [];

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/subgraph/queries/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const getCommunityState = async (
contributors
managers
baseInterval
estimatedFunds
}
}
`;
Expand Down
131 changes: 131 additions & 0 deletions packages/core/tests/integration/v2/community.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,137 @@ describe('community service v2', () => {
requestByAddress: users[2].address,
});
});

it('out of funds', async () => {
const communities = await CommunityFactory([
{
requestByAddress: users[0].address,
started: new Date(),
status: 'valid',
visibility: 'public',
contract: {
baseInterval: 60 * 60 * 24,
claimAmount: '1000000000000000000',
communityId: 0,
incrementInterval: 5 * 60,
maxClaim: '450000000000000000000',
},
hasAddress: true,
gps: {
latitude: -23.4378873,
longitude: -46.4841214,
},
},
{
requestByAddress: users[1].address,
started: new Date(),
status: 'valid',
visibility: 'public',
contract: {
baseInterval: 60 * 60 * 24,
claimAmount: '1000000000000000000',
communityId: 0,
incrementInterval: 5 * 60,
maxClaim: '450000000000000000000',
},
hasAddress: true,
gps: {
latitude: -23.4378873,
longitude: -46.4841214,
},
},
{
requestByAddress: users[2].address,
started: new Date(),
status: 'valid',
visibility: 'public',
contract: {
baseInterval: 60 * 60 * 24,
claimAmount: '1000000000000000000',
communityId: 0,
incrementInterval: 5 * 60,
maxClaim: '450000000000000000000',
},
hasAddress: true,
gps: {
latitude: -15.8697203,
longitude: -47.9207824,
},
},
]);

const claimed: SubgraphClaimed = [];
for (const community of communities) {
claimed.push({
id: community.contractAddress!,
claimed: 0,
});
await BeneficiaryFactory(
await UserFactory({
n:
community.requestByAddress === users[1].address
? 5
: 4,
}),
community.id
);
}

returnCommunityEntities.returns([
{
id: communities[1].contractAddress,
estimatedFunds: '0.01',
},
{
id: communities[2].contractAddress,
estimatedFunds: '1.50',
},
{
id: communities[0].contractAddress,
estimatedFunds: '100.00',
},
]);

communities.forEach((el) => {
returnCommunityStateSubgraph
.withArgs(el.contractAddress)
.returns({
claims: 0,
claimed: '0',
beneficiaries:
el.requestByAddress === users[1].address
? 5
: 4,
removedBeneficiaries: 0,
contributed: '0',
contributors: 0,
managers: 0,
});
});

const result = await communityListService.list({
orderBy: 'out_of_funds',
});

expect(result.rows[0]).to.include({
id: communities[1].id,
name: communities[1].name,
country: communities[1].country,
requestByAddress: users[1].address,
});
expect(result.rows[1]).to.include({
id: communities[2].id,
name: communities[2].name,
country: communities[2].country,
requestByAddress: users[2].address,
});
expect(result.rows[2]).to.include({
id: communities[0].id,
name: communities[0].name,
country: communities[0].country,
requestByAddress: users[0].address,
});
});
});

describe('query string filter', () => {
Expand Down