This commit is contained in:
2025-03-20 14:39:30 +05:00
parent a44eae17dc
commit 609b94b753
38 changed files with 1382 additions and 102 deletions
+45
View File
@@ -0,0 +1,45 @@
import { error } from "elysia";
import db from "../../db";
import { appsTable, serversTable } from "../../db/schema";
import { eq, inArray } from "drizzle-orm";
import type { AuthContext } from "./../../middlewares/auth";
export default async function createApp(
auth: AuthContext,
body: {
apps: string[];
serverId: string;
companyId: string;
}
) {
if (auth.userId !== "hmac-user") {
return error(403, "Forbidden");
}
try {
const existingApp = await db.query.appsTable.findMany({
where: inArray(appsTable.name, body.apps),
});
if (existingApp.length) {
return error(400, "App with this name already exists");
}
const apps = await db
.insert(appsTable)
.values(
body.apps.map((app) => ({
name: app,
fileName: app,
companyId: body.companyId,
serverId: body.serverId,
}))
)
.returning();
return apps;
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
View File
+36
View File
@@ -0,0 +1,36 @@
import { eq } from "drizzle-orm";
import db from "../../db";
import { tokensTable, usersTable } from "../../db/schema";
import { error } from "elysia";
import { generateToken } from "../../utils/generateToken";
export default async function login({
email,
password,
}: {
email: string;
password: string;
}) {
try {
const user = await db.query.usersTable.findFirst({
where: eq(usersTable.email, email),
});
if (!user || !Bun.password.verifySync(password, user.password))
return error(401, { error: "Wrong credentials" });
const token = await generateToken(user.id);
await db.insert(tokensTable).values({
token,
userId: user.id,
});
return {
token,
};
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
+13
View File
@@ -0,0 +1,13 @@
import { error } from "elysia";
import { eq } from "drizzle-orm";
import db from "../../db";
import { tokensTable } from "../../db/schema";
export default async function logout(token: string) {
try {
await db.delete(tokensTable).where(eq(tokensTable.token, token));
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
+26
View File
@@ -0,0 +1,26 @@
// import { error } from "elysia";
// import { eq } from "drizzle-orm";
// import db from "../../db";
// import { usersTable } from "../../db/schema";
// export default async function register(payload: {
// username: string;
// password: string;
// companyId: string;
// }) {
// const { companyId, username } = payload;
// try {
// const candidate = await db.query.usersTable.findFirst({
// where: eq(usersTable.username, username),
// });
// if (candidate) return error(400, "user already exists");
// const password = Bun.hash(payload.password);
// const user = await db
// .insert(usersTable)
// .values({ companyId, username, password });
// } catch (error) {}
// }
+41
View File
@@ -0,0 +1,41 @@
import { error } from "elysia";
import db from "../../db";
import { serversTable } from "../../db/schema";
import { eq } from "drizzle-orm";
import type { AuthContext } from "./../../middlewares/auth";
export default async function createServer(
auth: AuthContext,
body: {
hostname: string;
name: string;
location: string;
companyId: string;
}
) {
if (auth.userId !== "hmac-user") {
return error(403, "Forbidden");
}
try {
const existingServer = await db.query.serversTable.findFirst({
where: eq(serversTable.hostname, body.hostname),
});
if (existingServer) {
return error(400, "Server with this hostname already exists");
}
const server = await db
.insert(serversTable)
.values({
...body,
})
.returning();
return server;
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
+55
View File
@@ -0,0 +1,55 @@
import { error } from "elysia";
import db from "../../db";
import { desc, eq } from "drizzle-orm";
import { serversTable, sessionsTable } from "../../db/schema";
import type { AuthContext } from "../../middlewares/auth";
export default async function getServers(
auth: AuthContext,
query?: {
withLastSession?: boolean;
with?: string;
}
) {
try {
const servers = await db.query.serversTable.findMany({
where: eq(serversTable.companyId, auth.companyId),
with: {
...(query?.with
? Object.fromEntries(
JSON.parse(query.with).map((k: string) => [
k,
{
orderBy: desc(sessionsTable.createdAt),
},
])
)
: query?.withLastSession
? {
sessions: {
orderBy: desc(sessionsTable.createdAt),
limit: 1,
with: {
client: {
columns: {
fullname: true,
},
},
app: {
columns: {
name: true,
},
},
},
},
}
: undefined),
},
});
return servers;
} catch (err) {
console.log((err as Error).message);
return error(500, "Internal Server Error");
}
}
+53
View File
@@ -0,0 +1,53 @@
import { and, eq, or } from "drizzle-orm";
import db from "../../db";
import { sessionsTable } from "../../db/schema";
import { error } from "elysia";
async function createSession(
auth: {
userId: string;
companyId: string;
},
body: {
appId: string;
serverId: string;
clientId: string;
}
) {
try {
// Check for existing session
const [existingSession] = await db
.select()
.from(sessionsTable)
.where(
and(
eq(sessionsTable.serverId, body.serverId),
or(
eq(sessionsTable.status, "starting"),
eq(sessionsTable.status, "started")
)
)
);
if (existingSession) {
return error(409, "Session already exists");
}
// Create new session
const [newSession] = await db
.insert(sessionsTable)
.values({
...body,
ownerId: auth.userId,
companyId: auth.companyId,
status: "starting",
})
.returning();
return newSession;
} catch (err) {
return error(500, "Internal Server Error");
}
}
export default createSession;
+34
View File
@@ -0,0 +1,34 @@
import db from "../../db";
import { error } from "elysia";
import { and, desc, eq } from "drizzle-orm";
import { sessionsTable } from "../../db/schema";
import type { AuthContext } from "../../middlewares/auth";
async function getSessions(
auth: AuthContext,
query?: {
limit?: number;
}
) {
try {
const sessions = await db.query.sessionsTable.findMany({
where: and(
eq(sessionsTable.ownerId, auth.userId),
eq(sessionsTable.companyId, auth.companyId)
),
with: {
client: true,
app: true,
server: true,
},
limit: query?.limit,
orderBy: desc(sessionsTable.createdAt),
});
return sessions;
} catch (err) {
return error(500, "Internal Server Error");
}
}
export default getSessions;
+23
View File
@@ -0,0 +1,23 @@
import { eq } from "drizzle-orm";
import db from "../../db";
import { sessionsTable } from "../../db/schema";
import { error } from "elysia";
async function updateSession(
sessionId: string,
status: "starting" | "started" | "restarting" | "ending" | "ended"
) {
try {
const session = await db
.update(sessionsTable)
.set({ status })
.where(eq(sessionsTable.id, sessionId))
.returning();
return session;
} catch (err) {
return error(500, "Internal Server Error");
}
}
export default updateSession;
+21
View File
@@ -0,0 +1,21 @@
import { eq } from "drizzle-orm";
import db from "../../db";
import { usersTable } from "../../db/schema";
import { error } from "elysia";
export default async function me(userId: string) {
try {
const user = await db.query.usersTable.findFirst({
where: eq(usersTable.id, userId),
columns: {
id: true,
email: true,
fullname: true,
},
});
return user;
} catch (err) {
return error(500, "Internal Server Error");
}
}