+ Credits Management for User: {user?.id}
+ Credits Remaining: {$credits}
+ {#if errorMessage}
+ {errorMessage}
+ {/if}
+
+
+
+
+
+
diff --git a/src/routes/test/test-credits/+server.ts b/src/routes/test/test-credits/+server.ts
new file mode 100644
index 0000000..5949f67
--- /dev/null
+++ b/src/routes/test/test-credits/+server.ts
@@ -0,0 +1,29 @@
+import { json } from '@sveltejs/kit';
+import { addCredits, deductCredits, getUserCredits } from '$lib/utils/supabase/admin';
+
+export const POST = async ({ request }) => {
+ const { action, userId, credits, description } = await request.json();
+
+ if (!userId) {
+ return json({ success: false, error: 'User ID is required' }, { status: 400 });
+ }
+
+ try {
+ if (action === 'addCredits') {
+ await addCredits(userId, credits, description);
+ const updatedCredits = await getUserCredits(userId);
+ return json({ success: true, credits: updatedCredits });
+ } else if (action === 'deductCredits') {
+ await deductCredits(userId, credits, description);
+ const updatedCredits = await getUserCredits(userId);
+ return json({ success: true, credits: updatedCredits });
+ } else if (action === 'getUserCredits') {
+ const fetchedCredits = await getUserCredits(userId);
+ return json({ success: true, credits: fetchedCredits });
+ } else {
+ return json({ success: false, error: 'Invalid action' }, { status: 400 });
+ }
+ } catch (error) {
+ return json({ success: false, error: error.message }, { status: 500 });
+ }
+};
diff --git a/supabase/migrations/20240929120431_add_credits.sql b/supabase/migrations/20240929120431_add_credits.sql
new file mode 100644
index 0000000..44680d4
--- /dev/null
+++ b/supabase/migrations/20240929120431_add_credits.sql
@@ -0,0 +1,35 @@
+create table user_credits (
+ user_id uuid references auth.users not null primary key,
+ credits_remaining integer not null default 0,
+ last_updated timestamp with time zone not null default now()
+);
+
+alter table user_credits enable row level security;
+
+create policy "Can view own credits." on user_credits
+ for select using (auth.uid() = user_id);
+
+-- Uncomment and modify the update policy if allowing users to modify their own credits
+-- create policy "Can update own credits." on user_credits
+-- for update using (auth.uid() = user_id);
+
+create table credit_transactions (
+ id uuid default uuid_generate_v4() primary key,
+ user_id uuid references auth.users not null,
+ credits_change integer not null,
+ description text,
+ created_at timestamp with time zone not null default now()
+);
+
+alter table credit_transactions enable row level security;
+
+create policy "Can view own transactions." on credit_transactions
+ for select using (auth.uid() = user_id);
+
+-- Adding indexing to improve query performance
+create index on user_credits (user_id);
+create index on credit_transactions (user_id);
+
+-- Adding a check constraint to enforce data integrity
+alter table credit_transactions
+add constraint check_credits_change_nonzero check (credits_change <> 0);
From 5d83c79b9737d1817251625a413d1fad79c3eb8b Mon Sep 17 00:00:00 2001
From: Josh Creek <8179928+jcreek@users.noreply.github.com>
Date: Tue, 8 Oct 2024 19:41:08 +0100
Subject: [PATCH 2/2] docs(#37): Add testing notes
---
README.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/README.md b/README.md
index 62a8574..66416fb 100644
--- a/README.md
+++ b/README.md
@@ -55,3 +55,7 @@ Please see the Issues tab for `enhancement` tagged issues.
## [Database setup](/docs/database-setup.md)
## [Stripe webhook event listening](/docs/stripe-setup.md)
+
+## Testing the example credits management system
+
+There is an example system in this project for being able to manage credits that are sold to users. To test this system you can visit the `http://localhost:5173/test/test-credits` route. This will allow you to add credits to a user and then spend them by clicking the buttons on the page, and will show the user's credit balance updating in real time.