Fix some issues
All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 31s
All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 31s
This commit is contained in:
parent
3855c739bc
commit
1632720e1b
@ -13,7 +13,8 @@
|
||||
"dotenv": "^17.2.0",
|
||||
"express": "^4.18.2",
|
||||
"node-fetch": "^2.6.7",
|
||||
"ovh": "^2.0.3"
|
||||
"ovh": "^2.0.3",
|
||||
"uuid": "^11.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.1"
|
||||
|
761
src/server.js
761
src/server.js
@ -1,6 +1,7 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const fetch = require('node-fetch');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const ovh = require('ovh');
|
||||
const mailchimp = require('@mailchimp/mailchimp_transactional');
|
||||
require('dotenv').config();
|
||||
@ -19,52 +20,99 @@ const corsOptions = {
|
||||
app.use(cors(corsOptions));
|
||||
app.use(express.json());
|
||||
|
||||
const authTokens = [];
|
||||
|
||||
const ECivility = {
|
||||
MALE: 'MALE',
|
||||
FEMALE: 'FEMALE',
|
||||
OTHERS: 'OTHERS'
|
||||
};
|
||||
|
||||
const EOfficeStatus = {
|
||||
ACTIVATED: 'ACTIVATED',
|
||||
DESACTIVATED: 'DESACTIVATED'
|
||||
};
|
||||
|
||||
const EIdnotRole = {
|
||||
DIRECTEUR: "Directeur général du CSN",
|
||||
NOTAIRE_TITULAIRE: "Notaire titulaire",
|
||||
NOTAIRE_ASSOCIE: "Notaire associé",
|
||||
NOTAIRE_SALARIE: "Notaire salarié",
|
||||
COLLABORATEUR: "Collaborateur",
|
||||
SECRETAIRE_GENERAL: "Secrétaire général",
|
||||
SUPPLEANT: "Suppléant",
|
||||
ADMINISTRATEUR: "Administrateur",
|
||||
RESPONSABLE: "Responsable",
|
||||
CURATEUR: "Curateur",
|
||||
}
|
||||
|
||||
function getOfficeStatus(statusName) {
|
||||
switch (statusName) {
|
||||
case 'Pourvu':
|
||||
return 'ACTIVATED';
|
||||
case 'Pourvu mais décédé':
|
||||
return 'ACTIVATED';
|
||||
case 'Sans titulaire':
|
||||
return 'ACTIVATED';
|
||||
case 'Vacance':
|
||||
return 'ACTIVATED';
|
||||
case 'En activité':
|
||||
return 'ACTIVATED';
|
||||
case "Pourvu":
|
||||
return EOfficeStatus.ACTIVATED;
|
||||
case "Pourvu mais décédé":
|
||||
return EOfficeStatus.ACTIVATED;
|
||||
case "Sans titulaire":
|
||||
return EOfficeStatus.ACTIVATED;
|
||||
case "Vacance":
|
||||
return EOfficeStatus.ACTIVATED;
|
||||
case "En activité":
|
||||
return EOfficeStatus.ACTIVATED;
|
||||
default:
|
||||
return 'DESACTIVATED';
|
||||
return EOfficeStatus.DESACTIVATED;
|
||||
}
|
||||
}
|
||||
|
||||
function getOfficeRole(roleName) {
|
||||
switch (roleName) {
|
||||
case EIdnotRole.NOTAIRE_TITULAIRE:
|
||||
return { name: 'Notaire' };
|
||||
case EIdnotRole.NOTAIRE_ASSOCIE:
|
||||
return { name: 'Notaire' };
|
||||
case EIdnotRole.NOTAIRE_SALARIE:
|
||||
return { name: 'Notaire' };
|
||||
case EIdnotRole.COLLABORATEUR:
|
||||
return { name: 'Collaborateur' };
|
||||
case EIdnotRole.SUPPLEANT:
|
||||
return { name: 'Collaborateur' };
|
||||
case EIdnotRole.ADMINISTRATEUR:
|
||||
return { name: 'Collaborateur' };
|
||||
case EIdnotRole.CURATEUR:
|
||||
return { name: 'Collaborateur' };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getRole(roleName) {
|
||||
switch (roleName) {
|
||||
case 'Notaire titulaire':
|
||||
return { name: 'admin', label: 'Administrateur' };
|
||||
case 'Notaire associé':
|
||||
return { name: 'admin', label: 'Administrateur' };
|
||||
case 'Notaire salarié':
|
||||
return { name: 'notary', label: 'Notaire' };
|
||||
case 'Collaborateur':
|
||||
return { name: 'notary', label: 'Notaire' };
|
||||
case 'Suppléant':
|
||||
return { name: 'notary', label: 'Notaire' };
|
||||
case 'Administrateur':
|
||||
return { name: 'admin', label: 'Administrateur' };
|
||||
case 'Curateur':
|
||||
return { name: 'notary', label: 'Notaire' };
|
||||
case EIdnotRole.NOTAIRE_TITULAIRE:
|
||||
return { name: 'admin' };
|
||||
case EIdnotRole.NOTAIRE_ASSOCIE:
|
||||
return { name: 'admin' };
|
||||
case EIdnotRole.NOTAIRE_SALARIE:
|
||||
return { name: 'notary' };
|
||||
case EIdnotRole.COLLABORATEUR:
|
||||
return { name: 'notary' };
|
||||
case EIdnotRole.SUPPLEANT:
|
||||
return { name: 'notary' };
|
||||
case EIdnotRole.ADMINISTRATEUR:
|
||||
return { name: 'admin' };
|
||||
case EIdnotRole.CURATEUR:
|
||||
return { name: 'notary' };
|
||||
default:
|
||||
return { name: 'default', label: 'Défaut' };
|
||||
return { name: 'default' };
|
||||
}
|
||||
}
|
||||
|
||||
function getCivility(civility) {
|
||||
switch (civility) {
|
||||
case 'Monsieur':
|
||||
return 'MALE';
|
||||
return ECivility.MALE;
|
||||
case 'Madame':
|
||||
return 'FEMALE';
|
||||
return ECivility.FEMALE;
|
||||
default:
|
||||
return 'OTHERS';
|
||||
return ECivility.OTHERS;
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +186,7 @@ app.post('/api/v1/idnot/user/:code', async (req, res) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
const idnotUser = {
|
||||
const idNotUser = {
|
||||
idNot: payload.sub,
|
||||
office: {
|
||||
idNot: payload.entity_idn,
|
||||
@ -160,15 +208,19 @@ app.post('/api/v1/idnot/user/:code', async (req, res) => {
|
||||
phone_number: userData.numeroTelephone,
|
||||
cell_phone_number: userData.numeroMobile ?? userData.numeroTelephone,
|
||||
civility: getCivility(userData.personne.civilite)
|
||||
}
|
||||
},
|
||||
office_role: getOfficeRole(userData.typeLien.name)
|
||||
};
|
||||
|
||||
if (!idnotUser.contact.email) {
|
||||
console.error("User pro email empty");
|
||||
if (!idNotUser.contact.email) {
|
||||
console.error('User pro email empty');
|
||||
return null;
|
||||
}
|
||||
|
||||
res.json(idnotUser);
|
||||
const authToken = uuidv4();
|
||||
authTokens.push({ idNot: idNotUser.idNot, authToken });
|
||||
|
||||
res.json({ idNotUser, authToken });
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
error: 'Internal Server Error',
|
||||
@ -185,10 +237,10 @@ const configSms = {
|
||||
OVH_APP_SECRET: process.env.OVH_APP_SECRET,
|
||||
OVH_CONSUMER_KEY: process.env.OVH_CONSUMER_KEY,
|
||||
OVH_SMS_SERVICE_NAME: process.env.OVH_SMS_SERVICE_NAME,
|
||||
|
||||
|
||||
// SMS Factor config
|
||||
SMS_FACTOR_TOKEN: process.env.SMS_FACTOR_TOKEN,
|
||||
|
||||
|
||||
PORT: process.env.PORT || 8080
|
||||
};
|
||||
|
||||
@ -198,90 +250,90 @@ const verificationCodes = new Map();
|
||||
// Service SMS
|
||||
class SmsService {
|
||||
static generateCode() {
|
||||
return Math.floor(100000 + Math.random() * 900000);
|
||||
return Math.floor(100000 + Math.random() * 900000);
|
||||
}
|
||||
|
||||
// OVH Service
|
||||
static sendSmsWithOvh(phoneNumber, message) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const ovhClient = ovh({
|
||||
appKey: configSms.OVH_APP_KEY,
|
||||
appSecret: configSms.OVH_APP_SECRET,
|
||||
consumerKey: configSms.OVH_CONSUMER_KEY
|
||||
});
|
||||
|
||||
ovhClient.request('POST', `/sms/${configSms.OVH_SMS_SERVICE_NAME}/jobs`, {
|
||||
message: message,
|
||||
receivers: [phoneNumber],
|
||||
senderForResponse: false,
|
||||
sender: "not.IT Fact",
|
||||
noStopClause: true
|
||||
}, (error, result) => {
|
||||
if (error) {
|
||||
console.error('Erreur OVH SMS:', error);
|
||||
resolve({ success: false, error: 'Échec de l\'envoi du SMS via OVH' });
|
||||
} else {
|
||||
resolve({ success: true });
|
||||
}
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
const ovhClient = ovh({
|
||||
appKey: configSms.OVH_APP_KEY,
|
||||
appSecret: configSms.OVH_APP_SECRET,
|
||||
consumerKey: configSms.OVH_CONSUMER_KEY
|
||||
});
|
||||
|
||||
ovhClient.request('POST', `/sms/${configSms.OVH_SMS_SERVICE_NAME}/jobs`, {
|
||||
message: message,
|
||||
receivers: [phoneNumber],
|
||||
senderForResponse: false,
|
||||
sender: 'not.IT Fact',
|
||||
noStopClause: true
|
||||
}, (error, result) => {
|
||||
if (error) {
|
||||
console.error('Erreur OVH SMS:', error);
|
||||
resolve({ success: false, error: 'Échec de l\'envoi du SMS via OVH' });
|
||||
} else {
|
||||
resolve({ success: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// SMS Factor Service
|
||||
static async sendSmsWithSmsFactor(phoneNumber, message) {
|
||||
try {
|
||||
const url = new URL('https://api.smsfactor.com/send/simulate');
|
||||
url.searchParams.append('to', phoneNumber);
|
||||
url.searchParams.append('text', message);
|
||||
url.searchParams.append('sender', 'LeCoffre');
|
||||
url.searchParams.append('token', configSms.SMS_FACTOR_TOKEN);
|
||||
try {
|
||||
const url = new URL('https://api.smsfactor.com/send/simulate');
|
||||
url.searchParams.append('to', phoneNumber);
|
||||
url.searchParams.append('text', message);
|
||||
url.searchParams.append('sender', 'LeCoffre');
|
||||
url.searchParams.append('token', configSms.SMS_FACTOR_TOKEN);
|
||||
|
||||
const response = await fetch(url.toString());
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const response = await fetch(url.toString());
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('Erreur SMS Factor:', error);
|
||||
return { success: false, error: 'Échec de l\'envoi du SMS via SMS Factor' };
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('Erreur SMS Factor:', error);
|
||||
return { success: false, error: 'Échec de l\'envoi du SMS via SMS Factor' };
|
||||
}
|
||||
}
|
||||
|
||||
// Main method
|
||||
static async sendSms(phoneNumber, message) {
|
||||
// Try first with OVH
|
||||
const ovhResult = await this.sendSmsWithOvh(phoneNumber, message);
|
||||
|
||||
if (ovhResult.success) {
|
||||
return ovhResult;
|
||||
}
|
||||
// Try first with OVH
|
||||
const ovhResult = await this.sendSmsWithOvh(phoneNumber, message);
|
||||
|
||||
// If OVH fails, try with SMS Factor
|
||||
console.log('OVH SMS failed, trying SMS Factor...');
|
||||
return await this.sendSmsWithSmsFactor(phoneNumber, message);
|
||||
if (ovhResult.success) {
|
||||
return ovhResult;
|
||||
}
|
||||
|
||||
// If OVH fails, try with SMS Factor
|
||||
console.log('OVH SMS failed, trying SMS Factor...');
|
||||
return await this.sendSmsWithSmsFactor(phoneNumber, message);
|
||||
}
|
||||
}
|
||||
|
||||
// Phone number validation middleware
|
||||
const validatePhoneNumber = (req, res, next) => {
|
||||
const { phoneNumber } = req.body;
|
||||
|
||||
|
||||
if (!phoneNumber) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Le numéro de téléphone est requis'
|
||||
});
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Le numéro de téléphone est requis'
|
||||
});
|
||||
}
|
||||
|
||||
// Validation basique du format
|
||||
const phoneRegex = /^\+?[1-9]\d{1,14}$/;
|
||||
if (!phoneRegex.test(phoneNumber)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Format de numéro de téléphone invalide'
|
||||
});
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Format de numéro de téléphone invalide'
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
@ -292,49 +344,49 @@ app.post('/api/send-code', validatePhoneNumber, async (req, res) => {
|
||||
const { phoneNumber } = req.body;
|
||||
|
||||
try {
|
||||
// Check if a code already exists and is not expired
|
||||
const existingVerification = verificationCodes.get(phoneNumber);
|
||||
if (existingVerification) {
|
||||
const timeSinceLastSend = Date.now() - existingVerification.timestamp;
|
||||
if (timeSinceLastSend < 30000) { // 30 secondes
|
||||
return res.status(429).json({
|
||||
success: false,
|
||||
message: 'Veuillez attendre 30 secondes avant de demander un nouveau code'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a new code
|
||||
const code = SmsService.generateCode();
|
||||
|
||||
// Store the code
|
||||
verificationCodes.set(phoneNumber, {
|
||||
code,
|
||||
timestamp: Date.now(),
|
||||
attempts: 0
|
||||
});
|
||||
|
||||
// Send the SMS
|
||||
const message = `Votre code de vérification LeCoffre est : ${code}`;
|
||||
const result = await SmsService.sendSms(phoneNumber, message);
|
||||
|
||||
if (result.success) {
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Code envoyé avec succès',
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Échec de l\'envoi du SMS via les deux fournisseurs'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur:', error);
|
||||
res.status(500).json({
|
||||
// Check if a code already exists and is not expired
|
||||
const existingVerification = verificationCodes.get(phoneNumber);
|
||||
if (existingVerification) {
|
||||
const timeSinceLastSend = Date.now() - existingVerification.timestamp;
|
||||
if (timeSinceLastSend < 30000) { // 30 secondes
|
||||
return res.status(429).json({
|
||||
success: false,
|
||||
message: 'Erreur serveur lors de l\'envoi du code'
|
||||
message: 'Veuillez attendre 30 secondes avant de demander un nouveau code'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a new code
|
||||
const code = SmsService.generateCode();
|
||||
|
||||
// Store the code
|
||||
verificationCodes.set(phoneNumber, {
|
||||
code,
|
||||
timestamp: Date.now(),
|
||||
attempts: 0
|
||||
});
|
||||
|
||||
// Send the SMS
|
||||
const message = `Votre code de vérification LeCoffre est : ${code}`;
|
||||
const result = await SmsService.sendSms(phoneNumber, message);
|
||||
|
||||
if (result.success) {
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Code envoyé avec succès',
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Échec de l\'envoi du SMS via les deux fournisseurs'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Erreur serveur lors de l\'envoi du code'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -342,52 +394,52 @@ app.post('/api/verify-code', validatePhoneNumber, (req, res) => {
|
||||
const { phoneNumber, code } = req.body;
|
||||
|
||||
if (!code) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Le code est requis'
|
||||
});
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Le code est requis'
|
||||
});
|
||||
}
|
||||
|
||||
const verification = verificationCodes.get(phoneNumber);
|
||||
|
||||
if (!verification) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Aucun code n\'a été envoyé à ce numéro'
|
||||
});
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Aucun code n\'a été envoyé à ce numéro'
|
||||
});
|
||||
}
|
||||
|
||||
// Check if the code has not expired (5 minutes)
|
||||
if (Date.now() - verification.timestamp > 5 * 60 * 1000) {
|
||||
verificationCodes.delete(phoneNumber);
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Le code a expiré'
|
||||
});
|
||||
verificationCodes.delete(phoneNumber);
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Le code a expiré'
|
||||
});
|
||||
}
|
||||
|
||||
// Check if the code is correct
|
||||
if (verification.code.toString() === code.toString()) {
|
||||
verificationCodes.delete(phoneNumber);
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Code vérifié avec succès'
|
||||
});
|
||||
verificationCodes.delete(phoneNumber);
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Code vérifié avec succès'
|
||||
});
|
||||
} else {
|
||||
verification.attempts += 1;
|
||||
|
||||
if (verification.attempts >= 3) {
|
||||
verificationCodes.delete(phoneNumber);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
message: 'Trop de tentatives. Veuillez demander un nouveau code'
|
||||
});
|
||||
} else {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
message: 'Code incorrect'
|
||||
});
|
||||
}
|
||||
verification.attempts += 1;
|
||||
|
||||
if (verification.attempts >= 3) {
|
||||
verificationCodes.delete(phoneNumber);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
message: 'Trop de tentatives. Veuillez demander un nouveau code'
|
||||
});
|
||||
} else {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
message: 'Code incorrect'
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -410,122 +462,122 @@ const pendingEmails = new Map();
|
||||
|
||||
// Email service
|
||||
class EmailService {
|
||||
static async sendTransactionalEmail(to, templateName, subject, templateVariables) {
|
||||
static async sendTransactionalEmail(to, templateName, subject, templateVariables) {
|
||||
try {
|
||||
const mailchimpClient = mailchimp(configEmail.MAILCHIMP_API_KEY);
|
||||
|
||||
const message = {
|
||||
template_name: templateName,
|
||||
template_content: [],
|
||||
message: {
|
||||
global_merge_vars: this.buildVariables(templateVariables),
|
||||
from_email: configEmail.FROM_EMAIL,
|
||||
from_name: configEmail.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) {
|
||||
return Object.keys(templateVariables).map(key => ({
|
||||
name: key,
|
||||
content: templateVariables[key]
|
||||
}));
|
||||
}
|
||||
|
||||
// Add to Mailchimp diffusion list
|
||||
static async addToMailchimpList(email) {
|
||||
try {
|
||||
const url = `https://us17.api.mailchimp.com/3.0/lists/${configEmail.MAILCHIMP_LIST_ID}/members`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `apikey ${configEmail.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() {
|
||||
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) {
|
||||
try {
|
||||
const mailchimpClient = mailchimp(configEmail.MAILCHIMP_API_KEY);
|
||||
|
||||
const message = {
|
||||
template_name: templateName,
|
||||
template_content: [],
|
||||
message: {
|
||||
global_merge_vars: this.buildVariables(templateVariables),
|
||||
from_email: configEmail.FROM_EMAIL,
|
||||
from_name: configEmail.FROM_NAME,
|
||||
subject: subject,
|
||||
to: [
|
||||
{
|
||||
email: to,
|
||||
type: 'to'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const result = await this.sendTransactionalEmail(
|
||||
emailData.to,
|
||||
emailData.templateName,
|
||||
emailData.subject,
|
||||
emailData.templateVariables
|
||||
);
|
||||
|
||||
const result = await mailchimpClient.messages.sendTemplate(message);
|
||||
return { success: true, result };
|
||||
if (result.success) {
|
||||
pendingEmails.delete(emailId);
|
||||
} else {
|
||||
emailData.attempts += 1;
|
||||
emailData.lastAttempt = Date.now();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur envoi email:', error);
|
||||
return { success: false, error: 'Échec de l\'envoi de l\'email' };
|
||||
}
|
||||
}
|
||||
|
||||
static buildVariables(templateVariables) {
|
||||
return Object.keys(templateVariables).map(key => ({
|
||||
name: key,
|
||||
content: templateVariables[key]
|
||||
}));
|
||||
}
|
||||
|
||||
// Add to Mailchimp diffusion list
|
||||
static async addToMailchimpList(email) {
|
||||
try {
|
||||
const url = `https://us17.api.mailchimp.com/3.0/lists/${configEmail.MAILCHIMP_LIST_ID}/members`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `apikey ${configEmail.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() {
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
emailData.attempts += 1;
|
||||
emailData.lastAttempt = Date.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Email validation middleware
|
||||
const validateEmail = (req, res, next) => {
|
||||
const { email } = req.body;
|
||||
|
||||
|
||||
if (!email) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'L\'adresse email est requise'
|
||||
});
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'L\'adresse email est requise'
|
||||
});
|
||||
}
|
||||
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Format d\'email invalide'
|
||||
});
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Format d\'email invalide'
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
@ -533,12 +585,12 @@ const validateEmail = (req, res, next) => {
|
||||
|
||||
// Email templates
|
||||
const ETemplates = {
|
||||
DOCUMENT_ASKED: "DOCUMENT_ASKED",
|
||||
DOCUMENT_REFUSED: "DOCUMENT_REFUSED",
|
||||
DOCUMENT_RECAP: "DOCUMENT_RECAP",
|
||||
SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION",
|
||||
DOCUMENT_REMINDER: "DOCUMENT_REMINDER",
|
||||
DOCUMENT_SEND: "DOCUMENT_SEND",
|
||||
DOCUMENT_ASKED: "DOCUMENT_ASKED",
|
||||
DOCUMENT_REFUSED: "DOCUMENT_REFUSED",
|
||||
DOCUMENT_RECAP: "DOCUMENT_RECAP",
|
||||
SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION",
|
||||
DOCUMENT_REMINDER: "DOCUMENT_REMINDER",
|
||||
DOCUMENT_SEND: "DOCUMENT_SEND",
|
||||
};
|
||||
|
||||
// Routes
|
||||
@ -546,43 +598,43 @@ app.post('/api/send-email', validateEmail, async (req, res) => {
|
||||
const { email, firstName, lastName, officeName, template } = req.body;
|
||||
|
||||
try {
|
||||
const templateVariables = {
|
||||
first_name: firstName || '',
|
||||
last_name: lastName || '',
|
||||
office_name: officeName || '',
|
||||
link: `${process.env.APP_HOST}`
|
||||
};
|
||||
const templateVariables = {
|
||||
first_name: firstName || '',
|
||||
last_name: lastName || '',
|
||||
office_name: officeName || '',
|
||||
link: `${process.env.APP_HOST}`
|
||||
};
|
||||
|
||||
const result = await EmailService.sendTransactionalEmail(
|
||||
email,
|
||||
ETemplates[template],
|
||||
'Votre notaire vous envoie un message',
|
||||
templateVariables
|
||||
);
|
||||
const result = await EmailService.sendTransactionalEmail(
|
||||
email,
|
||||
ETemplates[template],
|
||||
'Votre notaire vous envoie un message',
|
||||
templateVariables
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
// Add to pending emails to retry later
|
||||
const emailId = `${email}-${Date.now()}`;
|
||||
pendingEmails.set(emailId, {
|
||||
to: email,
|
||||
templateName: ETemplates[template],
|
||||
subject: 'Votre notaire vous envoie un message',
|
||||
templateVariables,
|
||||
attempts: 1,
|
||||
lastAttempt: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Email envoyé avec succès'
|
||||
if (!result.success) {
|
||||
// Add to pending emails to retry later
|
||||
const emailId = `${email}-${Date.now()}`;
|
||||
pendingEmails.set(emailId, {
|
||||
to: email,
|
||||
templateName: ETemplates[template],
|
||||
subject: 'Votre notaire vous envoie un message',
|
||||
templateVariables,
|
||||
attempts: 1,
|
||||
lastAttempt: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Email envoyé avec succès'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Erreur:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Erreur serveur lors de l\'envoi de l\'email'
|
||||
});
|
||||
console.error('Erreur:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Erreur serveur lors de l\'envoi de l\'email'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -590,25 +642,92 @@ app.post('/api/subscribe-to-list', validateEmail, async (req, res) => {
|
||||
const { email } = req.body;
|
||||
|
||||
try {
|
||||
const result = await EmailService.addToMailchimpList(email);
|
||||
const result = await EmailService.addToMailchimpList(email);
|
||||
|
||||
if (result.success) {
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Inscription à la liste réussie'
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Échec de l\'inscription à la liste'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Erreur serveur lors de l\'inscription'
|
||||
if (result.success) {
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Inscription à la liste réussie'
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Échec de l\'inscription à la liste'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Erreur serveur lors de l\'inscription'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/:uid/send_reminder', validateEmail, async (req, res) => {
|
||||
const { email, documentsUid } = req.body;
|
||||
|
||||
try {
|
||||
const uid = req.params["uid"];
|
||||
if (!uid) {
|
||||
//this.httpBadRequest(response, "No uid provided");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!documentsUid || !Array.isArray(documentsUid)) {
|
||||
//this.httpBadRequest(response, "Invalid or missing documents");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
const documentEntities: Documents[] = [];
|
||||
//For each document uid, use DocumentsService.getByUid to get the document entity and add it to the documents array
|
||||
for (const documentUid of documentsUid) {
|
||||
const documentEntity = await this.documentsService.getByUid(documentUid, { document_type: true, folder: true });
|
||||
|
||||
if (!documentEntity) {
|
||||
this.httpBadRequest(response, "Document not found");
|
||||
return;
|
||||
}
|
||||
documentEntities.push(documentEntity);
|
||||
}
|
||||
|
||||
const customerEntity = await this.customersService.getByUid(uid, { contact: true, office: true });
|
||||
|
||||
if (!customerEntity) {
|
||||
this.httpNotFoundRequest(response, "customer not found");
|
||||
return;
|
||||
}
|
||||
|
||||
//Hydrate ressource with prisma entity
|
||||
const customer = Customer.hydrate < Customer > (customerEntity, { strategy: "excludeAll" });
|
||||
|
||||
// Call service to send reminder with documents
|
||||
await this.customersService.sendDocumentsReminder(customer, documentEntities);
|
||||
*/
|
||||
|
||||
const templateVariables = {
|
||||
first_name: 'firstName' || '',
|
||||
last_name: 'lastName' || '',
|
||||
office_name: 'officeName' || '',
|
||||
link: `${process.env.APP_HOST}`
|
||||
};
|
||||
|
||||
const result = await EmailService.sendTransactionalEmail(
|
||||
email,
|
||||
ETemplates.DOCUMENT_REMINDER,
|
||||
'Votre notaire vous envoie un message',
|
||||
templateVariables
|
||||
);
|
||||
console.log(result);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Email envoyé avec succès'
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user