diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OnboardingFlow.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OnboardingFlow.tsx index 57e92424..63de15b3 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OnboardingFlow.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OnboardingFlow.tsx @@ -113,7 +113,10 @@ function getAllFlowStates(onboardingStatus: AuthUserMetadata): FLOW_STATE[] { if (isUserCreatedThroughOrgInvitation) { flowStates.push("JOIN_INVITED_ORG"); } else { - flowStates.push("ORGANIZATION"); + if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION !== "true") { + flowStates.push("ORGANIZATION"); + } + } } diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx index 97ebd9bd..14310dbc 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx @@ -21,9 +21,11 @@ export function OrganizationCreation({ onSuccess }: OrganizationCreationProps) { resolver: zodResolver(createOrganizationSchema), }); + const createOrgMutation = useMutation({ mutationFn: async ({ organizationTitle, organizationSlug }: CreateOrganizationSchema) => { return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true }) + }, onSuccess: (data) => { const { data: orgId } = data as { data: string } diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/ProfileUpdate.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/ProfileUpdate.tsx index e8d7de73..532cdea3 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/ProfileUpdate.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/ProfileUpdate.tsx @@ -4,6 +4,7 @@ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { T } from "@/components/ui/Typography"; import { useToast } from "@/components/ui/use-toast"; +import { createOrganization } from "@/data/user/organizations"; import { updateUserProfileNameAndAvatar, uploadPublicUserAvatar } from "@/data/user/user"; import { generateSlug } from "@/lib/utils"; import { getUserAvatarUrl } from "@/utils/helpers"; @@ -38,6 +39,13 @@ export function ProfileUpdate({ mutationFn: () => updateUserProfileNameAndAvatar({ fullName, userName, avatarUrl }, { isOnboardingFlow: true }), onSuccess: () => { toast({ title: "Profile updated!", description: "Your profile has been successfully updated." }); + + // TODO: move this to server side component /src/app/(dynamic-pages)/(authenticated-pages)/onboarding/page.tsx + if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION === "true") { + console.log("creating default organisation for user") + createOrganization("digger", "digger", { isOnboardingFlow: true, ignoreIfOrgExists: true }) + } + onSuccess(); }, onError: () => { diff --git a/src/data/user/organizations.ts b/src/data/user/organizations.ts index 8179df2c..1b498da4 100644 --- a/src/data/user/organizations.ts +++ b/src/data/user/organizations.ts @@ -54,15 +54,17 @@ export const createOrganization = async ( slug: string, { isOnboardingFlow = false, + ignoreIfOrgExists = false, }: { isOnboardingFlow?: boolean; + ignoreIfOrgExists?: boolean; } = {}, ): Promise> => { try { const supabaseClient = createSupabaseUserServerActionClient(); const user = await serverGetLoggedInUser(); - const organizationId = uuidv4(); + let organizationId = uuidv4(); if (RESTRICTED_SLUG_NAMES.includes(slug)) { return { status: 'error', message: 'Slug is restricted' }; @@ -85,7 +87,16 @@ export const createOrganization = async ( if (insertError) { console.error('Error inserting organization:', insertError); - return { status: 'error', message: insertError.message }; + // if set we simply get the org if it already exists + if (ignoreIfOrgExists) { + try { + organizationId = await getOrganizationIdBySlug(slug); + } catch (fetchError) { + return { status: 'error', message: fetchError.message }; + } + } else { + return { status: 'error', message: insertError.message }; + } } const { error: orgMemberErrors } = await supabaseAdminClient @@ -268,6 +279,12 @@ export const getLoggedInUserOrganizationRole = async ( .single(); if (error) { + console.log( + 'error in getloggedinUserOrganizationRole:', + userId, + organizationId, + error, + ); throw error; } else if (!data) { throw new Error('User is not a member of this organization');