Skip to content

Commit

Permalink
temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Nov 19, 2024
1 parent 421d8fd commit 883fd7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 }
Expand Down
37 changes: 14 additions & 23 deletions src/data/user/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -56,15 +55,17 @@ export const createOrganization = async (
slug: string,
{
isOnboardingFlow = false,
ignoreIfOrgExists = false,
}: {
isOnboardingFlow?: boolean;
ignoreIfOrgExists?: boolean;
} = {},
): Promise<SAPayload<string>> => {
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' };
Expand All @@ -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
Expand Down Expand Up @@ -144,26 +155,6 @@ export const createOrganization = async (
}
};

export const setUserMetaDataWithOrgCreated = async () => {
const supabaseClient = createSupabaseUserServerComponentClient();
const user = await serverGetLoggedInUser();
const updateUserMetadataPayload: Partial<AuthUserMetadata> = {
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();
Expand Down

0 comments on commit 883fd7e

Please sign in to comment.