From 00f4e56b33b69e96456bbeb6799bd431f9c24406 Mon Sep 17 00:00:00 2001 From: Lanskikh Date: Wed, 18 Jun 2025 18:30:22 +0500 Subject: [PATCH] upd --- src/controllers/clientsController.ts | 21 ++++++++++++ src/db/schema/clients.ts | 2 +- src/services/apps/create.ts | 2 -- src/services/clients/createClient.ts | 15 +++------ src/services/clients/getByPhone.ts | 18 +++++++++++ src/services/clients/getClients.ts | 44 ++++++++++++++++++++++++++ src/services/clients/getCount.ts | 37 ++++++++++++++++++++++ src/services/sessions/updateSession.ts | 1 + 8 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 src/services/clients/getByPhone.ts create mode 100644 src/services/clients/getClients.ts create mode 100644 src/services/clients/getCount.ts diff --git a/src/controllers/clientsController.ts b/src/controllers/clientsController.ts index 0543f38..9fc1644 100644 --- a/src/controllers/clientsController.ts +++ b/src/controllers/clientsController.ts @@ -1,9 +1,30 @@ import Elysia, { t } from "elysia"; import authMiddleware from "../middlewares/auth"; import createClient from "../services/clients/createClient"; +import getByPhone from "../services/clients/getByPhone"; +import getClients from "../services/clients/getClients"; +import getCount from "../services/clients/getCount"; export const clientsController = new Elysia({ prefix: "/clients" }) .use(authMiddleware) + .get("/by-phone", async ({ query: { phone } }) => getByPhone(phone), { + query: t.Object({ phone: t.String() }), + }) + .get( + "/", + async ({ query, auth: { companyId } }) => + getClients({ companyId, ...query }), + { + query: t.Partial(t.Object({ limit: t.Number(), search: t.String() })), + } + ) + .get( + "/count", + async ({ auth: { companyId }, query }) => getCount({ companyId, ...query }), + { + query: t.Partial(t.Object({ search: t.String() })), + } + ) .post("/", async ({ auth, body }) => createClient(auth, body), { body: t.Object({ name: t.String(), diff --git a/src/db/schema/clients.ts b/src/db/schema/clients.ts index 59a82da..75525ae 100644 --- a/src/db/schema/clients.ts +++ b/src/db/schema/clients.ts @@ -7,7 +7,7 @@ import { usersTable } from "./users"; export const clientsTable = pgTable("clients", { id: uuid("id").defaultRandom().primaryKey(), name: varchar("name").notNull(), - phone: varchar("phone", { length: 11 }).notNull(), + phone: varchar("phone", { length: 11 }).unique().notNull(), email: varchar("email"), ownerId: uuid("owner_id") .notNull() diff --git a/src/services/apps/create.ts b/src/services/apps/create.ts index ba9a557..9f52db7 100644 --- a/src/services/apps/create.ts +++ b/src/services/apps/create.ts @@ -47,8 +47,6 @@ export default async function createApp( }) .returning(); - console.log(appsOfServer); - return appsOfServer; } catch (error) { console.log((error as Error).message); diff --git a/src/services/clients/createClient.ts b/src/services/clients/createClient.ts index a1541f9..bd3d11f 100644 --- a/src/services/clients/createClient.ts +++ b/src/services/clients/createClient.ts @@ -1,4 +1,3 @@ -import { eq } from "drizzle-orm"; import db from "../../db"; import { clientsTable } from "../../db/schema"; import { status } from "elysia"; @@ -15,16 +14,6 @@ async function createClient( } ) { try { - // Check for existing client - const [existingClient] = await db - .select() - .from(clientsTable) - .where(eq(clientsTable.name, body.name)); - - if (existingClient) { - return existingClient; - } - // Create new client const [newClient] = await db .insert(clientsTable) @@ -33,6 +22,10 @@ async function createClient( ownerId: auth.userId, companyId: auth.companyId, }) + .onConflictDoUpdate({ + target: [clientsTable.phone], + set: { ...body, ownerId: auth.userId, companyId: auth.companyId }, + }) .returning(); return newClient; diff --git a/src/services/clients/getByPhone.ts b/src/services/clients/getByPhone.ts new file mode 100644 index 0000000..f4cedd3 --- /dev/null +++ b/src/services/clients/getByPhone.ts @@ -0,0 +1,18 @@ +import { eq } from "drizzle-orm"; +import db from "../../db"; +import { clientsTable } from "../../db/schema"; +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"); + } catch (error) { + console.log((error as Error).message); + return status(500, "Internal Server Error"); + } +} + +export default getByPhone; diff --git a/src/services/clients/getClients.ts b/src/services/clients/getClients.ts new file mode 100644 index 0000000..ecad15e --- /dev/null +++ b/src/services/clients/getClients.ts @@ -0,0 +1,44 @@ +import { and, eq, ilike, or } from "drizzle-orm"; +import db from "../../db"; +import { clientsTable } from "../../db/schema"; +import { status } from "elysia"; + +async function getClients({ + companyId, + limit, + search, +}: { + limit?: number; + search?: string; + companyId: string; +}) { + try { + return await db.query.clientsTable.findMany({ + where: and( + eq(clientsTable.companyId, companyId), + search + ? or( + ilike(clientsTable.name, search), + ilike(clientsTable.email, search), + ilike(clientsTable.phone, search) + ) + : undefined + ), + limit, + with: { + sessions: { + with: { + owner: { columns: { password: false } }, + app: true, + comments: { with: { owner: { columns: { password: false } } } }, + }, + }, + }, + }); + } catch (error) { + console.log((error as Error).message); + return status(500, "Internal Server Error"); + } +} + +export default getClients; diff --git a/src/services/clients/getCount.ts b/src/services/clients/getCount.ts new file mode 100644 index 0000000..ae87ebb --- /dev/null +++ b/src/services/clients/getCount.ts @@ -0,0 +1,37 @@ +import { and, count, eq, ilike, or } from "drizzle-orm"; +import db from "../../db"; +import { clientsTable } from "../../db/schema"; +import { status } from "elysia"; + +async function getCount({ + search, + companyId, +}: { + search?: string; + companyId: string; +}) { + try { + return ( + await db + .select({ count: count() }) + .from(clientsTable) + .where( + and( + eq(clientsTable.companyId, companyId), + search + ? or( + ilike(clientsTable.name, search), + ilike(clientsTable.email, search), + ilike(clientsTable.phone, search) + ) + : undefined + ) + ) + )[0].count; + } catch (error) { + console.log((error as Error).message); + return status(500, "Internal Server Error"); + } +} + +export default getCount; diff --git a/src/services/sessions/updateSession.ts b/src/services/sessions/updateSession.ts index d392691..4b71e49 100644 --- a/src/services/sessions/updateSession.ts +++ b/src/services/sessions/updateSession.ts @@ -16,6 +16,7 @@ async function updateSession( return session; } catch (error) { + console.log((error as Error).message); return status(500, "Internal Server Error"); } }