feat(api): implement DELETE method for /api/user/me endpoint
Some checks failed
container-scan / Container Scan (pull_request) Failing after 32s
docker-build / docker (pull_request) Successful in 7m22s

This commit is contained in:
Dominik 2025-06-23 10:44:26 +02:00
parent 16b878a2e9
commit 4cf5ce26ff
Signed by: dominik
GPG key ID: 06A4003FC5049644
2 changed files with 62 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import {
import { FullUserResponseSchema } from '../validation'; import { FullUserResponseSchema } from '../validation';
import { import {
ErrorResponseSchema, ErrorResponseSchema,
SuccessResponseSchema,
ZodErrorResponseSchema, ZodErrorResponseSchema,
} from '@/app/api/validation'; } from '@/app/api/validation';
@ -117,3 +118,43 @@ export const PATCH = auth(async function PATCH(req) {
{ status: 200 }, { status: 200 },
); );
}); });
export const DELETE = auth(async function DELETE(req) {
const authCheck = userAuthenticated(req);
if (!authCheck.continue)
return returnZodTypeCheckedResponse(
ErrorResponseSchema,
authCheck.response,
authCheck.metadata,
);
const dbUser = await prisma.user.findUnique({
where: {
id: authCheck.user.id,
},
});
if (!dbUser)
return returnZodTypeCheckedResponse(
ErrorResponseSchema,
{
success: false,
message: 'User not found',
},
{ status: 404 },
);
await prisma.user.delete({
where: {
id: authCheck.user.id,
},
});
return returnZodTypeCheckedResponse(
SuccessResponseSchema,
{
success: true,
message: 'User deleted successfully',
},
{ status: 200 },
);
});

View file

@ -7,6 +7,7 @@ import {
serverReturnedDataValidationErrorResponse, serverReturnedDataValidationErrorResponse,
userNotFoundResponse, userNotFoundResponse,
} from '@/lib/defaultApiResponses'; } from '@/lib/defaultApiResponses';
import { SuccessResponseSchema } from '../../validation';
export default function registerSwaggerPaths(registry: OpenAPIRegistry) { export default function registerSwaggerPaths(registry: OpenAPIRegistry) {
registry.registerPath({ registry.registerPath({
@ -60,4 +61,24 @@ export default function registerSwaggerPaths(registry: OpenAPIRegistry) {
}, },
tags: ['User'], tags: ['User'],
}); });
registry.registerPath({
method: 'delete',
path: '/api/user/me',
description: 'Delete the currently authenticated user',
responses: {
200: {
description: 'User deleted successfully',
content: {
'application/json': {
schema: SuccessResponseSchema,
},
},
},
...notAuthenticatedResponse,
...userNotFoundResponse,
...serverReturnedDataValidationErrorResponse,
},
tags: ['User'],
});
} }