@@ -12,7 +12,7 @@ const upload = multer({ storage: multer.memoryStorage() });
1212router . post ( '/send-invite' , verifyIdToken , async ( req : Request , res : Response ) => {
1313 console . log ( '[/whatsapp/send-invite] Received request' ) ;
1414 const userId = req . userId ;
15- const { eventId, groupId, name, phone_no } = req . body ;
15+ const { eventId, groupId, name, phone_no, invites } = req . body ;
1616
1717 console . log ( '[/whatsapp/send-invite] Request body:' , req . body ) ;
1818 console . log ( '[/whatsapp/send-invite] Authenticated userId:' , userId ) ;
@@ -22,19 +22,86 @@ router.post('/send-invite', verifyIdToken, async (req: Request, res: Response) =
2222 return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
2323 }
2424
25- if ( ! eventId || ! groupId || ! name || ! phone_no ) {
26- console . log ( '[/whatsapp/send-invite] Bad Request: Missing required fields' ) ;
27- return res . status ( 400 ) . json ( { message : 'Missing required fields: eventId, groupId, name, phone_no' } ) ;
25+ if ( ! eventId || ! groupId ) {
26+ console . log ( '[/whatsapp/send-invite] Bad Request: Missing eventId or groupId' ) ;
27+ return res . status ( 400 ) . json ( { message : 'Missing required fields: eventId, groupId' } ) ;
28+ }
29+
30+ // Support multiple formats:
31+ // 1. New format: invites array with {name, phone_no} objects
32+ // 2. Legacy format: single name and phone_no (string)
33+ // 3. Legacy format: single name and multiple phone_no (array)
34+ let invitesList : Array < { name : string ; phone_no : string } > = [ ] ;
35+
36+ if ( invites && Array . isArray ( invites ) ) {
37+ // New format: array of {name, phone_no} objects
38+ console . log ( '[/whatsapp/send-invite] Using invites array format' ) ;
39+ invitesList = invites . filter ( ( invite : any ) => invite . name && invite . phone_no ) ;
40+ if ( invitesList . length === 0 ) {
41+ console . log ( '[/whatsapp/send-invite] Bad Request: invites array is empty or invalid' ) ;
42+ return res . status ( 400 ) . json ( { message : 'invites array must contain objects with name and phone_no' } ) ;
43+ }
44+ } else if ( name && phone_no ) {
45+ // Legacy format: single name with one or more phone numbers
46+ console . log ( '[/whatsapp/send-invite] Using legacy format with name and phone_no' ) ;
47+ if ( typeof phone_no === 'string' ) {
48+ invitesList = [ { name, phone_no } ] ;
49+ } else if ( Array . isArray ( phone_no ) ) {
50+ invitesList = phone_no . map ( ( phone : string ) => ( { name, phone_no : phone } ) ) ;
51+ } else {
52+ console . log ( '[/whatsapp/send-invite] Bad Request: phone_no must be a string or array' ) ;
53+ return res . status ( 400 ) . json ( { message : 'phone_no must be a string or an array of strings' } ) ;
54+ }
55+ } else {
56+ console . log ( '[/whatsapp/send-invite] Bad Request: No valid invite data provided' ) ;
57+ return res . status ( 400 ) . json ( { message : 'Either provide invites array [{name, phone_no}] or name and phone_no fields' } ) ;
58+ }
59+
60+ if ( invitesList . length === 0 ) {
61+ console . log ( '[/whatsapp/send-invite] Bad Request: No invites to send' ) ;
62+ return res . status ( 400 ) . json ( { message : 'At least one invite is required' } ) ;
2863 }
2964
3065 try {
31- console . log ( '[/whatsapp/send-invite] Calling sendWhatsappInviteWithTwilio service' ) ;
32- const result = await sendWhatsappInviteWithTwilio ( userId , eventId , groupId , name , phone_no ) ;
33- console . log ( '[/whatsapp/send-invite] Service response:' , result ) ;
34- if ( result . success ) {
35- res . status ( 200 ) . json ( { message : 'Invite sent successfully.' } ) ;
66+ console . log ( '[/whatsapp/send-invite] Sending invites to' , invitesList . length , 'recipient(s)' ) ;
67+ console . log ( '[/whatsapp/send-invite] Invites list:' , JSON . stringify ( invitesList ) ) ;
68+
69+ const results = {
70+ sent : [ ] as any [ ] ,
71+ failed : [ ] as any [ ] ,
72+ } ;
73+
74+ // Send invite to each recipient with their personalized name
75+ for ( const invite of invitesList ) {
76+ console . log ( '[/whatsapp/send-invite] Processing:' , invite ) ;
77+ const result = await sendWhatsappInviteWithTwilio (
78+ userId ,
79+ eventId ,
80+ groupId ,
81+ invite . name ,
82+ String ( invite . phone_no )
83+ ) ;
84+
85+ if ( result . success ) {
86+ results . sent . push ( { name : invite . name , phone_no : invite . phone_no } ) ;
87+ } else {
88+ results . failed . push ( { name : invite . name , phone_no : invite . phone_no , error : result . error } ) ;
89+ }
90+ }
91+
92+ console . log ( '[/whatsapp/send-invite] Results:' , results ) ;
93+
94+ // Return success if at least one invite was sent
95+ if ( results . sent . length > 0 ) {
96+ res . status ( 200 ) . json ( {
97+ message : `Successfully sent ${ results . sent . length } out of ${ invitesList . length } invite(s)` ,
98+ ...results
99+ } ) ;
36100 } else {
37- res . status ( 500 ) . json ( { message : 'Failed to send invite' , error : result . error } ) ;
101+ res . status ( 500 ) . json ( {
102+ message : 'Failed to send all invites' ,
103+ ...results
104+ } ) ;
38105 }
39106 } catch ( error : any ) {
40107 console . error ( '[/whatsapp/send-invite] An unexpected error occurred:' , error ) ;
0 commit comments