gigafibre-fsm/services/targo-hub/lib/email.js
louispaulb 320655b0a0 refactor: major cleanup — remove dead dispatch app, commit all backend code, extract client composables
- Remove apps/dispatch/ (100% replaced by ops dispatch module, unmaintained)
- Commit services/targo-hub/lib/ (24 modules, 6290 lines — was never tracked)
- Commit services/docuseal + services/legacy-db docker-compose configs
- Extract client app composables: useOTP, useAddressSearch, catalog data, format utils
- Refactor CartPage.vue 630→175 lines, CatalogPage.vue 375→95 lines
- Clean hardcoded credentials from config.js fallback values
- Add client portal: catalog, cart, checkout, OTP verification, address search
- Add ops: NetworkPage, AgentFlowsPage, ConversationPanel, UnifiedCreateModal
- Add ops composables: useBestTech, useConversations, usePermissions, useScanner
- Add field app: scanner composable, docker/nginx configs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 17:38:38 -04:00

122 lines
4.1 KiB
JavaScript

'use strict'
const cfg = require('./config')
const { log } = require('./helpers')
let _transporter = null
function getTransporter () {
if (_transporter) return _transporter
try {
const nodemailer = require('nodemailer')
if (cfg.SMTP_USER && cfg.SMTP_PASS) {
_transporter = nodemailer.createTransport({
host: cfg.SMTP_HOST,
port: cfg.SMTP_PORT,
secure: cfg.SMTP_SECURE,
auth: { user: cfg.SMTP_USER, pass: cfg.SMTP_PASS },
})
log(`Email transport configured: ${cfg.SMTP_HOST}:${cfg.SMTP_PORT} as ${cfg.SMTP_USER}`)
} else {
// Direct delivery (no SMTP relay) — sends directly to recipient MX
_transporter = nodemailer.createTransport({ direct: true, name: 'msg.gigafibre.ca' })
log('Email transport: direct MX delivery (no SMTP relay configured)')
}
} catch (e) {
log('Email transport init failed:', e.message)
return null
}
return _transporter
}
/**
* Send an HTML email with optional PDF attachment
* @param {object} opts
* @param {string} opts.to - Recipient email
* @param {string} opts.subject - Subject line
* @param {string} opts.html - HTML body
* @param {Buffer} [opts.pdfBuffer] - Optional PDF attachment
* @param {string} [opts.pdfFilename] - PDF filename
* @returns {Promise<boolean>} true if sent
*/
async function sendEmail (opts) {
const transport = getTransporter()
if (!transport) {
log('Cannot send email — no transport available')
return false
}
const mailOpts = {
from: cfg.MAIL_FROM,
to: opts.to,
subject: opts.subject,
html: opts.html,
attachments: [],
}
if (opts.pdfBuffer && opts.pdfFilename) {
mailOpts.attachments.push({
filename: opts.pdfFilename,
content: opts.pdfBuffer,
contentType: 'application/pdf',
})
}
try {
const info = await transport.sendMail(mailOpts)
log(`Email sent to ${opts.to}: ${info.messageId || 'OK'}`)
return true
} catch (e) {
log(`Email send failed to ${opts.to}: ${e.message}`)
return false
}
}
/**
* Send a quotation acceptance email with PDF attached
*/
async function sendQuotationEmail (opts) {
const { to, quotationName, acceptLink, pdfBuffer } = opts
const html = `<!DOCTYPE html>
<html><head><meta charset="utf-8"></head><body style="font-family:'Inter',system-ui,sans-serif;margin:0;padding:0;background:#f1f5f9">
<div style="max-width:580px;margin:0 auto;padding:24px">
<div style="background:white;border-radius:16px;overflow:hidden;box-shadow:0 4px 24px rgba(0,0,0,0.06)">
<div style="background:linear-gradient(135deg,#6366f1,#8b5cf6);color:white;padding:24px 28px">
<h1 style="margin:0;font-size:20px;font-weight:700">Votre devis est pr&ecirc;t</h1>
<p style="margin:6px 0 0;opacity:0.85;font-size:13px">Devis ${quotationName}</p>
</div>
<div style="padding:24px 28px">
<p style="color:#334155;font-size:15px;line-height:1.6;margin:0 0 20px">
Bonjour,<br><br>
Votre devis <b>${quotationName}</b> est pr&ecirc;t pour votre examen.
Cliquez sur le bouton ci-dessous pour le consulter et l'accepter.
</p>
<div style="text-align:center;margin:24px 0">
<a href="${acceptLink}" style="display:inline-block;background:#6366f1;color:white;padding:14px 32px;border-radius:10px;text-decoration:none;font-weight:700;font-size:15px">
Voir et accepter le devis
</a>
</div>
<p style="color:#64748b;font-size:13px;text-align:center;margin:16px 0 0">
${pdfBuffer ? 'Le PDF du devis est &eacute;galement joint &agrave; ce courriel.' : ''}
Ce lien est valide pour 7 jours.
</p>
</div>
<div style="border-top:1px solid #e2e8f0;padding:16px 28px;text-align:center">
<p style="color:#94a3b8;font-size:11px;margin:0">Gigafibre &mdash; Targo T&eacute;l&eacute;communications</p>
</div>
</div>
</div>
</body></html>`
return sendEmail({
to,
subject: `Devis ${quotationName} — Acceptation requise`,
html,
pdfBuffer: pdfBuffer || null,
pdfFilename: pdfBuffer ? `${quotationName}.pdf` : null,
})
}
module.exports = { sendEmail, sendQuotationEmail }