Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
estevamfurtado committed Oct 15, 2024
1 parent a7b3111 commit e76ce44
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 93 deletions.
16 changes: 7 additions & 9 deletions pages/api/ranking/degrees/[degree]/[class]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
async function run(req: DIContainerNextApiRequest, res: NextApiResponse) {
const prismaClient: PrismaClient = req.scope.resolve("dbClient");
try {
const rankingData = await getDegreeClassData(
{
dbClient: prismaClient,
degree: req.query.degree as string,
year: req.query.class as string,
initialDate: RANKING_INITIAL_DATA
}
);
const rankingData = await getDegreeClassData({
dbClient: prismaClient,
degree: req.query.degree as string,
year: req.query.class as string,
initialDate: RANKING_INITIAL_DATA,
});
res.statusCode = 200;
res.json(rankingData);
} catch (error) {
console.log(error);
res.statusCode = 500;
return;
}
}
}
4 changes: 2 additions & 2 deletions pages/api/ranking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function run(req: DIContainerNextApiRequest, res: NextApiResponse) {
try {
const rankingData = await getRankingList({
dbClient: prismaClient,
initialDate: RANKING_INITIAL_DATA
initialDate: RANKING_INITIAL_DATA,
});
res.statusCode = 200;
res.json(rankingData);
Expand All @@ -28,4 +28,4 @@ async function run(req: DIContainerNextApiRequest, res: NextApiResponse) {
res.statusCode = 500;
return;
}
}
}
7 changes: 3 additions & 4 deletions use_cases/getDegreeClassData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ beforeAll(async () => {
birthday: new Date("1990-01-01"),
tutorshipInterest: false,
mentorshipInterest: false,
volunteeringInterest: false
volunteeringInterest: false,
});

const contribution1 = await createContribution({
Expand All @@ -51,7 +51,7 @@ beforeAll(async () => {
birthday: new Date("1990-01-01"),
tutorshipInterest: false,
mentorshipInterest: false,
volunteeringInterest: false
volunteeringInterest: false,
});

const contribution2 = await createContribution({
Expand All @@ -78,10 +78,9 @@ beforeAll(async () => {
birthday: new Date("1955-02-24"),
tutorshipInterest: false,
mentorshipInterest: false,
volunteeringInterest: false
volunteeringInterest: false,
});


const contribution3 = await createContribution({
dbClient: prisma,
amountInCents: 20_00,
Expand Down
67 changes: 33 additions & 34 deletions use_cases/getDegreeClassData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import { PrismaClient } from "@prisma/client";
import { GetClassData } from "../pages/api/ranking/types";

type GetDegreeClassDataArgs = {
dbClient: PrismaClient;
degree: string;
year: string;
initialDate: Date;
}
dbClient: PrismaClient;
degree: string;
year: string;
initialDate: Date;
};

export async function getDegreeClassData(
args: GetDegreeClassDataArgs
): Promise<GetClassData> {
const minYear = Math.floor(Number(args.year) / 5) * 5;
const maxYear = minYear + 5;
const result: {
id: number;
firstName: string;
lastName: number;
url: string;
amount: number;
year: number;
}[] = await args.dbClient.$queryRaw`
args: GetDegreeClassDataArgs
): Promise<GetClassData> {
const minYear = Math.floor(Number(args.year) / 5) * 5;
const maxYear = minYear + 5;

const result: {
id: number;
firstName: string;
lastName: number;
url: string;
amount: number;
year: number;
}[] = await args.dbClient.$queryRaw`
SELECT
u."id",
u."first_name" AS "firstName",
Expand All @@ -39,21 +39,20 @@ export async function getDegreeClassData(
AND c."createdAt" > ${args.initialDate}
GROUP BY u."id", u."first_name", u."last_name", u."url", u."admission_year"
`;

const amount = result.reduce((acc, row) => acc + row.amount, 0);
const numberOfDonors = result.length;
const donors: GetClassData["donors"] = result.map((row) => {
return {
name: `${row.firstName} ${row.lastName}`,
url: row.url,
year: row.year,
};
});


const amount = result.reduce((acc, row) => acc + row.amount, 0);
const numberOfDonors = result.length;
const donors: GetClassData["donors"] = result.map((row) => {
return {
amount,
numberOfDonors,
donors,
name: `${row.firstName} ${row.lastName}`,
url: row.url,
year: row.year,
};
}

});

return {
amount,
numberOfDonors,
donors,
};
}
30 changes: 25 additions & 5 deletions use_cases/getRankingList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@ beforeAll(async () => {

// Create users and contributions
const users = [
{ email: "[email protected]", degree: "Computer Science", admissionYear: 2020, amount: 0 },
{ email: "[email protected]", degree: "Computer Science", admissionYear: 2020, amount: 200_00 },
{ email: "[email protected]", degree: "Computer Science", admissionYear: 2021, amount: 300_00 },
{ email: "[email protected]", degree: "Industrial Engineering", admissionYear: 2020, amount: 500_00 },
{
email: "[email protected]",
degree: "Computer Science",
admissionYear: 2020,
amount: 0,
},
{
email: "[email protected]",
degree: "Computer Science",
admissionYear: 2020,
amount: 200_00,
},
{
email: "[email protected]",
degree: "Computer Science",
admissionYear: 2021,
amount: 300_00,
},
{
email: "[email protected]",
degree: "Industrial Engineering",
admissionYear: 2020,
amount: 500_00,
},
];

for (const user of users) {
Expand All @@ -33,7 +53,7 @@ beforeAll(async () => {
birthday: new Date("1990-01-01"),
tutorshipInterest: false,
mentorshipInterest: false,
volunteeringInterest: false
volunteeringInterest: false,
});

if (user.amount > 0) {
Expand Down
88 changes: 49 additions & 39 deletions use_cases/getRankingList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,50 @@ import { PrismaClient } from "@prisma/client";
import { GetRankingData } from "../pages/api/ranking/types";

type GetRankingListArgs = {
dbClient: PrismaClient;
initialDate: Date;
degree?: string;
}
dbClient: PrismaClient;
initialDate: Date;
degree?: string;
};

export async function getRankingList(args: GetRankingListArgs): Promise<GetRankingData> {

// TODO(estevam): use pagination
// Could not use the same query for both because of the way Prisma handles $queryRaw
const result = args.degree ? await getDegreeRankingListQuery(args) : await getRankingListQuery(args);
export async function getRankingList(
args: GetRankingListArgs
): Promise<GetRankingData> {
// TODO(estevam): use pagination
// Could not use the same query for both because of the way Prisma handles $queryRaw
const result = args.degree
? await getDegreeRankingListQuery(args)
: await getRankingListQuery(args);

const amount = result.reduce((acc, row) => acc + row.amount, 0);
const numberOfDonors = result.reduce((acc, row) => acc + row.donors, 0);
const ranking: GetRankingData["ranking"] = result.map((row, index) => {
return {
position: index + 1,
degree: row.degree,
initialYear: row.period,
finalYear: row.period + 4,
amount: row.amount,
numberOfDonors: row.donors,
};
});

const amount = result.reduce((acc, row) => acc + row.amount, 0);
const numberOfDonors = result.reduce((acc, row) => acc + row.donors, 0);
const ranking: GetRankingData["ranking"] = result.map((row, index) => {
return {
amount,
numberOfDonors,
ranking,
position: index + 1,
degree: row.degree,
initialYear: row.period,
finalYear: row.period + 4,
amount: row.amount,
numberOfDonors: row.donors,
};
});

return {
amount,
numberOfDonors,
ranking,
};
}

async function getRankingListQuery(args: GetRankingListArgs): Promise<{
period: number;
degree: string;
amount: number;
donors: number;
}[]> {

async function getRankingListQuery(
args: GetRankingListArgs
): Promise<
{
period: number;
degree: string;
amount: number;
donors: number;
}[]
> {
return args.dbClient.$queryRaw`
SELECT
FLOOR((u."admission_year") / 5) * 5 AS "period",
Expand All @@ -55,12 +62,16 @@ async function getRankingListQuery(args: GetRankingListArgs): Promise<{
`;
}

async function getDegreeRankingListQuery(args: GetRankingListArgs): Promise<{
period: number;
degree: string;
amount: number;
donors: number;
}[]> {
async function getDegreeRankingListQuery(
args: GetRankingListArgs
): Promise<
{
period: number;
degree: string;
amount: number;
donors: number;
}[]
> {
return args.dbClient.$queryRaw`
SELECT
FLOOR((u."admission_year") / 5) * 5 AS "period",
Expand All @@ -77,4 +88,3 @@ async function getDegreeRankingListQuery(args: GetRankingListArgs): Promise<{
ORDER BY "amount" DESC;
`;
}

0 comments on commit e76ce44

Please sign in to comment.