1
1
"use server" ;
2
2
import { PRODUCT_NAME } from "@/constants" ;
3
+ import { generateOrganizationSlug } from "@/lib/utils" ;
3
4
import { createSupabaseUserServerActionClient } from "@/supabase-clients/user/createSupabaseUserServerActionClient" ;
4
5
import { createSupabaseUserServerComponentClient } from "@/supabase-clients/user/createSupabaseUserServerComponentClient" ;
5
6
import type { SAPayload , SupabaseFileUploadOptions , Table } from "@/types" ;
@@ -12,6 +13,8 @@ import ConfirmAccountDeletionEmail from "emails/account-deletion-request";
12
13
import { revalidatePath } from "next/cache" ;
13
14
import slugify from "slugify" ;
14
15
import urlJoin from "url-join" ;
16
+ import { acceptInvitationAction } from "./invitation" ;
17
+ import { createOrganization , setDefaultOrganization } from "./organizations" ;
15
18
import { refreshSessionAction } from "./session" ;
16
19
17
20
export async function getIsAppAdmin ( ) : Promise < boolean > {
@@ -238,6 +241,60 @@ export const acceptTermsOfService = async (
238
241
} ;
239
242
} ;
240
243
244
+
245
+ export const autoAcceptFirstInvitation = async ( ) => {
246
+ const user = await serverGetLoggedInUser ( ) ;
247
+ const pendingInvitations = await getUserPendingInvitationsById ( user . id ) ;
248
+ const supabaseClient = createSupabaseUserServerActionClient ( ) ;
249
+
250
+ if ( pendingInvitations . length > 0 ) {
251
+ const invitation = pendingInvitations [ 0 ] ;
252
+ const invitationAcceptanceResponse = await acceptInvitationAction ( invitation . id ) ;
253
+ if ( invitationAcceptanceResponse . status === "error" ) {
254
+ throw invitationAcceptanceResponse . message ;
255
+ } else if ( invitationAcceptanceResponse . status === "success" ) {
256
+ const joinedOrganizationId = invitationAcceptanceResponse . data ;
257
+ // let's make the joined organization the default one
258
+ await setDefaultOrganization ( joinedOrganizationId ) ;
259
+ }
260
+ const userProfile = await getUserProfile ( user . id ) ;
261
+ const userFullName = userProfile ?. full_name ?? `User ${ user . email ?? "" } ` ;
262
+ const defaultOrganizationCreationResponse = await createOrganization ( userFullName , generateOrganizationSlug ( userFullName ) ) ;
263
+
264
+ if ( defaultOrganizationCreationResponse . status === "error" ) {
265
+ throw defaultOrganizationCreationResponse . message ;
266
+ }
267
+ }
268
+
269
+ console . log ( 'updating user metadata' )
270
+
271
+
272
+ const updateUserMetadataPayload : Partial < AuthUserMetadata > = {
273
+ onboardingHasCreatedOrganization : true ,
274
+ } ;
275
+
276
+ const updateUserMetadataResponse = await supabaseClient . auth . updateUser ( {
277
+ data : updateUserMetadataPayload ,
278
+ } ) ;
279
+
280
+ if ( updateUserMetadataResponse . error ) {
281
+ return {
282
+ status : "error" ,
283
+ message : updateUserMetadataResponse . error . message ,
284
+ } ;
285
+ }
286
+
287
+ const refreshSessionResponse = await refreshSessionAction ( ) ;
288
+ if ( refreshSessionResponse . status === "error" ) {
289
+ return refreshSessionResponse ;
290
+ }
291
+
292
+ return {
293
+ status : "success" ,
294
+ data : true ,
295
+ } ;
296
+ }
297
+
241
298
export async function requestAccountDeletion ( ) : Promise <
242
299
SAPayload < Table < "account_delete_tokens" > >
243
300
> {
0 commit comments