From 10e9e06c27ceed5478887b778f62339e22d6996b Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sat, 25 Nov 2023 16:09:29 +0100 Subject: [PATCH] only have one session router --- src/modules/fastify/routes.ts | 2 +- src/routes/sessions/session.ts | 63 --------------------------- src/routes/{ => sessions}/sessions.ts | 8 ++-- 3 files changed, 5 insertions(+), 68 deletions(-) delete mode 100644 src/routes/sessions/session.ts rename src/routes/{ => sessions}/sessions.ts (84%) diff --git a/src/modules/fastify/routes.ts b/src/modules/fastify/routes.ts index 6d9849c..c51534f 100644 --- a/src/modules/fastify/routes.ts +++ b/src/modules/fastify/routes.ts @@ -2,7 +2,7 @@ import { loginAuthRouter } from '@/routes/auth/login'; import { manageAuthRouter } from '@/routes/auth/manage'; import { metaRouter } from '@/routes/meta'; import { metricsRouter } from '@/routes/metrics'; -import { sessionsRouter } from '@/routes/sessions'; +import { sessionsRouter } from '@/routes/sessions/sessions'; import { userBookmarkRouter } from '@/routes/users/bookmark'; import { userDeleteRouter } from '@/routes/users/delete'; import { userEditRouter } from '@/routes/users/edit'; diff --git a/src/routes/sessions/session.ts b/src/routes/sessions/session.ts deleted file mode 100644 index 59ca037..0000000 --- a/src/routes/sessions/session.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Session, formatSession } from '@/db/models/Session'; -import { StatusError } from '@/services/error'; -import { handle } from '@/services/handler'; -import { makeRouter } from '@/services/router'; -import { z } from 'zod'; - -export const sessionRouter = makeRouter((app) => { - app.delete( - '/sessions/:sid', - { - schema: { - params: z.object({ - sid: z.string(), - }), - }, - }, - handle(async ({ auth, params, em }) => { - await auth.assert(); - - const targetedSession = await em.findOne(Session, { id: params.sid }); - if (!targetedSession) - return { - id: params.sid, - }; - - if (targetedSession.user !== auth.user.id) - throw new StatusError('Cannot delete sessions you do not own', 401); - - await em.removeAndFlush(targetedSession); - return { - id: params.sid, - }; - }), - ); - - app.patch( - '/sessions/:sid', - { - schema: { - params: z.object({ - sid: z.string(), - }), - body: z.object({ - deviceName: z.string().min(1).optional(), - }), - }, - }, - handle(async ({ auth, params, body, em }) => { - await auth.assert(); - - const targetedSession = await em.findOne(Session, { id: params.sid }); - if (!targetedSession) throw new StatusError('Not found', 404); - if (targetedSession.id !== params.sid) - throw new StatusError('Cannot edit sessions other than your own', 401); - - if (body.deviceName) targetedSession.device = body.deviceName; - - await em.persistAndFlush(targetedSession); - - return formatSession(targetedSession); - }), - ); -}); diff --git a/src/routes/sessions.ts b/src/routes/sessions/sessions.ts similarity index 84% rename from src/routes/sessions.ts rename to src/routes/sessions/sessions.ts index ab022e1..f512eb5 100644 --- a/src/routes/sessions.ts +++ b/src/routes/sessions/sessions.ts @@ -13,7 +13,7 @@ export const sessionsRouter = makeRouter((app) => { sid: z.string(), }), body: z.object({ - name: z.string().max(500).min(1).optional(), + deviceName: z.string().max(500).min(1).optional(), }), }, }, @@ -25,10 +25,10 @@ export const sessionsRouter = makeRouter((app) => { if (!targetedSession) throw new StatusError('Session cannot be found', 404); - if (targetedSession.user !== auth.user.id) - throw new StatusError('Cannot modify sessions you do not own', 401); + if (targetedSession.id !== params.sid) + throw new StatusError('Cannot edit sessions other than your own', 401); - if (body.name) targetedSession.device = body.name; + if (body.deviceName) targetedSession.device = body.deviceName; await em.persistAndFlush(targetedSession);