kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { and, eq } from "drizzle-orm";
2import { HTTPException } from "hono/http-exception";
3import db from "../../database";
4import { columnTable, workflowRuleTable } from "../../database/schema";
5
6async function upsertWorkflowRule({
7 projectId,
8 integrationType,
9 eventType,
10 columnId,
11}: {
12 projectId: string;
13 integrationType: string;
14 eventType: string;
15 columnId: string;
16}) {
17 const targetColumn = await db.query.columnTable.findFirst({
18 where: and(
19 eq(columnTable.id, columnId),
20 eq(columnTable.projectId, projectId),
21 ),
22 });
23
24 if (!targetColumn) {
25 throw new HTTPException(400, {
26 message: "Column does not belong to the provided project",
27 });
28 }
29
30 const existing = await db.query.workflowRuleTable.findFirst({
31 where: and(
32 eq(workflowRuleTable.projectId, projectId),
33 eq(workflowRuleTable.integrationType, integrationType),
34 eq(workflowRuleTable.eventType, eventType),
35 ),
36 });
37
38 if (existing) {
39 const [updated] = await db
40 .update(workflowRuleTable)
41 .set({ columnId })
42 .where(eq(workflowRuleTable.id, existing.id))
43 .returning();
44
45 if (!updated) {
46 throw new HTTPException(500, {
47 message: "Failed to update workflow rule",
48 });
49 }
50
51 return updated;
52 }
53
54 const [created] = await db
55 .insert(workflowRuleTable)
56 .values({
57 projectId,
58 integrationType,
59 eventType,
60 columnId,
61 })
62 .returning();
63
64 if (!created) {
65 throw new HTTPException(500, {
66 message: "Failed to create workflow rule",
67 });
68 }
69
70 return created;
71}
72
73export default upsertWorkflowRule;