- v2: auth/signup, auth/login (+ code step, rate limited), PATCH /me; check-in look-up and scan returning an outcome for every normal result. Campus rule as v1. - Sign up/login/2FA/profile moved to src/services/auth.ts; scan preview and scan to src/services/checkin.ts. v1 answers unchanged (ServiceError v1Status). - Login, 2FA step, sign up and profile/password pages use v2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
92 lines
4.0 KiB
TypeScript
92 lines
4.0 KiB
TypeScript
import { z } from 'zod';
|
|
import { defineRoute } from '../route';
|
|
import { ApiError } from '../errors';
|
|
import { pageQuery, paged, single, skipTake, pageOf } from '../paging';
|
|
import { id, iso, isoDate } from '../schemas';
|
|
import { prisma } from '../legacy';
|
|
import { registrationSummary, SUMMARY_INCLUDE, toSummary } from './registrations';
|
|
import { updateProfile } from '../../services/auth';
|
|
|
|
const me = z.object({
|
|
id,
|
|
name: z.string(),
|
|
email: z.string(),
|
|
phone: z.string().nullable(),
|
|
role: z.enum(['admin', 'supervisor', 'staff', 'user']),
|
|
twoFactorEnabled: z.boolean(),
|
|
notificationPreference: z.enum(['email', 'whatsapp', 'both']),
|
|
homeCampusId: id.nullable(),
|
|
createdAt: isoDate,
|
|
});
|
|
|
|
type MeRow = { id: string; name: string; email: string; phoneNumber: string | null; role: 'admin' | 'supervisor' | 'staff' | 'user'; twoFactorEnabled: boolean | null; notificationPreference: 'email' | 'whatsapp' | 'both'; homeCampusId: string | null; createdAt: Date };
|
|
const toMe = (u: MeRow): z.input<typeof me> => ({
|
|
id: u.id, name: u.name, email: u.email, phone: u.phoneNumber ?? null, role: u.role, twoFactorEnabled: !!u.twoFactorEnabled,
|
|
notificationPreference: u.notificationPreference, homeCampusId: u.homeCampusId ?? null, createdAt: iso(u.createdAt),
|
|
});
|
|
|
|
export const getMe = defineRoute({
|
|
method: 'get',
|
|
path: '/me',
|
|
tag: 'Me',
|
|
summary: 'The signed-in person',
|
|
access: 'user',
|
|
response: single(me),
|
|
async handler({ user }) {
|
|
const u = await prisma.user.findUnique({
|
|
where: { id: user!.id },
|
|
select: { id: true, name: true, email: true, phoneNumber: true, role: true, twoFactorEnabled: true, notificationPreference: true, homeCampusId: true, createdAt: true },
|
|
});
|
|
if (!u) throw new ApiError('UNAUTHENTICATED', 'User not found');
|
|
return { data: toMe(u) };
|
|
},
|
|
});
|
|
|
|
export const updateMe = defineRoute({
|
|
method: 'patch',
|
|
path: '/me',
|
|
tag: 'Me',
|
|
summary: 'Change my details',
|
|
description: 'Only what is sent changes. A new `password` needs `currentPassword`; it sends a security notice and stops other devices skipping the 2FA code. The answer has a fresh `token` (the old one keeps working).',
|
|
access: 'user',
|
|
body: z.object({
|
|
name: z.string().trim().min(1).max(200).optional(),
|
|
email: z.email().max(200).optional(),
|
|
phone: z.string().trim().max(40).nullable().optional().describe('null or empty removes it'),
|
|
notificationPreference: z.enum(['email', 'whatsapp', 'both']).optional(),
|
|
homeCampusId: id.nullable().optional().describe('While campuses are on'),
|
|
password: z.string().min(8).max(200).optional(),
|
|
currentPassword: z.string().max(200).optional(),
|
|
}),
|
|
response: single(me).extend({ token: z.string() }),
|
|
async handler({ body, user, req }) {
|
|
const { getClientIp } = require('../../utils/requestUtils') as { getClientIp(req: unknown): string };
|
|
const { user: u, token } = await updateProfile(user!.id, { ...body, phoneNumber: body.phone }, { ip: getClientIp(req), userAgent: req.headers['user-agent'] });
|
|
return { data: toMe(u), token };
|
|
},
|
|
});
|
|
|
|
export const listMyRegistrations = defineRoute({
|
|
method: 'get',
|
|
path: '/me/registrations',
|
|
tag: 'Me',
|
|
summary: 'My registrations',
|
|
description: 'Newest first. By default only ones for events that have not ended, and not cancelled; `past=true` lists everything.',
|
|
access: 'user',
|
|
query: pageQuery.extend({
|
|
past: z.enum(['true', 'false']).default('false').transform(v => v === 'true').describe('Include past and cancelled registrations'),
|
|
}),
|
|
response: paged(registrationSummary),
|
|
async handler({ query, user }) {
|
|
const now = new Date();
|
|
const where = query.past
|
|
? { userId: user!.id }
|
|
: { userId: user!.id, status: { not: 'cancelled' as const }, event: { endDate: { gte: now } } };
|
|
const [rows, total] = await Promise.all([
|
|
prisma.registration.findMany({ where, include: SUMMARY_INCLUDE, orderBy: [{ createdAt: 'desc' }, { id: 'desc' }], ...skipTake(query) }),
|
|
prisma.registration.count({ where }),
|
|
]);
|
|
return pageOf(rows.map(r => toSummary(r, now)), total, query);
|
|
},
|
|
});
|