@@ -4,7 +4,7 @@ import { getAuditService } from "@/ee/features/audit/factory";
44import { env , getSMTPConnectionURL } from "@sourcebot/shared" ;
55import { addUserToOrganization , orgHasAvailability } from "@/lib/authUtils" ;
66import { ErrorCode } from "@/lib/errorCodes" ;
7- import { notFound , orgNotFound , ServiceError } from "@/lib/serviceError" ;
7+ import { notAuthenticated , notFound , orgNotFound , ServiceError } from "@/lib/serviceError" ;
88import { getOrgMetadata , isHttpError , isServiceError } from "@/lib/utils" ;
99import { __unsafePrisma } from "@/prisma" ;
1010import { render } from "@react-email/components" ;
@@ -24,7 +24,7 @@ import JoinRequestApprovedEmail from "./emails/joinRequestApprovedEmail";
2424import JoinRequestSubmittedEmail from "./emails/joinRequestSubmittedEmail" ;
2525import { AGENTIC_SEARCH_TUTORIAL_DISMISSED_COOKIE_NAME , MOBILE_UNSUPPORTED_SPLASH_SCREEN_DISMISSED_COOKIE_NAME , SINGLE_TENANT_ORG_ID , SOURCEBOT_SUPPORT_EMAIL } from "./lib/constants" ;
2626import { RepositoryQuery } from "./lib/types" ;
27- import { withAuth , withOptionalAuth } from "./middleware/withAuth" ;
27+ import { getAuthenticatedUser , withAuth , withOptionalAuth } from "./middleware/withAuth" ;
2828import { withMinimumOrgRole } from "./middleware/withMinimumOrgRole" ;
2929import { getBrowsePath } from "./app/(app)/browse/hooks/utils" ;
3030import { sew } from "@/middleware/sew" ;
@@ -817,17 +817,14 @@ export const getOrgAccountRequests = async () => sew(() =>
817817 } ) ) ;
818818 } ) ) ;
819819
820- export const createAccountRequest = async ( userId : string ) => sew ( async ( ) => {
821- const user = await __unsafePrisma . user . findUnique ( {
822- where : {
823- id : userId ,
824- } ,
825- } ) ;
826-
827- if ( ! user ) {
828- return notFound ( "User not found" ) ;
820+ export const createAccountRequest = async ( ) => sew ( async ( ) => {
821+ const authResult = await getAuthenticatedUser ( ) ;
822+ if ( ! authResult ) {
823+ return notAuthenticated ( ) ;
829824 }
830825
826+ const { user } = authResult ;
827+
831828 const org = await __unsafePrisma . org . findUnique ( {
832829 where : {
833830 id : SINGLE_TENANT_ORG_ID ,
@@ -841,14 +838,14 @@ export const createAccountRequest = async (userId: string) => sew(async () => {
841838 const existingRequest = await __unsafePrisma . accountRequest . findUnique ( {
842839 where : {
843840 requestedById_orgId : {
844- requestedById : userId ,
841+ requestedById : user . id ,
845842 orgId : org . id ,
846843 } ,
847844 } ,
848845 } ) ;
849846
850847 if ( existingRequest ) {
851- logger . warn ( `User ${ userId } already has an account request for org ${ org . id } . Skipping account request creation.` ) ;
848+ logger . warn ( `User ${ user . id } already has an account request for org ${ org . id } . Skipping account request creation.` ) ;
852849 return {
853850 success : true ,
854851 existingRequest : true ,
@@ -858,7 +855,7 @@ export const createAccountRequest = async (userId: string) => sew(async () => {
858855 if ( ! existingRequest ) {
859856 await __unsafePrisma . accountRequest . create ( {
860857 data : {
861- requestedById : userId ,
858+ requestedById : user . id ,
862859 orgId : org . id ,
863860 } ,
864861 } ) ;
@@ -869,7 +866,7 @@ export const createAccountRequest = async (userId: string) => sew(async () => {
869866 // on user creation (the header isn't set when next-auth calls onCreateUser for some reason)
870867 const deploymentUrl = env . AUTH_URL ;
871868
872- const owner = await __unsafePrisma . user . findFirst ( {
869+ const owners = await __unsafePrisma . user . findMany ( {
873870 where : {
874871 orgs : {
875872 some : {
@@ -880,8 +877,8 @@ export const createAccountRequest = async (userId: string) => sew(async () => {
880877 } ,
881878 } ) ;
882879
883- if ( ! owner ) {
884- logger . error ( `Failed to find owner for org ${ org . id } when drafting email for account request from ${ userId } ` ) ;
880+ if ( owners . length === 0 ) {
881+ logger . error ( `Failed to find any owners for org ${ org . id } when drafting email for account request from ${ user . id } ` ) ;
885882 } else {
886883 const html = await render ( JoinRequestSubmittedEmail ( {
887884 baseUrl : deploymentUrl ,
@@ -894,9 +891,13 @@ export const createAccountRequest = async (userId: string) => sew(async () => {
894891 orgImageUrl : org . imageUrl ?? undefined ,
895892 } ) ) ;
896893
894+ const ownerEmails = owners
895+ . map ( ( owner ) => owner . email )
896+ . filter ( ( email ) : email is string => email !== null ) ;
897+
897898 const transport = createTransport ( smtpConnectionUrl ) ;
898899 const result = await transport . sendMail ( {
899- to : owner . email ! ,
900+ to : ownerEmails ,
900901 from : env . EMAIL_FROM_ADDRESS ,
901902 subject : `New account request for ${ org . name } on Sourcebot` ,
902903 html,
@@ -905,7 +906,7 @@ export const createAccountRequest = async (userId: string) => sew(async () => {
905906
906907 const failed = result . rejected . concat ( result . pending ) . filter ( Boolean ) ;
907908 if ( failed . length > 0 ) {
908- logger . error ( `Failed to send account request email to ${ owner . email } : ${ failed } ` ) ;
909+ logger . error ( `Failed to send account request email to ${ ownerEmails . join ( ', ' ) } : ${ failed } ` ) ;
909910 }
910911 }
911912 } else {
0 commit comments