Skip to content

Commit 8dc35b5

Browse files
committed
bug fix
1 parent dfd38d1 commit 8dc35b5

File tree

3 files changed

+39
-87
lines changed

3 files changed

+39
-87
lines changed

src/app/PuzzleClient.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export default function PuzzleClient() {
253253
} catch (error) {
254254
console.error("Failed to send completion email:", error);
255255
}
256-
}, []);
256+
}, [gridSize, elapsedTime, moveCount]);
257257

258258
// Whenever tiles change, check if puzzle is solved
259259
useEffect(() => {

supabase/README.md

Lines changed: 0 additions & 79 deletions
This file was deleted.

supabase/functions/send-puzzle-completion-email/index.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ serve(async (req) => {
1919
// Get the request data
2020
const data: PuzzleCompletionData = await req.json();
2121

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+
2237
// Format the time
2338
const minutes = Math.floor(data.timeInSeconds / 60);
2439
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"}`;
2643

2744
// Your email service configuration
2845
const RESEND_EMAIL_SERVICE_API_KEY = Deno.env.get(
@@ -40,43 +57,57 @@ serve(async (req) => {
4057
}`;
4158
const body = `Completed the ${data.gridSize}x${data.gridSize} puzzle in ${timeFormatted} with ${data.moveCount} moves.`;
4259

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);
4562

63+
// Send the email using Resend
4664
const response = await fetch("https://api.resend.com/emails", {
4765
method: "POST",
4866
headers: {
4967
"Content-Type": "application/json",
5068
Authorization: `Bearer ${RESEND_EMAIL_SERVICE_API_KEY}`,
5169
},
5270
body: JSON.stringify({
53-
71+
from: "Brainsx3 Robots <[email protected]>",
5472
to: NOTIFICATION_EMAIL,
5573
subject: subject,
5674
text: body,
5775
}),
5876
});
5977

6078
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}`);
6382
}
6483

6584
// Return a success response
6685
return new Response(
6786
JSON.stringify({
6887
success: true,
6988
message: "Email notification sent successfully",
89+
data: {
90+
subject,
91+
body,
92+
recipient: NOTIFICATION_EMAIL,
93+
},
7094
}),
7195
{
7296
headers: { ...corsHeaders, "Content-Type": "application/json" },
7397
status: 200,
7498
}
7599
);
76100
} catch (error) {
101+
// Log the error
102+
console.error("Error in send-puzzle-completion-email:", error);
103+
77104
// Return an error response
78105
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+
}),
80111
{
81112
headers: { ...corsHeaders, "Content-Type": "application/json" },
82113
status: 500,

0 commit comments

Comments
 (0)