From 8a5c3da3854357e0bd74be5c7f89ab14a1599db7 Mon Sep 17 00:00:00 2001 From: Aidan Bleser Date: Mon, 16 Jun 2025 14:01:58 -0500 Subject: [PATCH] update rules --- src/lib/backend/convex/user_rules.ts | 47 +++++++++ src/lib/components/ui/label/index.ts | 2 +- src/lib/components/ui/textarea/index.ts | 2 +- src/routes/account/customization/+page.svelte | 17 ++-- src/routes/account/customization/rule.svelte | 96 +++++++++++++++++++ 5 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 src/routes/account/customization/rule.svelte diff --git a/src/lib/backend/convex/user_rules.ts b/src/lib/backend/convex/user_rules.ts index 32e73b1..f8d4ba7 100644 --- a/src/lib/backend/convex/user_rules.ts +++ b/src/lib/backend/convex/user_rules.ts @@ -34,6 +34,53 @@ export const create = mutation({ }, }); +export const update = mutation({ + args: { + ruleId: v.id('user_rules'), + attach: ruleAttachValidator, + rule: v.string(), + sessionToken: v.string(), + }, + handler: async (ctx, args) => { + const session = await ctx.runQuery(internal.betterAuth.getSession, { + sessionToken: args.sessionToken, + }); + + if (!session) throw new Error('Invalid session token'); + + const existing = await ctx.db.get(args.ruleId); + + if (!existing) throw new Error('Rule not found'); + if (existing.user_id !== session.userId) throw new Error('You are not the owner of this rule'); + + await ctx.db.patch(args.ruleId, { + attach: args.attach, + rule: args.rule, + }); + }, +}); + +export const remove = mutation({ + args: { + ruleId: v.id('user_rules'), + sessionToken: v.string(), + }, + handler: async (ctx, args) => { + const session = await ctx.runQuery(internal.betterAuth.getSession, { + sessionToken: args.sessionToken, + }); + + if (!session) throw new Error('Invalid session token'); + + const existing = await ctx.db.get(args.ruleId); + + if (!existing) throw new Error('Rule not found'); + if (existing.user_id !== session.userId) throw new Error('You are not the owner of this rule'); + + await ctx.db.delete(args.ruleId); + }, +}); + export const all = query({ args: { sessionToken: v.string(), diff --git a/src/lib/components/ui/label/index.ts b/src/lib/components/ui/label/index.ts index d16fed0..41dd50d 100644 --- a/src/lib/components/ui/label/index.ts +++ b/src/lib/components/ui/label/index.ts @@ -1,3 +1,3 @@ import Label from './label.svelte'; -export { Label }; \ No newline at end of file +export { Label }; diff --git a/src/lib/components/ui/textarea/index.ts b/src/lib/components/ui/textarea/index.ts index 209df6e..e161e1f 100644 --- a/src/lib/components/ui/textarea/index.ts +++ b/src/lib/components/ui/textarea/index.ts @@ -1,3 +1,3 @@ import Textarea from './textarea.svelte'; -export { Textarea }; \ No newline at end of file +export { Textarea }; diff --git a/src/routes/account/customization/+page.svelte b/src/routes/account/customization/+page.svelte index 5d581b9..96f4f24 100644 --- a/src/routes/account/customization/+page.svelte +++ b/src/routes/account/customization/+page.svelte @@ -12,6 +12,7 @@ import type { Doc } from '$lib/backend/convex/_generated/dataModel'; import { Input } from '$lib/components/ui/input'; import { api } from '$lib/backend/convex/_generated/api'; + import Rule from './rule.svelte'; const client = useConvexClient(); @@ -28,9 +29,9 @@ async function submitNewRule(e: SubmitEvent) { e.preventDefault(); const formData = new FormData(e.target as HTMLFormElement); - const name = formData.get('name'); - const attach = formData.get('attach'); - const rule = formData.get('rule'); + const name = formData.get('name') as string; + const attach = formData.get('attach') as 'always' | 'manual'; + const rule = formData.get('rule') as string; if (rule === '' || !rule) return; @@ -59,7 +60,7 @@
-

Rules

+

Rules

diff --git a/src/routes/account/customization/rule.svelte b/src/routes/account/customization/rule.svelte new file mode 100644 index 0000000..0e30d64 --- /dev/null +++ b/src/routes/account/customization/rule.svelte @@ -0,0 +1,96 @@ + + + + + {rule.name} + + +
+ + +
+
+ +