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

filter beneficiaries by inactivity #704

Merged
merged 3 commits into from
Jul 7, 2023
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
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
6 changes: 3 additions & 3 deletions packages/api/src/services/referral/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export const getReferralCode = async (userId: number, campaignId: number) => {
});

if (!user) {
throw new BaseError('USER_NOT_FOUND', 'User not found');
throw new utils.BaseError('USER_NOT_FOUND', 'User not found');
}
if (!user.phoneValidated || !user.emailValidated) {
throw new BaseError('NOT_ILLEGIBLE', 'User has not verified account');
throw new utils.BaseError('NOT_ILLEGIBLE', 'User has not verified account');
}

const referralCodeExists = await appReferralCode.findOne({
Expand Down Expand Up @@ -202,7 +202,7 @@ export const useReferralCode = async (userId: number, referralCode: string) => {
);
} catch (error) {
if (error.error?.message?.indexOf('ReferralLink') !== -1) {
throw new BaseError(
throw new utils.BaseError(
'REFERRAL_LINK_ERROR',
error.error?.message?.match(/\"execution reverted: ([\w\s\d:]*)\",/)[1]
);
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);
}
}