@@ -4,23 +4,13 @@ import * as path from 'path';
44import type { ValidatedEventAPIGatewayProxyEvent } from '@libs/api-gateway' ;
55import { middyfy } from '@libs/lambda' ;
66import schema from './schema' ;
7+ import type { Failure , TeamInvite , TeamDocument , UserDocument } from '../../../types' ;
78import { MongoDB , validateToken , userExistsLogic } from '../../../util' ;
89
910dotenv . config ( { path : path . resolve ( process . cwd ( ) , '.env' ) } ) ;
1011
1112const MAX_TEAM_SIZE = 4 ;
1213
13- interface Failure {
14- email : string ;
15- reason : string ;
16- }
17-
18- interface PendingInvite {
19- team_id : string ;
20- invited_by : string ;
21- invited_at : Date ;
22- }
23-
2414const teamInvite : ValidatedEventAPIGatewayProxyEvent < typeof schema > = async ( event ) => {
2515 // Alias snake_case request fields to camelCase locals to satisfy naming-convention
2616 const { auth_token : authToken , auth_email : authEmail , team_id : teamId , emails } = event . body ;
@@ -37,8 +27,8 @@ const teamInvite: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (eve
3727 const db = MongoDB . getInstance ( process . env . MONGO_URI ! ) ;
3828 await db . connect ( ) ;
3929 const client = db . getClient ( ) ;
40- const users = db . getCollection ( 'users' ) ;
41- const teams = db . getCollection ( 'teams' ) ;
30+ const users = db . getCollection < UserDocument > ( 'users' ) ;
31+ const teams = db . getCollection < TeamDocument > ( 'teams' ) ;
4232
4333 // verify auth user
4434 const authUser = await users . findOne ( { email : authEmail } ) ;
@@ -64,8 +54,16 @@ const teamInvite: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (eve
6454 } ;
6555 }
6656
57+ // check team status
58+ if ( team . status !== 'Active' ) {
59+ return {
60+ statusCode : 400 ,
61+ body : JSON . stringify ( { message : 'Team is not active' } ) ,
62+ } ;
63+ }
64+
6765 // capacity check
68- const confirmedCount = Array . isArray ( team . members ) ? team . members . length : 0 ;
66+ const confirmedCount = ( Array . isArray ( team . members ) ? team . members . length : 0 ) + 1 ; // + 1 for the leader
6967 const pendingCount = await users . countDocuments ( {
7068 'team_info.pending_invites.team_id' : teamId ,
7169 } ) ;
@@ -114,7 +112,7 @@ const teamInvite: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (eve
114112 }
115113
116114 // prevent duplicate invites (type the pending list instead of using any)
117- const pending = ( user . team_info ?. pending_invites ?? [ ] ) as PendingInvite [ ] ;
115+ const pending = ( user . team_info ?. pending_invites ?? [ ] ) as TeamInvite [ ] ;
118116 if ( pending . some ( ( inv ) => inv . team_id === teamId ) ) {
119117 failed . push ( { email, reason : 'Already invited to this team' } ) ;
120118 continue ;
@@ -129,7 +127,8 @@ const teamInvite: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (eve
129127 team_id : teamId ,
130128 invited_by : authEmail ,
131129 invited_at : new Date ( ) ,
132- } as PendingInvite ,
130+ team_name : team . team_name ,
131+ } as TeamInvite ,
133132 } ,
134133 } ,
135134 { session }
0 commit comments