Move Mail feature to the main file
This commit is contained in:
parent
8b6a25e174
commit
5032fa3134
@ -1,179 +0,0 @@
|
|||||||
const express = require('express');
|
|
||||||
const cors = require('cors');
|
|
||||||
const fetch = require('node-fetch');
|
|
||||||
|
|
||||||
// Initialisation de l'application Express
|
|
||||||
const app = express();
|
|
||||||
const PORT = process.env.PORT || 8080;
|
|
||||||
|
|
||||||
// Configuration CORS
|
|
||||||
const corsOptions = {
|
|
||||||
origin: ['http://local.lecoffreio.4nkweb:3000', 'http://localhost:3000'],
|
|
||||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
||||||
allowedHeaders: ['Content-Type', 'Authorization']
|
|
||||||
};
|
|
||||||
|
|
||||||
app.use(cors(corsOptions));
|
|
||||||
app.use(express.json());
|
|
||||||
|
|
||||||
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';
|
|
||||||
default:
|
|
||||||
return 'DESACTIVATED';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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' };
|
|
||||||
default:
|
|
||||||
return { name: 'default', label: 'Défaut' };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCivility(civility) {
|
|
||||||
switch (civility) {
|
|
||||||
case 'Monsieur':
|
|
||||||
return 'MALE';
|
|
||||||
case 'Madame':
|
|
||||||
return 'FEMALE';
|
|
||||||
default:
|
|
||||||
return 'OTHERS';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.get('/api/v1/health', (req, res) => {
|
|
||||||
res.json({ message: 'OK' });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/api/v1/idnot/user/:code', async (req, res) => {
|
|
||||||
const code = req.params.code;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const params = {
|
|
||||||
client_id: 'B3CE56353EDB15A9',
|
|
||||||
client_secret: '3F733549E879878344B6C949B366BB5CDBB2DB5B7F7AB7EBBEBB0F0DD0776D1C',
|
|
||||||
redirect_uri: 'http://local.lecoffreio.4nkweb:3000/authorized-client',
|
|
||||||
grant_type: 'authorization_code',
|
|
||||||
code: code
|
|
||||||
};
|
|
||||||
|
|
||||||
const tokens = await (
|
|
||||||
await fetch('https://qual-connexion.idnot.fr/user/IdPOAuth2/token/idnot_idp_v1', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
|
||||||
},
|
|
||||||
body: new URLSearchParams(params).toString()
|
|
||||||
})
|
|
||||||
).json();
|
|
||||||
|
|
||||||
const jwt = tokens.id_token;
|
|
||||||
if (!jwt) {
|
|
||||||
console.error('jwt not defined');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const payload = JSON.parse(Buffer.from(jwt.split('.')[1], 'base64').toString('utf8'));
|
|
||||||
|
|
||||||
const searchParams = new URLSearchParams({
|
|
||||||
key: 'ba557f84-0bf6-4dbf-844f-df2767555e3e'
|
|
||||||
});
|
|
||||||
|
|
||||||
let userData;
|
|
||||||
try {
|
|
||||||
userData = await (
|
|
||||||
await fetch(`https://qual-api.notaires.fr/annuaire/api/pp/v2/rattachements/${payload.profile_idn}?` + searchParams, {
|
|
||||||
method: 'GET'
|
|
||||||
})
|
|
||||||
).json();
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching ' + `https://qual-api.notaires.fr/annuaire/api/pp/v2/rattachements/${payload.profile_idn}`, error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (!userData || !userData.statutDuRattachement || userData.entite.typeEntite.name !== 'office') {
|
|
||||||
console.error('User not attached to an office (May be a partner)');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
let officeLocationData;
|
|
||||||
try {
|
|
||||||
officeLocationData = (await (
|
|
||||||
await fetch(`https://qual-api.notaires.fr/annuaire${userData.entite.locationsUrl}?` + searchParams,
|
|
||||||
{
|
|
||||||
method: 'GET'
|
|
||||||
})
|
|
||||||
).json());
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching' + `https://qual-api.notaires.fr/annuaire${userData.entite.locationsUrl}`, error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (!officeLocationData || !officeLocationData.result || officeLocationData.result.length === 0) {
|
|
||||||
console.error('Office location data not found');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const idnotUser = {
|
|
||||||
idNot: payload.sub,
|
|
||||||
office: {
|
|
||||||
idNot: payload.entity_idn,
|
|
||||||
name: userData.entite.denominationSociale ?? userData.entite.codeCrpcen,
|
|
||||||
crpcen: userData.entite.codeCrpcen,
|
|
||||||
office_status: getOfficeStatus(userData.entite.statutEntite.name),
|
|
||||||
address: {
|
|
||||||
address: officeLocationData.result[0].adrGeo4,
|
|
||||||
city: officeLocationData.result[0].adrGeoVille.split(' ')[0] ?? officeLocationData.result[0].adrGeoVille,
|
|
||||||
zip_code: Number(officeLocationData.result[0].adrGeoCodePostal)
|
|
||||||
},
|
|
||||||
status: 'ACTIVE'
|
|
||||||
},
|
|
||||||
role: getRole(userData.typeLien.name),
|
|
||||||
contact: {
|
|
||||||
first_name: userData.personne.prenom,
|
|
||||||
last_name: userData.personne.nomUsuel,
|
|
||||||
email: userData.mailRattachement,
|
|
||||||
phone_number: userData.numeroTelephone,
|
|
||||||
cell_phone_number: userData.numeroMobile ?? userData.numeroTelephone,
|
|
||||||
civility: getCivility(userData.personne.civilite)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!idnotUser.contact.email) {
|
|
||||||
console.error("User pro email empty");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json(idnotUser);
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
error: 'Internal Server Error',
|
|
||||||
message: error.message
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
|
||||||
console.log(`Server is running on port ${PORT}`);
|
|
||||||
});
|
|
369
src/server.js
369
src/server.js
@ -1,25 +1,197 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const cors = require('cors');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const mailchimp = require('@mailchimp/mailchimp_transactional');
|
const mailchimp = require('@mailchimp/mailchimp_transactional');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
|
// Initialisation de l'application Express
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
const PORT = process.env.PORT || 8080;
|
||||||
|
|
||||||
// Configuration
|
// Configuration CORS
|
||||||
const config = {
|
const corsOptions = {
|
||||||
MAILCHIMP_API_KEY: process.env.MAILCHIMP_API_KEY,
|
origin: ['http://local.lecoffreio.4nkweb:3000', 'http://localhost:3000'],
|
||||||
MAILCHIMP_KEY: process.env.MAILCHIMP_KEY,
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||||
MAILCHIMP_LIST_ID: process.env.MAILCHIMP_LIST_ID,
|
allowedHeaders: ['Content-Type', 'Authorization']
|
||||||
PORT: process.env.PORT || 8080,
|
|
||||||
FROM_EMAIL: 'no-reply@lecoffre.io',
|
|
||||||
FROM_NAME: 'LeCoffre.io'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Stockage temporaire des emails en attente
|
app.use(cors(corsOptions));
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
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';
|
||||||
|
default:
|
||||||
|
return 'DESACTIVATED';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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' };
|
||||||
|
default:
|
||||||
|
return { name: 'default', label: 'Défaut' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCivility(civility) {
|
||||||
|
switch (civility) {
|
||||||
|
case 'Monsieur':
|
||||||
|
return 'MALE';
|
||||||
|
case 'Madame':
|
||||||
|
return 'FEMALE';
|
||||||
|
default:
|
||||||
|
return 'OTHERS';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get('/api/v1/health', (req, res) => {
|
||||||
|
res.json({ message: 'OK' });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/api/v1/idnot/user/:code', async (req, res) => {
|
||||||
|
const code = req.params.code;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const params = {
|
||||||
|
client_id: 'B3CE56353EDB15A9',
|
||||||
|
client_secret: '3F733549E879878344B6C949B366BB5CDBB2DB5B7F7AB7EBBEBB0F0DD0776D1C',
|
||||||
|
redirect_uri: 'http://local.lecoffreio.4nkweb:3000/authorized-client',
|
||||||
|
grant_type: 'authorization_code',
|
||||||
|
code: code
|
||||||
|
};
|
||||||
|
|
||||||
|
const tokens = await (
|
||||||
|
await fetch('https://qual-connexion.idnot.fr/user/IdPOAuth2/token/idnot_idp_v1', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
body: new URLSearchParams(params).toString()
|
||||||
|
})
|
||||||
|
).json();
|
||||||
|
|
||||||
|
const jwt = tokens.id_token;
|
||||||
|
if (!jwt) {
|
||||||
|
console.error('jwt not defined');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const payload = JSON.parse(Buffer.from(jwt.split('.')[1], 'base64').toString('utf8'));
|
||||||
|
|
||||||
|
const searchParams = new URLSearchParams({
|
||||||
|
key: 'ba557f84-0bf6-4dbf-844f-df2767555e3e'
|
||||||
|
});
|
||||||
|
|
||||||
|
let userData;
|
||||||
|
try {
|
||||||
|
userData = await (
|
||||||
|
await fetch(`https://qual-api.notaires.fr/annuaire/api/pp/v2/rattachements/${payload.profile_idn}?` + searchParams, {
|
||||||
|
method: 'GET'
|
||||||
|
})
|
||||||
|
).json();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching ' + `https://qual-api.notaires.fr/annuaire/api/pp/v2/rattachements/${payload.profile_idn}`, error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!userData || !userData.statutDuRattachement || userData.entite.typeEntite.name !== 'office') {
|
||||||
|
console.error('User not attached to an office (May be a partner)');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let officeLocationData;
|
||||||
|
try {
|
||||||
|
officeLocationData = (await (
|
||||||
|
await fetch(`https://qual-api.notaires.fr/annuaire${userData.entite.locationsUrl}?` + searchParams,
|
||||||
|
{
|
||||||
|
method: 'GET'
|
||||||
|
})
|
||||||
|
).json());
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching' + `https://qual-api.notaires.fr/annuaire${userData.entite.locationsUrl}`, error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!officeLocationData || !officeLocationData.result || officeLocationData.result.length === 0) {
|
||||||
|
console.error('Office location data not found');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idnotUser = {
|
||||||
|
idNot: payload.sub,
|
||||||
|
office: {
|
||||||
|
idNot: payload.entity_idn,
|
||||||
|
name: userData.entite.denominationSociale ?? userData.entite.codeCrpcen,
|
||||||
|
crpcen: userData.entite.codeCrpcen,
|
||||||
|
office_status: getOfficeStatus(userData.entite.statutEntite.name),
|
||||||
|
address: {
|
||||||
|
address: officeLocationData.result[0].adrGeo4,
|
||||||
|
city: officeLocationData.result[0].adrGeoVille.split(' ')[0] ?? officeLocationData.result[0].adrGeoVille,
|
||||||
|
zip_code: Number(officeLocationData.result[0].adrGeoCodePostal)
|
||||||
|
},
|
||||||
|
status: 'ACTIVE'
|
||||||
|
},
|
||||||
|
role: getRole(userData.typeLien.name),
|
||||||
|
contact: {
|
||||||
|
first_name: userData.personne.prenom,
|
||||||
|
last_name: userData.personne.nomUsuel,
|
||||||
|
email: userData.mailRattachement,
|
||||||
|
phone_number: userData.numeroTelephone,
|
||||||
|
cell_phone_number: userData.numeroMobile ?? userData.numeroTelephone,
|
||||||
|
civility: getCivility(userData.personne.civilite)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!idnotUser.contact.email) {
|
||||||
|
console.error("User pro email empty");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(idnotUser);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Internal Server Error',
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//------------------------------------ Email Section ------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
MAILCHIMP_API_KEY: process.env.MAILCHIMP_API_KEY,
|
||||||
|
MAILCHIMP_KEY: process.env.MAILCHIMP_KEY,
|
||||||
|
MAILCHIMP_LIST_ID: process.env.MAILCHIMP_LIST_ID,
|
||||||
|
PORT: process.env.PORT || 8080,
|
||||||
|
FROM_EMAIL: 'no-reply@lecoffre.io',
|
||||||
|
FROM_NAME: 'LeCoffre.io'
|
||||||
|
};
|
||||||
|
|
||||||
|
// Email storage
|
||||||
const pendingEmails = new Map();
|
const pendingEmails = new Map();
|
||||||
|
|
||||||
// Service Email
|
// Email service
|
||||||
class EmailService {
|
class EmailService {
|
||||||
static async sendTransactionalEmail(to, templateName, subject, templateVariables) {
|
static async sendTransactionalEmail(to, templateName, subject, templateVariables) {
|
||||||
try {
|
try {
|
||||||
@ -57,7 +229,7 @@ class EmailService {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ajout à la liste Mailchimp de diffusion
|
// Add to Mailchimp diffusion list
|
||||||
static async addToMailchimpList(email) {
|
static async addToMailchimpList(email) {
|
||||||
try {
|
try {
|
||||||
const url = `https://us17.api.mailchimp.com/3.0/lists/${config.MAILCHIMP_LIST_ID}/members`;
|
const url = `https://us17.api.mailchimp.com/3.0/lists/${config.MAILCHIMP_LIST_ID}/members`;
|
||||||
@ -120,115 +292,114 @@ class EmailService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Middleware de validation
|
// Email validation middleware
|
||||||
const validateEmail = (req, res, next) => {
|
const validateEmail = (req, res, next) => {
|
||||||
const { email } = req.body;
|
const { email } = req.body;
|
||||||
|
|
||||||
if (!email) {
|
if (!email) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'L\'adresse email est requise'
|
message: 'L\'adresse email est requise'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
if (!emailRegex.test(email)) {
|
if (!emailRegex.test(email)) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Format d\'email invalide'
|
message: 'Format d\'email invalide'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Templates
|
// Email templates
|
||||||
const ETemplates = {
|
const ETemplates = {
|
||||||
DOCUMENT_ASKED: "DOCUMENT_ASKED",
|
DOCUMENT_ASKED: "DOCUMENT_ASKED",
|
||||||
DOCUMENT_REFUSED: "DOCUMENT_REFUSED",
|
DOCUMENT_REFUSED: "DOCUMENT_REFUSED",
|
||||||
DOCUMENT_RECAP: "DOCUMENT_RECAP",
|
DOCUMENT_RECAP: "DOCUMENT_RECAP",
|
||||||
SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION",
|
SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION",
|
||||||
DOCUMENT_REMINDER: "DOCUMENT_REMINDER",
|
DOCUMENT_REMINDER: "DOCUMENT_REMINDER",
|
||||||
DOCUMENT_SEND: "DOCUMENT_SEND",
|
DOCUMENT_SEND: "DOCUMENT_SEND",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
app.post('/api/send-email', validateEmail, async (req, res) => {
|
app.post('/api/send-email', validateEmail, async (req, res) => {
|
||||||
const { email, firstName, lastName, officeName, template } = req.body;
|
const { email, firstName, lastName, officeName, template } = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const templateVariables = {
|
const templateVariables = {
|
||||||
first_name: firstName || '',
|
first_name: firstName || '',
|
||||||
last_name: lastName || '',
|
last_name: lastName || '',
|
||||||
office_name: officeName || '',
|
office_name: officeName || '',
|
||||||
link: `${process.env.APP_HOST}`
|
link: `${process.env.APP_HOST}`
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await EmailService.sendTransactionalEmail(
|
const result = await EmailService.sendTransactionalEmail(
|
||||||
email,
|
email,
|
||||||
ETemplates[template],
|
ETemplates[template],
|
||||||
'Votre notaire vous envoie un message',
|
'Votre notaire vous envoie un message',
|
||||||
templateVariables
|
templateVariables
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
// Ajouter aux emails en attente pour réessayer plus tard
|
// Add to pending emails to retry later
|
||||||
const emailId = `${email}-${Date.now()}`;
|
const emailId = `${email}-${Date.now()}`;
|
||||||
pendingEmails.set(emailId, {
|
pendingEmails.set(emailId, {
|
||||||
to: email,
|
to: email,
|
||||||
templateName: ETemplates[template],
|
templateName: ETemplates[template],
|
||||||
subject: 'Votre notaire vous envoie un message',
|
subject: 'Votre notaire vous envoie un message',
|
||||||
templateVariables,
|
templateVariables,
|
||||||
attempts: 1,
|
attempts: 1,
|
||||||
lastAttempt: Date.now()
|
lastAttempt: Date.now()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Email envoyé avec succès'
|
message: 'Email envoyé avec succès'
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur:', error);
|
console.error('Erreur:', error);
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Erreur serveur lors de l\'envoi de l\'email'
|
message: 'Erreur serveur lors de l\'envoi de l\'email'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/api/subscribe-to-list', validateEmail, async (req, res) => {
|
app.post('/api/subscribe-to-list', validateEmail, async (req, res) => {
|
||||||
const { email } = req.body;
|
const { email } = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await EmailService.addToMailchimpList(email);
|
const result = await EmailService.addToMailchimpList(email);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Inscription à la liste réussie'
|
message: 'Inscription à la liste réussie'
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Échec de l\'inscription à la liste'
|
message: 'Échec de l\'inscription à la liste'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur:', error);
|
console.error('Erreur:', error);
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Erreur serveur lors de l\'inscription'
|
message: 'Erreur serveur lors de l\'inscription'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Système de retry automatique
|
// Automatic retry system
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
EmailService.retryFailedEmails();
|
EmailService.retryFailedEmails();
|
||||||
}, 60000); // Vérifie toutes les minutes
|
}, 60000); // Check every minute
|
||||||
|
|
||||||
// Démarrage du serveur
|
app.listen(PORT, () => {
|
||||||
app.listen(config.PORT, () => {
|
console.log(`Server is running on port ${PORT}`);
|
||||||
console.log(`Serveur démarré sur le port ${config.PORT}`);
|
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user