66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import db from "../../db";
|
|
import { error } from "elysia";
|
|
import { and, desc, eq, ilike, inArray } from "drizzle-orm";
|
|
import { clientsTable, sessionsTable } from "../../db/schema";
|
|
import type { AuthContext } from "../../middlewares/auth";
|
|
|
|
async function getSessions(
|
|
auth: AuthContext,
|
|
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.companyId, auth.companyId),
|
|
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,
|
|
app: true,
|
|
server: true,
|
|
comments: {
|
|
with: {
|
|
owner: {
|
|
columns: {
|
|
fullname: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
owner: {
|
|
columns: {
|
|
password: false,
|
|
fullname: true,
|
|
},
|
|
},
|
|
},
|
|
limit: query?.limit,
|
|
orderBy: desc(sessionsTable.createdAt),
|
|
});
|
|
|
|
return sessions;
|
|
} catch (err) {
|
|
return error(500, "Internal Server Error");
|
|
}
|
|
}
|
|
|
|
export default getSessions;
|