aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/routes/study.ts
blob: 9c16699a52b2526fa3f38d4a90ec203572f296ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import {
	type Card as FSRSCard,
	type State as FSRSState,
	fsrs,
	type Grade,
} from "ts-fsrs";
import { z } from "zod";
import { authMiddleware, Errors, getAuthUser } from "../middleware/index.js";
import {
	type CardRepository,
	cardRepository,
	type DeckRepository,
	deckRepository,
	type ReviewLogRepository,
	reviewLogRepository,
} from "../repositories/index.js";
import { submitReviewSchema } from "../schemas/index.js";

export interface StudyDependencies {
	cardRepo: CardRepository;
	deckRepo: DeckRepository;
	reviewLogRepo: ReviewLogRepository;
}

const deckIdParamSchema = z.object({
	deckId: z.uuid(),
});

const cardIdParamSchema = z.object({
	deckId: z.uuid(),
	cardId: z.uuid(),
});

const f = fsrs();

export function createStudyRouter(deps: StudyDependencies) {
	const { cardRepo, deckRepo, reviewLogRepo } = deps;

	return new Hono()
		.use("*", authMiddleware)
		.get("/", zValidator("param", deckIdParamSchema), async (c) => {
			const user = getAuthUser(c);
			const { deckId } = c.req.valid("param");

			// Verify deck ownership
			const deck = await deckRepo.findById(deckId, user.id);
			if (!deck) {
				throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
			}

			const now = new Date();
			const dueCards = await cardRepo.findDueCardsForStudy(deckId, now, 100);

			return c.json({ cards: dueCards }, 200);
		})
		.post(
			"/:cardId",
			zValidator("param", cardIdParamSchema),
			zValidator("json", submitReviewSchema),
			async (c) => {
				const user = getAuthUser(c);
				const { deckId, cardId } = c.req.valid("param");
				const { rating, durationMs } = c.req.valid("json");

				// Verify deck ownership
				const deck = await deckRepo.findById(deckId, user.id);
				if (!deck) {
					throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
				}

				// Get the card
				const card = await cardRepo.findById(cardId, deckId);
				if (!card) {
					throw Errors.notFound("Card not found", "CARD_NOT_FOUND");
				}

				const now = new Date();

				// Convert our card to FSRS card format
				const fsrsCard: FSRSCard = {
					due: card.due,
					stability: card.stability,
					difficulty: card.difficulty,
					elapsed_days: card.elapsedDays,
					scheduled_days: card.scheduledDays,
					reps: card.reps,
					lapses: card.lapses,
					state: card.state as FSRSState,
					last_review: card.lastReview ?? undefined,
					learning_steps: 0,
				};

				// Schedule the card with the given rating
				const result = f.next(fsrsCard, now, rating as Grade);

				// Calculate elapsed days for review log
				const elapsedDays = card.lastReview
					? Math.round(
							(now.getTime() - card.lastReview.getTime()) /
								(1000 * 60 * 60 * 24),
						)
					: 0;

				// Update the card with new FSRS values
				const updatedCard = await cardRepo.updateFSRSFields(cardId, deckId, {
					state: result.card.state,
					due: result.card.due,
					stability: result.card.stability,
					difficulty: result.card.difficulty,
					elapsedDays: result.card.elapsed_days,
					scheduledDays: result.card.scheduled_days,
					reps: result.card.reps,
					lapses: result.card.lapses,
					lastReview: now,
				});

				// Create review log
				await reviewLogRepo.create({
					cardId,
					userId: user.id,
					rating,
					state: card.state,
					scheduledDays: result.card.scheduled_days,
					elapsedDays,
					durationMs: durationMs ?? null,
				});

				return c.json({ card: updatedCard }, 200);
			},
		);
}

export const study = createStudyRouter({
	cardRepo: cardRepository,
	deckRepo: deckRepository,
	reviewLogRepo: reviewLogRepository,
});