This commit is contained in:
2025-06-19 14:03:04 +05:00
parent daca480edf
commit 971cd8ff8e
30 changed files with 148 additions and 139 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ export default async function createApp(
companyId: string;
}
) {
if (auth.userId !== "hmac-user") {
if (auth.managerId !== "hmac-manager") {
return status(403, {
error: "Forbidden",
});
+6 -6
View File
@@ -1,6 +1,6 @@
import { eq } from "drizzle-orm";
import db from "../../db";
import { tokensTable, usersTable } from "../../db/schema";
import { tokensTable, managersTable } from "../../db/schema";
import { status } from "elysia";
import { generateToken } from "../../utils/generateToken";
@@ -12,18 +12,18 @@ export default async function login({
password: string;
}) {
try {
const user = await db.query.usersTable.findFirst({
where: eq(usersTable.email, email),
const manager = await db.query.managersTable.findFirst({
where: eq(managersTable.email, email),
});
if (!user || !Bun.password.verifySync(password, user.password))
if (!manager || !Bun.password.verifySync(password, manager.password))
return status(401, { error: "Wrong credentials" });
const token = await generateToken(user.id);
const token = await generateToken(manager.id);
await db.insert(tokensTable).values({
token,
userId: user.id,
managerId: manager.id,
});
return {
+4 -4
View File
@@ -1,13 +1,13 @@
import { eq } from "drizzle-orm";
import db from "../../db";
import { usersTable } from "../../db/schema";
import { managersTable } from "../../db/schema";
import { status } from "elysia";
import type { AuthContext } from "../../middlewares/auth";
export default async function me(auth: AuthContext) {
try {
const user = await db.query.usersTable.findFirst({
where: eq(usersTable.id, auth.userId),
const manager = await db.query.managersTable.findFirst({
where: eq(managersTable.id, auth.managerId),
columns: {
id: true,
email: true,
@@ -15,7 +15,7 @@ export default async function me(auth: AuthContext) {
},
});
return user;
return manager;
} catch (error) {
console.log((error as Error).message);
return status(500, "Internal Server Error");
-26
View File
@@ -1,26 +0,0 @@
// 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) {}
// }
+21 -15
View File
@@ -1,10 +1,10 @@
import db from "../../db";
import { clientsTable } from "../../db/schema";
import { clientsTable, clientsToManagers } from "../../db/schema";
import { status } from "elysia";
async function createClient(
auth: {
userId: string;
managerId: string;
companyId: string;
},
body: {
@@ -15,20 +15,26 @@ async function createClient(
) {
try {
// Create new client
const [newClient] = await db
.insert(clientsTable)
.values({
...body,
ownerId: auth.userId,
companyId: auth.companyId,
})
.onConflictDoUpdate({
target: [clientsTable.phone],
set: { ...body, ownerId: auth.userId, companyId: auth.companyId },
})
.returning();
return await db.transaction(async (tx) => {
const [newClient] = await tx
.insert(clientsTable)
.values({
...body,
companyId: auth.companyId,
})
.onConflictDoUpdate({
target: [clientsTable.phone],
set: { ...body, companyId: auth.companyId },
})
.returning();
return newClient;
await tx.insert(clientsToManagers).values({
clientId: newClient.id,
managerId: auth.managerId,
});
return newClient;
});
} catch (error) {
console.log((error as Error).message);
return status(500, "Internal Server Error");
+5 -4
View File
@@ -5,10 +5,11 @@ import { status } from "elysia";
async function getByPhone(phone: string) {
try {
const user = await db.query.clientsTable.findFirst({
where: eq(clientsTable.phone, phone),
});
return user || status(404, "Not Found");
return (
(await db.query.clientsTable.findFirst({
where: eq(clientsTable.phone, phone),
})) || status(404, "Not Found")
);
} catch (error) {
console.log((error as Error).message);
return status(500, "Internal Server Error");
+3 -2
View File
@@ -26,11 +26,12 @@ async function getClients({
),
limit,
with: {
managers: { with: { manager: { columns: { password: false } } } },
sessions: {
with: {
owner: { columns: { password: false } },
manager: { columns: { password: false } },
app: true,
comments: { with: { owner: { columns: { password: false } } } },
comments: { with: { manager: { columns: { password: false } } } },
},
},
},
+3 -3
View File
@@ -4,11 +4,11 @@ import { commentsTable } from "../../db/schema";
export async function createComment({
sessionId,
userId,
managerId,
text,
}: {
sessionId: string;
userId: string;
managerId: string;
text: string;
}) {
try {
@@ -16,7 +16,7 @@ export async function createComment({
.insert(commentsTable)
.values({
sessionId,
ownerId: userId,
managerId,
text,
})
.returning();
+3 -3
View File
@@ -3,15 +3,15 @@ import { and, desc, eq } from "drizzle-orm";
import db from "../../db";
import { commentsTable } from "../../db/schema";
export async function getComments(sessionId: string, userId: string) {
export async function getComments(sessionId: string, managerId: string) {
try {
const comments = await db.query.commentsTable.findMany({
where: and(
eq(commentsTable.sessionId, sessionId),
eq(commentsTable.ownerId, userId)
eq(commentsTable.managerId, managerId)
),
with: {
owner: {
manager: {
columns: {
fullname: true,
id: true,
+1 -1
View File
@@ -13,7 +13,7 @@ export default async function createServer(
companyId: string;
}
) {
if (auth.userId !== "hmac-user") return status(403, "Forbidden");
if (auth.managerId !== "hmac-manager") return status(403, "Forbidden");
try {
const server = await db
+1 -1
View File
@@ -45,7 +45,7 @@ export default async function getServers(
},
},
server: true,
owner: {
manager: {
columns: {
fullname: true,
},
+1 -1
View File
@@ -1,7 +1,7 @@
import { status } from "elysia";
import { eq } from "drizzle-orm";
import db from "../../db";
import { serversTable } from "../../db/schema/servers";
import { serversTable } from "../../db/schema";
async function updateServer({
params,
+2 -2
View File
@@ -5,7 +5,7 @@ import { status } from "elysia";
async function createSession(
auth: {
userId: string;
managerId: string;
companyId: string;
},
body: {
@@ -36,7 +36,7 @@ async function createSession(
.insert(sessionsTable)
.values({
...body,
ownerId: auth.userId,
managerId: auth.managerId,
companyId: auth.companyId,
})
.returning();
+3 -3
View File
@@ -4,12 +4,12 @@ import { and, count, eq, ilike, inArray } from "drizzle-orm";
import { clientsTable, sessionsTable } from "../../db/schema";
async function getCount({
ownerIds,
managerIds,
appIds,
clientSearch,
companyId,
}: {
ownerIds?: string[];
managerIds?: string[];
appIds?: string[];
clientSearch?: string;
companyId: string;
@@ -21,7 +21,7 @@ async function getCount({
.where(
and(
eq(sessionsTable.companyId, companyId),
ownerIds ? inArray(sessionsTable.ownerId, ownerIds) : undefined,
managerIds ? inArray(sessionsTable.managerId, managerIds) : undefined,
appIds ? inArray(sessionsTable.appId, appIds) : undefined,
clientSearch
? inArray(
+5 -5
View File
@@ -11,7 +11,7 @@ async function getSessions(
status?: "starting" | "started" | "restarting" | "ending" | "ended";
clientSearch?: string;
appIds?: string[];
ownerIds?: string[];
managerIds?: string[];
}
) {
try {
@@ -30,8 +30,8 @@ async function getSessions(
)
: undefined,
query.appIds ? inArray(sessionsTable.appId, query.appIds) : undefined,
query.ownerIds
? inArray(sessionsTable.ownerId, query.ownerIds)
query.managerIds
? inArray(sessionsTable.managerId, query.managerIds)
: undefined
),
with: {
@@ -40,14 +40,14 @@ async function getSessions(
server: true,
comments: {
with: {
owner: {
manager: {
columns: {
fullname: true,
},
},
},
},
owner: {
manager: {
columns: {
password: false,
fullname: true,
+3 -3
View File
@@ -1,13 +1,13 @@
import db from "../../db";
import type { AuthContext } from "../../middlewares/auth";
import { eq } from "drizzle-orm";
import { usersTable } from "../../db/schema";
import { managersTable } from "../../db/schema";
import { status } from "elysia";
async function getManagers(auth: AuthContext) {
try {
return await db.query.usersTable.findMany({
where: eq(usersTable.companyId, auth.companyId),
return await db.query.managersTable.findMany({
where: eq(managersTable.companyId, auth.companyId),
columns: {
id: true,
fullname: true,