108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
import mailchimp = require('@mailchimp/mailchimp_transactional');
|
|
import fetch from 'node-fetch';
|
|
import { emailConfig } from '../../config/email';
|
|
import { PendingEmail } from '../../types';
|
|
|
|
// Email storage
|
|
export const pendingEmails = new Map<string, PendingEmail>();
|
|
|
|
export class EmailService {
|
|
static async sendTransactionalEmail(to: string, templateName: string, subject: string, templateVariables: Record<string, string>): Promise<{ success: boolean; result?: any; error?: string }> {
|
|
try {
|
|
const mailchimpClient = mailchimp(emailConfig.MAILCHIMP_API_KEY!);
|
|
|
|
const message = {
|
|
template_name: templateName,
|
|
template_content: [],
|
|
message: {
|
|
global_merge_vars: this.buildVariables(templateVariables),
|
|
from_email: emailConfig.FROM_EMAIL,
|
|
from_name: emailConfig.FROM_NAME,
|
|
subject: subject,
|
|
to: [
|
|
{
|
|
email: to,
|
|
type: 'to'
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const result = await mailchimpClient.messages.sendTemplate(message);
|
|
return { success: true, result };
|
|
} catch (error) {
|
|
console.error('Erreur envoi email:', error);
|
|
return { success: false, error: 'Échec de l\'envoi de l\'email' };
|
|
}
|
|
}
|
|
|
|
static buildVariables(templateVariables: Record<string, string>): Array<{ name: string; content: string }> {
|
|
return Object.keys(templateVariables).map(key => ({
|
|
name: key,
|
|
content: templateVariables[key]
|
|
}));
|
|
}
|
|
|
|
// Add to Mailchimp diffusion list
|
|
static async addToMailchimpList(email: string): Promise<{ success: boolean; data?: any; error?: string }> {
|
|
try {
|
|
const url = `https://us17.api.mailchimp.com/3.0/lists/${emailConfig.MAILCHIMP_LIST_ID}/members`;
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `apikey ${emailConfig.MAILCHIMP_KEY}`,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
email_address: email,
|
|
status: 'subscribed'
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return { success: true, data };
|
|
} catch (error) {
|
|
console.error('Erreur ajout à la liste:', error);
|
|
return { success: false, error: 'Échec de l\'ajout à la liste Mailchimp' };
|
|
}
|
|
}
|
|
|
|
static async retryFailedEmails(): Promise<void> {
|
|
for (const [emailId, emailData] of pendingEmails) {
|
|
if (emailData.attempts >= 10) {
|
|
pendingEmails.delete(emailId);
|
|
continue;
|
|
}
|
|
|
|
const nextRetryDate = new Date(emailData.lastAttempt);
|
|
nextRetryDate.setMinutes(nextRetryDate.getMinutes() + Math.pow(emailData.attempts, 2));
|
|
|
|
if (Date.now() >= nextRetryDate.getTime()) {
|
|
try {
|
|
const result = await this.sendTransactionalEmail(
|
|
emailData.to,
|
|
emailData.templateName,
|
|
emailData.subject,
|
|
emailData.templateVariables
|
|
);
|
|
|
|
if (result.success) {
|
|
pendingEmails.delete(emailId);
|
|
} else {
|
|
emailData.attempts += 1;
|
|
emailData.lastAttempt = Date.now();
|
|
}
|
|
} catch (error) {
|
|
emailData.attempts += 1;
|
|
emailData.lastAttempt = Date.now();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|