@@ -35,24 +35,37 @@ export const teamsCrudHandlers = createLazyProxy(() => createCrudHandlers(teamsC
3535 team_id : yupString ( ) . uuid ( ) . defined ( ) ,
3636 } ) ,
3737 onCreate : async ( { query, auth, data } ) => {
38+ let addUserId = data . creator_user_id ;
39+
3840 if ( data . creator_user_id && query . add_current_user ) {
3941 throw new StatusError ( StatusError . BadRequest , "Cannot use both creator_user_id and add_current_user. add_current_user is deprecated, please only use creator_user_id in the body." ) ;
4042 }
4143
42- if ( auth . type === 'client' && ! auth . user ) {
43- throw new KnownErrors . UserAuthenticationRequired ;
44- }
44+ if ( auth . type === 'client' ) {
45+ if ( ! auth . user ) {
46+ throw new KnownErrors . UserAuthenticationRequired ;
47+ }
4548
46- if ( auth . type === 'client' && ! auth . tenancy . config . client_team_creation_enabled ) {
47- throw new StatusError ( StatusError . Forbidden , 'Client team creation is disabled for this project' ) ;
48- }
49+ if ( ! auth . tenancy . config . client_team_creation_enabled ) {
50+ throw new StatusError ( StatusError . Forbidden , 'Client team creation is disabled for this project' ) ;
51+ }
4952
50- if ( auth . type === 'client' && data . profile_image_url && ! validateBase64Image ( data . profile_image_url ) ) {
51- throw new StatusError ( 400 , "Invalid profile image URL" ) ;
53+ if ( data . profile_image_url && ! validateBase64Image ( data . profile_image_url ) ) {
54+ throw new StatusError ( 400 , "Invalid profile image URL" ) ;
55+ }
56+
57+ if ( ! data . creator_user_id ) {
58+ addUserId = auth . user . id ;
59+ } else if ( data . creator_user_id !== auth . user . id ) {
60+ throw new StatusError ( StatusError . Forbidden , "You cannot create a team as a user that is not yourself. Make sure you set the creator_user_id to 'me'." ) ;
61+ }
5262 }
5363
54- if ( auth . type === 'client' && ( ! data . creator_user_id || data . creator_user_id !== auth . user ?. id ) ) {
55- throw new StatusError ( StatusError . Forbidden , "You cannot create a team as a user that is not yourself. Make sure you set the creator_user_id to 'me'." ) ;
64+ if ( query . add_current_user === 'true' ) {
65+ if ( ! auth . user ) {
66+ throw new StatusError ( StatusError . Unauthorized , "You must be logged in to create a team with the current user as a member." ) ;
67+ }
68+ addUserId = auth . user . id ;
5669 }
5770
5871 const db = await retryTransaction ( async ( tx ) => {
@@ -69,22 +82,6 @@ export const teamsCrudHandlers = createLazyProxy(() => createCrudHandlers(teamsC
6982 } ,
7083 } ) ;
7184
72- let addUserId : string | undefined ;
73- if ( data . creator_user_id ) {
74- if ( auth . type === 'client' ) {
75- const currentUserId = auth . user ?. id ?? throwErr ( new KnownErrors . CannotGetOwnUserWithoutUser ( ) ) ;
76- if ( data . creator_user_id !== currentUserId ) {
77- throw new StatusError ( StatusError . Forbidden , "You cannot add a user to the team as the creator that is not yourself on the client." ) ;
78- }
79- }
80- addUserId = data . creator_user_id ;
81- } else if ( query . add_current_user === 'true' ) {
82- if ( ! auth . user ) {
83- throw new StatusError ( StatusError . Unauthorized , "You must be logged in to create a team with the current user as a member." ) ;
84- }
85- addUserId = auth . user . id ;
86- }
87-
8885 if ( addUserId ) {
8986 await ensureUserExists ( tx , { tenancyId : auth . tenancy . id , userId : addUserId } ) ;
9087 await addUserToTeam ( tx , {
0 commit comments