diff --git a/package.json b/package.json index c2a78d4..88e99bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "backend", - "version": "1.1.2", + "version": "1.1.3", "private": true, "homepage": "https://github.com/movie-web/backend", "engines": { diff --git a/src/routes/sessions/session.ts b/src/routes/sessions/session.ts index b8393e2..0f0eced 100644 --- a/src/routes/sessions/session.ts +++ b/src/routes/sessions/session.ts @@ -1,4 +1,4 @@ -import { Session } from '@/db/models/Session'; +import { Session, formatSession } from '@/db/models/Session'; import { StatusError } from '@/services/error'; import { handle } from '@/services/handler'; import { makeRouter } from '@/services/router'; @@ -32,4 +32,32 @@ export const sessionRouter = makeRouter((app) => { }; }), ); + + app.patch( + '/sessions/:sid', + { + schema: { + params: z.object({ + sid: z.string(), + }), + body: z.object({ + name: z.string().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.name) targetedSession.device = body.name; + + await em.persistAndFlush(targetedSession); + + return formatSession(targetedSession); + }), + ); });