2025-02-08 21:27:17 +03:00

33 lines
1.0 KiB
TypeScript

'use server';
import { authOptions } from '@/config/auth';
import { getCustomer, updateCustomerProfile } from '@repo/graphql/api';
import { type CustomerInput, type GetCustomerQueryVariables } from '@repo/graphql/types';
import { getServerSession } from 'next-auth/next';
export async function getProfile(input?: GetCustomerQueryVariables) {
const session = await getServerSession(authOptions);
if (!session) throw new Error('Missing session');
const { user } = session;
const telegramId = input?.telegramId || user?.telegramId;
const customer = await getCustomer({ telegramId });
return customer;
}
export async function updateProfile(input: CustomerInput) {
const session = await getServerSession(authOptions);
if (!session) throw new Error('Missing session');
const { user } = session;
const customer = await getCustomer({ telegramId: user?.telegramId });
if (!customer) throw new Error('Customer not found');
await updateCustomerProfile({
data: input,
documentId: customer.documentId,
});
}