-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
618c8c7
commit a29686e
Showing
11 changed files
with
766 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/frontend/app/server/schema/EventParsingRuleSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createSelectSchema } from "drizzle-zod"; | ||
import { eventParsingRuleTable } from "../drizzle/schema"; | ||
|
||
export const EventParsingRule = createSelectSchema(eventParsingRuleTable) |
15 changes: 15 additions & 0 deletions
15
packages/frontend/app/server/schema/NewEventParsingRuleSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { z } from "zod"; | ||
|
||
export const NewEventParsingRuleSchema = z.object({ | ||
channelId: z.string(), | ||
name: z.string(), | ||
regex: z.string().refine((v) => { | ||
try { | ||
new RegExp(v); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}), | ||
rolesIds: z.array(z.string()), | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/frontend/app/server/schema/UpdateEventParsingRuleSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from "zod"; | ||
import { NewEventParsingRuleSchema } from "./NewEventParsingRuleSchema"; | ||
|
||
export const UpdateEventParsingRuleSchema = z.object({ | ||
channelId: z.string().optional(), | ||
name: z.string().optional(), | ||
regex: z.string().refine((v) => { | ||
try { | ||
new RegExp(v); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}).optional(), | ||
rolesIds: z.array(z.string()).optional(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { eq } from "drizzle-orm"; | ||
import db from "../drizzle/db"; | ||
import { apiKeyTable, eventParsingRuleTable } from "../drizzle/schema"; | ||
import { z } from "zod"; | ||
import { NewEventParsingRuleSchema, UpdateEventParsingRuleSchema } from "../schema"; | ||
import { TRPCError } from "@trpc/server"; | ||
|
||
/** | ||
* Get all API keys | ||
* @returns A list of all API keys | ||
*/ | ||
export async function getApiKeys() { | ||
const apiKeys = await db.query.apiKeyTable.findMany(); | ||
|
||
return apiKeys; | ||
} | ||
|
||
/** | ||
* Delete an API key | ||
* @param id The ID of the API key to delete | ||
* @returns The deleted API key | ||
*/ | ||
export async function deleteApiKey(id: string) { | ||
const [deleted] = await db.delete(apiKeyTable).where(eq(apiKeyTable.id, id)).returning() | ||
|
||
return deleted; | ||
} | ||
|
||
/** | ||
* Create a new API key | ||
* @param userId The ID of the user creating the API key | ||
* @returns The created API key | ||
*/ | ||
export async function createApiKey(userId: string) { | ||
const [apiKey] = await db.insert(apiKeyTable).values({ | ||
createdBy: userId, | ||
}).returning(); | ||
|
||
return apiKey; | ||
} | ||
|
||
/** | ||
* Create a new parsing rule | ||
* @param data The data to create a new parsing rule | ||
* @returns The created parsing rule | ||
*/ | ||
export async function createParsingRule (data: z.infer<typeof NewEventParsingRuleSchema>) { | ||
const { channelId, name, regex, rolesIds } = data; | ||
// Create a new parsing rule | ||
const [parsingRule] = await db.insert(eventParsingRuleTable).values({ | ||
channelId, | ||
name, | ||
regex, | ||
rolesIds, | ||
}).returning(); | ||
|
||
if (!parsingRule) { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR" | ||
}) | ||
} | ||
|
||
return parsingRule; | ||
} | ||
|
||
/** | ||
* Get all parsing rules | ||
* @returns A list of all parsing rules | ||
*/ | ||
export async function getParsingRules () { | ||
const parsingRules = await db.query.eventParsingRuleTable.findMany(); | ||
|
||
return parsingRules; | ||
} | ||
|
||
/** | ||
* Delete a parsing rule | ||
* @param id The ID of the parsing rule to delete | ||
* @returns The deleted parsing rule | ||
*/ | ||
export async function deleteParsingRule (id: string) { | ||
const [deleted] = await db.delete(eventParsingRuleTable).where(eq(eventParsingRuleTable.id, id)).returning() | ||
|
||
return deleted; | ||
} | ||
|
||
/** | ||
* Update a parsing rule | ||
* @param id The ID of the parsing rule to update | ||
* @param data The data to update the parsing rule with | ||
* @returns The updated parsing rule | ||
*/ | ||
export async function updateParsingRule (id: string, data: z.infer<typeof UpdateEventParsingRuleSchema>) { | ||
const { channelId, name, regex, rolesIds } = data; | ||
const [updated] = await db.update(eventParsingRuleTable).set({ | ||
channelId, | ||
name, | ||
regex, | ||
rolesIds, | ||
}).where(eq(eventParsingRuleTable.id, id)).returning(); | ||
|
||
if (!updated) { | ||
throw new TRPCError({ | ||
code: "NOT_FOUND", | ||
message: "Parsing rule not found" | ||
}) | ||
} | ||
|
||
return updated; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CREATE TABLE "ApiKey" ( | ||
"id" text PRIMARY KEY NOT NULL, | ||
"createdBy" text, | ||
"createdAt" timestamp (3) with time zone DEFAULT now() NOT NULL, | ||
"updatedAt" timestamp (3) with time zone DEFAULT now() NOT NULL | ||
); | ||
--> statement-breakpoint | ||
CREATE TABLE "EventParsingRule" ( | ||
"id" text PRIMARY KEY NOT NULL, | ||
"createdAt" timestamp (3) with time zone DEFAULT now() NOT NULL, | ||
"updatedAt" timestamp (3) with time zone DEFAULT now() NOT NULL, | ||
"name" text NOT NULL, | ||
"regex" text DEFAULT '' NOT NULL, | ||
"roles" text[] DEFAULT '{}' NOT NULL, | ||
"channelId" text NOT NULL | ||
); | ||
--> statement-breakpoint | ||
ALTER TABLE "ApiKey" ADD CONSTRAINT "ApiKey_createdBy_User_id_fk" FOREIGN KEY ("createdBy") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action; |
Oops, something went wrong.