'use server'; import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; import { enhanceAction } from '@kit/next/actions'; import { getLogger } from '@kit/shared/logger'; import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client'; import { getSupabaseServerClient } from '@kit/supabase/server-client'; import { BanUserSchema, DeleteAccountSchema, DeleteUserSchema, ImpersonateUserSchema, ReactivateUserSchema, } from './schema/admin-actions.schema'; import { CreateUserSchema } from './schema/create-user.schema'; import { ResetPasswordSchema } from './schema/reset-password.schema'; import { createAdminAccountsService } from './services/admin-accounts.service'; import { createAdminAuthUserService } from './services/admin-auth-user.service'; import { adminAction } from './utils/admin-action'; import { CreateCompanySchema } from './schema/create-company.schema'; import { createCreateCompanyAccountService } from './services/admin-create-company-account.service'; /** * @name banUserAction * @description Ban a user from the system. */ export const banUserAction = adminAction( enhanceAction( async ({ userId }) => { const service = getAdminAuthService(); const logger = await getLogger(); logger.info({ userId }, `Super Admin is banning user...`); await service.banUser(userId); logger.info({ userId }, `Super Admin has successfully banned user`); revalidateAdmin(); return { success: true, }; }, { schema: BanUserSchema, }, ), ); /** * @name reactivateUserAction * @description Reactivate a user in the system. */ export const reactivateUserAction = adminAction( enhanceAction( async ({ userId }) => { const service = getAdminAuthService(); const logger = await getLogger(); logger.info({ userId }, `Super Admin is reactivating user...`); await service.reactivateUser(userId); logger.info({ userId }, `Super Admin has successfully reactivated user`); revalidateAdmin(); return { success: true, }; }, { schema: ReactivateUserSchema, }, ), ); /** * @name impersonateUserAction * @description Impersonate a user in the system. */ export const impersonateUserAction = adminAction( enhanceAction( async ({ userId }) => { const service = getAdminAuthService(); const logger = await getLogger(); logger.info({ userId }, `Super Admin is impersonating user...`); return await service.impersonateUser(userId); }, { schema: ImpersonateUserSchema, }, ), ); /** * @name deleteUserAction * @description Delete a user from the system. */ export const deleteUserAction = adminAction( enhanceAction( async ({ userId }) => { const service = getAdminAuthService(); const logger = await getLogger(); logger.info({ userId }, `Super Admin is deleting user...`); await service.deleteUser(userId); logger.info({ userId }, `Super Admin has successfully deleted user`); revalidateAdmin(); return redirect('/admin/accounts'); }, { schema: DeleteUserSchema, }, ), ); /** * @name deleteAccountAction * @description Delete an account from the system. */ export const deleteAccountAction = adminAction( enhanceAction( async ({ accountId }) => { const service = getAdminAccountsService(); const logger = await getLogger(); logger.info({ accountId }, `Super Admin is deleting account...`); await service.deleteAccount(accountId); logger.info( { accountId }, `Super Admin has successfully deleted account`, ); revalidateAdmin(); return redirect('/admin/accounts'); }, { schema: DeleteAccountSchema, }, ), ); /** * @name createUserAction * @description Create a new user in the system. */ export const createUserAction = adminAction( enhanceAction( async ({ email, password, emailConfirm }) => { const adminClient = getSupabaseServerAdminClient(); const logger = await getLogger(); logger.info({ email }, `Super Admin is creating a new user...`); const { data, error } = await adminClient.auth.admin.createUser({ email, password, email_confirm: emailConfirm, }); if (error) { logger.error({ error }, `Error creating user`); throw new Error(`Error creating user: ${error.message}`); } logger.info( { userId: data.user.id }, `Super Admin has successfully created a new user`, ); revalidateAdmin(); return { success: true, user: data.user, }; }, { schema: CreateUserSchema, }, ), ); /** * @name resetPasswordAction * @description Reset a user's password by sending a password reset email. */ export const resetPasswordAction = adminAction( enhanceAction( async ({ userId }) => { const service = getAdminAuthService(); const logger = await getLogger(); logger.info({ userId }, `Super Admin is resetting user password...`); const result = await service.resetPassword(userId); logger.info( { userId }, `Super Admin has successfully sent password reset email`, ); revalidateAdmin(); return result; }, { schema: ResetPasswordSchema, }, ), ); export const createCompanyAccountAction = enhanceAction( async ({ name }, user) => { const logger = await getLogger(); const client = getSupabaseServerClient(); const service = createCreateCompanyAccountService(client); const ctx = { name: 'team-accounts.create', userId: user.id, accountName: name, }; logger.info(ctx, `Creating company account...`); const { data, error } = await service.createNewOrganizationAccount({ name, userId: user.id, }); if (error) { logger.error({ ...ctx, error }, `Failed to create company account`); return { error: true, }; } logger.info(ctx, `Company account created`); redirect(`/home/${data.slug}/settings`); }, { schema: CreateCompanySchema, }, ); function revalidateAdmin() { revalidatePath('/admin', 'layout'); } function getAdminAuthService() { const client = getSupabaseServerClient(); const adminClient = getSupabaseServerAdminClient(); return createAdminAuthUserService(client, adminClient); } function getAdminAccountsService() { const adminClient = getSupabaseServerAdminClient(); return createAdminAccountsService(adminClient); }