From 6530e81402fb0f93a9637940e941c958155ec090 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 4 Mar 2026 14:16:23 +0300 Subject: [PATCH] feat: add profile routes Made-with: Cursor --- src/routes/profile.ts | 73 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/routes/profile.ts diff --git a/src/routes/profile.ts b/src/routes/profile.ts new file mode 100644 index 0000000..ef2b97a --- /dev/null +++ b/src/routes/profile.ts @@ -0,0 +1,73 @@ +import type { FastifyInstance } from 'fastify'; +import { UserService } from '../services/user/user.service.js'; + +const patchProfileSchema = { + body: { + type: 'object', + properties: { + nickname: { type: 'string', minLength: 2, maxLength: 30 }, + avatarUrl: { type: ['string', 'null'], maxLength: 500 }, + country: { type: ['string', 'null'], maxLength: 100 }, + city: { type: ['string', 'null'], maxLength: 100 }, + selfLevel: { type: ['string', 'null'], enum: ['jun', 'mid', 'sen'] }, + isPublic: { type: 'boolean' }, + }, + additionalProperties: false, + }, +}; + +const usernameParamsSchema = { + params: { + type: 'object', + required: ['username'], + properties: { + username: { type: 'string', minLength: 2, maxLength: 30 }, + }, + }, +}; + +export async function profileRoutes(app: FastifyInstance) { + const userService = new UserService(app.db); + const { rateLimitOptions } = app; + + app.get( + '/', + { + config: { rateLimit: rateLimitOptions.apiAuthed }, + preHandler: [app.authenticate, app.withSubscription], + }, + async (req, reply) => { + const userId = req.user!.id; + const profile = await userService.getPrivateProfile(userId); + return reply.send(profile); + }, + ); + + app.patch( + '/', + { + schema: patchProfileSchema, + config: { rateLimit: rateLimitOptions.apiAuthed }, + preHandler: [app.authenticate], + }, + async (req, reply) => { + const userId = req.user!.id; + const body = req.body as Parameters[1]; + const profile = await userService.updateProfile(userId, body); + return reply.send(profile); + }, + ); + + app.get( + '/:username', + { + schema: usernameParamsSchema, + config: { rateLimit: rateLimitOptions.apiGuest }, + }, + async (req, reply) => { + const { username } = req.params as { username: string }; + const profile = await userService.getPublicProfile(username); + return reply.send(profile); + }, + ); +}