diff --git a/src/controllers/clientsController.ts b/src/controllers/clientsController.ts index 8895744..b20af30 100644 --- a/src/controllers/clientsController.ts +++ b/src/controllers/clientsController.ts @@ -5,6 +5,7 @@ import getByPhone from "../services/clients/getByPhone"; import getClients from "../services/clients/getClients"; import getCount from "../services/clients/getCount"; import { editClient } from "../services/clients/editClient"; +import { getOne } from "../services/clients/getOne"; export const clientsController = new Elysia({ prefix: "/clients" }) .use(authMiddleware) @@ -19,6 +20,9 @@ export const clientsController = new Elysia({ prefix: "/clients" }) query: t.Partial(t.Object({ limit: t.Number(), search: t.String() })), } ) + .get("/:id", async ({ params: { id } }) => getOne(id), { + params: t.Object({ id: t.String({ format: "uuid" }) }), + }) .get( "/count", async ({ auth: { companyId }, query }) => getCount({ companyId, ...query }), @@ -33,15 +37,11 @@ export const clientsController = new Elysia({ prefix: "/clients" }) email: t.Nullable(t.String()), }), }) - .put( - "/:clientId", - async ({ body, params: { clientId } }) => editClient(clientId, body), - { - params: t.Object({ clientId: t.String() }), - body: t.Object({ - name: t.String(), - phone: t.String(), - email: t.Nullable(t.String()), - }), - } - ); + .put("/:id", async ({ body, params: { id } }) => editClient(id, body), { + params: t.Object({ id: t.String({ format: "uuid" }) }), + body: t.Object({ + name: t.String(), + phone: t.String(), + email: t.Nullable(t.String()), + }), + }); diff --git a/src/controllers/filesController.ts b/src/controllers/filesController.ts index 222f35a..3b6ac6d 100644 --- a/src/controllers/filesController.ts +++ b/src/controllers/filesController.ts @@ -1,5 +1,5 @@ import Elysia, { status, t } from "elysia"; -import got from "got"; +import got, { HTTPError } from "got"; import db from "../db"; import { eq } from "drizzle-orm"; import { sessionsTable } from "../db/schema"; diff --git a/src/services/clients/getOne.ts b/src/services/clients/getOne.ts new file mode 100644 index 0000000..147b0ac --- /dev/null +++ b/src/services/clients/getOne.ts @@ -0,0 +1,40 @@ +import { eq } from "drizzle-orm"; +import db from "../../db"; +import { clientsTable } from "../../db/schema"; +import { status } from "elysia"; + +export async function getOne(id: string) { + try { + const client = await db.query.clientsTable.findFirst({ + where: eq(clientsTable.id, id), + with: { + clientsToManagers: { + with: { + manager: { columns: { password: false } }, + }, + }, + sessions: { + with: { + manager: { columns: { password: false } }, + app: true, + server: true, + client: true, + comments: { with: { manager: { columns: { password: false } } } }, + }, + }, + }, + }); + + if (!client) return status(404, "Client not found"); + + const { clientsToManagers, ...rest } = client; + + return { + ...rest, + managers: clientsToManagers.map(({ manager }) => manager), + }; + } catch (error) { + console.log((error as Error).message); + return status(500, "Internal Server Error"); + } +} diff --git a/src/services/sessions/updateSession.ts b/src/services/sessions/updateSession.ts index 4b71e49..ff292c3 100644 --- a/src/services/sessions/updateSession.ts +++ b/src/services/sessions/updateSession.ts @@ -8,7 +8,7 @@ async function updateSession( sessionStatus: "starting" | "started" | "restarting" | "ending" | "ended" ) { try { - const session = await db + const [session] = await db .update(sessionsTable) .set({ status: sessionStatus }) .where(eq(sessionsTable.id, sessionId))