diff --git a/pages/api/ranking/degrees/[degree]/[class]/index.ts b/pages/api/ranking/degrees/[degree]/[class]/index.ts index 96938bb..54e0f24 100644 --- a/pages/api/ranking/degrees/[degree]/[class]/index.ts +++ b/pages/api/ranking/degrees/[degree]/[class]/index.ts @@ -17,14 +17,12 @@ 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) { @@ -32,4 +30,4 @@ async function run(req: DIContainerNextApiRequest, res: NextApiResponse) { res.statusCode = 500; return; } -} \ No newline at end of file +} diff --git a/pages/api/ranking/index.ts b/pages/api/ranking/index.ts index ed80107..38d257e 100644 --- a/pages/api/ranking/index.ts +++ b/pages/api/ranking/index.ts @@ -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); @@ -28,4 +28,4 @@ async function run(req: DIContainerNextApiRequest, res: NextApiResponse) { res.statusCode = 500; return; } -} \ No newline at end of file +} diff --git a/use_cases/getDegreeClassData.test.ts b/use_cases/getDegreeClassData.test.ts index 3913b8a..d0dc1eb 100644 --- a/use_cases/getDegreeClassData.test.ts +++ b/use_cases/getDegreeClassData.test.ts @@ -24,7 +24,7 @@ beforeAll(async () => { birthday: new Date("1990-01-01"), tutorshipInterest: false, mentorshipInterest: false, - volunteeringInterest: false + volunteeringInterest: false, }); const contribution1 = await createContribution({ @@ -51,7 +51,7 @@ beforeAll(async () => { birthday: new Date("1990-01-01"), tutorshipInterest: false, mentorshipInterest: false, - volunteeringInterest: false + volunteeringInterest: false, }); const contribution2 = await createContribution({ @@ -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, diff --git a/use_cases/getDegreeClassData.ts b/use_cases/getDegreeClassData.ts index 2df1ac2..227f973 100644 --- a/use_cases/getDegreeClassData.ts +++ b/use_cases/getDegreeClassData.ts @@ -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 { - 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 { + 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", @@ -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, }; - } - \ No newline at end of file + }); + + return { + amount, + numberOfDonors, + donors, + }; +} diff --git a/use_cases/getRankingList.test.ts b/use_cases/getRankingList.test.ts index a1dc91f..4770884 100644 --- a/use_cases/getRankingList.test.ts +++ b/use_cases/getRankingList.test.ts @@ -14,10 +14,30 @@ beforeAll(async () => { // Create users and contributions const users = [ - { email: "user0@example.com", degree: "Computer Science", admissionYear: 2020, amount: 0 }, - { email: "user1@example.com", degree: "Computer Science", admissionYear: 2020, amount: 200_00 }, - { email: "user2@example.com", degree: "Computer Science", admissionYear: 2021, amount: 300_00 }, - { email: "user3@example.com", degree: "Industrial Engineering", admissionYear: 2020, amount: 500_00 }, + { + email: "user0@example.com", + degree: "Computer Science", + admissionYear: 2020, + amount: 0, + }, + { + email: "user1@example.com", + degree: "Computer Science", + admissionYear: 2020, + amount: 200_00, + }, + { + email: "user2@example.com", + degree: "Computer Science", + admissionYear: 2021, + amount: 300_00, + }, + { + email: "user3@example.com", + degree: "Industrial Engineering", + admissionYear: 2020, + amount: 500_00, + }, ]; for (const user of users) { @@ -33,7 +53,7 @@ beforeAll(async () => { birthday: new Date("1990-01-01"), tutorshipInterest: false, mentorshipInterest: false, - volunteeringInterest: false + volunteeringInterest: false, }); if (user.amount > 0) { diff --git a/use_cases/getRankingList.ts b/use_cases/getRankingList.ts index 7bf8ed7..4726c07 100644 --- a/use_cases/getRankingList.ts +++ b/use_cases/getRankingList.ts @@ -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 { - - // 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 { + // 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", @@ -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", @@ -77,4 +88,3 @@ async function getDegreeRankingListQuery(args: GetRankingListArgs): Promise<{ ORDER BY "amount" DESC; `; } -