52c34cf63a
- edge function send-welcome-email: e-mail de boas-vindas ao DONO do tenant recem-provisionado (destinatario do JWT, SMTP global/sistema, defaults Mailpit). Best-effort, disparada fire-and-forget no OnboardingPage so no provisionamento novo. - vitrine: seed plan_public + bullets dos planos free (cartao "Gratis"); Landingpage passa a mostrar "Gratis para sempre" (isFreePlan) em vez de "—". - build OK Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
126 lines
5.9 KiB
TypeScript
126 lines
5.9 KiB
TypeScript
/*
|
|
|--------------------------------------------------------------------------
|
|
| Agência PSI — Edge Function: send-welcome-email (Freemium F2)
|
|
|--------------------------------------------------------------------------
|
|
| E-mail de boas-vindas ao DONO de um tenant recém-provisionado. Best-effort:
|
|
| nunca quebra o login/onboarding — se o SMTP falhar, só loga.
|
|
|
|
|
| • Destinatário derivado do JWT (não do body) — segurança.
|
|
| • Usa um SMTP GLOBAL/de sistema (env), não o canal do tenant (um tenant
|
|
| novo ainda não configurou notification_channels). Defaults = Mailpit local.
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
|
|
import { SmtpClient } from 'https://deno.land/x/smtp@v0.7.0/mod.ts'
|
|
|
|
const corsHeaders = {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
}
|
|
|
|
function json(body: unknown, status = 200) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
function welcomeHtml(name: string, tenantName: string, loginUrl: string): string {
|
|
const ola = name ? `Olá, <strong>${name}</strong>!` : 'Olá!'
|
|
return `
|
|
<div style="font-family: system-ui, sans-serif; max-width: 520px; margin: 0 auto; color: #1f2937;">
|
|
<p>${ola}</p>
|
|
<p>Seu ambiente <strong>${tenantName}</strong> está pronto. Sua conta gratuita já foi ativada — é só entrar e começar.</p>
|
|
<p style="margin: 24px 0;">
|
|
<a href="${loginUrl}" style="background:#10b981;color:#fff;padding:10px 18px;border-radius:8px;text-decoration:none;font-weight:600;">Acessar meu ambiente →</a>
|
|
</p>
|
|
<p style="color:#6b7280;font-size:13px;">No plano gratuito você já tem o essencial. Quando precisar de mais, é só clicar em <strong>Upgrade PRO</strong> dentro do sistema.</p>
|
|
<p style="color:#9ca3af;font-size:12px;margin-top:24px;">Agência PSI — gestão clínica sem ruído.</p>
|
|
</div>`
|
|
}
|
|
|
|
Deno.serve(async (req: Request) => {
|
|
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
|
|
if (req.method !== 'POST') return json({ ok: false, error: 'method_not_allowed' }, 405)
|
|
|
|
try {
|
|
const authHeader = req.headers.get('Authorization') || ''
|
|
const supabaseUrl = Deno.env.get('SUPABASE_URL')!
|
|
|
|
// cliente no contexto do usuário (resolve o JWT → destinatário)
|
|
const userClient = createClient(supabaseUrl, Deno.env.get('SUPABASE_ANON_KEY')!, {
|
|
global: { headers: { Authorization: authHeader } },
|
|
})
|
|
const { data: userData } = await userClient.auth.getUser()
|
|
const user = userData?.user
|
|
if (!user?.id || !user?.email) {
|
|
return json({ ok: false, error: 'no_session' }, 401)
|
|
}
|
|
|
|
const meta = (user.user_metadata || {}) as Record<string, string>
|
|
|
|
// nome do tenant: metadata OU consulta via admin
|
|
const admin = createClient(supabaseUrl, Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!)
|
|
let tenantName = String(meta.tenant_name || '').trim()
|
|
let ownerName = String(meta.display_name || '').trim()
|
|
if (!tenantName || !ownerName) {
|
|
const { data: member } = await admin
|
|
.from('tenant_members')
|
|
.select('tenant_id')
|
|
.eq('user_id', user.id)
|
|
.eq('status', 'active')
|
|
.order('created_at', { ascending: true })
|
|
.limit(1)
|
|
.maybeSingle()
|
|
if (member?.tenant_id) {
|
|
const { data: t } = await admin.from('tenants').select('name').eq('id', member.tenant_id).maybeSingle()
|
|
if (!tenantName) tenantName = t?.name || 'seu ambiente'
|
|
}
|
|
if (!ownerName) {
|
|
const { data: pr } = await admin.from('profiles').select('full_name').eq('id', user.id).maybeSingle()
|
|
ownerName = pr?.full_name || ''
|
|
}
|
|
}
|
|
tenantName = tenantName || 'seu ambiente'
|
|
|
|
// SMTP global de sistema (env) — defaults = Mailpit local
|
|
const host = Deno.env.get('SMTP_HOST') || 'mailpit'
|
|
const port = parseInt(Deno.env.get('SMTP_PORT') || '1025', 10)
|
|
const username = Deno.env.get('SMTP_USER') || 'test'
|
|
const password = Deno.env.get('SMTP_PASS') || 'test'
|
|
const fromEmail = Deno.env.get('SMTP_FROM') || 'no-reply@agenciapsi.local'
|
|
const fromName = Deno.env.get('SMTP_FROM_NAME') || 'Agência PSI'
|
|
const appUrl = (Deno.env.get('APP_URL') || 'http://localhost:5173').replace(/\/+$/, '')
|
|
const loginUrl = `${appUrl}/auth/login`
|
|
|
|
const subject = `Bem-vindo(a) — ${tenantName} está pronto`
|
|
const html = welcomeHtml(ownerName, tenantName, loginUrl)
|
|
const text = `${ownerName ? 'Olá, ' + ownerName + '!' : 'Olá!'}\n\nSeu ambiente ${tenantName} está pronto. Acesse: ${loginUrl}\n\nAgência PSI`
|
|
|
|
try {
|
|
const client = new SmtpClient()
|
|
const connectConfig = { hostname: host, port, username, password }
|
|
if (port === 465) await client.connectTLS(connectConfig)
|
|
else await client.connect(connectConfig)
|
|
await client.send({
|
|
from: `${fromName} <${fromEmail}>`,
|
|
to: user.email,
|
|
subject,
|
|
content: text,
|
|
html,
|
|
})
|
|
await client.close()
|
|
} catch (smtpErr) {
|
|
// best-effort: não quebra o onboarding
|
|
console.error('[send-welcome-email] SMTP falhou (ignorado):', smtpErr)
|
|
return json({ ok: false, error: 'smtp_failed' })
|
|
}
|
|
|
|
return json({ ok: true })
|
|
} catch (err) {
|
|
console.error('[send-welcome-email] fatal:', err)
|
|
return json({ ok: false, error: 'internal' }, 500)
|
|
}
|
|
})
|