From 421d8fd56f4dc45681b45128848d4893490c368d Mon Sep 17 00:00:00 2001 From: motatoes Date: Tue, 19 Nov 2024 15:21:19 +0000 Subject: [PATCH 1/4] 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(); From 883fd7e9bfca7ea0e46de8213971d541cfb91934 Mon Sep 17 00:00:00 2001 From: motatoes Date: Tue, 19 Nov 2024 21:55:22 +0000 Subject: [PATCH 2/4] temp commit --- .../onboarding/OrganizationCreation.tsx | 12 +++--- src/data/user/organizations.ts | 37 +++++++------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx index b9a58845..53498cdc 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx @@ -3,7 +3,7 @@ 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, setUserMetaDataWithOrgCreated } from "@/data/user/organizations"; +import { createOrganization } from "@/data/user/organizations"; import { generateOrganizationSlug } from "@/lib/utils"; import { CreateOrganizationSchema, createOrganizationSchema } from "@/utils/zod-schemas/organization"; import { zodResolver } from "@hookform/resolvers/zod"; @@ -24,15 +24,17 @@ export function OrganizationCreation({ onSuccess }: OrganizationCreationProps) { useEffect(() => { if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION === "true") { - setUserMetaDataWithOrgCreated() - onSuccess() - return + createOrgMutation.mutate({ "organizationTitle": "digger", organizationSlug: "digger" }); } }, [onSuccess]) const createOrgMutation = useMutation({ mutationFn: async ({ organizationTitle, organizationSlug }: CreateOrganizationSchema) => { - return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true }) + if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION === "true") { + return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true, ignoreIfOrgExists: true }) + } else { + return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true }) + } }, onSuccess: (data) => { const { data: orgId } = data as { data: string } diff --git a/src/data/user/organizations.ts b/src/data/user/organizations.ts index e4d4ebb8..2f29377a 100644 --- a/src/data/user/organizations.ts +++ b/src/data/user/organizations.ts @@ -11,7 +11,6 @@ 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'; @@ -56,15 +55,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' }; @@ -87,7 +88,17 @@ 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 @@ -144,26 +155,6 @@ 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(); From 2f608ec43436764dcc8361a71a1b54280c393826 Mon Sep 17 00:00:00 2001 From: motatoes Date: Wed, 20 Nov 2024 19:55:51 +0000 Subject: [PATCH 3/4] fix org flow --- .../@navbar/[...catchAll]/page.tsx | 2 ++ .../onboarding/OnboardingFlow.tsx | 5 ++++- .../onboarding/OrganizationCreation.tsx | 13 ++----------- .../onboarding/ProfileUpdate.tsx | 8 ++++++++ src/data/user/organizations.ts | 13 ++++++++----- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx index 9363f287..f9c70492 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx @@ -44,6 +44,8 @@ export default async function OrganizationProjectsNavbar({ }: { params: unknown; }) { + + console.log("in catchall", params) const { organizationId } = organizationParamSchema.parse(params); return ( 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 53498cdc..14310dbc 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/onboarding/OrganizationCreation.tsx @@ -9,7 +9,6 @@ import { CreateOrganizationSchema, createOrganizationSchema } from "@/utils/zod- 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 = { @@ -22,19 +21,11 @@ export function OrganizationCreation({ onSuccess }: OrganizationCreationProps) { resolver: zodResolver(createOrganizationSchema), }); - useEffect(() => { - if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION === "true") { - createOrgMutation.mutate({ "organizationTitle": "digger", organizationSlug: "digger" }); - } - }, [onSuccess]) const createOrgMutation = useMutation({ mutationFn: async ({ organizationTitle, organizationSlug }: CreateOrganizationSchema) => { - if (process.env.NEXT_PUBLIC_SKIP_ORG_CREATION === "true") { - return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true, ignoreIfOrgExists: true }) - } else { - return createOrganization(organizationTitle, organizationSlug, { isOnboardingFlow: true }) - } + 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 2f29377a..1b498da4 100644 --- a/src/data/user/organizations.ts +++ b/src/data/user/organizations.ts @@ -16,7 +16,6 @@ import { v4 as uuidv4 } from 'uuid'; import { refreshSessionAction } from './session'; import { updateUserProfileMetadata } from './user'; - export const getOrganizationIdBySlug = async (slug: string) => { const supabaseClient = createSupabaseUserServerComponentClient(); @@ -91,11 +90,10 @@ export const createOrganization = async ( // if set we simply get the org if it already exists if (ignoreIfOrgExists) { try { - organizationId = await getOrganizationIdBySlug(slug) - } catch(fetchError) { + organizationId = await getOrganizationIdBySlug(slug); + } catch (fetchError) { return { status: 'error', message: fetchError.message }; } - } else { return { status: 'error', message: insertError.message }; } @@ -155,7 +153,6 @@ export const createOrganization = async ( } }; - export async function fetchSlimOrganizations() { const currentUser = await serverGetLoggedInUser(); const supabaseClient = createSupabaseUserServerComponentClient(); @@ -282,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'); From 4809d5e4c68ba8e1364747d1a23a5c1f9375dfc3 Mon Sep 17 00:00:00 2001 From: motatoes Date: Wed, 20 Nov 2024 19:56:56 +0000 Subject: [PATCH 4/4] remove console log --- .../@navbar/[...catchAll]/page.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx index f9c70492..9363f287 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/@navbar/[...catchAll]/page.tsx @@ -44,8 +44,6 @@ export default async function OrganizationProjectsNavbar({ }: { params: unknown; }) { - - console.log("in catchall", params) const { organizationId } = organizationParamSchema.parse(params); return (