Skip to content

Commit

Permalink
filter beneficiaries by inactivity (#704)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernardo Vieira <bernardo@impactmarket.com>
  • Loading branch information
Joao Pedro da Silva and Bernardo Vieira authored Jul 7, 2023
1 parent 65f594f commit 5b73a25
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
6 changes: 5 additions & 1 deletion packages/api/src/controllers/v2/community/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class CommunityController {
loginInactivity,
search,
orderBy,
lastActivity_lt,
} = req.query;
if (state === undefined || typeof state !== 'string') {
state = undefined;
Expand Down Expand Up @@ -100,7 +101,10 @@ class CommunityController {
search !== undefined && typeof search === 'string'
? search
: undefined,
orderBy
orderBy,
lastActivity_lt && typeof lastActivity_lt === 'string'
? parseInt(lastActivity_lt, 10)
: undefined,
)
.then((r) => standardResponse(res, 200, true, r))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/routes/v2/community/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ export default (route: Router): void => {
* type: string
* required: false
* description: order key and order direction separated by colon (claimed:desc)
* - in: query
* name: lastActivity_lt
* required: false
* descripition: timestamp to filter the inactive beneficiaries
* security:
* - BearerToken: []
* responses:
Expand Down
29 changes: 21 additions & 8 deletions packages/core/src/services/ubi/community/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getBeneficiariesByAddress,
getBeneficiaries,
countBeneficiaries,
countInactiveBeneficiaries,
} from '../../../subgraph/queries/beneficiary';
import {
getCommunityAmbassador,
Expand Down Expand Up @@ -516,7 +517,8 @@ export class CommunityDetailsService {
limit: number,
filter: any,
searchInput?: string,
orderBy?: string
orderBy?: string,
lastActivity_lt?: number,
): Promise<{
count: number;
rows: any[];
Expand Down Expand Up @@ -616,7 +618,8 @@ export class CommunityDetailsService {
undefined,
community.contractAddress,
orderKey ? `orderBy: ${orderKey}` : undefined,
orderDirection ? `orderDirection: ${orderDirection}` : undefined
orderDirection ? `orderDirection: ${orderDirection}` : undefined,
lastActivity_lt,
);
count = beneficiariesSubgraph.length;

Expand All @@ -633,7 +636,8 @@ export class CommunityDetailsService {
undefined,
community.contractAddress,
orderKey ? `orderBy: ${orderKey}` : undefined,
orderDirection ? `orderDirection: ${orderDirection}` : undefined
orderDirection ? `orderDirection: ${orderDirection}` : undefined,
lastActivity_lt,
);
count = beneficiariesSubgraph.length;
appUsers = await models.appUser.findAll({
Expand All @@ -657,12 +661,21 @@ export class CommunityDetailsService {
undefined,
beneficiaryState,
orderKey ? `orderBy: ${orderKey}` : undefined,
orderDirection ? `orderDirection: ${orderDirection}` : undefined
);
count = await countBeneficiaries(
community.contractAddress,
filter.state !== null ? (filter.state as number) : undefined
orderDirection ? `orderDirection: ${orderDirection}` : undefined,
lastActivity_lt,
);

if (lastActivity_lt) {
count = await countInactiveBeneficiaries(
community.contractAddress,
lastActivity_lt,
)
} else {
count = await countBeneficiaries(
community.contractAddress,
filter.state !== null ? (filter.state as number) : undefined
);
}
addresses = beneficiariesSubgraph.map((beneficiary) =>
ethers.utils.getAddress(beneficiary.address)
);
Expand Down
55 changes: 53 additions & 2 deletions packages/core/src/subgraph/queries/beneficiary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export const getBeneficiariesByAddress = async (
inactive?: string,
community?: string,
orderBy?: string,
orderDirection?: string
orderDirection?: string,
lastActivity?: number,
): Promise<BeneficiarySubgraph[]> => {
try {
const idsFormated = addresses.map((el) => `"${el.toLowerCase()}"`);
Expand All @@ -110,6 +111,7 @@ export const getBeneficiariesByAddress = async (
? `community: "${community.toLowerCase()}"`
: ''
}
${lastActivity ? `lastActivity_lt: ${lastActivity}` : ''}
}
) {
address
Expand Down Expand Up @@ -157,7 +159,8 @@ export const getBeneficiaries = async (
lastClaimAt?: string,
state?: string,
orderBy?: string,
orderDirection?: string
orderDirection?: string,
lastActivity?: number,
): Promise<BeneficiarySubgraph[]> => {
try {
const graphqlQuery = {
Expand All @@ -172,6 +175,7 @@ export const getBeneficiaries = async (
community:"${community.toLowerCase()}"
${lastClaimAt ? lastClaimAt : ''}
${state ? state : ''}
${lastActivity ? `lastActivity_lt: ${lastActivity}` : ''}
}
) {
address
Expand Down Expand Up @@ -320,3 +324,50 @@ export const countBeneficiaries = async (
throw new Error(error);
}
};

export const countInactiveBeneficiaries = async (
community: string,
lastActivity: number
): Promise<number> => {
try {
const graphqlQuery = {
operationName: 'beneficiaryEntities',
query: `query beneficiaryEntities {
beneficiaryEntities(
where: {
community:"${community.toLowerCase()}"
lastActivity_lt: ${lastActivity}
state: 0
}
) {
address
}
}`,
};

const response = await axiosSubgraph.post<
any,
{
data: {
data: {
beneficiaryEntities: {
address: string,
claimed: string,
since: number,
state: number,
community: {
id: string,
}
}[];
};
};
}
>('', graphqlQuery);

const beneficiaryEntities = response.data?.data.beneficiaryEntities;

return beneficiaryEntities.length;
} catch (error) {
throw new Error(error);
}
}

0 comments on commit 5b73a25

Please sign in to comment.