Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/app/_components/fetchData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { api } from "~/trpc/react";

export function TestButton() {
const utils = api.useUtils();
const { data } = api.leetcode.hasCompletedProblemRecently.useQuery({
username: "Oscar_gg",
problemSlug: "final-array-state-after-k-multiplication-operations-i",
const { data } = api.leetcode.checkNewCompletions.useQuery({
week: 1,
userId: "cmc6w9tlp000025100bzz0zwb",
leetcodeUser: "pyoro",
});

const { data: data2 } = api.leetcode.getProblemById.useQuery({
Expand Down
13 changes: 12 additions & 1 deletion src/app/_components/week/weekInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import Subtitle from "../subtitle";
import { auth } from "~/server/auth";

const WeekInfo = async ({ id }: { id: number }) => {
const week = await api.week.getWeek(id);
const session = await auth();
const userId = session?.user?.id;
const leetcodeUser = session?.user?.leetcodeUser;

// Only call backend if needed information exists.
if (userId && leetcodeUser) {
await api.leetcode.checkNewCompletions({
week: id,
userId: userId,
leetcodeUser: leetcodeUser,
});
}

const week = await api.week.getWeek(id);

return (
<div>
Expand Down
57 changes: 48 additions & 9 deletions src/server/api/routers/leetcode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { timeStamp } from "console";
import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
Expand All @@ -17,16 +18,54 @@ export const leetcodeRouter = createTRPCRouter({
return await getProblemsSolved({ username: input.username });
}),

hasCompletedProblemRecently: publicProcedure
.input(z.object({ username: z.string(), problemSlug: z.string() }))
.query(async ({ input }) => {
const recentAccepted = await getAcceptedProblems({
username: input.username,
});
checkNewCompletions: publicProcedure
.input(z.object({ week: z.number(), userId: z.string(), leetcodeUser: z.string() }))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the week argument as it was being unused

.query(async ({ input, ctx }) => {
const recentAccepted = await getAcceptedProblems({ username: input.leetcodeUser });
if (!recentAccepted) {
throw new Error("Failed to fetch recent accepted problems");
}

const trainingStartDate = new Date(`2025-06-28T00:00:00Z`).getTime();

// Get all recently accepted problems solved after the beginning of the training period.
const data = recentAccepted
.filter(problem => new Date(problem.timestamp * 1000).getTime() >= trainingStartDate)
.map(problem => ({
name: problem.title,
timestamp: problem.timestamp,
}));

const db = await ctx.db.problem.findMany({ include: { week: true, solvedBy: true } });

// Perform db update.
const updates = data.map(problem => {
// If not a match, ignore.
const matched = db.find(p => p.name === problem.name);

if (!matched) return null;

// If already solved, ignore.
const alreadySolved = matched.solvedBy.some(
user => user.leetcodeUser === input.leetcodeUser
);

if (alreadySolved) return null;

// Update matched, not previously solved problem.
return ctx.db.problem.update({
where: {id : matched.id},
data: {
solvedBy: {
connect: {id: input.userId}
}
}
})
})

return recentAccepted.some(
(submission) => submission.titleSlug === input.problemSlug,
);
// Filter and run all updates
const filteredPromises = updates.filter(Boolean);
await Promise.all(filteredPromises);
}),

getProblemById: publicProcedure
Expand Down
1 change: 1 addition & 0 deletions src/server/auth/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module "next-auth" {
user: {
id: string;
role: UserRole;
leetcodeUser?: string;
// ...other properties
} & DefaultSession["user"];
}
Expand Down