mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 18:13:26 +00:00
fix bugs + add a lot of endpoints
Co-authored-by: William Oldham <github@binaryoverload.co.uk>
This commit is contained in:
36
src/routes/users/delete.ts
Normal file
36
src/routes/users/delete.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Session } from '@/db/models/Session';
|
||||
import { User } from '@/db/models/User';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const userDeleteRouter = makeRouter((app) => {
|
||||
app.delete(
|
||||
'/users/:uid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
const user = await em.findOne(User, { id: params.uid });
|
||||
if (!user) throw new StatusError('User does not exist', 404);
|
||||
|
||||
if (auth.user.id !== user.id)
|
||||
throw new StatusError('Cannot delete user other than yourself', 403);
|
||||
|
||||
const sessions = await em.find(Session, { user: user.id });
|
||||
|
||||
await em.remove([user, ...sessions]);
|
||||
await em.flush();
|
||||
return {
|
||||
id: user.id,
|
||||
};
|
||||
}),
|
||||
);
|
||||
});
|
Reference in New Issue
Block a user