Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 8 additions & 1 deletion src/app/_components/week/weekInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ 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;

await api.leetcode.checkNewCompletions({
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.

could we avoid making the call to the backend if we know that there is no user session?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

changed, please check. is checking "if (session)" enough?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can check something like

    if (!session?.user) {
        return (
            <div>
                Unauthorized
            </div>
        )
    }

But adding

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Would it be better to throw an error (which I think is what happens in weekly-problems?), or should it stay as a simple message?

I was thinking something like

if (!session?.user) {
    throw new Error("TRPCError: UNAUTHORIZED");
  }

but I'd like some feedback before pushing it

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.

I would say the 'Unauthorized' message is a better choice, as this is not an error but adequate behavior. Perhaps we could add standard 404 or 401 pages later

week: id,
userId: userId ?? "",
leetcodeUser: session?.user?.leetcodeUser ?? "",
});

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

return (
<div>
{week ? (
Expand Down
48 changes: 39 additions & 9 deletions src/server/api/routers/leetcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,46 @@ 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 data = recentAccepted.map((problem) => ({
name: problem.title,
timestamp: problem.timestamp,
}));

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

// For every problem, check if it exists in the database
// If it exists, check if the user has solved it
// If the user has not solved it, update the problem in the database
for (const problem of data) {
// Make sure time is at least the beginning of the training period
const trainingStartDate = new Date(`2025-06-28T00:00:00Z`).getTime();
if (new Date(problem.timestamp * 1000).getTime() < trainingStartDate) {
continue;
}

return recentAccepted.some(
(submission) => submission.titleSlug === input.problemSlug,
);
// Perform match
const matchedProblem = db.find((p => p.name === problem.name));
if (matchedProblem) {
if (!matchedProblem.solvedBy.some(user => user.name === input.leetcodeUser)) {
await ctx.db.problem.update({
where: { id: matchedProblem.id },
data: {
solvedBy: {
connect: { id: input.userId },
},
},
});
}
}
}
}),

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