Skip to content

Commit

Permalink
add optimism data queries
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Oct 6, 2023
1 parent 97d1a2f commit ad343cf
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/repositories/donationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const findDonationById = async (
export const donationsTotalAmountPerDateRange = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(`COALESCE(SUM(donation."valueUsd"), 0)`, 'sum')
Expand All @@ -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(
Expand All @@ -140,6 +146,7 @@ export const donationsTotalAmountPerDateRange = async (
export const donationsTotalAmountPerDateRangeByMonth = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -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');
Expand All @@ -171,6 +182,7 @@ export const donationsTotalAmountPerDateRangeByMonth = async (
export const donationsNumberPerDateRange = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(`COALESCE(COUNT(donation."valueUsd"), 0)`, 'count')
Expand All @@ -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(
Expand All @@ -196,6 +213,7 @@ export const donationsNumberPerDateRange = async (
export const donationsTotalNumberPerDateRangeByMonth = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -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');
Expand All @@ -227,6 +249,7 @@ export const donationsTotalNumberPerDateRangeByMonth = async (
export const donorsCountPerDate = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -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();
Expand All @@ -252,6 +279,7 @@ export const donorsCountPerDate = async (
export const donorsCountPerDateByMonthAndYear = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -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');
Expand Down
32 changes: 32 additions & 0 deletions src/repositories/projectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ export const userIsOwnerOfProject = async (
export const totalProjectsPerDate = async (
fromDate?: string,
toDate?: string,
verified?: boolean,
includesOptimism?: boolean,
): Promise<number> => {
const query = Project.createQueryBuilder('project');

Expand All @@ -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();
Expand All @@ -346,6 +362,8 @@ export const totalProjectsPerDate = async (
export const totalProjectsPerDateByMonthAndYear = async (
fromDate?: string,
toDate?: string,
verified?: boolean,
includesOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
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`,
Expand All @@ -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');
Expand Down
30 changes: 25 additions & 5 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourcePerDateRange> {
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,
Expand All @@ -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<ResourcePerDateRange> {
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,
Expand Down Expand Up @@ -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<ResourcePerDateRange> {
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,
Expand Down
11 changes: 10 additions & 1 deletion src/resolvers/projectResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourcePerDateRange> {
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 {
Expand Down

0 comments on commit ad343cf

Please sign in to comment.