feat(api): implement /api/user/[user] endpoint
This commit is contained in:
parent
3e890d4363
commit
eb04c276ab
2 changed files with 88 additions and 0 deletions
55
src/app/api/user/[user]/route.ts
Normal file
55
src/app/api/user/[user]/route.ts
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
import { auth } from '@/auth';
|
||||||
|
import { prisma } from '@/prisma';
|
||||||
|
import {
|
||||||
|
returnZodTypeCheckedResponse,
|
||||||
|
userAuthenticated,
|
||||||
|
} from '@/lib/apiHelpers';
|
||||||
|
import { PublicUserResponseSchema } from '../validation';
|
||||||
|
import { ErrorResponseSchema } from '@/app/api/validation';
|
||||||
|
|
||||||
|
export const GET = auth(async function GET(req, { params }) {
|
||||||
|
const authCheck = userAuthenticated(req);
|
||||||
|
if (!authCheck.continue)
|
||||||
|
return returnZodTypeCheckedResponse(
|
||||||
|
ErrorResponseSchema,
|
||||||
|
authCheck.response,
|
||||||
|
authCheck.metadata,
|
||||||
|
);
|
||||||
|
|
||||||
|
const requestedUser = (await params).user;
|
||||||
|
const dbUser = await prisma.user.findFirst({
|
||||||
|
where: {
|
||||||
|
OR: [{ id: requestedUser }, { name: requestedUser }],
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
first_name: true,
|
||||||
|
last_name: true,
|
||||||
|
email: true,
|
||||||
|
created_at: true,
|
||||||
|
updated_at: true,
|
||||||
|
image: true,
|
||||||
|
timezone: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!dbUser)
|
||||||
|
return returnZodTypeCheckedResponse(
|
||||||
|
ErrorResponseSchema,
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: 'User not found',
|
||||||
|
},
|
||||||
|
{ status: 404 },
|
||||||
|
);
|
||||||
|
|
||||||
|
return returnZodTypeCheckedResponse(
|
||||||
|
PublicUserResponseSchema,
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
user: dbUser,
|
||||||
|
},
|
||||||
|
{ status: 200 },
|
||||||
|
);
|
||||||
|
});
|
33
src/app/api/user/[user]/swagger.ts
Normal file
33
src/app/api/user/[user]/swagger.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import { PublicUserResponseSchema } from '../validation';
|
||||||
|
import {
|
||||||
|
notAuthenticatedResponse,
|
||||||
|
userNotFoundResponse,
|
||||||
|
} from '@/lib/defaultApiResponses';
|
||||||
|
import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
|
||||||
|
import zod from 'zod/v4';
|
||||||
|
import { UserIdParamSchema } from '../../validation';
|
||||||
|
|
||||||
|
export default function registerSwaggerPaths(registry: OpenAPIRegistry) {
|
||||||
|
registry.registerPath({
|
||||||
|
method: 'get',
|
||||||
|
path: '/api/user/{user}',
|
||||||
|
request: {
|
||||||
|
params: zod.object({
|
||||||
|
user: UserIdParamSchema,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
description: 'User information retrieved successfully.',
|
||||||
|
content: {
|
||||||
|
'application/json': {
|
||||||
|
schema: PublicUserResponseSchema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
...notAuthenticatedResponse,
|
||||||
|
...userNotFoundResponse,
|
||||||
|
},
|
||||||
|
tags: ['User'],
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue