@@ -19,10 +19,27 @@ serve(async (req) => {
19
19
// Get the request data
20
20
const data : PuzzleCompletionData = await req . json ( ) ;
21
21
22
+ // Validate the data
23
+ console . log ( "Received puzzle completion data:" , JSON . stringify ( data ) ) ;
24
+
25
+ if ( ! data . gridSize || data . gridSize <= 0 ) {
26
+ throw new Error ( `Invalid grid size: ${ data . gridSize } ` ) ;
27
+ }
28
+
29
+ if ( data . timeInSeconds === undefined || data . timeInSeconds < 0 ) {
30
+ throw new Error ( `Invalid time: ${ data . timeInSeconds } ` ) ;
31
+ }
32
+
33
+ if ( data . moveCount === undefined || data . moveCount < 0 ) {
34
+ throw new Error ( `Invalid move count: ${ data . moveCount } ` ) ;
35
+ }
36
+
22
37
// Format the time
23
38
const minutes = Math . floor ( data . timeInSeconds / 60 ) ;
24
39
const seconds = data . timeInSeconds % 60 ;
25
- const timeFormatted = `${ minutes } minutes and ${ seconds } seconds` ;
40
+ const timeFormatted = `${ minutes } ${
41
+ minutes === 1 ? "minute" : "minutes"
42
+ } and ${ seconds } ${ seconds === 1 ? "second" : "seconds" } `;
26
43
27
44
// Your email service configuration
28
45
const RESEND_EMAIL_SERVICE_API_KEY = Deno . env . get (
@@ -40,43 +57,57 @@ serve(async (req) => {
40
57
} `;
41
58
const body = `Completed the ${ data . gridSize } x${ data . gridSize } puzzle in ${ timeFormatted } with ${ data . moveCount } moves.` ;
42
59
43
- // Send the email using your preferred email service
44
- // This example uses a generic fetch approach - you would replace this with your specific email service
60
+ console . log ( "Sending email with subject:" , subject ) ;
61
+ console . log ( "Email body:" , body ) ;
45
62
63
+ // Send the email using Resend
46
64
const response = await fetch ( "https://api.resend.com/emails" , {
47
65
method : "POST" ,
48
66
headers : {
49
67
"Content-Type" : "application/json" ,
50
68
Authorization : `Bearer ${ RESEND_EMAIL_SERVICE_API_KEY } ` ,
51
69
} ,
52
70
body : JSON . stringify ( {
53
-
71
+ from :
"Brainsx3 Robots < [email protected] > " ,
54
72
to : NOTIFICATION_EMAIL ,
55
73
subject : subject ,
56
74
text : body ,
57
75
} ) ,
58
76
} ) ;
59
77
60
78
if ( ! response . ok ) {
61
- const error = await response . text ( ) ;
62
- throw new Error ( `Failed to send email: ${ error } ` ) ;
79
+ const errorText = await response . text ( ) ;
80
+ console . error ( "Email service error:" , errorText ) ;
81
+ throw new Error ( `Failed to send email: ${ errorText } ` ) ;
63
82
}
64
83
65
84
// Return a success response
66
85
return new Response (
67
86
JSON . stringify ( {
68
87
success : true ,
69
88
message : "Email notification sent successfully" ,
89
+ data : {
90
+ subject,
91
+ body,
92
+ recipient : NOTIFICATION_EMAIL ,
93
+ } ,
70
94
} ) ,
71
95
{
72
96
headers : { ...corsHeaders , "Content-Type" : "application/json" } ,
73
97
status : 200 ,
74
98
}
75
99
) ;
76
100
} catch ( error ) {
101
+ // Log the error
102
+ console . error ( "Error in send-puzzle-completion-email:" , error ) ;
103
+
77
104
// Return an error response
78
105
return new Response (
79
- JSON . stringify ( { success : false , error : error . message } ) ,
106
+ JSON . stringify ( {
107
+ success : false ,
108
+ error : error instanceof Error ? error . message : String ( error ) ,
109
+ stack : error instanceof Error ? error . stack : undefined ,
110
+ } ) ,
80
111
{
81
112
headers : { ...corsHeaders , "Content-Type" : "application/json" } ,
82
113
status : 500 ,
0 commit comments