This commit is contained in:
2025-06-11 11:22:16 +05:00
parent 4535d4f308
commit f41a15f4c8
7 changed files with 146 additions and 39 deletions
+2
View File
@@ -1,9 +1,11 @@
import Elysia, { t } from "elysia"; import Elysia, { t } from "elysia";
import authMiddleware from "../middlewares/auth"; import authMiddleware from "../middlewares/auth";
import create from "../services/apps/create"; import create from "../services/apps/create";
import getByCompanyId from "../services/apps/getByCompanyId";
const appsController = new Elysia({ prefix: "/apps" }) const appsController = new Elysia({ prefix: "/apps" })
.use(authMiddleware) .use(authMiddleware)
.get("/", ({ auth }) => getByCompanyId(auth))
.post("/", async ({ auth, body }) => await create(auth, body), { .post("/", async ({ auth, body }) => await create(auth, body), {
body: t.Object({ body: t.Object({
apps: t.Array(t.String()), apps: t.Array(t.String()),
+36 -34
View File
@@ -1,32 +1,41 @@
import Elysia, { error, t } from "elysia"; import Elysia, { error, t } from "elysia";
import authMiddleware from "../middlewares/auth"; import authMiddleware from "../middlewares/auth";
import createSession from "../services/sessions/create"; import createSession from "../services/sessions/create";
import getSessions from "../services/sessions/get"; import getSessions from "../services/sessions/getSessions";
import updateSession from "../services/sessions/update"; import updateSession from "../services/sessions/update";
import db from "../db"; import db from "../db";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { sessionsTable } from "../db/schema/sessions"; import { sessionsTable } from "../db/schema/sessions";
import getCount from "../services/sessions/getCount";
const sessionsController = new Elysia({ prefix: "/sessions" }) const sessionsController = new Elysia({ prefix: "/sessions" })
.use(authMiddleware) .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(),
appId: t.String(),
ownerId: t.String(),
})
),
})
.get( .get(
"/", "/count",
async ({ auth, query }) => { ({ query, auth: { companyId } }) => getCount({ ...query, companyId }),
return await getSessions(auth, query);
},
{ {
query: t.Partial( query: t.Object({
t.Object({ ownerId: t.Optional(t.String()),
limit: t.Number(), appId: t.Optional(t.String()),
status: t.Union([ clientSearch: t.Optional(t.String()),
t.Literal("starting"), }),
t.Literal("started"),
t.Literal("restarting"),
t.Literal("ending"),
t.Literal("ended"),
]),
})
),
} }
) )
.get("/:id", async ({ params }) => { .get("/:id", async ({ params }) => {
@@ -51,23 +60,17 @@ const sessionsController = new Elysia({ prefix: "/sessions" })
return error(404, "Session not found"); return error(404, "Session not found");
} }
}) })
.post( .post("/", ({ body, auth }) => createSession(auth, body), {
"/", body: t.Object({
async ({ body, auth }) => { appId: t.String(),
return await createSession(auth, body); serverId: t.String(),
}, clientId: t.String(),
{ }),
body: t.Object({ })
appId: t.String(),
serverId: t.String(),
clientId: t.String(),
}),
}
)
.put( .put(
"/:id", "/:id",
async ({ params, body }) => { ({ params, body }) =>
return await updateSession( updateSession(
params.id, params.id,
body.status as body.status as
| "starting" | "starting"
@@ -75,8 +78,7 @@ const sessionsController = new Elysia({ prefix: "/sessions" })
| "restarting" | "restarting"
| "ending" | "ending"
| "ended" | "ended"
); ),
},
{ {
params: t.Object({ params: t.Object({
id: t.String(), id: t.String(),
+4 -1
View File
@@ -1,6 +1,9 @@
import Elysia from "elysia"; import Elysia from "elysia";
import authMiddleware from "../middlewares/auth"; import authMiddleware from "../middlewares/auth";
import getManagers from "../services/users/getManagers";
const usersController = new Elysia({ prefix: "/users" }).use(authMiddleware); const usersController = new Elysia({ prefix: "/users" })
.use(authMiddleware)
.get("/", ({ auth }) => getManagers(auth));
export default usersController; export default usersController;
+18
View File
@@ -0,0 +1,18 @@
import db from "../../db";
import { appsTable } from "../../db/schema";
import type { AuthContext } from "../../middlewares/auth";
import { eq } from "drizzle-orm";
import { error } from "elysia";
async function getByCompanyId(auth: AuthContext) {
try {
return await db.query.appsTable.findMany({
where: eq(appsTable.companyId, auth.companyId),
});
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
export default getByCompanyId;
+45
View File
@@ -0,0 +1,45 @@
import { error } from "elysia";
import db from "../../db";
import { and, count, eq, ilike, inArray } from "drizzle-orm";
import { clientsTable, sessionsTable } from "../../db/schema";
async function getCount({
ownerId,
appId,
clientSearch,
companyId,
}: {
ownerId?: string;
appId?: string;
clientSearch?: string;
companyId: string;
}) {
try {
const [{ count: countSessions }] = await db
.select({ count: count() })
.from(sessionsTable)
.where(
and(
eq(sessionsTable.companyId, companyId),
ownerId ? eq(sessionsTable.ownerId, ownerId) : undefined,
appId ? eq(sessionsTable.appId, appId) : undefined,
clientSearch
? inArray(
sessionsTable.clientId,
(
await db.query.clientsTable.findMany({
where: ilike(clientsTable.name, `%${clientSearch}%`),
})
).map((client) => client.id)
)
: undefined
)
);
return countSessions;
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
export default getCount;
@@ -1,7 +1,7 @@
import db from "../../db"; import db from "../../db";
import { error } from "elysia"; import { error } from "elysia";
import { and, desc, eq } from "drizzle-orm"; import { and, desc, eq, ilike, inArray } from "drizzle-orm";
import { sessionsTable } from "../../db/schema"; import { clientsTable, sessionsTable } from "../../db/schema";
import type { AuthContext } from "../../middlewares/auth"; import type { AuthContext } from "../../middlewares/auth";
async function getSessions( async function getSessions(
@@ -9,14 +9,28 @@ async function getSessions(
query: { query: {
limit?: number; limit?: number;
status?: "starting" | "started" | "restarting" | "ending" | "ended"; status?: "starting" | "started" | "restarting" | "ending" | "ended";
clientSearch?: string;
appId?: string;
ownerId?: string;
} }
) { ) {
try { try {
const sessions = await db.query.sessionsTable.findMany({ const sessions = await db.query.sessionsTable.findMany({
where: and( where: and(
// eq(sessionsTable.ownerId, auth.userId),
eq(sessionsTable.companyId, auth.companyId), eq(sessionsTable.companyId, auth.companyId),
query.status ? eq(sessionsTable.status, query.status) : undefined query.status ? eq(sessionsTable.status, query.status) : undefined,
query.clientSearch
? inArray(
sessionsTable.clientId,
(
await db.query.clientsTable.findMany({
where: ilike(clientsTable.name, `%${query.clientSearch}%`),
})
).map((client) => client.id)
)
: undefined,
query.appId ? eq(sessionsTable.appId, query.appId) : undefined,
query.ownerId ? eq(sessionsTable.ownerId, query.ownerId) : undefined
), ),
with: { with: {
client: true, client: true,
+23
View File
@@ -0,0 +1,23 @@
import db from "../../db";
import type { AuthContext } from "../../middlewares/auth";
import { eq } from "drizzle-orm";
import { usersTable } from "../../db/schema";
import { error } from "elysia";
async function getManagers(auth: AuthContext) {
try {
return await db.query.usersTable.findMany({
where: eq(usersTable.companyId, auth.companyId),
columns: {
id: true,
fullname: true,
email: true,
},
});
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
export default getManagers;