upd
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import authMiddleware from "../middlewares/auth";
|
||||
import create from "../services/apps/create";
|
||||
import getByCompanyId from "../services/apps/getByCompanyId";
|
||||
|
||||
const appsController = new Elysia({ prefix: "/apps" })
|
||||
.use(authMiddleware)
|
||||
.get("/", ({ auth }) => getByCompanyId(auth))
|
||||
.post("/", async ({ auth, body }) => await create(auth, body), {
|
||||
body: t.Object({
|
||||
apps: t.Array(t.String()),
|
||||
|
||||
@@ -1,32 +1,41 @@
|
||||
import Elysia, { error, t } from "elysia";
|
||||
import authMiddleware from "../middlewares/auth";
|
||||
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 db from "../db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { sessionsTable } from "../db/schema/sessions";
|
||||
import getCount from "../services/sessions/getCount";
|
||||
|
||||
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(),
|
||||
appId: t.String(),
|
||||
ownerId: t.String(),
|
||||
})
|
||||
),
|
||||
})
|
||||
.get(
|
||||
"/",
|
||||
async ({ auth, query }) => {
|
||||
return await getSessions(auth, query);
|
||||
},
|
||||
"/count",
|
||||
({ query, auth: { companyId } }) => getCount({ ...query, companyId }),
|
||||
{
|
||||
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"),
|
||||
]),
|
||||
})
|
||||
),
|
||||
query: t.Object({
|
||||
ownerId: t.Optional(t.String()),
|
||||
appId: t.Optional(t.String()),
|
||||
clientSearch: t.Optional(t.String()),
|
||||
}),
|
||||
}
|
||||
)
|
||||
.get("/:id", async ({ params }) => {
|
||||
@@ -51,23 +60,17 @@ const sessionsController = new Elysia({ prefix: "/sessions" })
|
||||
return error(404, "Session not found");
|
||||
}
|
||||
})
|
||||
.post(
|
||||
"/",
|
||||
async ({ body, auth }) => {
|
||||
return await createSession(auth, body);
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
appId: t.String(),
|
||||
serverId: t.String(),
|
||||
clientId: t.String(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
.post("/", ({ body, auth }) => createSession(auth, body), {
|
||||
body: t.Object({
|
||||
appId: t.String(),
|
||||
serverId: t.String(),
|
||||
clientId: t.String(),
|
||||
}),
|
||||
})
|
||||
.put(
|
||||
"/:id",
|
||||
async ({ params, body }) => {
|
||||
return await updateSession(
|
||||
({ params, body }) =>
|
||||
updateSession(
|
||||
params.id,
|
||||
body.status as
|
||||
| "starting"
|
||||
@@ -75,8 +78,7 @@ const sessionsController = new Elysia({ prefix: "/sessions" })
|
||||
| "restarting"
|
||||
| "ending"
|
||||
| "ended"
|
||||
);
|
||||
},
|
||||
),
|
||||
{
|
||||
params: t.Object({
|
||||
id: t.String(),
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import Elysia from "elysia";
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 { error } from "elysia";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { sessionsTable } from "../../db/schema";
|
||||
import { and, desc, eq, ilike, inArray } from "drizzle-orm";
|
||||
import { clientsTable, sessionsTable } from "../../db/schema";
|
||||
import type { AuthContext } from "../../middlewares/auth";
|
||||
|
||||
async function getSessions(
|
||||
@@ -9,14 +9,28 @@ async function getSessions(
|
||||
query: {
|
||||
limit?: number;
|
||||
status?: "starting" | "started" | "restarting" | "ending" | "ended";
|
||||
clientSearch?: string;
|
||||
appId?: string;
|
||||
ownerId?: string;
|
||||
}
|
||||
) {
|
||||
try {
|
||||
const sessions = await db.query.sessionsTable.findMany({
|
||||
where: and(
|
||||
// eq(sessionsTable.ownerId, auth.userId),
|
||||
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: {
|
||||
client: true,
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user