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

onlyEndaement option added to donationResolvers to get only endaoment… #1796

Merged
78 changes: 72 additions & 6 deletions src/repositories/donationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ResourcesTotalPerMonthAndYear } from '../resolvers/donationResolver';
import { logger } from '../utils/logger';
import { QfRound } from '../entities/qfRound';
import { ChainType } from '../types/network';
import { ORGANIZATION_LABELS } from '../entities/organization';

export const fillQfRoundDonationsUserScores = async (): Promise<void> => {
await Donation.query(`
Expand Down Expand Up @@ -175,6 +176,7 @@ export const donationsTotalAmountPerDateRange = async (
toDate?: string,
networkId?: number,
onlyVerified?: boolean,
onlyEndaoment?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(`COALESCE(SUM(donation."valueUsd"), 0)`, 'sum')
Expand All @@ -198,12 +200,23 @@ export const donationsTotalAmountPerDateRange = async (
.andWhere('project.verified = true');
}

if (onlyEndaoment) {
if (!onlyVerified) {
query.leftJoin('donation.project', 'project');
}
query
.leftJoin('project.organization', 'organization')
.andWhere('organization."label" = :label', {
label: ORGANIZATION_LABELS.ENDAOMENT,
});
}

const donationsUsdAmount = await query.getRawOne();

query.cache(
`donationsTotalAmountPerDateRange-${fromDate || ''}-${toDate || ''}-${
networkId || 'all'
}-${onlyVerified || 'all'}`,
}-${onlyVerified || 'all'}-${onlyEndaoment || 'all'}`,
300000,
);

Expand All @@ -215,6 +228,7 @@ export const donationsTotalAmountPerDateRangeByMonth = async (
toDate?: string,
networkId?: number,
onlyVerified?: boolean,
onlyEndaoment?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -241,14 +255,25 @@ export const donationsTotalAmountPerDateRangeByMonth = async (
.andWhere('project.verified = true');
}

if (onlyEndaoment) {
if (!onlyVerified) {
query.leftJoin('donation.project', 'project');
}
query
.leftJoin('project.organization', 'organization')
.andWhere('organization."label" = :label', {
label: ORGANIZATION_LABELS.ENDAOMENT,
});
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');

query.cache(
`donationsTotalAmountPerDateRangeByMonth-${fromDate || ''}-${
toDate || ''
}-${networkId || 'all'}-${onlyVerified || 'all'}`,
}-${networkId || 'all'}-${onlyVerified || 'all'}-${onlyEndaoment || 'all'}`,
300000,
);

Expand All @@ -260,6 +285,7 @@ export const donationsNumberPerDateRange = async (
toDate?: string,
networkId?: number,
onlyVerified?: boolean,
onlyEndaoment?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(`COALESCE(COUNT(donation.id), 0)`, 'count')
Expand All @@ -283,12 +309,23 @@ export const donationsNumberPerDateRange = async (
.andWhere('project.verified = true');
}

if (onlyEndaoment) {
if (!onlyVerified) {
query.leftJoin('donation.project', 'project');
}
query
.leftJoin('project.organization', 'organization')
.andWhere('organization."label" = :label', {
label: ORGANIZATION_LABELS.ENDAOMENT,
});
}

const donationsUsdAmount = await query.getRawOne();

query.cache(
`donationsTotalNumberPerDateRange-${fromDate || ''}-${toDate || ''}--${
networkId || 'all'
}-${onlyVerified || 'all'}`,
}-${onlyVerified || 'all'}-${onlyEndaoment || 'all'}`,
300000,
);

Expand All @@ -300,6 +337,7 @@ export const donationsTotalNumberPerDateRangeByMonth = async (
toDate?: string,
networkId?: number,
onlyVerified?: boolean,
onlyEndaoment?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -325,14 +363,25 @@ export const donationsTotalNumberPerDateRangeByMonth = async (
.andWhere('project.verified = true');
}

if (onlyEndaoment) {
if (!onlyVerified) {
query.leftJoin('donation.project', 'project');
}
query
.leftJoin('project.organization', 'organization')
.andWhere('organization."label" = :label', {
label: ORGANIZATION_LABELS.ENDAOMENT,
});
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');

query.cache(
`donationsTotalNumberPerDateRangeByMonth-${fromDate || ''}-${
toDate || ''
}-${networkId || 'all'}-${onlyVerified || 'all'}`,
}-${networkId || 'all'}-${onlyVerified || 'all'}-${onlyEndaoment || 'all'}`,
300000,
);

Expand All @@ -343,6 +392,7 @@ export const donorsCountPerDate = async (
fromDate?: string,
toDate?: string,
networkId?: number,
onlyEndaoment?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -362,11 +412,18 @@ export const donorsCountPerDate = async (
if (networkId) {
query.andWhere(`donation."transactionNetworkId" = ${networkId}`);
}
if (onlyEndaoment) {
query.leftJoin('donation.project', 'project');
query.leftJoin('project.organization', 'organization');
query.andWhere('organization."label" = :label', {
label: ORGANIZATION_LABELS.ENDAOMENT,
});
}

query.cache(
`donorsCountPerDate-${fromDate || ''}-${toDate || ''}-${
networkId || 'all'
}`,
}-${onlyEndaoment || 'all'}`,
300000,
);

Expand Down Expand Up @@ -411,6 +468,7 @@ export const donorsCountPerDateByMonthAndYear = async (
fromDate?: string,
toDate?: string,
networkId?: number,
onlyEndaoment?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -430,14 +488,22 @@ export const donorsCountPerDateByMonthAndYear = async (
query.andWhere(`donation."transactionNetworkId" = ${networkId}`);
}

if (onlyEndaoment) {
query.leftJoin('donation.project', 'project');
query.leftJoin('project.organization', 'organization');
query
.andWhere('organization."label" = :label')
.setParameter('label', ORGANIZATION_LABELS.ENDAOMENT);
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');

query.cache(
`donorsCountPerDateByMonthAndYear-${fromDate || ''}-${toDate || ''}-${
networkId || 'all'
}`,
} - ${onlyEndaoment || 'all'}`,
300000,
);

Expand Down
24 changes: 23 additions & 1 deletion src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
DraftDonation,
} from '../entities/draftDonation';
import { nonZeroRecurringDonationsByProjectId } from '../repositories/recurringDonationRepository';
import { ORGANIZATION_LABELS } from '../entities/organization';

const draftDonationEnabled = process.env.ENABLE_DRAFT_DONATION === 'true';

Expand Down Expand Up @@ -295,6 +296,7 @@ export class DonationResolver {
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('networkId', { nullable: true }) networkId?: number,
@Arg('onlyVerified', { nullable: true }) onlyVerified?: boolean,
@Arg('onlyEndaoment', { nullable: true }) onlyEndaoment?: boolean,
): Promise<MainCategoryDonations[] | []> {
try {
validateWithJoiSchema(
Expand Down Expand Up @@ -335,6 +337,13 @@ export class DonationResolver {
query.andWhere('projects.verified = true');
}

if (onlyEndaoment) {
query
.leftJoin('projects.organization', 'organization')
.andWhere('organization."label" = :label', {
label: ORGANIZATION_LABELS.ENDAOMENT,
});
}
return await query.getRawMany();
} catch (e) {
logger.error('totalDonationsPerCategory query error', e);
Expand All @@ -349,6 +358,7 @@ export class DonationResolver {
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('networkId', { nullable: true }) networkId?: number,
@Arg('onlyVerified', { nullable: true }) onlyVerified?: boolean,
@Arg('onlyEndaoment', { nullable: true }) onlyEndaoment?: boolean,
): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
Expand All @@ -360,13 +370,15 @@ export class DonationResolver {
toDate,
networkId,
onlyVerified,
onlyEndaoment,
);
const totalPerMonthAndYear =
await donationsTotalAmountPerDateRangeByMonth(
fromDate,
toDate,
networkId,
onlyVerified,
onlyEndaoment,
);

return {
Expand All @@ -386,6 +398,7 @@ export class DonationResolver {
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('networkId', { nullable: true }) networkId?: number,
@Arg('onlyVerified', { nullable: true }) onlyVerified?: boolean,
@Arg('onlyEndaoment', { nullable: true }) onlyEndaoment?: boolean,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Incorrect parameter usage and naming issue detected

The onlyEndaoment parameter has been added to the totalDonationsNumberPerDate method, but there are some issues:

  1. The parameter is not being used in the method implementation or passed to the repository methods (donationsNumberPerDateRange and donationsTotalNumberPerDateRangeByMonth).
  2. There's a typo in the parameter name: onlyEndaoment should be onlyEndowment.

To resolve these issues:

  • Correct the parameter name to onlyEndowment.
  • Implement the parameter usage in the totalDonationsNumberPerDate method.
  • Pass the parameter to the relevant repository methods if needed.
Analysis chain

Verify the usage of the new onlyEndaoment parameter.

The addition of the optional onlyEndaoment parameter to the totalDonationsNumberPerDate method is syntactically correct. Ensure that this parameter is properly passed to the relevant repository methods (donationsNumberPerDateRange and donationsTotalNumberPerDateRangeByMonth) and handled correctly within those methods.

Run the following script to verify the parameter usage:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `onlyEndaoment` parameter in `totalDonationsNumberPerDate`.

# Test: Search for the parameter usage. Expect: Occurrences within the method implementation and relevant repository methods.
rg --type typescript -A 5 $'onlyEndaoment\?\: boolean'

Length of output: 159


Script:

#!/bin/bash
# Description: Verify the usage of the `onlyEndaoment` parameter in `totalDonationsNumberPerDate`.

# Search for the method implementation and parameter usage
ast-grep --lang typescript --pattern $'@Arg("onlyEndaoment", { nullable: true }) onlyEndaoment\?: boolean,
  $$$
  async totalDonationsNumberPerDate($$$) {
    $$$
  }'

# Search for both correct and misspelled parameter usage in the entire codebase
rg --type typescript -i '(onlyendao?wment)'

# Search for related repository method calls
rg --type typescript 'donationsNumberPerDateRange|donationsTotalNumberPerDateRangeByMonth'

Length of output: 927


Script:

#!/bin/bash
# Description: Verify the usage of the `onlyEndaoment` parameter in `totalDonationsNumberPerDate`.

# Search for the parameter declaration
ast-grep --lang typescript --pattern '@Arg("onlyEndaoment", { nullable: true }) onlyEndaoment?: boolean,'

# Search for the method implementation and parameter usage
rg 'async totalDonationsNumberPerDate' -A 10

# Search for both correct and misspelled parameter usage in the entire codebase
rg -i '(onlyendao?wment)'

# Search for related repository method calls
rg 'donationsNumberPerDateRange|donationsTotalNumberPerDateRangeByMonth'

Length of output: 3540

): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
Expand All @@ -397,13 +410,15 @@ export class DonationResolver {
toDate,
networkId,
onlyVerified,
onlyEndaoment,
);
const totalPerMonthAndYear =
await donationsTotalNumberPerDateRangeByMonth(
fromDate,
toDate,
networkId,
onlyVerified,
onlyEndaoment,
);

return {
Expand Down Expand Up @@ -434,17 +449,24 @@ export class DonationResolver {
@Arg('fromDate', { nullable: true }) fromDate?: string,
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('networkId', { nullable: true }) networkId?: number,
@Arg('onlyEndaoment', { nullable: true }) onlyEndaoment?: boolean,
): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
{ fromDate, toDate },
resourcePerDateReportValidator,
);
const total = await donorsCountPerDate(fromDate, toDate, networkId);
const total = await donorsCountPerDate(
fromDate,
toDate,
networkId,
onlyEndaoment,
);
const totalPerMonthAndYear = await donorsCountPerDateByMonthAndYear(
fromDate,
toDate,
networkId,
onlyEndaoment,
);
return {
total,
Expand Down
Loading