From b15028c385409b791f1dec23fbd1b801aea7d37d Mon Sep 17 00:00:00 2001 From: EmaDev Date: Sat, 29 Mar 2025 18:39:54 +0100 Subject: [PATCH] stop free daily generations --- src/lib/config.ts | 4 +++ src/routes/+page.server.ts | 71 ++++++++++++++++++++------------------ src/routes/+page.svelte | 4 +-- src/routes/sidebar.svelte | 12 +++++-- 4 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/lib/config.ts b/src/lib/config.ts index e84b0e0..da4c648 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -25,3 +25,7 @@ const config: ConfigType = { export type Config = typeof config; export default config; + +export const FEATURES = { + DAILY_FREE_CREDITS: false +} as const; diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 68ba7d7..c02319d 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -3,6 +3,7 @@ import { PUBLIC_SUPABASE_URL } from '$env/static/public'; import { PRIVATE_SUPABASE_SERVICE_ROLE } from '$env/static/private'; import type { Database } from '$lib/supabase-types'; import { isBefore, startOfDay, parseISO, formatISO } from 'date-fns'; +import { FEATURES } from '$lib/config'; const supabaseAdmin = createClient(PUBLIC_SUPABASE_URL, PRIVATE_SUPABASE_SERVICE_ROLE); @@ -17,40 +18,42 @@ export async function load({ locals: { session }, depends }) { }) ]); - // If no payment record exists or if remaining generations is 0 and last update was before today, + // If daily free credits are enabled and no payment record exists or if remaining generations is 0 and last update was before today, // create/update record with 1 free generation - if ( - !paymentData || - (paymentData.remaining_generations === 0 && - (!paymentData.updated_at || - isBefore(parseISO(paymentData.updated_at), startOfDay(new Date())))) - ) { - if (!paymentData) { - // Insert new record - const { data: newPaymentData } = await supabaseAdmin - .from('user_payments') - .insert({ - user_id: session.user.id, - remaining_generations: 1, - updated_at: formatISO(new Date()) - }) - .select() - .single(); + if (FEATURES.DAILY_FREE_CREDITS) { + if ( + !paymentData || + (paymentData.remaining_generations === 0 && + (!paymentData.updated_at || + isBefore(parseISO(paymentData.updated_at), startOfDay(new Date())))) + ) { + if (!paymentData) { + // Insert new record + const { data: newPaymentData } = await supabaseAdmin + .from('user_payments') + .insert({ + user_id: session.user.id, + remaining_generations: 1, + updated_at: formatISO(new Date()) + }) + .select() + .single(); - paymentData = newPaymentData; - } else { - // Update existing record - const { data: updatedPaymentData } = await supabaseAdmin - .from('user_payments') - .update({ - remaining_generations: 1, - updated_at: formatISO(new Date()) - }) - .eq('user_id', session.user.id) - .select() - .single(); + paymentData = newPaymentData; + } else { + // Update existing record + const { data: updatedPaymentData } = await supabaseAdmin + .from('user_payments') + .update({ + remaining_generations: 1, + updated_at: formatISO(new Date()) + }) + .eq('user_id', session.user.id) + .select() + .single(); - paymentData = updatedPaymentData; + paymentData = updatedPaymentData; + } } } @@ -73,11 +76,13 @@ export async function load({ locals: { session }, depends }) { return { remainingGenerations: paymentData?.remaining_generations || 0, - images + images, + features: FEATURES }; } return { remainingGenerations: 0, - images: [] + images: [], + features: FEATURES }; } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 9b05f67..2338307 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -5,7 +5,7 @@ let { data } = $props(); - const { images, remainingGenerations } = $derived(data); + const { images, remainingGenerations, features } = $derived(data); - + diff --git a/src/routes/sidebar.svelte b/src/routes/sidebar.svelte index 5988d7e..e410f85 100644 --- a/src/routes/sidebar.svelte +++ b/src/routes/sidebar.svelte @@ -37,10 +37,12 @@ let { user, - remainingGenerations + remainingGenerations, + features }: { user: User | null; remainingGenerations: number; + features: { DAILY_FREE_CREDITS: boolean }; } = $props(); let dropZone: DropZone | undefined = $state(); @@ -463,7 +465,13 @@ >
{#if !user} -

Login to get daily free credits or buy more

+

+ {#if features.DAILY_FREE_CREDITS} + Login to get daily free credits or buy more + {:else} + Login to generate avatars or buy credits + {/if} +

{/if}