55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import { Database } from '@/types_db';
|
|
import { createServerComponentClient } from '@supabase/auth-helpers-nextjs';
|
|
import { cookies } from 'next/headers';
|
|
import { cache } from 'react';
|
|
|
|
export const createServerSupabaseClient = cache(() =>
|
|
createServerComponentClient<Database>({ cookies })
|
|
);
|
|
|
|
export async function getUserDetails() {
|
|
const supabase = createServerSupabaseClient();
|
|
try {
|
|
const { data: userDetails } = await supabase
|
|
.from('users')
|
|
.select('*')
|
|
.single();
|
|
return userDetails;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getSubscription() {
|
|
const supabase = createServerSupabaseClient();
|
|
try {
|
|
const { data: subscription } = await supabase
|
|
.from('subscriptions')
|
|
.select('*, prices(*, products(*))')
|
|
.in('status', ['trialing', 'active'])
|
|
.maybeSingle()
|
|
.throwOnError();
|
|
return subscription;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const getActiveProductsWithPrices = async () => {
|
|
const supabase = createServerSupabaseClient();
|
|
const { data, error } = await supabase
|
|
.from('products')
|
|
.select('*, prices(*)')
|
|
.eq('active', true)
|
|
.eq('prices.active', true)
|
|
.order('metadata->index')
|
|
.order('unit_amount', { foreignTable: 'prices' });
|
|
|
|
if (error) {
|
|
console.log(error.message);
|
|
}
|
|
return data ?? [];
|
|
};
|