diff --git a/src/repositories/donationRepository.ts b/src/repositories/donationRepository.ts index 3b1943f43..74ba39aa7 100644 --- a/src/repositories/donationRepository.ts +++ b/src/repositories/donationRepository.ts @@ -115,6 +115,7 @@ export const findDonationById = async ( export const donationsTotalAmountPerDateRange = async ( fromDate?: string, toDate?: string, + fromOptimism?: boolean, ): Promise => { const query = Donation.createQueryBuilder('donation') .select(`COALESCE(SUM(donation."valueUsd"), 0)`, 'sum') @@ -127,6 +128,11 @@ export const donationsTotalAmountPerDateRange = async ( if (toDate) { query.andWhere(`donation."createdAt" <= '${toDate}'`); } + + if (fromOptimism) { + query.andWhere(`donation."transactionNetworkId" = 10`); + } + const donationsUsdAmount = await query.getRawOne(); query.cache( @@ -140,6 +146,7 @@ export const donationsTotalAmountPerDateRange = async ( export const donationsTotalAmountPerDateRangeByMonth = async ( fromDate?: string, toDate?: string, + fromOptimism?: boolean, ): Promise => { const query = Donation.createQueryBuilder('donation') .select( @@ -156,6 +163,10 @@ export const donationsTotalAmountPerDateRangeByMonth = async ( query.andWhere(`donation."createdAt" <= '${toDate}'`); } + if (fromOptimism) { + query.andWhere(`donation."transactionNetworkId" = 10`); + } + query.groupBy('year, month'); query.orderBy('year', 'ASC'); query.addOrderBy('month', 'ASC'); @@ -171,6 +182,7 @@ export const donationsTotalAmountPerDateRangeByMonth = async ( export const donationsNumberPerDateRange = async ( fromDate?: string, toDate?: string, + fromOptimism?: boolean, ): Promise => { const query = Donation.createQueryBuilder('donation') .select(`COALESCE(COUNT(donation."valueUsd"), 0)`, 'count') @@ -183,6 +195,11 @@ export const donationsNumberPerDateRange = async ( if (toDate) { query.andWhere(`donation."createdAt" <= '${toDate}'`); } + + if (fromOptimism) { + query.andWhere(`donation."transactionNetworkId" = 10`); + } + const donationsUsdAmount = await query.getRawOne(); query.cache( @@ -196,6 +213,7 @@ export const donationsNumberPerDateRange = async ( export const donationsTotalNumberPerDateRangeByMonth = async ( fromDate?: string, toDate?: string, + fromOptimism?: boolean, ): Promise => { const query = Donation.createQueryBuilder('donation') .select( @@ -212,6 +230,10 @@ export const donationsTotalNumberPerDateRangeByMonth = async ( query.andWhere(`donation."createdAt" <= '${toDate}'`); } + if (fromOptimism) { + query.andWhere(`donation."transactionNetworkId" = 10`); + } + query.groupBy('year, month'); query.orderBy('year', 'ASC'); query.addOrderBy('month', 'ASC'); @@ -227,6 +249,7 @@ export const donationsTotalNumberPerDateRangeByMonth = async ( export const donorsCountPerDate = async ( fromDate?: string, toDate?: string, + fromOptimism?: boolean, ): Promise => { const query = Donation.createQueryBuilder('donation') .select( @@ -243,6 +266,10 @@ export const donorsCountPerDate = async ( query.andWhere(`donation."createdAt" <= '${toDate}'`); } + if (fromOptimism) { + query.andWhere(`donation."transactionNetworkId" = 10`); + } + query.cache(`donorsCountPerDate-${fromDate || ''}-${toDate || ''}`, 300000); const queryResult = await query.getRawOne(); @@ -252,6 +279,7 @@ export const donorsCountPerDate = async ( export const donorsCountPerDateByMonthAndYear = async ( fromDate?: string, toDate?: string, + fromOptimism?: boolean, ): Promise => { const query = Donation.createQueryBuilder('donation') .select( @@ -267,6 +295,10 @@ export const donorsCountPerDateByMonthAndYear = async ( query.andWhere(`donation."createdAt" <= '${toDate}'`); } + if (fromOptimism) { + query.andWhere(`donation."transactionNetworkId" = 10`); + } + query.groupBy('year, month'); query.orderBy('year', 'ASC'); query.addOrderBy('month', 'ASC'); diff --git a/src/repositories/projectRepository.ts b/src/repositories/projectRepository.ts index db9b2fa42..ed7322599 100644 --- a/src/repositories/projectRepository.ts +++ b/src/repositories/projectRepository.ts @@ -327,6 +327,8 @@ export const userIsOwnerOfProject = async ( export const totalProjectsPerDate = async ( fromDate?: string, toDate?: string, + verified?: boolean, + includesOptimism?: boolean, ): Promise => { const query = Project.createQueryBuilder('project'); @@ -338,6 +340,20 @@ export const totalProjectsPerDate = async ( query.andWhere(`project."creationDate" <= '${toDate}'`); } + if (verified) { + query.andWhere( + `project."verified" = true AND project."reviewStatus" = 'Listed'`, + ); + } + + if (includesOptimism) { + query.innerJoin( + `project.addresses`, + 'addresses', + 'addresses."networkId" = 10', + ); + } + query.cache(`totalProjectPerDate-${fromDate || ''}-${toDate || ''}`, 300000); return await query.getCount(); @@ -346,6 +362,8 @@ export const totalProjectsPerDate = async ( export const totalProjectsPerDateByMonthAndYear = async ( fromDate?: string, toDate?: string, + verified?: boolean, + includesOptimism?: boolean, ): Promise => { const query = Project.createQueryBuilder('project').select( `COUNT(project.id) as total, EXTRACT(YEAR from project."creationDate") as year, EXTRACT(MONTH from project."creationDate") as month, CONCAT(CAST(EXTRACT(YEAR from project."creationDate") as VARCHAR), '/', CAST(EXTRACT(MONTH from project."creationDate") as VARCHAR)) as date`, @@ -359,6 +377,20 @@ export const totalProjectsPerDateByMonthAndYear = async ( query.andWhere(`project."creationDate" <= '${toDate}'`); } + if (verified) { + query.andWhere( + `project."verified" = true AND project."reviewStatus" = 'Listed'`, + ); + } + + if (includesOptimism) { + query.innerJoin( + `project.addresses`, + 'addresses', + 'addresses."networkId" = 10', + ); + } + query.groupBy('year, month'); query.orderBy('year', 'ASC'); query.addOrderBy('month', 'ASC'); diff --git a/src/resolvers/donationResolver.ts b/src/resolvers/donationResolver.ts index 48360e277..433fbbf96 100644 --- a/src/resolvers/donationResolver.ts +++ b/src/resolvers/donationResolver.ts @@ -292,15 +292,24 @@ export class DonationResolver { // fromDate and toDate should be in this format YYYYMMDD HH:mm:ss @Arg('fromDate', { nullable: true }) fromDate?: string, @Arg('toDate', { nullable: true }) toDate?: string, + @Arg('fromOptimism', { nullable: true }) fromOptimism?: boolean, ): Promise { try { validateWithJoiSchema( { fromDate, toDate }, resourcePerDateReportValidator, ); - const total = await donationsTotalAmountPerDateRange(fromDate, toDate); + const total = await donationsTotalAmountPerDateRange( + fromDate, + toDate, + fromOptimism, + ); const totalPerMonthAndYear = - await donationsTotalAmountPerDateRangeByMonth(fromDate, toDate); + await donationsTotalAmountPerDateRangeByMonth( + fromDate, + toDate, + fromOptimism, + ); return { total, @@ -317,15 +326,24 @@ export class DonationResolver { // fromDate and toDate should be in this format YYYYMMDD HH:mm:ss @Arg('fromDate', { nullable: true }) fromDate?: string, @Arg('toDate', { nullable: true }) toDate?: string, + @Arg('fromOptimism', { nullable: true }) fromOptimism?: boolean, ): Promise { try { validateWithJoiSchema( { fromDate, toDate }, resourcePerDateReportValidator, ); - const total = await donationsNumberPerDateRange(fromDate, toDate); + const total = await donationsNumberPerDateRange( + fromDate, + toDate, + fromOptimism, + ); const totalPerMonthAndYear = - await donationsTotalNumberPerDateRangeByMonth(fromDate, toDate); + await donationsTotalNumberPerDateRangeByMonth( + fromDate, + toDate, + fromOptimism, + ); return { total, @@ -354,16 +372,18 @@ export class DonationResolver { // fromDate and toDate should be in this format YYYYMMDD HH:mm:ss @Arg('fromDate', { nullable: true }) fromDate?: string, @Arg('toDate', { nullable: true }) toDate?: string, + @Arg('fromOptimism', { nullable: true }) fromOptimism?: boolean, ): Promise { try { validateWithJoiSchema( { fromDate, toDate }, resourcePerDateReportValidator, ); - const total = await donorsCountPerDate(fromDate, toDate); + const total = await donorsCountPerDate(fromDate, toDate, fromOptimism); const totalPerMonthAndYear = await donorsCountPerDateByMonthAndYear( fromDate, toDate, + fromOptimism, ); return { total, diff --git a/src/resolvers/projectResolver.ts b/src/resolvers/projectResolver.ts index 52da03b38..322ce8311 100644 --- a/src/resolvers/projectResolver.ts +++ b/src/resolvers/projectResolver.ts @@ -1197,16 +1197,25 @@ export class ProjectResolver { // fromDate and toDate should be in this format YYYYMMDD HH:mm:ss @Arg('fromDate', { nullable: true }) fromDate?: string, @Arg('toDate', { nullable: true }) toDate?: string, + @Arg('verified', { nullable: true }) verified?: boolean, + @Arg('includesOptimism', { nullable: true }) includesOptimism?: boolean, ): Promise { try { validateWithJoiSchema( { fromDate, toDate }, resourcePerDateReportValidator, ); - const total = await totalProjectsPerDate(fromDate, toDate); + const total = await totalProjectsPerDate( + fromDate, + toDate, + verified, + includesOptimism, + ); const totalPerMonthAndYear = await totalProjectsPerDateByMonthAndYear( fromDate, toDate, + verified, + includesOptimism, ); return { diff --git a/test/graphqlQueries.ts b/test/graphqlQueries.ts index 10ad579dd..0e60ab686 100644 --- a/test/graphqlQueries.ts +++ b/test/graphqlQueries.ts @@ -343,10 +343,14 @@ export const fetchNewProjectsPerDate = ` query ( $fromDate: String $toDate: String + $verified: Boolean + $includesOptimism: Boolean ) { projectsPerDate( fromDate: $fromDate toDate: $toDate + verified: $verified + includesOptimism: $includesOptimism ) { total totalPerMonthAndYear { @@ -399,10 +403,12 @@ export const fetchTotalDonors = ` query ( $fromDate: String $toDate: String + $fromOptimism: Boolean ) { totalDonorsCountPerDate( fromDate: $fromDate toDate: $toDate + fromOptimism: $fromOptimism ) { total totalPerMonthAndYear { @@ -417,10 +423,12 @@ export const fetchTotalDonationsUsdAmount = ` query ( $fromDate: String $toDate: String + $fromOptimism: Boolean ) { donationsTotalUsdPerDate ( fromDate: $fromDate toDate: $toDate + fromOptimism: $fromOptimism ) { total totalPerMonthAndYear { @@ -435,10 +443,12 @@ export const fetchTotalDonationsNumberPerDateRange = ` query ( $fromDate: String $toDate: String + $fromOptimism: Boolean ) { totalDonationsNumberPerDate ( fromDate: $fromDate toDate: $toDate + fromOptimism: $fromOptimism ) { total totalPerMonthAndYear {