aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/schemas/index.ts
blob: d2a8fb0d60e1693cfd8d5b591975b0fea8134f52 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { z } from "zod";

// Card states for FSRS algorithm
export const cardStateSchema = z.union([
	z.literal(0), // New
	z.literal(1), // Learning
	z.literal(2), // Review
	z.literal(3), // Relearning
]);

// Rating values for reviews
export const ratingSchema = z.union([
	z.literal(1), // Again
	z.literal(2), // Hard
	z.literal(3), // Good
	z.literal(4), // Easy
]);

// User schema
export const userSchema = z.object({
	id: z.uuid(),
	username: z.string().min(1).max(255),
	passwordHash: z.string(),
	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
});

// User creation input schema
export const createUserSchema = z.object({
	username: z.string().min(1).max(255),
	password: z.string().min(15).max(255),
});

// Login input schema
export const loginSchema = z.object({
	username: z.string().min(1),
	password: z.string().min(1),
});

// Refresh token input schema
export const refreshTokenSchema = z.object({
	refreshToken: z.string().min(1),
});

// Deck schema
export const deckSchema = z.object({
	id: z.uuid(),
	userId: z.uuid(),
	name: z.string().min(1).max(255),
	description: z.string().max(1000).nullable(),
	newCardsPerDay: z.number().int().min(0).default(20),
	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
	deletedAt: z.coerce.date().nullable(),
	syncVersion: z.number().int().min(0),
});

// Deck creation input schema
export const createDeckSchema = z.object({
	name: z.string().min(1).max(255),
	description: z.string().max(1000).nullable().optional(),
	newCardsPerDay: z.number().int().min(0).default(20),
});

// Deck update input schema
export const updateDeckSchema = z.object({
	name: z.string().min(1).max(255).optional(),
	description: z.string().max(1000).nullable().optional(),
	newCardsPerDay: z.number().int().min(0).optional(),
});

// Card schema
export const cardSchema = z.object({
	id: z.uuid(),
	deckId: z.uuid(),
	front: z.string().min(1),
	back: z.string().min(1),

	// FSRS fields
	state: cardStateSchema,
	due: z.coerce.date(),
	stability: z.number().min(0),
	difficulty: z.number().min(0).max(10),
	elapsedDays: z.number().int().min(0),
	scheduledDays: z.number().int().min(0),
	reps: z.number().int().min(0),
	lapses: z.number().int().min(0),
	lastReview: z.coerce.date().nullable(),

	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
	deletedAt: z.coerce.date().nullable(),
	syncVersion: z.number().int().min(0),
});

// Card creation input schema
export const createCardSchema = z.object({
	front: z.string().min(1),
	back: z.string().min(1),
});

// Card update input schema
export const updateCardSchema = z.object({
	front: z.string().min(1).optional(),
	back: z.string().min(1).optional(),
});

// Field type schema
export const fieldTypeSchema = z.literal("text");

// NoteType schema
export const noteTypeSchema = z.object({
	id: z.uuid(),
	userId: z.uuid(),
	name: z.string().min(1).max(255),
	frontTemplate: z.string().min(1),
	backTemplate: z.string().min(1),
	isReversible: z.boolean(),
	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
	deletedAt: z.coerce.date().nullable(),
	syncVersion: z.number().int().min(0),
});

// NoteType creation input schema
export const createNoteTypeSchema = z.object({
	name: z.string().min(1).max(255),
	frontTemplate: z.string().min(1),
	backTemplate: z.string().min(1),
	isReversible: z.boolean().default(false),
});

// NoteType update input schema
export const updateNoteTypeSchema = z.object({
	name: z.string().min(1).max(255).optional(),
	frontTemplate: z.string().min(1).optional(),
	backTemplate: z.string().min(1).optional(),
	isReversible: z.boolean().optional(),
});

// NoteFieldType schema
export const noteFieldTypeSchema = z.object({
	id: z.uuid(),
	noteTypeId: z.uuid(),
	name: z.string().min(1).max(255),
	order: z.number().int().min(0),
	fieldType: fieldTypeSchema,
	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
	deletedAt: z.coerce.date().nullable(),
	syncVersion: z.number().int().min(0),
});

// NoteFieldType creation input schema
export const createNoteFieldTypeSchema = z.object({
	name: z.string().min(1).max(255),
	order: z.number().int().min(0),
	fieldType: fieldTypeSchema.default("text"),
});

// NoteFieldType update input schema
export const updateNoteFieldTypeSchema = z.object({
	name: z.string().min(1).max(255).optional(),
	order: z.number().int().min(0).optional(),
});

// Note schema
export const noteSchema = z.object({
	id: z.uuid(),
	deckId: z.uuid(),
	noteTypeId: z.uuid(),
	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
	deletedAt: z.coerce.date().nullable(),
	syncVersion: z.number().int().min(0),
});

// Note creation input schema (fields is a map of fieldTypeId -> value)
export const createNoteSchema = z.object({
	noteTypeId: z.uuid(),
	fields: z.record(z.uuid(), z.string()),
});

// Note update input schema
export const updateNoteSchema = z.object({
	fields: z.record(z.uuid(), z.string()),
});

// NoteFieldValue schema
export const noteFieldValueSchema = z.object({
	id: z.uuid(),
	noteId: z.uuid(),
	noteFieldTypeId: z.uuid(),
	value: z.string(),
	createdAt: z.coerce.date(),
	updatedAt: z.coerce.date(),
	syncVersion: z.number().int().min(0),
});

// ReviewLog schema
export const reviewLogSchema = z.object({
	id: z.uuid(),
	cardId: z.uuid(),
	userId: z.uuid(),
	rating: ratingSchema,
	state: cardStateSchema,
	scheduledDays: z.number().int().min(0),
	elapsedDays: z.number().int().min(0),
	reviewedAt: z.coerce.date(),
	durationMs: z.number().int().min(0).nullable(),
	syncVersion: z.number().int().min(0),
});

// Submit review input schema
export const submitReviewSchema = z.object({
	rating: ratingSchema,
	durationMs: z.number().int().min(0).nullable().optional(),
});

// Inferred types from schemas
export type UserSchema = z.infer<typeof userSchema>;
export type CreateUserSchema = z.infer<typeof createUserSchema>;
export type LoginSchema = z.infer<typeof loginSchema>;
export type RefreshTokenSchema = z.infer<typeof refreshTokenSchema>;
export type DeckSchema = z.infer<typeof deckSchema>;
export type CreateDeckSchema = z.infer<typeof createDeckSchema>;
export type UpdateDeckSchema = z.infer<typeof updateDeckSchema>;
export type CardSchema = z.infer<typeof cardSchema>;
export type CreateCardSchema = z.infer<typeof createCardSchema>;
export type UpdateCardSchema = z.infer<typeof updateCardSchema>;
export type ReviewLogSchema = z.infer<typeof reviewLogSchema>;
export type SubmitReviewSchema = z.infer<typeof submitReviewSchema>;
export type FieldTypeSchema = z.infer<typeof fieldTypeSchema>;
export type NoteTypeSchema = z.infer<typeof noteTypeSchema>;
export type CreateNoteTypeSchema = z.infer<typeof createNoteTypeSchema>;
export type UpdateNoteTypeSchema = z.infer<typeof updateNoteTypeSchema>;
export type NoteFieldTypeSchema = z.infer<typeof noteFieldTypeSchema>;
export type CreateNoteFieldTypeSchema = z.infer<
	typeof createNoteFieldTypeSchema
>;
export type UpdateNoteFieldTypeSchema = z.infer<
	typeof updateNoteFieldTypeSchema
>;
export type NoteSchema = z.infer<typeof noteSchema>;
export type CreateNoteSchema = z.infer<typeof createNoteSchema>;
export type UpdateNoteSchema = z.infer<typeof updateNoteSchema>;
export type NoteFieldValueSchema = z.infer<typeof noteFieldValueSchema>;