upd
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -47,8 +47,6 @@ export default async function createApp(
|
||||
})
|
||||
.returning();
|
||||
|
||||
console.log(appsOfServer);
|
||||
|
||||
return appsOfServer;
|
||||
} catch (error) {
|
||||
console.log((error as Error).message);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -16,6 +16,7 @@ async function updateSession(
|
||||
|
||||
return session;
|
||||
} catch (error) {
|
||||
console.log((error as Error).message);
|
||||
return status(500, "Internal Server Error");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user