Skip to content

Commit

Permalink
more parsing rules stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Jan 15, 2025
1 parent 618c8c7 commit a29686e
Show file tree
Hide file tree
Showing 11 changed files with 766 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/frontend/app/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRouter as createTanStackRouter } from "@tanstack/react-router";
import { routeTree } from "./routeTree.gen";
import { QueryClient } from "@tanstack/react-query";
import { routerWithQueryClient } from "@tanstack/react-router-with-query";
import SuperJSON from "superjson";

export function createRouter() {
const queryClient = new QueryClient();
Expand Down
44 changes: 44 additions & 0 deletions packages/frontend/app/server/drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
uniqueIndex,
} from "drizzle-orm/pg-core";
import { v4 } from "uuid";
import { ulid } from "ulidx"

export const eventTypes = pgEnum("EventTypes", [
"Social",
Expand Down Expand Up @@ -69,6 +70,7 @@ export const sessionTable = pgTable("session", {
export const userTableRelations = relations(userTable, ({ many }) => ({
rsvps: many(rsvpTable),
scancodes: many(scancodeTable),
apiKeys: many(apiKeyTable),
}));

export const rsvpTable = pgTable(
Expand Down Expand Up @@ -210,3 +212,45 @@ export const scancodeTableRelations = relations(scancodeTable, ({ one }) => ({
references: [userTable.id],
}),
}));

// API Key
export const apiKeyTable = pgTable("ApiKey", {
id: text("id").primaryKey().notNull().$default(() => ulid()),
createdBy: text("createdBy").references(() => userTable.id),
createdAt: timestamp("createdAt", {
precision: 3,
mode: "date",
withTimezone: true,
}).notNull().defaultNow(),
updatedAt: timestamp("updatedAt", {
precision: 3,
mode: "date",
withTimezone: true,
}).notNull().defaultNow(),
});

export const apiKeyTableRelations = relations(apiKeyTable, ({ one }) => ({
createdBy: one(userTable, {
fields: [apiKeyTable.createdBy],
references: [userTable.id],
}),
}))

/** Parsing rules for event names */
export const eventParsingRuleTable = pgTable("EventParsingRule", {
id: text("id").primaryKey().notNull().$default(() => ulid()),
createdAt: timestamp("createdAt", {
precision: 3,
mode: "date",
withTimezone: true,
}).notNull().defaultNow(),
updatedAt: timestamp("updatedAt", {
precision: 3,
mode: "date",
withTimezone: true,
}).notNull().defaultNow(),
name: text("name").notNull(),
regex: text("regex").notNull().default(""),
rolesIds: text("roles").array().notNull().default([]),
channelId: text("channelId").notNull(),
});
6 changes: 6 additions & 0 deletions packages/frontend/app/server/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ export const authQueryKeys = {
auth: ["auth"] as const,
status: () => [...authQueryKeys.auth, "status"] as const,
};

export const adminQueryKeys = {
admin: ["admin"] as const,
apiKeys: ["admin", "apiKeys"] as const,
parsingRules: ["admin", "parsingRules"] as const,
};
4 changes: 4 additions & 0 deletions packages/frontend/app/server/schema/EventParsingRuleSchema.ts
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 packages/frontend/app/server/schema/NewEventParsingRuleSchema.ts
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()),
})
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(),
})
3 changes: 3 additions & 0 deletions packages/frontend/app/server/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ export * from "./PagedEventsSchema";
export * from "./PagedSchema";
export * from "./UserListParamsSchema";
export * from "./OutreachTimeSchema";
export * from "./NewEventParsingRuleSchema"
export * from "./EventParsingRuleSchema"
export * from "./UpdateEventParsingRuleSchema"
110 changes: 110 additions & 0 deletions packages/frontend/app/server/services/adminService.ts
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;
}
18 changes: 18 additions & 0 deletions packages/frontend/drizzle/0016_legal_blade.sql
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;
Loading

0 comments on commit a29686e

Please sign in to comment.