Fix send-auth-otp: Remove notification_logs references

- Remove notification_logs table references (table doesn't exist)
- This was causing the function to crash after sending email
- Now the function should complete successfully
- Added better email payload logging
- Keep .env file for local development
This commit is contained in:
dwindown
2026-01-02 15:07:41 +07:00
parent fa1adcf291
commit 08e56a22d8
8 changed files with 421 additions and 31 deletions

View File

@@ -138,7 +138,6 @@ serve(async (req: Request) => {
hasFromName: !!settings.from_name,
hasFromEmail: !!settings.from_email,
platformName: settings.platform_name,
allSettings: JSON.stringify(settings)
});
// Use api_token (not mailketing_api_token)
@@ -148,6 +147,16 @@ serve(async (req: Request) => {
throw new Error('API token not found in notification_settings');
}
// Log email details (truncate HTML body for readability)
console.log('Email payload:', {
to: email,
from_name: settings.from_name || settings.platform_name || 'Admin',
from_email: settings.from_email || 'noreply@example.com',
subject: subject,
html_body_length: htmlBody.length,
html_body_preview: htmlBody.substring(0, 200),
});
const emailResponse = await fetch(`${supabaseUrl}/functions/v1/send-email-v2`, {
method: 'POST',
headers: {
@@ -173,17 +182,7 @@ serve(async (req: Request) => {
const emailResult = await emailResponse.json();
console.log('Email sent successfully:', emailResult);
// Log notification
await supabase
.from('notification_logs')
.insert({
user_id,
email: email,
notification_type: 'auth_email_verification',
status: 'sent',
provider: 'mailketing',
error_message: null,
});
// Note: notification_logs table doesn't exist, skipping logging
return new Response(
JSON.stringify({
@@ -196,25 +195,7 @@ serve(async (req: Request) => {
} catch (error: any) {
console.error("Error sending OTP:", error);
// Try to log error notification
try {
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
const supabase = createClient(supabaseUrl, supabaseServiceKey);
await supabase
.from('notification_logs')
.insert({
user_id: null,
email: null,
notification_type: 'auth_email_verification',
status: 'failed',
provider: 'mailketing',
error_message: error.message || 'Unknown error',
});
} catch (logError) {
console.error('Failed to log error:', logError);
}
// Note: notification_logs table doesn't exist, skipping error logging
return new Response(
JSON.stringify({

View File

@@ -0,0 +1,145 @@
-- ============================================================================
-- Fix Auth Email Verification Template - Remove YAML delimiters
-- ============================================================================
-- Update the auth_email_verification template with proper HTML
UPDATE notification_templates
SET
email_subject = 'Kode Verifikasi Email Anda - {platform_name}',
email_body_html = '<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Verifikasi Email</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 20px;
line-height: 1.6;
}
.container {
max-width: 600px;
margin: 0 auto;
background: white;
border: 3px solid #000;
box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.2);
}
.header {
background: #000;
color: #fff;
padding: 30px;
text-align: center;
}
.header h1 {
margin: 0;
font-size: 24px;
font-weight: 700;
}
.content {
padding: 30px;
}
.greeting {
font-size: 18px;
margin-bottom: 20px;
}
.message {
margin-bottom: 30px;
color: #333;
}
.otp-container {
background: #f9f9f9;
border: 2px dashed #000;
padding: 30px;
text-align: center;
margin: 30px 0;
}
.otp-code {
font-size: 36px;
font-weight: 700;
letter-spacing: 8px;
color: #000;
font-family: "Courier New", monospace;
margin: 20px 0;
}
.expiry {
font-size: 14px;
color: #666;
margin-top: 10px;
}
.instructions {
background: #fffbeb;
border-left: 4px solid #f59e0b;
padding: 15px;
margin: 20px 0;
font-size: 14px;
}
.footer {
background: #f9f9f9;
padding: 20px;
text-align: center;
font-size: 12px;
color: #666;
border-top: 2px solid #000;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🔐 Verifikasi Email</h1>
</div>
<div class="content">
<p class="greeting">Halo {nama},</p>
<p class="message">
Terima kasih telah mendaftar di <strong>{platform_name}</strong>!
Gunakan kode OTP berikut untuk memverifikasi alamat email Anda:
</p>
<div class="otp-container">
<div class="otp-code">{otp_code}</div>
<div class="expiry">⏰ Berlaku selama {expiry_minutes} menit</div>
</div>
<div class="instructions">
<strong>Cara menggunakan:</strong><br>
1. Salin kode 6 digit di atas<br>
2. Kembali ke halaman pendaftaran<br>
3. Masukkan kode tersebut pada form verifikasi
</div>
<p class="message" style="margin-top: 30px;">
Jika Anda tidak merasa mendaftar di {platform_name},
abaikan email ini dengan aman.
</p>
</div>
<div class="footer">
<p>Email ini dikirim ke {email}</p>
<p>© {year} {platform_name}. Semua hak dilindungi.</p>
</div>
</div>
</body>
</html>'
WHERE key = 'auth_email_verification';
-- Verify the update
SELECT
key,
name,
is_active,
email_subject,
LENGTH(email_body_html) as html_length,
SUBSTRING(email_body_html, 1, 100) as html_start,
CASE
WHEN email_body_html LIKE '%<!DOCTYPE%' THEN '✓ Has DOCTYPE'
WHEN email_body_html LIKE '%<html>%' THEN '✓ Has HTML tag'
WHEN email_body_html LIKE '%---%' THEN '✗ Still has YAML delimiters'
ELSE 'Unknown format'
END as template_status
FROM notification_templates
WHERE key = 'auth_email_verification';