Fix minimum age 12 and ensure assignment catalog seeds on startup.
Persian age validation messages, clearer homework entry points, and seed assignment definitions so «تکالیف من» is not empty after deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+2
-2
@@ -33,7 +33,7 @@ npm run dev
|
||||
API default: `http://localhost:3001`
|
||||
PWA (CORS): `http://localhost:5173`
|
||||
|
||||
`prisma:seed` upserts the 11 course sessions. Server startup also upserts them as a safety net.
|
||||
`prisma:seed` upserts the 11 course sessions and 9 assignment definitions. Server startup also upserts both as a safety net.
|
||||
|
||||
## Scripts
|
||||
|
||||
@@ -49,7 +49,7 @@ PWA (CORS): `http://localhost:5173`
|
||||
| `npm run prisma:validate` | Validate schema |
|
||||
| `npm run prisma:migrate:dev` | Dev migrations |
|
||||
| `npm run prisma:migrate:deploy` | Apply migrations (prod) |
|
||||
| `npm run prisma:seed` | Upsert 11 `course_sessions` |
|
||||
| `npm run prisma:seed` | Upsert 11 `course_sessions` + 9 assignment definitions |
|
||||
|
||||
## Project Structure
|
||||
|
||||
|
||||
@@ -27,10 +27,11 @@ export const errorHandler: ErrorHandler = (err, c) => {
|
||||
|
||||
if (err instanceof ZodError) {
|
||||
log.warn({ issues: err.issues }, 'validation error');
|
||||
const firstMessage = err.issues[0]?.message?.trim();
|
||||
|
||||
return errorResponse(c, {
|
||||
code: ERROR_CODES.VALIDATION_ERROR,
|
||||
message: 'Validation failed',
|
||||
message: firstMessage || 'اطلاعات واردشده نامعتبر است',
|
||||
status: HTTP_STATUS.UNPROCESSABLE_ENTITY,
|
||||
details: err.issues,
|
||||
});
|
||||
|
||||
@@ -485,7 +485,7 @@ export async function listAssignmentsForSession(userId: bigint, sessionOrder: nu
|
||||
|
||||
/** All session assignment slots (definitions 2–10) with optional user progress. */
|
||||
export async function listAssignmentCatalog(userId: bigint) {
|
||||
await seedAssignmentDefinitions();
|
||||
const seededCount = await seedAssignmentDefinitions();
|
||||
await backfillAssignmentsForCompletedSessions(userId);
|
||||
|
||||
const completedOrders = await getCompletedSessionOrderSet(userId);
|
||||
@@ -494,6 +494,14 @@ export async function listAssignmentCatalog(userId: bigint) {
|
||||
orderBy: [{ displayOrder: 'asc' }, { sessionOrder: 'asc' }],
|
||||
});
|
||||
|
||||
if (definitions.length === 0) {
|
||||
throw new ValidationError(
|
||||
seededCount === 0
|
||||
? 'تعاریف تکالیف در پایگاهداده موجود نیست. لطفاً migrate و seed را اجرا کنید.'
|
||||
: 'بارگذاری فهرست تکالیف ناموفق بود',
|
||||
);
|
||||
}
|
||||
|
||||
const userRows = await prisma.userAssignment.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
@@ -506,8 +514,35 @@ export async function listAssignmentCatalog(userId: bigint) {
|
||||
|
||||
const byDefinitionId = new Map(userRows.map((r) => [r.definitionId.toString(), r]));
|
||||
|
||||
let activatedMissing = 0;
|
||||
for (const definition of definitions) {
|
||||
if (
|
||||
isAssignmentUnlockedByCompletedSessions(definition.sessionOrder, completedOrders) &&
|
||||
!byDefinitionId.has(definition.id.toString())
|
||||
) {
|
||||
activatedMissing += await activateAssignmentsForSession(userId, definition.sessionOrder, prisma, {
|
||||
ensureSeeded: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const rows =
|
||||
activatedMissing > 0
|
||||
? await prisma.userAssignment.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
definition: true,
|
||||
reports: { orderBy: { createdAt: 'desc' }, take: 1 },
|
||||
dailyEntries: { orderBy: { entryDate: 'desc' } },
|
||||
_count: { select: { reports: true, dailyEntries: true } },
|
||||
},
|
||||
})
|
||||
: userRows;
|
||||
|
||||
const byId = new Map(rows.map((r) => [r.definitionId.toString(), r]));
|
||||
|
||||
return definitions.map((definition) => {
|
||||
const ua = byDefinitionId.get(definition.id.toString());
|
||||
const ua = byId.get(definition.id.toString());
|
||||
const unlocked = isAssignmentUnlockedByCompletedSessions(
|
||||
definition.sessionOrder,
|
||||
completedOrders,
|
||||
|
||||
@@ -2,21 +2,22 @@ import { z } from 'zod';
|
||||
|
||||
export const NAME_MAX_LENGTH = 100;
|
||||
|
||||
export const profileNameSchema = z
|
||||
.string({ required_error: 'Name is required' })
|
||||
.trim()
|
||||
.min(1, 'Name cannot be empty')
|
||||
.max(NAME_MAX_LENGTH, `Name must be at most ${NAME_MAX_LENGTH} characters`);
|
||||
|
||||
/** Minimum allowed profile age (years). Keep in sync with frontend ProfileCompletionPage. */
|
||||
export const PROFILE_AGE_MIN = 12;
|
||||
|
||||
export const profileNameSchema = z
|
||||
.string({ required_error: 'نام الزامی است' })
|
||||
.trim()
|
||||
.min(1, 'نام نمیتواند خالی باشد')
|
||||
.max(NAME_MAX_LENGTH, `نام حداکثر ${NAME_MAX_LENGTH} کاراکتر باشد`);
|
||||
|
||||
export const profileAgeSchema = z
|
||||
.number({
|
||||
required_error: 'Age is required',
|
||||
invalid_type_error: 'Age must be an integer',
|
||||
required_error: 'سن الزامی است',
|
||||
invalid_type_error: 'سن باید یک عدد صحیح باشد',
|
||||
})
|
||||
.int('Age must be an integer')
|
||||
.min(PROFILE_AGE_MIN, `Age must be at least ${PROFILE_AGE_MIN}`);
|
||||
.int('سن باید یک عدد صحیح باشد')
|
||||
.min(PROFILE_AGE_MIN, `حداقل سن ${PROFILE_AGE_MIN} سال است`);
|
||||
|
||||
export const patchProfileSchema = z
|
||||
.object({
|
||||
@@ -24,7 +25,7 @@ export const patchProfileSchema = z
|
||||
age: profileAgeSchema.optional(),
|
||||
})
|
||||
.refine((value) => value.name !== undefined || value.age !== undefined, {
|
||||
message: 'At least one of name or age is required',
|
||||
message: 'نام یا سن لازم است',
|
||||
});
|
||||
|
||||
export type PatchProfileInput = z.infer<typeof patchProfileSchema>;
|
||||
|
||||
@@ -49,7 +49,7 @@ export async function updateOwnProfile(
|
||||
|
||||
if (!existing.profile) {
|
||||
if (input.name === undefined || input.age === undefined) {
|
||||
throw new ValidationError('Name and age are both required to create a profile');
|
||||
throw new ValidationError('برای تکمیل مشخصات، نام و سن هر دو لازم است');
|
||||
}
|
||||
|
||||
await prisma.userProfile.create({
|
||||
|
||||
@@ -16,8 +16,13 @@ async function startServer() {
|
||||
await connectPrisma();
|
||||
logger.info('database connection established');
|
||||
const { seedCourseSessions } = await import('./modules/sessions/sessions.service.js');
|
||||
const { seedAssignmentDefinitions } = await import(
|
||||
'./modules/assignments/assignments.service.js'
|
||||
);
|
||||
await seedCourseSessions();
|
||||
logger.info('course sessions seeded');
|
||||
const assignmentCount = await seedAssignmentDefinitions();
|
||||
logger.info({ assignmentCount }, 'assignment definitions seeded');
|
||||
} catch (err) {
|
||||
logger.warn({ err }, 'database connection failed at startup; /ready will report unavailable');
|
||||
}
|
||||
|
||||
@@ -109,6 +109,7 @@ describe('profile API', () => {
|
||||
body: JSON.stringify({ name: 'Sara', age: 11 }),
|
||||
});
|
||||
assert.equal(underage.status, 422);
|
||||
assert.match(String(underage.body.error?.message ?? ''), /حداقل سن 12/);
|
||||
|
||||
const teenOk = await requestJson(app, '/users/me/profile', {
|
||||
method: 'PATCH',
|
||||
|
||||
Reference in New Issue
Block a user