57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
'use server';
|
|
import { authOptions } from '@/config/auth';
|
|
import { getCustomer, updateCustomerProfile } from '@repo/graphql/api';
|
|
import { type CustomerInput, type Enum_Customer_Role } from '@repo/graphql/types';
|
|
import { getServerSession } from 'next-auth/next';
|
|
import { revalidatePath } from 'next/cache';
|
|
|
|
export async function getProfile() {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (session) {
|
|
const { user } = session;
|
|
const customer = await getCustomer({ telegramId: user?.telegramId });
|
|
if (customer) {
|
|
return customer;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export async function updateProfile(input: CustomerInput) {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (session) {
|
|
const { user } = session;
|
|
const customer = await getCustomer({ telegramId: user?.telegramId });
|
|
if (customer) {
|
|
await updateCustomerProfile({
|
|
data: input,
|
|
documentId: customer.documentId,
|
|
});
|
|
}
|
|
}
|
|
|
|
revalidatePath('/profile');
|
|
}
|
|
|
|
export async function updateRole(role: Enum_Customer_Role) {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (session) {
|
|
const { user } = session;
|
|
const customer = await getCustomer({ telegramId: user?.telegramId });
|
|
if (customer) {
|
|
await updateCustomerProfile({
|
|
data: {
|
|
role,
|
|
},
|
|
documentId: customer.documentId,
|
|
});
|
|
}
|
|
}
|
|
|
|
revalidatePath('/profile');
|
|
}
|