-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved db operations to use cases and added tests
- Loading branch information
Estevam Furtado
authored and
Estevam Furtado
committed
Oct 14, 2024
1 parent
37151eb
commit 8fb26e4
Showing
6 changed files
with
394 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { getDegreeClassData } from "./getDegreeClassData"; | ||
import createContribution from "./createContribution"; | ||
import completeContribution from "./completeContribution"; | ||
import createUser from "./createUser"; | ||
|
||
let prisma: PrismaClient; | ||
|
||
beforeAll(async () => { | ||
prisma = new PrismaClient(); | ||
|
||
await prisma.contribution.deleteMany(); | ||
await prisma.user.deleteMany(); | ||
|
||
const user1 = await createUser({ | ||
dbClient: prisma, | ||
email: "[email protected]", | ||
firstName: "John", | ||
lastName: "Doe", | ||
university: "UFRJ", | ||
degree: "Industrial Engineering", | ||
admissionYear: 2020, | ||
url: "http://example.com/john", | ||
birthday: new Date("1990-01-01"), | ||
tutorshipInterest: false, | ||
mentorshipInterest: false, | ||
volunteeringInterest: false | ||
}); | ||
|
||
const contribution1 = await createContribution({ | ||
dbClient: prisma, | ||
amountInCents: 50_00, | ||
email: user1.email, | ||
}); | ||
|
||
await completeContribution({ | ||
dbClient: prisma, | ||
contributionId: contribution1.id, | ||
externalId: "123456", | ||
}); | ||
|
||
const user2 = await createUser({ | ||
dbClient: prisma, | ||
email: "[email protected]", | ||
firstName: "Jane", | ||
lastName: "Smith", | ||
university: "UFRJ", | ||
degree: "Industrial Engineering", | ||
admissionYear: 2021, | ||
url: "http://example.com/jane", | ||
birthday: new Date("1990-01-01"), | ||
tutorshipInterest: false, | ||
mentorshipInterest: false, | ||
volunteeringInterest: false | ||
}); | ||
|
||
const contribution2 = await createContribution({ | ||
dbClient: prisma, | ||
amountInCents: 30_00, | ||
email: user2.email, | ||
}); | ||
|
||
await completeContribution({ | ||
dbClient: prisma, | ||
contributionId: contribution2.id, | ||
externalId: "123456", | ||
}); | ||
|
||
const user3 = await createUser({ | ||
dbClient: prisma, | ||
email: "[email protected]", | ||
firstName: "Steve", | ||
lastName: "Jobs", | ||
university: "UFRJ", | ||
degree: "Industrial Engineering", | ||
admissionYear: 2015, | ||
url: "http://example.com/steve", | ||
birthday: new Date("1955-02-24"), | ||
tutorshipInterest: false, | ||
mentorshipInterest: false, | ||
volunteeringInterest: false | ||
}); | ||
|
||
|
||
const contribution3 = await createContribution({ | ||
dbClient: prisma, | ||
amountInCents: 20_00, | ||
email: user3.email, | ||
}); | ||
|
||
await completeContribution({ | ||
dbClient: prisma, | ||
contributionId: contribution3.id, | ||
externalId: "123456", | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await prisma.$disconnect(); | ||
}); | ||
|
||
describe("getDegreeClassData", () => { | ||
it("should return the correct data for a given degree and year (5y window)", async () => { | ||
const initialDate = new Date("2023-01-01"); | ||
const result = await getDegreeClassData({ | ||
dbClient: prisma, | ||
year: "2020", | ||
degree: "Industrial Engineering", | ||
initialDate, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
amount: 80, | ||
numberOfDonors: 2, | ||
donors: [ | ||
{ | ||
name: "John Doe", | ||
year: 2020, | ||
url: "http://example.com/john", | ||
}, | ||
{ | ||
name: "Jane Smith", | ||
year: 2021, | ||
url: "http://example.com/jane", | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { GetClassData } from "../pages/api/ranking/types"; | ||
|
||
type GetDegreeClassDataArgs = { | ||
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` | ||
SELECT | ||
u."id", | ||
u."first_name" AS "firstName", | ||
u."last_name" AS "lastName", | ||
u."url", | ||
SUM(c."amount_in_cents")/100 AS "amount", | ||
u."admission_year" AS "year" | ||
FROM "users" u | ||
LEFT JOIN "contributions" c ON u."id" = c."userId" | ||
WHERE | ||
u."degree" = ${args.degree} | ||
AND u."admission_year" >= ${minYear} | ||
AND u."admission_year" < ${maxYear} | ||
AND c."state" = 'completed' | ||
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, | ||
}; | ||
}); | ||
|
||
return { | ||
amount, | ||
numberOfDonors, | ||
donors, | ||
}; | ||
} | ||
|
Oops, something went wrong.