Skip to content

Commit 1853dbe

Browse files
committed
feat: added basic crud operations
1 parent 9fc8bd8 commit 1853dbe

8 files changed

Lines changed: 2081 additions & 36 deletions

File tree

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
import { eq, and, desc, isNull, like, or, count, sql } from "drizzle-orm";
2+
import { aiAgent, type Database, type AiAgentInsert } from "@repo/database";
3+
4+
// Create AI agent
5+
export async function createAiAgent(
6+
db: Database,
7+
params: {
8+
orgId: string;
9+
data: Omit<AiAgentInsert, "organizationId">;
10+
}
11+
) {
12+
const [newAiAgent] = await db
13+
.insert(aiAgent)
14+
.values({
15+
...params.data,
16+
organizationId: params.orgId,
17+
})
18+
.returning();
19+
20+
return newAiAgent;
21+
}
22+
23+
// Get AI agent by ID (with org check)
24+
export async function getAiAgentById(
25+
db: Database,
26+
params: {
27+
orgId: string;
28+
aiAgentId: string;
29+
}
30+
) {
31+
const [agent] = await db
32+
.select()
33+
.from(aiAgent)
34+
.where(
35+
and(
36+
eq(aiAgent.id, params.aiAgentId),
37+
eq(aiAgent.organizationId, params.orgId),
38+
isNull(aiAgent.deletedAt)
39+
)
40+
)
41+
.limit(1);
42+
43+
return agent;
44+
}
45+
46+
// Get AI agents for organization
47+
export async function getAiAgentsByOrganization(
48+
db: Database,
49+
params: {
50+
orgId: string;
51+
websiteId?: string;
52+
isActive?: boolean;
53+
limit?: number;
54+
offset?: number;
55+
}
56+
) {
57+
const agents = await db
58+
.select()
59+
.from(aiAgent)
60+
.where(
61+
and(
62+
eq(aiAgent.organizationId, params.orgId),
63+
params.websiteId ? eq(aiAgent.websiteId, params.websiteId) : undefined,
64+
params.isActive !== undefined
65+
? eq(aiAgent.isActive, params.isActive)
66+
: undefined,
67+
isNull(aiAgent.deletedAt)
68+
)
69+
)
70+
.orderBy(desc(aiAgent.lastUsedAt))
71+
.limit(params.limit ?? 50)
72+
.offset(params.offset ?? 0);
73+
74+
return agents;
75+
}
76+
77+
// Get active AI agents for website
78+
export async function getActiveAiAgentsForWebsite(
79+
db: Database,
80+
params: {
81+
orgId: string;
82+
websiteId: string;
83+
}
84+
) {
85+
const agents = await db
86+
.select()
87+
.from(aiAgent)
88+
.where(
89+
and(
90+
eq(aiAgent.organizationId, params.orgId),
91+
eq(aiAgent.websiteId, params.websiteId),
92+
eq(aiAgent.isActive, true),
93+
isNull(aiAgent.deletedAt)
94+
)
95+
)
96+
.orderBy(desc(aiAgent.lastUsedAt));
97+
98+
return agents;
99+
}
100+
101+
// Search AI agents by name or description
102+
export async function searchAiAgents(
103+
db: Database,
104+
params: {
105+
orgId: string;
106+
query: string;
107+
websiteId?: string;
108+
limit?: number;
109+
offset?: number;
110+
}
111+
) {
112+
const agents = await db
113+
.select()
114+
.from(aiAgent)
115+
.where(
116+
and(
117+
eq(aiAgent.organizationId, params.orgId),
118+
or(
119+
like(aiAgent.name, `%${params.query}%`),
120+
like(aiAgent.description, `%${params.query}%`)
121+
),
122+
params.websiteId ? eq(aiAgent.websiteId, params.websiteId) : undefined,
123+
isNull(aiAgent.deletedAt)
124+
)
125+
)
126+
.orderBy(desc(aiAgent.lastUsedAt))
127+
.limit(params.limit ?? 20)
128+
.offset(params.offset ?? 0);
129+
130+
return agents;
131+
}
132+
133+
// Get AI agents by model
134+
export async function getAiAgentsByModel(
135+
db: Database,
136+
params: {
137+
orgId: string;
138+
model: string;
139+
isActive?: boolean;
140+
limit?: number;
141+
offset?: number;
142+
}
143+
) {
144+
const agents = await db
145+
.select()
146+
.from(aiAgent)
147+
.where(
148+
and(
149+
eq(aiAgent.organizationId, params.orgId),
150+
eq(aiAgent.model, params.model),
151+
params.isActive !== undefined
152+
? eq(aiAgent.isActive, params.isActive)
153+
: undefined,
154+
isNull(aiAgent.deletedAt)
155+
)
156+
)
157+
.orderBy(desc(aiAgent.usageCount))
158+
.limit(params.limit ?? 50)
159+
.offset(params.offset ?? 0);
160+
161+
return agents;
162+
}
163+
164+
// Update AI agent
165+
export async function updateAiAgent(
166+
db: Database,
167+
params: {
168+
orgId: string;
169+
aiAgentId: string;
170+
data: Partial<Omit<AiAgentInsert, "organizationId">>;
171+
}
172+
) {
173+
const [updatedAgent] = await db
174+
.update(aiAgent)
175+
.set({
176+
...params.data,
177+
updatedAt: new Date(),
178+
})
179+
.where(
180+
and(
181+
eq(aiAgent.id, params.aiAgentId),
182+
eq(aiAgent.organizationId, params.orgId)
183+
)
184+
)
185+
.returning();
186+
187+
return updatedAgent;
188+
}
189+
190+
// Update AI agent usage
191+
export async function updateAiAgentUsage(
192+
db: Database,
193+
params: {
194+
orgId: string;
195+
aiAgentId: string;
196+
}
197+
) {
198+
const [updatedAgent] = await db
199+
.update(aiAgent)
200+
.set({
201+
lastUsedAt: new Date(),
202+
usageCount: sql`${aiAgent.usageCount} + 1`,
203+
updatedAt: new Date(),
204+
})
205+
.where(
206+
and(
207+
eq(aiAgent.id, params.aiAgentId),
208+
eq(aiAgent.organizationId, params.orgId)
209+
)
210+
)
211+
.returning();
212+
213+
return updatedAgent;
214+
}
215+
216+
// Activate/Deactivate AI agent
217+
export async function toggleAiAgentStatus(
218+
db: Database,
219+
params: {
220+
orgId: string;
221+
aiAgentId: string;
222+
isActive: boolean;
223+
}
224+
) {
225+
const [updatedAgent] = await db
226+
.update(aiAgent)
227+
.set({
228+
isActive: params.isActive,
229+
updatedAt: new Date(),
230+
})
231+
.where(
232+
and(
233+
eq(aiAgent.id, params.aiAgentId),
234+
eq(aiAgent.organizationId, params.orgId)
235+
)
236+
)
237+
.returning();
238+
239+
return updatedAgent;
240+
}
241+
242+
// Soft delete AI agent
243+
export async function deleteAiAgent(
244+
db: Database,
245+
params: {
246+
orgId: string;
247+
aiAgentId: string;
248+
}
249+
) {
250+
const [deletedAgent] = await db
251+
.update(aiAgent)
252+
.set({
253+
deletedAt: new Date(),
254+
updatedAt: new Date(),
255+
})
256+
.where(
257+
and(
258+
eq(aiAgent.id, params.aiAgentId),
259+
eq(aiAgent.organizationId, params.orgId)
260+
)
261+
)
262+
.returning();
263+
264+
return deletedAgent;
265+
}
266+
267+
// Restore AI agent
268+
export async function restoreAiAgent(
269+
db: Database,
270+
params: {
271+
orgId: string;
272+
aiAgentId: string;
273+
}
274+
) {
275+
const [restoredAgent] = await db
276+
.update(aiAgent)
277+
.set({
278+
deletedAt: null,
279+
updatedAt: new Date(),
280+
})
281+
.where(
282+
and(
283+
eq(aiAgent.id, params.aiAgentId),
284+
eq(aiAgent.organizationId, params.orgId)
285+
)
286+
)
287+
.returning();
288+
289+
return restoredAgent;
290+
}
291+
292+
// Get AI agent statistics
293+
export async function getAiAgentStats(
294+
db: Database,
295+
params: {
296+
orgId: string;
297+
websiteId?: string;
298+
}
299+
) {
300+
const stats = await db
301+
.select({
302+
total: count(),
303+
active: count(eq(aiAgent.isActive, true)),
304+
inactive: count(eq(aiAgent.isActive, false)),
305+
})
306+
.from(aiAgent)
307+
.where(
308+
and(
309+
eq(aiAgent.organizationId, params.orgId),
310+
params.websiteId ? eq(aiAgent.websiteId, params.websiteId) : undefined,
311+
isNull(aiAgent.deletedAt)
312+
)
313+
);
314+
315+
return stats[0];
316+
}
317+
318+
// Get most used AI agents
319+
export async function getMostUsedAiAgents(
320+
db: Database,
321+
params: {
322+
orgId: string;
323+
websiteId?: string;
324+
limit?: number;
325+
}
326+
) {
327+
const agents = await db
328+
.select()
329+
.from(aiAgent)
330+
.where(
331+
and(
332+
eq(aiAgent.organizationId, params.orgId),
333+
params.websiteId ? eq(aiAgent.websiteId, params.websiteId) : undefined,
334+
eq(aiAgent.isActive, true),
335+
isNull(aiAgent.deletedAt)
336+
)
337+
)
338+
.orderBy(desc(aiAgent.usageCount))
339+
.limit(params.limit ?? 10);
340+
341+
return agents;
342+
}

0 commit comments

Comments
 (0)