@@ -5,11 +5,13 @@ import { requireAdmin } from "@/lib/authUtils";
55import { EmailService } from "@/lib/services/EmailService" ;
66
77const sendEmailSchema = z . object ( {
8- subject : z . string ( ) ,
9- subtitle : z . string ( ) . optional ( ) ,
10- body : z . string ( ) ,
11- footer : z . string ( ) . optional ( ) ,
12- recipientIds : z . array ( z . string ( ) ) ,
8+ subject : z . string ( ) . trim ( ) . min ( 1 , "Subject is required" ) ,
9+ body : z . string ( ) . trim ( ) . min ( 1 , "Message text is required" ) ,
10+ subtitle : z . string ( ) . trim ( ) . optional ( ) ,
11+ footer : z . string ( ) . trim ( ) . optional ( ) ,
12+ recipientIds : z
13+ . array ( z . string ( ) . min ( 1 ) )
14+ . min ( 1 , "Select at least one recipient" ) ,
1315} ) ;
1416
1517function composeEmailBody ( {
@@ -29,14 +31,21 @@ function composeEmailBody({
2931
3032const app = new Hono ( ) . post (
3133 "/" ,
32- zValidator ( "json" , sendEmailSchema ) ,
34+ zValidator ( "json" , sendEmailSchema , ( result , c ) => {
35+ if ( ! result . success ) {
36+ return c . json (
37+ { error : result . error . issues [ 0 ] ?. message ?? "Invalid email" } ,
38+ 400 ,
39+ ) ;
40+ }
41+ } ) ,
3342 async ( c ) => {
3443 const session = await requireAdmin ( c ) ;
3544 const organizationId = session . session . activeOrganizationId ! ;
3645 const { subject, subtitle, body, footer, recipientIds } =
3746 c . req . valid ( "json" ) ;
3847
39- await EmailService . emailMembers ( organizationId , {
48+ const result = await EmailService . emailMembers ( organizationId , {
4049 subject,
4150 content : [
4251 {
@@ -47,6 +56,19 @@ const app = new Hono().post(
4756 targetUserIds : recipientIds ,
4857 } ) ;
4958
59+ if ( result === "no-recipients" ) {
60+ return c . json (
61+ {
62+ error : "None of the selected recipients belong to this organization" ,
63+ } ,
64+ 400 ,
65+ ) ;
66+ }
67+
68+ if ( result === "delivery-failed" ) {
69+ return c . json ( { error : "Failed to send email" } , 500 ) ;
70+ }
71+
5072 return c . json ( { success : true } ) ;
5173 } ,
5274) ;
0 commit comments