add update session endpoint

This commit is contained in:
mrjvs 2023-11-24 18:00:06 +01:00
parent b30623c483
commit 72657e73c8
2 changed files with 30 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "backend",
"version": "1.1.2",
"version": "1.1.3",
"private": true,
"homepage": "https://github.com/movie-web/backend",
"engines": {

View File

@ -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);
}),
);
});