This commit is contained in:
2025-06-18 18:30:22 +05:00
parent 46dc5fa25f
commit 00f4e56b33
8 changed files with 126 additions and 14 deletions
-2
View File
@@ -47,8 +47,6 @@ export default async function createApp(
})
.returning();
console.log(appsOfServer);
return appsOfServer;
} catch (error) {
console.log((error as Error).message);
+4 -11
View File
@@ -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;
+18
View File
@@ -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;
+44
View File
@@ -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;
+37
View File
@@ -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;
+1
View File
@@ -16,6 +16,7 @@ async function updateSession(
return session;
} catch (error) {
console.log((error as Error).message);
return status(500, "Internal Server Error");
}
}