Files
graff-mate-server/src/controllers/sessionsController.ts
T
2025-06-17 12:27:20 +05:00

103 lines
2.6 KiB
TypeScript

import Elysia, { status, t } from "elysia";
import authMiddleware from "../middlewares/auth";
import createSession from "../services/sessions/createSession";
import getSessions from "../services/sessions/getSessions";
import updateSession from "../services/sessions/updateSession";
import db from "../db";
import { eq } from "drizzle-orm";
import { sessionsTable } from "../db/schema/sessions";
import getCount from "../services/sessions/getCountSessions";
const sessionsController = new Elysia({ prefix: "/sessions" })
.use(authMiddleware)
.get("/", ({ auth, query }) => getSessions(auth, query), {
query: t.Partial(
t.Object({
limit: t.Number(),
status: t.Union([
t.Literal("starting"),
t.Literal("started"),
t.Literal("restarting"),
t.Literal("ending"),
t.Literal("ended"),
]),
clientSearch: t.String(),
appIds: t.Array(t.String()),
ownerIds: t.Array(t.String()),
})
),
})
.get(
"/count",
({ query, auth: { companyId } }) => getCount({ ...query, companyId }),
{
query: t.Partial(
t.Object({
ownerIds: t.ArrayString(),
appIds: t.ArrayString(),
clientSearch: t.String(),
})
),
}
)
.get("/:id", async ({ params }) => {
try {
const session = await db.query.sessionsTable.findFirst({
where: eq(sessionsTable.id, params.id),
with: {
client: true,
app: true,
server: true,
owner: {
columns: {
password: false,
fullname: true,
},
},
},
});
return session;
} catch (error) {
console.log((error as Error).message);
return status(404, "Session not found");
}
})
.post("/", ({ body, auth }) => createSession(auth, body), {
body: t.Object({
appId: t.String(),
serverId: t.String(),
clientId: t.String(),
}),
})
.put(
"/:id",
({ params, body }) =>
updateSession(
params.id,
body.status as
| "starting"
| "started"
| "restarting"
| "ending"
| "ended"
),
{
params: t.Object({
id: t.String(),
}),
body: t.Object({
status: t.Union([
t.Literal("starting"),
t.Literal("started"),
t.Literal("restarting"),
t.Literal("ending"),
t.Literal("ended"),
t.Literal("null"),
t.Literal("undefined"),
]),
}),
}
);
export default sessionsController;