feat: simplify testing structure into api and e2e categories
This commit is contained in:
@@ -4,6 +4,7 @@ import Elysia from "elysia";
|
||||
import { apiMiddleware } from "../middleware/apiMiddleware";
|
||||
import { auth } from "../utils/auth";
|
||||
import { apikey } from "./apikey";
|
||||
import { profile } from "./profile";
|
||||
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
|
||||
@@ -18,7 +19,8 @@ const api = new Elysia({
|
||||
return { data };
|
||||
})
|
||||
.use(apiMiddleware)
|
||||
.use(apikey);
|
||||
.use(apikey)
|
||||
.use(profile);
|
||||
|
||||
if (!isProduction) {
|
||||
api.use(
|
||||
|
||||
67
src/api/profile.ts
Normal file
67
src/api/profile.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import { prisma } from "../utils/db";
|
||||
import logger from "../utils/logger";
|
||||
|
||||
export const profile = new Elysia({
|
||||
prefix: "/profile",
|
||||
}).post(
|
||||
"/update",
|
||||
async (ctx) => {
|
||||
const { body, set, user } = ctx as any;
|
||||
try {
|
||||
if (!user) {
|
||||
set.status = 401;
|
||||
return { error: "Unauthorized" };
|
||||
}
|
||||
|
||||
const { name, image } = body;
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
name: name || undefined,
|
||||
image: image || undefined,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
image: true,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
logger.info({ userId: user.id }, "Profile updated successfully");
|
||||
|
||||
return { user: updatedUser };
|
||||
} catch (error) {
|
||||
logger.error({ error, userId: user?.id }, "Failed to update profile");
|
||||
set.status = 500;
|
||||
return { error: "Failed to update profile" };
|
||||
}
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.Optional(t.String()),
|
||||
image: t.Optional(t.String()),
|
||||
}),
|
||||
response: {
|
||||
200: t.Object({
|
||||
user: t.Object({
|
||||
id: t.String(),
|
||||
name: t.Any(),
|
||||
email: t.String(),
|
||||
image: t.Any(),
|
||||
role: t.Any(),
|
||||
}),
|
||||
}),
|
||||
401: t.Object({ error: t.String() }),
|
||||
500: t.Object({ error: t.String() }),
|
||||
},
|
||||
|
||||
detail: {
|
||||
summary: "Update user profile",
|
||||
description: "Update the authenticated user's name or profile image",
|
||||
},
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user