@@ -10,23 +10,39 @@ export async function submitSupportFeedback(
1010 data : FeedbackData ,
1111) : Promise < { success : true } | { error : string } > {
1212 try {
13- const siwaUrl = process . env . NEXT_PUBLIC_SIWA_URL ;
14-
13+ // Fail fast on missing configuration
14+ const siwaUrl =
15+ process . env . SIWA_URL ?? process . env . NEXT_PUBLIC_SIWA_URL ?? "" ;
1516 if ( ! siwaUrl ) {
1617 throw new Error ( "SIWA URL not configured" ) ;
1718 }
1819
20+ const apiKey = process . env . SERVICE_AUTH_KEY_SIWA ;
21+ if ( ! apiKey ) {
22+ throw new Error ( "SERVICE_AUTH_KEY_SIWA not configured" ) ;
23+ }
24+
25+ // Basic input validation/normalization
26+ if ( ! Number . isFinite ( data . rating ) || data . rating < 1 || data . rating > 5 ) {
27+ return { error : "Rating must be an integer between 1 and 5." } ;
28+ }
29+
30+ const normalizedFeedback = ( data . feedback ?? "" )
31+ . toString ( )
32+ . trim ( )
33+ . slice ( 0 , 1000 ) ; // hard cap length
34+
1935 const payload = {
20- rating : data . rating ,
21- feedback : data . feedback ,
36+ rating : Math . round ( data . rating ) ,
37+ feedback : normalizedFeedback ,
2238 ticket_id : data . ticketId || null ,
2339 } ;
2440
2541 const response = await fetch ( `${ siwaUrl } /v1/csat/saveCSATFeedback` , {
2642 method : "POST" ,
2743 headers : {
2844 "Content-Type" : "application/json" ,
29- "x-service-api-key" : process . env . SERVICE_AUTH_KEY_SIWA || "" ,
45+ "x-service-api-key" : apiKey ,
3046 } ,
3147 body : JSON . stringify ( payload ) ,
3248 } ) ;
0 commit comments