1+ import { VerificationCodeType } from "@/generated/prisma/client" ;
12import { ensureTeamExists , ensureTeamMembershipExists , ensureUserTeamPermissionExists } from "@/lib/request-checks" ;
2- import { globalPrismaClient , getPrismaClientForTenancy , retryTransaction } from "@/prisma-client" ;
3+ import { getPrismaClientForTenancy , globalPrismaClient , retryTransaction } from "@/prisma-client" ;
34import { createCrudHandlers } from "@/route-handlers/crud-handler" ;
4- import { VerificationCodeType } from "@/generated/prisma/client" ;
55import { KnownErrors } from "@stackframe/stack-shared" ;
66import { teamInvitationCrud } from "@stackframe/stack-shared/dist/interface/crud/team-invitation" ;
77import { userIdOrMeSchema , yupObject , yupString } from "@stackframe/stack-shared/dist/schema-fields" ;
8- import { StackAssertionError , throwErr } from "@stackframe/stack-shared/dist/utils/errors" ;
8+ import { StatusError , throwErr } from "@stackframe/stack-shared/dist/utils/errors" ;
99import { createLazyProxy } from "@stackframe/stack-shared/dist/utils/proxies" ;
1010import { teamsCrudHandlers } from "../teams/crud" ;
1111import { teamInvitationCodeHandler } from "./accept/verification-code-handler" ;
@@ -20,17 +20,19 @@ export const teamInvitationsCrudHandlers = createLazyProxy(() => createCrudHandl
2020 } ) ,
2121 onList : async ( { auth, query } ) => {
2222 if ( query . team_id != null && query . user_id != null ) {
23- throw new StackAssertionError ( "Cannot specify both team_id and user_id" ) ;
23+ throw new StatusError ( StatusError . BadRequest , "Cannot specify both team_id and user_id" ) ;
2424 }
2525 if ( query . team_id == null && query . user_id == null ) {
26- throw new StackAssertionError ( "Must specify either team_id or user_id" ) ;
26+ throw new StatusError ( StatusError . BadRequest , "Must specify either team_id or user_id" ) ;
2727 }
2828
2929 if ( query . user_id != null ) {
30- // List invitations sent to the current user's verified emails
31- const currentUserId = auth . user ?. id ?? throwErr ( new KnownErrors . CannotGetOwnUserWithoutUser ( ) ) ;
32- if ( auth . type === 'client' && query . user_id !== currentUserId ) {
33- throw new KnownErrors . CannotGetOwnUserWithoutUser ( ) ;
30+ // List invitations sent to the user's verified emails
31+ if ( auth . type === 'client' ) {
32+ const currentUserId = auth . user ?. id ?? throwErr ( new KnownErrors . CannotGetOwnUserWithoutUser ( ) ) ;
33+ if ( query . user_id !== currentUserId ) {
34+ throw new KnownErrors . CannotGetOwnUserWithoutUser ( ) ;
35+ }
3436 }
3537
3638 const targetUserId = query . user_id ;
@@ -74,10 +76,15 @@ export const teamInvitationsCrudHandlers = createLazyProxy(() => createCrudHandl
7476 const team = await teamsCrudHandlers . adminRead ( {
7577 tenancy : auth . tenancy ,
7678 team_id : teamId ,
79+ allowedErrorTypes : [ KnownErrors . TeamNotFound ] ,
7780 } ) ;
7881 teamsMap . set ( teamId , team . display_name ) ;
79- } catch {
80- // Team may have been deleted since the invitation was created; skip these invitations
82+ } catch ( e ) {
83+ if ( KnownErrors . TeamNotFound . isInstance ( e ) ) {
84+ // Team may have been deleted since the invitation was created; skip these invitations
85+ continue ;
86+ }
87+ throw e ;
8188 }
8289 }
8390
@@ -133,16 +140,11 @@ export const teamInvitationsCrudHandlers = createLazyProxy(() => createCrudHandl
133140 } ,
134141 } ) ;
135142
136- let teamDisplayName : string ;
137- try {
138- const team = await teamsCrudHandlers . adminRead ( {
139- tenancy : auth . tenancy ,
140- team_id : teamId ,
141- } ) ;
142- teamDisplayName = team . display_name ;
143- } catch {
144- teamDisplayName = "" ;
145- }
143+ const team = await teamsCrudHandlers . adminRead ( {
144+ tenancy : auth . tenancy ,
145+ team_id : teamId ,
146+ } ) ;
147+ const teamDisplayName = team . display_name ;
146148
147149 return {
148150 items : allCodes . map ( code => ( {
@@ -157,7 +159,7 @@ export const teamInvitationsCrudHandlers = createLazyProxy(() => createCrudHandl
157159 } ) ;
158160 } ,
159161 onDelete : async ( { auth, query, params } ) => {
160- const teamId = query . team_id ?? throwErr ( new StackAssertionError ( "team_id is required for deleting a team invitation" ) ) ;
162+ const teamId = query . team_id ?? throwErr ( new StatusError ( StatusError . BadRequest , "team_id is required for deleting a team invitation" ) ) ;
161163 const prisma = await getPrismaClientForTenancy ( auth . tenancy ) ;
162164 await retryTransaction ( prisma , async ( tx ) => {
163165 if ( auth . type === 'client' ) {
0 commit comments