From d00f8475f243682cfe6fbf2697f443132c39a315 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 15 Jan 2025 17:23:17 +0300 Subject: [PATCH] beautify actions --- apps/web/actions/contacts.ts | 29 ++++++++++++----------------- apps/web/actions/profile.ts | 33 ++++++++++++++------------------- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/apps/web/actions/contacts.ts b/apps/web/actions/contacts.ts index ef6677e..395781c 100644 --- a/apps/web/actions/contacts.ts +++ b/apps/web/actions/contacts.ts @@ -5,30 +5,25 @@ import { getServerSession } from 'next-auth/next'; export async function getClients() { const session = await getServerSession(authOptions); + if (!session) throw new Error('Missing session'); - if (session) { - const { user } = session; - const getCustomerClientsResponse = await getCustomerClients({ telegramId: user?.telegramId }); + const { user } = session; - return { - clients: getCustomerClientsResponse?.clients, - }; - } + const getCustomerClientsResponse = await getCustomerClients({ telegramId: user?.telegramId }); - return null; + return { + clients: getCustomerClientsResponse?.clients, + }; } export async function getMasters() { const session = await getServerSession(authOptions); + if (!session) throw new Error('Missing session'); - if (session) { - const { user } = session; - const getCustomerMastersResponse = await getCustomerMasters({ telegramId: user?.telegramId }); + const { user } = session; + const getCustomerMastersResponse = await getCustomerMasters({ telegramId: user?.telegramId }); - return { - masters: getCustomerMastersResponse?.masters, - }; - } - - return null; + return { + masters: getCustomerMastersResponse?.masters, + }; } diff --git a/apps/web/actions/profile.ts b/apps/web/actions/profile.ts index 49b3ba6..ffb96ef 100644 --- a/apps/web/actions/profile.ts +++ b/apps/web/actions/profile.ts @@ -7,31 +7,26 @@ import { revalidatePath } from 'next/cache'; export async function getProfile() { const session = await getServerSession(authOptions); + if (!session) throw new Error('Missing session'); - if (session) { - const { user } = session; - const customer = await getCustomer({ telegramId: user?.telegramId }); - if (customer) { - return customer; - } - } - - return null; + const { user } = session; + const customer = await getCustomer({ telegramId: user?.telegramId }); + return customer; } export async function updateProfile(input: CustomerInput) { const session = await getServerSession(authOptions); + if (!session) throw new Error('Missing session'); - if (session) { - const { user } = session; - const customer = await getCustomer({ telegramId: user?.telegramId }); - if (customer) { - await updateCustomerProfile({ - data: input, - documentId: customer.documentId, - }); - } - } + 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, + }); revalidatePath('/profile'); }