The Map CSV migrated from the legacy ERP carries names with two common
defects: missing French accents (Stephane, Andre, Frederic), and
compound first names that were typed without a separator (Marcandre,
Mariejosee, Jeanphilippe). Sending an email "Bonjour stephane," instead
of "Bonjour Stéphane," reads as sloppy automation. Fix both at parse
time so the user sees the corrected names in Step 2 and can override
inline if the auto-cleaner got it wrong.
Backend (lib/campaigns.js):
- FR_NAME_FIXES — 100+ entry dictionary mapping lowercase no-accent
Québec first names to their canonical accented form (André, Stéphane,
Frédéric, Geneviève, Hélène, Joséée, etc.). Sourced from MIQ baby
names + older-generation curation.
- COMPOUND_PARTS — list of common name parts (jean, marie, anne, marc,
philippe, françois, etc.) that combine into QC compound first names.
When two parts appear concatenated with no separator, the cleaner
splits and hyphenates them. Example: "Marcandre" → ["marc","andre"]
→ "Marc-André" (dictionary then applies accent).
- titleCaseToken — proper Title Case respecting apostrophes (O'Brien,
L'Heureux) and hyphens (Marie-Ève). Uses \p{L} Unicode class so it
works on accented chars correctly.
- cleanName(raw) — full pipeline: trim → Title Case → dictionary
lookup per word → compound split fallback. Applied to firstname AND
lastname in parseMapCsv.
- nameWarning(name) — heuristic flag for cases the cleaner couldn't
confidently handle: digit in name, single letter, abnormally long
without separator (likely two stuck names not in COMPOUND_PARTS).
Returns a short FR description for the UI tooltip.
- parseMapCsv now returns firstname/lastname (cleaned) + firstname_raw/
lastname_raw (original from CSV) + cleaned_changed bool + name_warnings
per recipient. UI uses these to show before/after + flags.
UI (CampaignNewPage Step 2):
- New counter card "Noms à vérifier" — count of recipients with at least
one nameWarning. Only renders if > 0.
- Info banner above the recipients table:
"X nom(s) auto-corrigés (...) Y nom(s) suspects (...)"
- Per-row icons in the firstname + lastname columns:
• ⚠ amber WARNING — cleaner flagged this name as suspicious
(tooltip shows the reason: "deux prénoms collés", "contient un
chiffre", etc.)
• ✨ green AUTO_FIX_HIGH — auto-cleaner changed something at parse
time (tooltip shows the original raw value)
Both icons are tooltip-only — no action required.
- Click any name cell → q-popup-edit opens an inline input. Type the
correction, Enter saves. ESC cancels. This is the manual override
path for any name the auto-cleaner mishandled.
Tests (manual via end-to-end smoke against prod):
STEPHANE TREMBLAY → Stéphane Tremblay ✓ accent + Title Case
marie tremblay → Marie Tremblay ✓ Title Case only
Marcandre Boileau → Marc-André Boileau ✓ compound + accent
Jean Francois Lebrun → Jean François Lebrun ✓ accent only
Mariejosee Lapierre → Marie-Josée Lapierre ✓ compound + double accent
Andre LAPRISE → André Laprise ✓ both fixed
Helene St-Pierre → Hélène St-Pierre ✓ accent, hyphen preserved
Frederic O'Brien → Frédéric O'Brien ✓ accent, apostrophe preserved
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
993 lines
41 KiB
JavaScript
993 lines
41 KiB
JavaScript
'use strict'
|
||
// ─────────────────────────────────────────────────────────────────────────────
|
||
// Gift campaigns — backend for the ops UI.
|
||
//
|
||
// One campaign = one Mailjet blast of personalized French emails containing a
|
||
// Giftbit shortlink. Two CSVs come in (raw Map export + Giftbit shortlinks),
|
||
// we match each contact to an ERPNext Customer (email / phone / civic address),
|
||
// store the send list as JSON, and run the send in the background with live
|
||
// progress over SSE.
|
||
//
|
||
// Storage layout:
|
||
// data/campaigns/<campaign-id>.json
|
||
// {
|
||
// id, name, created_at, status: draft|sending|completed|failed,
|
||
// params: { amount, expiry, commitment_months, subject, from, template,
|
||
// throttle_ms, smtp_host, smtp_port },
|
||
// counters: { total, sent, failed, queued, opened, clicked, bounced },
|
||
// recipients: [{
|
||
// firstname, lastname, email, phone, civic_address, postal_code,
|
||
// gift_url, giftbit_uuid, gift_value_cents,
|
||
// customer_id, # null if unmatched
|
||
// customer_name, # display name from ERPNext
|
||
// match_method, # 'email' | 'phone' | 'civic' | null
|
||
// match_confidence, # 1.0 (exact) | 0.8 (postal+civic) | 0 (none)
|
||
// status, # pending | queued | sent | failed | clicked | bounced
|
||
// mailjet_uuid, # set when sent (used by webhook to update)
|
||
// error, sent_at, opened_at, clicked_at,
|
||
// excluded # true = skip on send
|
||
// }]
|
||
// }
|
||
//
|
||
// SSE topic: 'campaign:<id>' — emits 'recipient-update' on every status change
|
||
// and 'campaign-done' when send finishes.
|
||
// ─────────────────────────────────────────────────────────────────────────────
|
||
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
const crypto = require('crypto')
|
||
const cfg = require('./config')
|
||
const { log, json, parseBody } = require('./helpers')
|
||
const erp = require('./erp')
|
||
const email = require('./email')
|
||
const sse = require('./sse')
|
||
|
||
const DATA_DIR = path.join(__dirname, '..', 'data', 'campaigns')
|
||
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true })
|
||
|
||
// ── CSV utilities ────────────────────────────────────────────────────────────
|
||
// Same RFC-4180-ish parser as the CLI scripts. Handles quoted fields with
|
||
// embedded delimiters and escaped double-quotes. Delimiter auto-detect
|
||
// (comma / tab / pipe) based on the first line.
|
||
function parseCsv (text, opts = {}) {
|
||
const skipPreamble = !!opts.skipPreamble
|
||
let work = text.replace(/^/, '') // strip BOM
|
||
if (skipPreamble) work = work.replace(/^[^\n]*\n/, '') // drop title line
|
||
|
||
const sample = work.split(/\r?\n/, 1)[0] || ''
|
||
const delim = sample.includes('|') ? '|'
|
||
: sample.includes('\t') ? '\t'
|
||
: ','
|
||
const rows = []; let row = [], field = '', inQ = false
|
||
for (let i = 0; i < work.length; i++) {
|
||
const c = work[i]
|
||
if (inQ) {
|
||
if (c === '"' && work[i + 1] === '"') { field += '"'; i++ }
|
||
else if (c === '"') inQ = false
|
||
else field += c
|
||
} else {
|
||
if (c === '"') inQ = true
|
||
else if (c === delim) { row.push(field); field = '' }
|
||
else if (c === '\n' || c === '\r') {
|
||
if (field !== '' || row.length) { row.push(field); rows.push(row); row = []; field = '' }
|
||
if (c === '\r' && work[i + 1] === '\n') i++
|
||
} else field += c
|
||
}
|
||
}
|
||
if (field !== '' || row.length) { row.push(field); rows.push(row) }
|
||
if (!rows.length) return []
|
||
const header = rows[0].map(h => h.trim())
|
||
return rows.slice(1).filter(r => r.some(c => c !== '')).map(r => {
|
||
const o = {}; header.forEach((h, i) => { o[h] = (r[i] || '').trim() }); return o
|
||
})
|
||
}
|
||
|
||
// ── Address / name normalization (mirrors contacts_from_legacy.py) ──────────
|
||
const LOWER_WORDS = new Set(['de','du','des','la','le','les','au','aux','à',
|
||
'et','sur','en'])
|
||
const EMAIL_SPLIT = /\s*[;,]\s*/
|
||
|
||
// ── French Québécois first-name accent restoration ──────────────────────────
|
||
// Top ~100 names from MIQ Service Canada Québec baby names + manual additions
|
||
// for older generations (legacy customer base skews 40+). Lookup is on the
|
||
// LOWERCASE no-accent form → returns the properly accented version.
|
||
// Missing from this list: rare names, English names that don't need fixing,
|
||
// already-correctly-accented names (handled by Title Case alone).
|
||
const FR_NAME_FIXES = {
|
||
// É (acute)
|
||
'andre': 'André', 'andree': 'Andrée', 'aimee': 'Aimée',
|
||
'amelie': 'Amélie', 'angele': 'Angèle', 'audrey': 'Audrey',
|
||
'beatrice': 'Béatrice', 'benedict': 'Bénédict',
|
||
'cecile': 'Cécile', 'cedric': 'Cédric', 'celine': 'Céline', 'chloe': 'Chloé',
|
||
'clemence': 'Clémence', 'clement': 'Clément',
|
||
'desire': 'Désiré', 'desiree': 'Désirée',
|
||
'edith': 'Édith', 'edouard': 'Édouard', 'eliane': 'Éliane',
|
||
'elie': 'Élie', 'elisa': 'Élisa', 'elise': 'Élise',
|
||
'eloi': 'Éloi', 'emile': 'Émile', 'emilie': 'Émilie',
|
||
'emmanuel': 'Emmanuel', 'eric': 'Éric', 'ethan': 'Éthan', 'eugene': 'Eugène',
|
||
'felix': 'Félix', 'fleur': 'Fleur', 'francois': 'François', 'francoise': 'Françoise',
|
||
'frederic': 'Frédéric', 'frederique': 'Frédérique',
|
||
'gabrielle': 'Gabrielle', 'gaetan': 'Gaétan', 'gaetane': 'Gaétane',
|
||
'genevieve': 'Geneviève', 'gerald': 'Gérald', 'gerard': 'Gérard',
|
||
'helena': 'Helena', 'helene': 'Hélène', 'herve': 'Hervé',
|
||
'irene': 'Irène', 'jeremie': 'Jérémie', 'jeremy': 'Jérémie',
|
||
'jerome': 'Jérôme', 'jose': 'José', 'josee': 'Josée',
|
||
'leandre': 'Léandre', 'leon': 'Léon', 'leo': 'Léo', 'leonard': 'Léonard',
|
||
'magali': 'Magali', 'medard': 'Médard',
|
||
'melanie': 'Mélanie', 'michele': 'Michèle',
|
||
'noemie': 'Noémie', 'noel': 'Noël', 'noella': 'Noëlla',
|
||
'pamela': 'Paméla', 'pierre': 'Pierre',
|
||
'raphael': 'Raphaël', 'raphaele': 'Raphaëlle', 'regina': 'Régina',
|
||
'regine': 'Régine', 'rejean': 'Réjean', 'rejeanne': 'Réjeanne',
|
||
'remi': 'Rémi', 'rene': 'René', 'renee': 'Renée',
|
||
'salome': 'Salomé', 'sebastien': 'Sébastien', 'severin': 'Séverin',
|
||
'severine': 'Séverine', 'solange': 'Solange',
|
||
'stephane': 'Stéphane', 'stephanie': 'Stéphanie',
|
||
'theo': 'Théo', 'theodore': 'Théodore', 'therese': 'Thérèse',
|
||
'valerie': 'Valérie', 'veronique': 'Véronique', 'zoe': 'Zoé',
|
||
}
|
||
|
||
// Parts known to form compound first names in QC (Marie-André, Jean-Philippe,
|
||
// etc.). When two parts appear concatenated with no separator (e.g. "Marcandre"
|
||
// or "Mariejose"), we split + hyphenate them. Ordered roughly by frequency so
|
||
// the longest match wins on lookups.
|
||
const COMPOUND_PARTS = [
|
||
'jean', 'marie', 'anne', 'pierre', 'paul', 'louis', 'claude', 'marc',
|
||
'andre', 'philippe', 'francois', 'francoise', 'jose', 'josee',
|
||
'luc', 'olivier', 'antoine', 'sebastien', 'michel', 'francois',
|
||
'christian', 'henri', 'denis', 'rene', 'roger',
|
||
]
|
||
|
||
function splitCompoundName (lower) {
|
||
for (const a of COMPOUND_PARTS) {
|
||
if (!lower.startsWith(a)) continue
|
||
const rest = lower.slice(a.length)
|
||
if (!rest) continue
|
||
if (COMPOUND_PARTS.includes(rest)) {
|
||
return [a, rest]
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
// Title-case a single token. Handles apostrophes (O'Brien, L'Heureux) and
|
||
// hyphens (Marie-André). Returns input unchanged if empty.
|
||
function titleCaseToken (tok) {
|
||
if (!tok) return tok
|
||
return tok.replace(/(^|[\s\-'])([\p{L}])/gu, (_, sep, ch) => sep + ch.toUpperCase())
|
||
.replace(/(?<=[\p{L}])([\p{L}]+)/gu, m => m.toLowerCase())
|
||
}
|
||
|
||
// Clean a person name: trim, Title Case, accent-restore from dictionary,
|
||
// split known compound concatenations. Returns the cleaned name.
|
||
function cleanName (raw) {
|
||
if (!raw) return ''
|
||
let s = raw.trim()
|
||
if (!s) return ''
|
||
// First pass: Title Case (so 'MARC' → 'Marc', 'marc' → 'Marc')
|
||
s = s.split(/\s+/).map(titleCaseToken).join(' ')
|
||
|
||
// Apply dictionary fixes to each space-separated word
|
||
s = s.split(' ').map(word => {
|
||
// For hyphenated compounds, try each part separately
|
||
if (word.includes('-')) {
|
||
return word.split('-').map(p => {
|
||
const fix = FR_NAME_FIXES[p.toLowerCase()]
|
||
return fix || titleCaseToken(p)
|
||
}).join('-')
|
||
}
|
||
const lower = word.toLowerCase()
|
||
if (FR_NAME_FIXES[lower]) return FR_NAME_FIXES[lower]
|
||
// Try compound split: "Marcandre" → "Marc-Andre" → "Marc-André"
|
||
const parts = splitCompoundName(lower)
|
||
if (parts) {
|
||
return parts.map(p => FR_NAME_FIXES[p] || titleCaseToken(p)).join('-')
|
||
}
|
||
return word
|
||
}).join(' ')
|
||
|
||
return s
|
||
}
|
||
|
||
// Heuristics to flag names the cleaner may not have caught — surface to the
|
||
// user in the preview UI so they can manually verify / edit before sending.
|
||
// Returns null if the name looks fine, or a short FR string explaining why.
|
||
function nameWarning (name) {
|
||
if (!name) return null
|
||
const trimmed = name.trim()
|
||
if (!trimmed) return null
|
||
if (/\d/.test(trimmed)) return 'contient un chiffre'
|
||
if (trimmed.length === 1) return 'une seule lettre'
|
||
if (trimmed.length > 25) return 'trop long, peut-être plusieurs noms collés'
|
||
// After cleanName, common-name lookups should have hit. If we still see a
|
||
// name in the unaccented form, it's likely an uncommon name OR a typo.
|
||
// We can't tell which, so just inform the user.
|
||
const SUSPECT_PATTERNS = [
|
||
// Common unaccented forms that the dict should have caught — if they
|
||
// survive, the name is unusual and worth a glance
|
||
/^(ana|ari|cleo|elo|jul|leo|maw|oce|pen|sof|the|val)/i,
|
||
]
|
||
// Likely two names mashed (no separator, length 13-25, lowercase or with
|
||
// unexpected capitalisation pattern like 'Marcandre')
|
||
if (/^[\p{Lu}][\p{Ll}]+[\p{Ll}]{8,}$/u.test(trimmed) && !/[\s\-']/.test(trimmed)) {
|
||
return 'peut-être deux prénoms collés (vérifier)'
|
||
}
|
||
return null
|
||
}
|
||
|
||
function titleAddress (addr) {
|
||
if (!addr) return ''
|
||
return addr.split(/\s+/).map((word, i) => {
|
||
const lw = word.toLowerCase()
|
||
if (i > 0 && LOWER_WORDS.has(lw)) return lw
|
||
if (word.includes('-')) {
|
||
return word.split('-').map(c => {
|
||
const cl = c.toLowerCase()
|
||
return LOWER_WORDS.has(cl) ? cl : (c[0] || '').toUpperCase() + c.slice(1).toLowerCase()
|
||
}).join('-')
|
||
}
|
||
return (word[0] || '').toUpperCase() + word.slice(1).toLowerCase()
|
||
}).join(' ')
|
||
}
|
||
|
||
function normalizeEmail (raw) {
|
||
return (raw || '').trim().toLowerCase()
|
||
}
|
||
|
||
// Strip non-digit chars; treat as Canadian 10-digit if longer than 10
|
||
function normalizePhone (raw) {
|
||
const digits = (raw || '').replace(/\D/g, '')
|
||
if (digits.length === 11 && digits.startsWith('1')) return digits.slice(1)
|
||
if (digits.length === 10) return digits
|
||
return digits || null
|
||
}
|
||
|
||
// Canadian postal H1A 1B1 — uppercase, no internal space
|
||
function normalizePostal (raw) {
|
||
return (raw || '').replace(/\s+/g, '').toUpperCase().replace(/^([A-Z]\d[A-Z])(\d[A-Z]\d)$/, '$1 $2')
|
||
}
|
||
|
||
// Civic = numeric prefix + first non-empty street word. We compare prefix +
|
||
// first 5 chars of street to avoid false-positives from "Rue" vs "Route".
|
||
function normalizeCivic (addr) {
|
||
if (!addr) return null
|
||
const m = addr.trim().match(/^(\d+[A-Za-z]?)\s+(.+)$/)
|
||
if (!m) return null
|
||
const num = m[1].toUpperCase()
|
||
// Strip "Rue", "Route", "Boulevard" etc. — they're noise for matching
|
||
const street = m[2].replace(/^(rue|route|boulevard|boul\.?|avenue|av\.?|chemin|ch\.?|place|pl\.?)\s+/i, '').trim()
|
||
return num + '|' + street.toLowerCase().slice(0, 12)
|
||
}
|
||
|
||
// ── Map CSV parsing — port of contacts_from_legacy.py ────────────────────────
|
||
// The legacy export has a 1-line title preamble + pipe-delimited columns.
|
||
// Columns we care about:
|
||
// "nom au compte" — billing contact name (preferred)
|
||
// "nom à l'adresse" — service-address name (fallback)
|
||
// "email au compte" — billing email (preferred)
|
||
// "email à l'adresse" — service-address email (fallback)
|
||
// "telephone au compte" or "telephone à l'adresse" — phone for matching
|
||
// "adresse dans F" — street address
|
||
// "code postal au compte" or "code postal à l'adresse" — postal
|
||
// "id emplacement" — legacy_delivery_id (note: only 25% resolves)
|
||
function parseMapCsv (text, multi = 'first') {
|
||
const rows = parseCsv(text, { skipPreamble: true })
|
||
const contacts = []
|
||
const seen = new Set()
|
||
let skippedNoEmail = 0, skippedNoName = 0
|
||
|
||
for (const r of rows) {
|
||
// Pull emails from either source column
|
||
let rawEmails = (r['email au compte'] || r["email à l'adresse"] || '').trim()
|
||
if (!rawEmails) { skippedNoEmail++; continue }
|
||
const emails = rawEmails.split(EMAIL_SPLIT)
|
||
.map(e => normalizeEmail(e))
|
||
.filter(e => e.includes('@') && e.split('@')[1]?.includes('.'))
|
||
if (!emails.length) { skippedNoEmail++; continue }
|
||
|
||
const sendEmails = multi === 'split' ? emails
|
||
: multi === 'skip' ? (emails.length > 1 ? [] : emails)
|
||
: emails.slice(0, 1)
|
||
if (!sendEmails.length) continue
|
||
|
||
const full = (r['nom au compte'] || r["nom à l'adresse"] || '').trim()
|
||
if (!full) { skippedNoName++; continue }
|
||
const parts = full.split(/\s+/, 2)
|
||
const firstnameRaw = parts[0] || ''
|
||
const lastnameRaw = parts.length > 1 ? full.slice(parts[0].length).trim() : ''
|
||
|
||
// Auto-clean: Title Case, accent restoration from QC dictionary, compound
|
||
// name detection. The user sees these cleaned versions in Step 2 and can
|
||
// edit inline. Original raw values are kept on the recipient (firstname_raw
|
||
// + lastname_raw) so we know whether the auto-cleaner touched anything.
|
||
const firstname = cleanName(firstnameRaw)
|
||
const lastname = cleanName(lastnameRaw)
|
||
const name_warnings = {
|
||
firstname: nameWarning(firstname),
|
||
lastname: nameWarning(lastname),
|
||
}
|
||
const cleaned_changed = (firstname !== firstnameRaw || lastname !== lastnameRaw)
|
||
|
||
const civic_address = titleAddress(r['adresse dans F'] || '')
|
||
const postal_code = normalizePostal(r['code postal au compte'] || r["code postal à l'adresse"] || '')
|
||
const phone = normalizePhone(r['telephone au compte'] || r["telephone à l'adresse"] || '')
|
||
|
||
for (const em of sendEmails) {
|
||
if (seen.has(em)) continue
|
||
seen.add(em)
|
||
contacts.push({
|
||
firstname, lastname,
|
||
firstname_raw: firstnameRaw, lastname_raw: lastnameRaw,
|
||
cleaned_changed,
|
||
name_warnings,
|
||
email: em,
|
||
phone,
|
||
civic_address,
|
||
postal_code,
|
||
})
|
||
}
|
||
}
|
||
return { contacts, skipped: { no_email: skippedNoEmail, no_name: skippedNoName, total_rows: rows.length } }
|
||
}
|
||
|
||
// ── Giftbit CSV parsing ──────────────────────────────────────────────────────
|
||
// Two supported formats:
|
||
//
|
||
// 1. "Link Order" export (headerless, one URL per line):
|
||
// http://gft.link/4kpZMApLK4B
|
||
// http://gft.link/Dn2cb27xYJ8
|
||
//
|
||
// This is what Giftbit ships when you pre-buy N gift links upfront without
|
||
// targeting specific recipients. Each line = one redeemable shortlink. The
|
||
// recipient mapping is done entirely on our side (Map CSV ↔ link by row).
|
||
//
|
||
// 2. "Campaign" export (with header row, multiple columns):
|
||
// firstname,lastname,email,gift_url,giftbit_uuid,gift_value_cents,internal_id
|
||
// Alice,Tremblay,alice@x.com,https://...,uuid-001,5000,ACC-1
|
||
//
|
||
// This is what create_giftbit_campaign.js produces (and what Giftbit
|
||
// sends when you use their Campaign API with delivery_type=SHORTLINK).
|
||
function parseGiftbitCsv (text) {
|
||
const cleaned = text.replace(/^/, '').trim()
|
||
if (!cleaned) return []
|
||
|
||
// Detect headerless one-URL-per-line format: first non-empty line starts
|
||
// with http:// or https:// and has no comma/pipe/tab separators.
|
||
const firstLine = cleaned.split(/\r?\n/, 1)[0].trim()
|
||
if (/^https?:\/\/\S+$/.test(firstLine) && !firstLine.includes(',') && !firstLine.includes('\t') && !firstLine.includes('|')) {
|
||
return cleaned.split(/\r?\n/)
|
||
.map(l => l.trim())
|
||
.filter(l => /^https?:\/\//.test(l))
|
||
.map(url => ({
|
||
gift_url: url,
|
||
giftbit_uuid: '',
|
||
gift_value_cents: 0,
|
||
email: '',
|
||
firstname: '',
|
||
lastname: '',
|
||
internal_id: '',
|
||
}))
|
||
}
|
||
|
||
// Otherwise treat as CSV with header row
|
||
const rows = parseCsv(text)
|
||
if (!rows.length) return []
|
||
const keys = Object.keys(rows[0])
|
||
const urlCandidates = ['gift_url','gift link','gift_link','url','link','shortlink',
|
||
'redemption_url','gift','giftbit_link','campaign_link']
|
||
let urlCol = null
|
||
for (const c of urlCandidates) {
|
||
const hit = keys.find(k => k.toLowerCase().replace(/\s+/g, '_') === c)
|
||
if (hit) { urlCol = hit; break }
|
||
}
|
||
if (!urlCol) {
|
||
for (const k of keys) {
|
||
if ((rows[0][k] || '').match(/^https?:\/\//)) { urlCol = k; break }
|
||
}
|
||
}
|
||
if (!urlCol) return []
|
||
|
||
return rows.map(r => ({
|
||
gift_url: r[urlCol],
|
||
giftbit_uuid: r.giftbit_uuid || r.uuid || r.gift_id || '',
|
||
gift_value_cents: parseInt(r.gift_value_cents || r.value_cents || r.amount || '0', 10) || 0,
|
||
email: normalizeEmail(r.email || ''),
|
||
firstname: r.firstname || '',
|
||
lastname: r.lastname || '',
|
||
internal_id: r.internal_id || '',
|
||
}))
|
||
}
|
||
|
||
// ── Customer matching against ERPNext ────────────────────────────────────────
|
||
// Strategy: email → phone → civic+postal. First hit wins. Confidence:
|
||
// 1.0 = exact email match
|
||
// 0.9 = exact phone match
|
||
// 0.8 = civic + postal_code on a Service Location (then customer link)
|
||
// 0 = unmatched
|
||
async function matchCustomer (recipient) {
|
||
// 1) Email match on Customer.email_id (most reliable)
|
||
if (recipient.email) {
|
||
try {
|
||
const r = await erp.list('Customer', {
|
||
fields: ['name', 'customer_name', 'email_id', 'mobile_no', 'language'],
|
||
filters: [['email_id', '=', recipient.email]],
|
||
limit: 1,
|
||
})
|
||
if (r && r.length) {
|
||
return { customer_id: r[0].name, customer_name: r[0].customer_name,
|
||
language: r[0].language || 'fr',
|
||
match_method: 'email', match_confidence: 1.0 }
|
||
}
|
||
} catch (e) { log('match email error:', e.message) }
|
||
}
|
||
|
||
// 2) Phone match
|
||
if (recipient.phone) {
|
||
try {
|
||
const r = await erp.list('Customer', {
|
||
fields: ['name', 'customer_name', 'mobile_no', 'language'],
|
||
filters: [['mobile_no', 'like', '%' + recipient.phone.slice(-7) + '%']],
|
||
limit: 5,
|
||
})
|
||
const hit = (r || []).find(c => normalizePhone(c.mobile_no) === recipient.phone)
|
||
if (hit) {
|
||
return { customer_id: hit.name, customer_name: hit.customer_name,
|
||
language: hit.language || 'fr',
|
||
match_method: 'phone', match_confidence: 0.9 }
|
||
}
|
||
} catch (e) { log('match phone error:', e.message) }
|
||
}
|
||
|
||
// 3) Civic + postal on Service Location → Customer (+ fetch Customer.language separately)
|
||
if (recipient.civic_address && recipient.postal_code) {
|
||
try {
|
||
const civic = normalizeCivic(recipient.civic_address)
|
||
if (civic) {
|
||
const [num, streetPrefix] = civic.split('|')
|
||
const r = await erp.list('Service Location', {
|
||
fields: ['name', 'address_line', 'postal_code', 'customer', 'customer_name'],
|
||
filters: [
|
||
['postal_code', '=', recipient.postal_code],
|
||
['address_line', 'like', num + '%'],
|
||
],
|
||
limit: 10,
|
||
})
|
||
const hit = (r || []).find(sl => {
|
||
const slCivic = normalizeCivic(sl.address_line)
|
||
return slCivic && slCivic.startsWith(num + '|') &&
|
||
slCivic.split('|')[1]?.startsWith(streetPrefix.slice(0, 5))
|
||
})
|
||
if (hit && hit.customer) {
|
||
// Service Location doesn't carry language — fetch it from the linked Customer
|
||
let language = 'fr'
|
||
try {
|
||
const c = await erp.list('Customer', {
|
||
fields: ['language'], filters: [['name', '=', hit.customer]], limit: 1,
|
||
})
|
||
if (c?.[0]?.language) language = c[0].language
|
||
} catch {}
|
||
return { customer_id: hit.customer, customer_name: hit.customer_name || hit.customer,
|
||
language,
|
||
match_method: 'civic', match_confidence: 0.8 }
|
||
}
|
||
}
|
||
} catch (e) { log('match civic error:', e.message) }
|
||
}
|
||
|
||
// Unmatched: default to French (93% of customer base)
|
||
return { customer_id: null, customer_name: null, language: 'fr',
|
||
match_method: null, match_confidence: 0 }
|
||
}
|
||
|
||
// ── Storage helpers ──────────────────────────────────────────────────────────
|
||
function campaignPath (id) {
|
||
// Defensive: prevent path traversal — IDs are uuid-like; reject anything else
|
||
if (!/^[a-zA-Z0-9_-]+$/.test(id)) throw new Error('invalid campaign id')
|
||
return path.join(DATA_DIR, id + '.json')
|
||
}
|
||
|
||
function listCampaigns () {
|
||
if (!fs.existsSync(DATA_DIR)) return []
|
||
return fs.readdirSync(DATA_DIR)
|
||
.filter(f => f.endsWith('.json'))
|
||
.map(f => {
|
||
try {
|
||
const c = JSON.parse(fs.readFileSync(path.join(DATA_DIR, f), 'utf8'))
|
||
return { id: c.id, name: c.name, created_at: c.created_at,
|
||
status: c.status, counters: c.counters,
|
||
total: (c.recipients || []).length }
|
||
} catch { return null }
|
||
})
|
||
.filter(Boolean)
|
||
.sort((a, b) => (b.created_at || '').localeCompare(a.created_at || ''))
|
||
}
|
||
|
||
function loadCampaign (id) {
|
||
const p = campaignPath(id)
|
||
if (!fs.existsSync(p)) return null
|
||
return JSON.parse(fs.readFileSync(p, 'utf8'))
|
||
}
|
||
|
||
function saveCampaign (campaign) {
|
||
// Recompute counters from recipients every save — single source of truth
|
||
const c = { ...campaign }
|
||
c.counters = (c.recipients || []).reduce((acc, r) => {
|
||
acc[r.status] = (acc[r.status] || 0) + 1
|
||
return acc
|
||
}, { total: (c.recipients || []).length })
|
||
fs.writeFileSync(campaignPath(c.id), JSON.stringify(c, null, 2))
|
||
return c
|
||
}
|
||
|
||
function newCampaignId () {
|
||
const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, '')
|
||
const slug = crypto.randomBytes(3).toString('hex')
|
||
return `cmp-${stamp}-${slug}`
|
||
}
|
||
|
||
// ── Template rendering (mirrors send_gift_campaign.js) ───────────────────────
|
||
function renderTemplate (tpl, vars) {
|
||
// Section blocks first: {{#var}}...{{/var}} kept if var truthy
|
||
tpl = tpl.replace(/\{\{\s*#\s*(\w+)\s*\}\}([\s\S]*?)\{\{\s*\/\s*\1\s*\}\}/g,
|
||
(_, k, body) => (vars[k] ? body : ''))
|
||
return tpl.replace(/\{\{\s*(\w+)\s*\}\}/g, (_, k) => {
|
||
const v = vars[k]
|
||
return v == null ? '' : String(v)
|
||
})
|
||
}
|
||
|
||
// Templates are bundled inside the hub at services/targo-hub/templates/.
|
||
// Keep them in sync with scripts/campaigns/templates/ for the CLI use case.
|
||
const TEMPLATES_DIR = path.resolve(__dirname, '..', 'templates')
|
||
const DEFAULT_TEMPLATE = path.join(TEMPLATES_DIR, 'gift-email-fr.html')
|
||
|
||
// Allow-list templates that can be edited via the API. Naming convention:
|
||
// gift-email-<lang> — the recipient's language drives which one gets used
|
||
// at send time. Adding a new lang = drop a new .html here + add to this list.
|
||
const EDITABLE_TEMPLATES = ['gift-email-fr', 'gift-email-en']
|
||
|
||
// Resolve the template path for a given recipient language. Falls back to
|
||
// the French version (most of the customer base) if the language-specific
|
||
// file doesn't exist. Per-campaign override via params.template_path skips
|
||
// language routing entirely.
|
||
function templateForLanguage (lang) {
|
||
const safe = (lang || 'fr').toLowerCase().split('-')[0] // 'fr-CA' → 'fr'
|
||
const candidate = path.join(TEMPLATES_DIR, `gift-email-${safe}.html`)
|
||
if (fs.existsSync(candidate)) return candidate
|
||
return DEFAULT_TEMPLATE
|
||
}
|
||
|
||
function templatePath (name) {
|
||
if (!EDITABLE_TEMPLATES.includes(name)) {
|
||
throw new Error(`template not editable: ${name}`)
|
||
}
|
||
return path.join(TEMPLATES_DIR, name + '.html')
|
||
}
|
||
|
||
function listEditableTemplates () {
|
||
return EDITABLE_TEMPLATES.map(name => {
|
||
const p = path.join(TEMPLATES_DIR, name + '.html')
|
||
let size = 0, modified = null
|
||
try {
|
||
const stat = fs.statSync(p)
|
||
size = stat.size
|
||
modified = stat.mtime.toISOString()
|
||
} catch {}
|
||
return { name, size, modified }
|
||
})
|
||
}
|
||
|
||
function readTemplate (tplPath) {
|
||
const p = tplPath || DEFAULT_TEMPLATE
|
||
if (!fs.existsSync(p)) {
|
||
throw new Error(`Template not found: ${p}`)
|
||
}
|
||
return fs.readFileSync(p, 'utf8')
|
||
}
|
||
|
||
// ── Send-async worker ────────────────────────────────────────────────────────
|
||
// Iterates recipients (not excluded, status=pending), sends each via the
|
||
// existing email lib, broadcasts each status change over SSE. Throttle is
|
||
// configurable. Failures stop only that recipient — the loop continues.
|
||
//
|
||
// Runs in the background — caller fires this and returns immediately.
|
||
const activeWorkers = new Set()
|
||
|
||
async function sendCampaignAsync (id) {
|
||
if (activeWorkers.has(id)) {
|
||
log(`campaign ${id} already sending`)
|
||
return
|
||
}
|
||
activeWorkers.add(id)
|
||
const topic = `campaign:${id}`
|
||
|
||
try {
|
||
let campaign = loadCampaign(id)
|
||
if (!campaign) throw new Error(`campaign ${id} not found`)
|
||
campaign.status = 'sending'
|
||
campaign.send_started_at = new Date().toISOString()
|
||
campaign = saveCampaign(campaign)
|
||
sse.broadcast(topic, 'campaign-status', { id, status: 'sending' })
|
||
|
||
const p = campaign.params || {}
|
||
const throttle = parseInt(p.throttle_ms || 600, 10)
|
||
// Pre-load templates by language to avoid re-reading from disk on every
|
||
// recipient. Cache map keyed by resolved template path.
|
||
const tplCache = new Map()
|
||
const getTpl = (lang) => {
|
||
const tplPath = p.template_path || templateForLanguage(lang)
|
||
if (!tplCache.has(tplPath)) {
|
||
tplCache.set(tplPath, fs.readFileSync(tplPath, 'utf8'))
|
||
}
|
||
return tplCache.get(tplPath)
|
||
}
|
||
|
||
for (let i = 0; i < campaign.recipients.length; i++) {
|
||
const r = campaign.recipients[i]
|
||
if (r.excluded || r.status !== 'pending') continue
|
||
|
||
// Mark queued so the UI shows movement immediately
|
||
r.status = 'queued'
|
||
saveCampaign(campaign)
|
||
sse.broadcast(topic, 'recipient-update', { i, recipient: r })
|
||
|
||
const lang = (r.language || 'fr').toLowerCase().split('-')[0]
|
||
const tplText = getTpl(lang)
|
||
const vars = {
|
||
firstname: r.firstname || (lang === 'en' ? 'dear customer' : 'cher client'),
|
||
lastname: r.lastname || '',
|
||
email: r.email,
|
||
description: r.civic_address || '',
|
||
gift_url: r.gift_url,
|
||
amount: p.amount || '50 $',
|
||
expiry: p.expiry || '',
|
||
commitment_months: p.commitment_months || '3',
|
||
year: new Date().getFullYear(),
|
||
}
|
||
const html = renderTemplate(tplText, vars)
|
||
const toName = `${r.firstname || ''} ${r.lastname || ''}`.trim()
|
||
const to = toName ? `"${toName}" <${r.email}>` : r.email
|
||
|
||
// Set Mailjet CustomID = campaign_id:recipient_index so the Event API
|
||
// webhook events can be matched back to a specific recipient row.
|
||
// Mailjet echoes this value in every event under the CustomID field;
|
||
// SMTP messageId alone is not a reliable join key (different ID space).
|
||
const customId = `${id}:${i}`
|
||
r.mailjet_custom_id = customId
|
||
|
||
// email.sendEmail returns the nodemailer info object on success
|
||
// (truthy, with .messageId), or `false` on failure (error logged in
|
||
// lib/email.js). It doesn't throw. We treat falsy = failed.
|
||
let sendRes
|
||
try {
|
||
sendRes = await email.sendEmail({
|
||
to,
|
||
subject: p.subject || 'Un cadeau pour vous, de la part de TARGO',
|
||
html,
|
||
from: p.from || cfg.MAIL_FROM,
|
||
headers: { 'X-MJ-CustomID': customId },
|
||
})
|
||
} catch (e) {
|
||
sendRes = false
|
||
r.error = String(e.message || e).slice(0, 500)
|
||
}
|
||
if (sendRes && sendRes.messageId !== undefined) {
|
||
r.mailjet_uuid = sendRes.messageId || null // SMTP Message-ID for reference
|
||
r.status = 'sent'
|
||
r.sent_at = new Date().toISOString()
|
||
r.error = null
|
||
} else {
|
||
r.status = 'failed'
|
||
if (!r.error) r.error = 'SMTP send returned false (see hub logs)'
|
||
log(`campaign ${id} recipient ${i} failed:`, r.error)
|
||
}
|
||
|
||
saveCampaign(campaign)
|
||
sse.broadcast(topic, 'recipient-update', { i, recipient: r })
|
||
|
||
if (throttle > 0) await new Promise(rs => setTimeout(rs, throttle))
|
||
}
|
||
|
||
campaign = loadCampaign(id)
|
||
campaign.status = 'completed'
|
||
campaign.send_completed_at = new Date().toISOString()
|
||
campaign = saveCampaign(campaign)
|
||
sse.broadcast(topic, 'campaign-done', { id, counters: campaign.counters })
|
||
log(`campaign ${id} done — ${campaign.counters.sent || 0} sent, ${campaign.counters.failed || 0} failed`)
|
||
} catch (e) {
|
||
log(`campaign ${id} worker failed:`, e.message)
|
||
try {
|
||
const c = loadCampaign(id)
|
||
if (c) {
|
||
c.status = 'failed'
|
||
c.error = String(e.message || e)
|
||
saveCampaign(c)
|
||
sse.broadcast(topic, 'campaign-done', { id, error: c.error })
|
||
}
|
||
} catch {}
|
||
} finally {
|
||
activeWorkers.delete(id)
|
||
}
|
||
}
|
||
|
||
// ── Mailjet webhook receiver ─────────────────────────────────────────────────
|
||
// Mailjet's Event API posts a JSON array of events:
|
||
// [{"event": "sent"|"open"|"click"|"bounce"|"blocked"|"spam"|"unsub",
|
||
// "MessageID": 1234, "CustomID": "<our reference>",
|
||
// "time": 1234567890, "email": "...", "Payload": "..."}, ...]
|
||
// We match by mailjet_uuid (== MessageID) and update status across all
|
||
// campaigns. We don't know which campaign a given event belongs to without
|
||
// scanning, but for the volumes we're dealing with (a few campaigns × ~200
|
||
// recipients each) scanning all open JSON files per webhook is fine.
|
||
function mailjetEventToStatus (event) {
|
||
switch ((event || '').toLowerCase()) {
|
||
case 'sent': return null // already 'sent' from SMTP — informational
|
||
case 'open': return 'opened'
|
||
case 'click': return 'clicked'
|
||
case 'bounce':
|
||
case 'hardbounce':
|
||
case 'softbounce': return 'bounced'
|
||
case 'blocked':
|
||
case 'spam':
|
||
case 'unsub': return 'failed'
|
||
default: return null
|
||
}
|
||
}
|
||
|
||
function applyWebhookEvent (ev) {
|
||
const newStatus = mailjetEventToStatus(ev.event)
|
||
if (!newStatus) return false
|
||
// Primary join key: CustomID = "<campaign-id>:<recipient-index>" which we
|
||
// injected on send via X-MJ-CustomID. Falling back to MessageID for events
|
||
// that might predate the CustomID rollout.
|
||
const customId = String(ev.CustomID || ev.custom_id || '')
|
||
const msgId = String(ev.MessageID || ev.message_id || '')
|
||
|
||
// Fast path: parse CustomID to skip the campaign scan entirely
|
||
if (customId && customId.includes(':')) {
|
||
const [campId, idxStr] = customId.split(':')
|
||
const idx = parseInt(idxStr, 10)
|
||
const c = loadCampaign(campId)
|
||
if (c && c.recipients && c.recipients[idx]) {
|
||
const r = c.recipients[idx]
|
||
r.status = newStatus
|
||
if (newStatus === 'opened') r.opened_at = new Date((ev.time || 0) * 1000).toISOString()
|
||
if (newStatus === 'clicked') r.clicked_at = new Date((ev.time || 0) * 1000).toISOString()
|
||
if (newStatus === 'bounced' || newStatus === 'failed') {
|
||
r.error = ev.error || ev.error_related_to || ev.event
|
||
}
|
||
saveCampaign(c)
|
||
sse.broadcast(`campaign:${c.id}`, 'recipient-update', { i: idx, recipient: r })
|
||
return true
|
||
}
|
||
}
|
||
|
||
// Fallback: scan all campaigns for a recipient matching msgId (slower)
|
||
if (!msgId) return false
|
||
for (const meta of listCampaigns()) {
|
||
const c = loadCampaign(meta.id)
|
||
if (!c) continue
|
||
for (let i = 0; i < (c.recipients || []).length; i++) {
|
||
const r = c.recipients[i]
|
||
if (String(r.mailjet_uuid) !== msgId) continue
|
||
r.status = newStatus
|
||
if (newStatus === 'opened') r.opened_at = new Date((ev.time || 0) * 1000).toISOString()
|
||
if (newStatus === 'clicked') r.clicked_at = new Date((ev.time || 0) * 1000).toISOString()
|
||
if (newStatus === 'bounced' || newStatus === 'failed') {
|
||
r.error = ev.error || ev.error_related_to || ev.event
|
||
}
|
||
saveCampaign(c)
|
||
sse.broadcast(`campaign:${c.id}`, 'recipient-update', { i, recipient: r })
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// ── HTTP routing ─────────────────────────────────────────────────────────────
|
||
async function handle (req, res, method, path) {
|
||
// POST /campaigns/parse — preview matched send list (no save)
|
||
// Returns:
|
||
// - recipients[]: contacts paired with a gift_url (ready to send)
|
||
// - unpaired_contacts[]: contacts with no matching gift (won't be sent —
|
||
// these are surfaced so the user can decide whether to acquire more
|
||
// gift links and re-upload, or proceed anyway)
|
||
// - unused_gifts[]: gift URLs with no matching contact (lost capacity —
|
||
// surfaced so the user knows the imbalance)
|
||
if (path === '/campaigns/parse' && method === 'POST') {
|
||
const body = await parseBody(req)
|
||
const { map_csv, giftbit_csv, multi } = body || {}
|
||
if (!map_csv || !giftbit_csv) {
|
||
return json(res, 400, { error: 'map_csv and giftbit_csv required' })
|
||
}
|
||
const { contacts, skipped } = parseMapCsv(map_csv, multi || 'first')
|
||
const gifts = parseGiftbitCsv(giftbit_csv)
|
||
|
||
// Match contacts ↔ gifts (by row order, fallback to email when Giftbit
|
||
// echoed back our recipient email in their export)
|
||
const recipients = []
|
||
const n = Math.min(contacts.length, gifts.length)
|
||
for (let i = 0; i < n; i++) {
|
||
const c = contacts[i]; const g = gifts[i]
|
||
const giftByEmail = gifts.find(gg => normalizeEmail(gg.email) === c.email)
|
||
const gift = giftByEmail || g
|
||
const match = await matchCustomer(c)
|
||
recipients.push({
|
||
row_index: i + 1,
|
||
...c,
|
||
gift_url: gift.gift_url,
|
||
giftbit_uuid: gift.giftbit_uuid,
|
||
gift_value_cents: gift.gift_value_cents,
|
||
...match,
|
||
status: 'pending',
|
||
excluded: false,
|
||
})
|
||
}
|
||
|
||
// Capture the imbalance: leftover contacts have no gift, leftover gifts
|
||
// have no contact. Returning them as arrays (not just counts) lets the
|
||
// UI render them so the user makes an informed decision.
|
||
const unpaired_contacts = contacts.slice(n).map((c, idx) => ({
|
||
row_index: n + idx + 1,
|
||
...c,
|
||
}))
|
||
const unused_gifts = gifts.slice(n).map((g, idx) => ({
|
||
row_index: n + idx + 1,
|
||
...g,
|
||
}))
|
||
|
||
return json(res, 200, {
|
||
recipients,
|
||
unpaired_contacts,
|
||
unused_gifts,
|
||
// Kept for backward compat with any older callers
|
||
leftover_gifts: unused_gifts.length,
|
||
leftover_contacts: unpaired_contacts.length,
|
||
skipped,
|
||
})
|
||
}
|
||
|
||
// POST /campaigns — create from a parsed send list
|
||
if (path === '/campaigns' && method === 'POST') {
|
||
const body = await parseBody(req)
|
||
const { name, params, recipients } = body || {}
|
||
if (!Array.isArray(recipients) || !recipients.length) {
|
||
return json(res, 400, { error: 'recipients[] required' })
|
||
}
|
||
const id = newCampaignId()
|
||
const campaign = {
|
||
id,
|
||
name: name || `Campagne ${id}`,
|
||
created_at: new Date().toISOString(),
|
||
status: 'draft',
|
||
params: params || {},
|
||
recipients: recipients.map(r => ({ ...r, status: r.status || 'pending' })),
|
||
}
|
||
const saved = saveCampaign(campaign)
|
||
return json(res, 200, saved)
|
||
}
|
||
|
||
// GET /campaigns — list summaries
|
||
if (path === '/campaigns' && method === 'GET') {
|
||
return json(res, 200, { campaigns: listCampaigns() })
|
||
}
|
||
|
||
// ── Template CRUD (for the GrapesJS editor in the ops UI) ─────────────────
|
||
// ORDER MATTERS: these template routes must be BEFORE the /campaigns/:id
|
||
// wildcard below, otherwise paths like /campaigns/templates get matched
|
||
// by the wildcard as if "templates" were a campaign ID.
|
||
|
||
// GET /campaigns/templates — list editable templates with metadata
|
||
if (path === '/campaigns/templates' && method === 'GET') {
|
||
return json(res, 200, { templates: listEditableTemplates() })
|
||
}
|
||
|
||
// GET /campaigns/templates/:name — return current HTML content
|
||
const tplGet = path.match(/^\/campaigns\/templates\/([a-zA-Z0-9_-]+)$/)
|
||
if (tplGet && method === 'GET') {
|
||
try {
|
||
const html = fs.readFileSync(templatePath(tplGet[1]), 'utf8')
|
||
return json(res, 200, { name: tplGet[1], html })
|
||
} catch (e) {
|
||
return json(res, 404, { error: 'template not found', detail: e.message })
|
||
}
|
||
}
|
||
|
||
// PUT /campaigns/templates/:name — save new HTML
|
||
// Keeps the previous version as <name>.bak-<ts>.html so a bad edit can
|
||
// be rolled back without git access.
|
||
if (tplGet && method === 'PUT') {
|
||
const body = await parseBody(req)
|
||
if (typeof body.html !== 'string' || !body.html) {
|
||
return json(res, 400, { error: 'html string required' })
|
||
}
|
||
try {
|
||
const p = templatePath(tplGet[1])
|
||
// Backup current version (if any) — best effort, don't fail save on backup error
|
||
try {
|
||
if (fs.existsSync(p)) {
|
||
const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19)
|
||
fs.copyFileSync(p, p.replace(/\.html$/, `.bak-${ts}.html`))
|
||
}
|
||
} catch (e) { log('template backup failed:', e.message) }
|
||
fs.writeFileSync(p, body.html, 'utf8')
|
||
log(`template ${tplGet[1]} updated by ops (${body.html.length} bytes)`)
|
||
return json(res, 200, { name: tplGet[1], saved: true, size: body.html.length })
|
||
} catch (e) {
|
||
return json(res, 400, { error: e.message })
|
||
}
|
||
}
|
||
|
||
// POST /campaigns/templates/:name/preview — render with sample data
|
||
// Useful for the editor's "Preview" pane to see what {{vars}} resolve to.
|
||
const tplPreview = path.match(/^\/campaigns\/templates\/([a-zA-Z0-9_-]+)\/preview$/)
|
||
if (tplPreview && method === 'POST') {
|
||
const body = await parseBody(req)
|
||
const html = body.html || fs.readFileSync(templatePath(tplPreview[1]), 'utf8')
|
||
const vars = {
|
||
firstname: 'Louis', lastname: 'Paul', email: 'louis@targo.ca',
|
||
description: '123 Rue de Test', gift_url: 'http://gtbt.co/PREVIEW',
|
||
amount: '60 $', expiry: '31 décembre 2026', commitment_months: '3',
|
||
...(body.vars || {}),
|
||
}
|
||
return json(res, 200, { rendered: renderTemplate(html, vars) })
|
||
}
|
||
|
||
// POST /campaigns/webhook — Mailjet Event API receiver
|
||
// Mailjet sends an array of events; we process all of them.
|
||
if (path === '/campaigns/webhook' && method === 'POST') {
|
||
const body = await parseBody(req)
|
||
const events = Array.isArray(body) ? body : [body]
|
||
let applied = 0
|
||
for (const ev of events) {
|
||
if (applyWebhookEvent(ev)) applied++
|
||
}
|
||
log(`mailjet webhook: ${events.length} events, ${applied} applied`)
|
||
return json(res, 200, { received: events.length, applied })
|
||
}
|
||
|
||
// ── Per-campaign wildcard routes (MUST stay below the /templates and
|
||
// /webhook fixed paths above, otherwise the wildcard captures them) ─────
|
||
|
||
// GET /campaigns/:id — full detail
|
||
const detailMatch = path.match(/^\/campaigns\/([^/]+)$/)
|
||
if (detailMatch && method === 'GET') {
|
||
const c = loadCampaign(detailMatch[1])
|
||
if (!c) return json(res, 404, { error: 'not found' })
|
||
return json(res, 200, c)
|
||
}
|
||
|
||
// PATCH /campaigns/:id — update recipients (e.g. exclude rows, edit email)
|
||
if (detailMatch && method === 'PATCH') {
|
||
const body = await parseBody(req)
|
||
const c = loadCampaign(detailMatch[1])
|
||
if (!c) return json(res, 404, { error: 'not found' })
|
||
if (body.name) c.name = body.name
|
||
if (body.params) c.params = { ...c.params, ...body.params }
|
||
if (Array.isArray(body.recipients)) c.recipients = body.recipients
|
||
return json(res, 200, saveCampaign(c))
|
||
}
|
||
|
||
// POST /campaigns/:id/send — fire background worker
|
||
const sendMatch = path.match(/^\/campaigns\/([^/]+)\/send$/)
|
||
if (sendMatch && method === 'POST') {
|
||
const id = sendMatch[1]
|
||
const c = loadCampaign(id)
|
||
if (!c) return json(res, 404, { error: 'not found' })
|
||
if (activeWorkers.has(id)) return json(res, 409, { error: 'already sending' })
|
||
// Fire and forget
|
||
setImmediate(() => sendCampaignAsync(id))
|
||
return json(res, 202, { id, status: 'sending' })
|
||
}
|
||
|
||
return json(res, 404, { error: 'campaigns endpoint not found' })
|
||
}
|
||
|
||
module.exports = {
|
||
handle,
|
||
// Exposed for testing
|
||
parseCsv, parseMapCsv, parseGiftbitCsv,
|
||
matchCustomer, normalizeCivic, normalizePhone, normalizePostal,
|
||
renderTemplate,
|
||
}
|