From 421d8fd56f4dc45681b45128848d4893490c368d Mon Sep 17 00:00:00 2001 From: motatoes Date: Tue, 19 Nov 2024 15:21:19 +0000 Subject: [PATCH] tmp commit --- .../onboarding/OrganizationCreation.tsx | 11 ++++++++- src/data/user/organizations.ts | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx index 97ebd9bd..b9a58845 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx @@ -3,12 +3,13 @@ import { CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useToast } from "@/components/ui/use-toast"; -import { createOrganization } from "@/data/user/organizations"; +import { createOrganization, setUserMetaDataWithOrgCreated } from "@/data/user/organizations"; import { generateOrganizationSlug } from "@/lib/utils"; import { CreateOrganizationSchema, createOrganizationSchema } from "@/utils/zod-schemas/organization"; import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation } from "@tanstack/react-query"; import Cookies from 'js-cookie'; +import { useEffect } from "react"; import { useForm } from "react-hook-form"; type OrganizationCreationProps = { @@ -21,6 +22,14 @@ export function OrganizationCreation({ onSuccess }: OrganizationCreationProps) { resolver: zodResolver(createOrganizationSchema), }); + useEffect(() => { + if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION === "true") { + setUserMetaDataWithOrgCreated() + onSuccess() + return + } + }, [onSuccess]) + const createOrgMutation = useMutation({ mutationFn: async ({ organizationTitle, organizationSlug }: CreateOrganizationSchema) => { return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true }) diff --git a/src/data/user/organizations.ts b/src/data/user/organizations.ts index 8179df2c..e4d4ebb8 100644 --- a/src/data/user/organizations.ts +++ b/src/data/user/organizations.ts @@ -11,11 +11,13 @@ import type { UnwrapPromise, } from '@/types'; import { serverGetLoggedInUser } from '@/utils/server/serverGetLoggedInUser'; +import type { AuthUserMetadata } from '@/utils/zod-schemas/authUserMetadata'; import { revalidatePath } from 'next/cache'; import { v4 as uuidv4 } from 'uuid'; import { refreshSessionAction } from './session'; import { updateUserProfileMetadata } from './user'; + export const getOrganizationIdBySlug = async (slug: string) => { const supabaseClient = createSupabaseUserServerComponentClient(); @@ -142,6 +144,27 @@ export const createOrganization = async ( } }; +export const setUserMetaDataWithOrgCreated = async () => { + const supabaseClient = createSupabaseUserServerComponentClient(); + const user = await serverGetLoggedInUser(); + const updateUserMetadataPayload: Partial = { + onboardingHasCreatedOrganization: true, + }; + + const updateUserMetadataResponse = await supabaseClient.auth.updateUser({ + data: updateUserMetadataPayload, + }); + + if (updateUserMetadataResponse.error) { + console.error( + 'Error updating user metadata:', + updateUserMetadataResponse.error, + ); + + throw updateUserMetadataResponse.error; + } +}; + export async function fetchSlimOrganizations() { const currentUser = await serverGetLoggedInUser(); const supabaseClient = createSupabaseUserServerComponentClient();