@@ -2077,7 +2077,7 @@ export function accessRoutes(
20772077
20782078 const actorEmail =
20792079 requestType === "human" ? await resolveActorEmail ( db , req ) : null ;
2080- const created = ! inviteAlreadyAccepted
2080+ let created = ! inviteAlreadyAccepted
20812081 ? await db . transaction ( async ( tx ) => {
20822082 await tx
20832083 . update ( invites )
@@ -2249,6 +2249,40 @@ export function accessRoutes(
22492249 }
22502250 }
22512251
2252+ // Auto-approve human join requests — the inviter already made the decision.
2253+ let autoApproved = false ;
2254+ if ( requestType === "human" && ! inviteAlreadyAccepted && created . requestingUserId ) {
2255+ await access . ensureMembership (
2256+ companyId ,
2257+ "user" ,
2258+ created . requestingUserId ,
2259+ "member" ,
2260+ "active"
2261+ ) ;
2262+ const grants = grantsFromDefaults (
2263+ invite . defaultsPayload as Record < string , unknown > | null ,
2264+ "human"
2265+ ) ;
2266+ await access . setPrincipalGrants (
2267+ companyId ,
2268+ "user" ,
2269+ created . requestingUserId ,
2270+ grants ,
2271+ invite . invitedByUserId ?? null
2272+ ) ;
2273+ await db
2274+ . update ( joinRequests )
2275+ . set ( {
2276+ status : "approved" ,
2277+ approvedByUserId : invite . invitedByUserId ?? "system" ,
2278+ approvedAt : new Date ( ) ,
2279+ updatedAt : new Date ( )
2280+ } )
2281+ . where ( eq ( joinRequests . id , created . id ) ) ;
2282+ created = { ...created , status : "approved" as const , approvedAt : new Date ( ) } ;
2283+ autoApproved = true ;
2284+ }
2285+
22522286 await logActivity ( db , {
22532287 companyId,
22542288 actorType : req . actor . type === "agent" ? "agent" : "user" ,
@@ -2257,15 +2291,18 @@ export function accessRoutes(
22572291 ? req . actor . agentId ?? "invite-agent"
22582292 : req . actor . userId ??
22592293 ( requestType === "agent" ? "invite-anon" : "board" ) ,
2260- action : inviteAlreadyAccepted
2261- ? "join.request_replayed"
2262- : "join.requested" ,
2294+ action : autoApproved
2295+ ? "join.auto_approved"
2296+ : inviteAlreadyAccepted
2297+ ? "join.request_replayed"
2298+ : "join.requested" ,
22632299 entityType : "join_request" ,
22642300 entityId : created . id ,
22652301 details : {
22662302 requestType,
22672303 requestIp : created . requestIp ,
2268- inviteReplay : inviteAlreadyAccepted
2304+ inviteReplay : inviteAlreadyAccepted ,
2305+ autoApproved
22692306 }
22702307 } ) ;
22712308
0 commit comments