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
+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 { 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,
+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;