feat: implement comprehensive Firebase Functions backend for equipment management and migrate core repository services
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
const admin = require('firebase-admin');
|
||||
const handlebars = require('handlebars');
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const {EMAIL_CONFIG} = require('./emailConfig');
|
||||
const admin = require("firebase-admin");
|
||||
const handlebars = require("handlebars");
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const {EMAIL_CONFIG} = require("./emailConfig");
|
||||
|
||||
/**
|
||||
* Vérifie si l'utilisateur souhaite recevoir ce type d'alerte
|
||||
*/
|
||||
function checkAlertPreference(alertType, preferences) {
|
||||
const typeMapping = {
|
||||
'EVENT_CREATED': 'eventsNotifications',
|
||||
'EVENT_MODIFIED': 'eventsNotifications',
|
||||
'EVENT_CANCELLED': 'eventsNotifications',
|
||||
'LOST': 'equipmentNotifications',
|
||||
'EQUIPMENT_MISSING': 'equipmentNotifications',
|
||||
'DAMAGED': 'equipmentNotifications',
|
||||
'QUANTITY_MISMATCH': 'equipmentNotifications',
|
||||
'MAINTENANCE_REMINDER': 'maintenanceNotifications',
|
||||
'STOCK_LOW': 'stockNotifications',
|
||||
"EVENT_CREATED": "eventsNotifications",
|
||||
"EVENT_MODIFIED": "eventsNotifications",
|
||||
"EVENT_CANCELLED": "eventsNotifications",
|
||||
"LOST": "equipmentNotifications",
|
||||
"EQUIPMENT_MISSING": "equipmentNotifications",
|
||||
"DAMAGED": "equipmentNotifications",
|
||||
"QUANTITY_MISMATCH": "equipmentNotifications",
|
||||
"MAINTENANCE_REMINDER": "maintenanceNotifications",
|
||||
"STOCK_LOW": "stockNotifications",
|
||||
};
|
||||
|
||||
const prefKey = typeMapping[alertType];
|
||||
@@ -29,12 +29,12 @@ function checkAlertPreference(alertType, preferences) {
|
||||
*/
|
||||
async function prepareTemplateData(alert, user) {
|
||||
const data = {
|
||||
userName: `${user.firstName || ''} ${user.lastName || ''}`.trim() ||
|
||||
'Utilisateur',
|
||||
userName: `${user.firstName || ""} ${user.lastName || ""}`.trim() ||
|
||||
"Utilisateur",
|
||||
alertTitle: getAlertTitle(alert.type),
|
||||
alertMessage: alert.message,
|
||||
isCritical: alert.severity === 'CRITICAL',
|
||||
actionUrl: `${EMAIL_CONFIG.appUrl}${alert.actionUrl || '/alerts'}`,
|
||||
isCritical: alert.severity === "CRITICAL",
|
||||
actionUrl: `${EMAIL_CONFIG.appUrl}${alert.actionUrl || "/alerts"}`,
|
||||
appUrl: EMAIL_CONFIG.appUrl,
|
||||
unsubscribeUrl: `${EMAIL_CONFIG.appUrl}/my_account?tab=notifications`,
|
||||
year: new Date().getFullYear(),
|
||||
@@ -45,20 +45,20 @@ async function prepareTemplateData(alert, user) {
|
||||
if (alert.eventId) {
|
||||
try {
|
||||
const eventDoc = await admin.firestore()
|
||||
.collection('events')
|
||||
.collection("events")
|
||||
.doc(alert.eventId)
|
||||
.get();
|
||||
|
||||
if (eventDoc.exists) {
|
||||
const event = eventDoc.data();
|
||||
data.eventName = event.Name || event.name || 'Événement';
|
||||
data.eventName = event.Name || event.name || "Événement";
|
||||
if (event.StartDateTime || event.startDate) {
|
||||
const dateField = event.StartDateTime || event.startDate;
|
||||
const date = dateField.toDate ? dateField.toDate() : new Date(dateField);
|
||||
data.eventDate = date.toLocaleDateString('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
data.eventDate = date.toLocaleDateString("fr-FR", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "numeric",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ async function prepareTemplateData(alert, user) {
|
||||
if (alert.equipmentId) {
|
||||
try {
|
||||
const eqDoc = await admin.firestore()
|
||||
.collection('equipments')
|
||||
.collection("equipments")
|
||||
.doc(alert.equipmentId)
|
||||
.get();
|
||||
|
||||
@@ -90,18 +90,18 @@ async function prepareTemplateData(alert, user) {
|
||||
*/
|
||||
function getEmailSubject(alert) {
|
||||
const subjects = {
|
||||
'EVENT_CREATED': '📅 Nouvel événement créé',
|
||||
'EVENT_MODIFIED': '📝 Événement modifié',
|
||||
'EVENT_CANCELLED': '❌ Événement annulé',
|
||||
'LOST': '🔴 Alerte critique : Équipement perdu',
|
||||
'EQUIPMENT_MISSING': '⚠️ Équipement manquant',
|
||||
'DAMAGED': '⚠️ Équipement endommagé',
|
||||
'QUANTITY_MISMATCH': 'ℹ️ Quantité incorrecte',
|
||||
'MAINTENANCE_REMINDER': '🔧 Rappel de maintenance',
|
||||
'STOCK_LOW': '📦 Stock faible',
|
||||
"EVENT_CREATED": "📅 Nouvel événement créé",
|
||||
"EVENT_MODIFIED": "📝 Événement modifié",
|
||||
"EVENT_CANCELLED": "❌ Événement annulé",
|
||||
"LOST": "🔴 Alerte critique : Équipement perdu",
|
||||
"EQUIPMENT_MISSING": "⚠️ Équipement manquant",
|
||||
"DAMAGED": "⚠️ Équipement endommagé",
|
||||
"QUANTITY_MISMATCH": "ℹ️ Quantité incorrecte",
|
||||
"MAINTENANCE_REMINDER": "🔧 Rappel de maintenance",
|
||||
"STOCK_LOW": "📦 Stock faible",
|
||||
};
|
||||
|
||||
return subjects[alert.type] || '🔔 Nouvelle alerte - EM2 Events';
|
||||
return subjects[alert.type] || "🔔 Nouvelle alerte - EM2 Events";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,18 +109,18 @@ function getEmailSubject(alert) {
|
||||
*/
|
||||
function getAlertTitle(type) {
|
||||
const titles = {
|
||||
'EVENT_CREATED': 'Nouvel événement créé',
|
||||
'EVENT_MODIFIED': 'Événement modifié',
|
||||
'EVENT_CANCELLED': 'Événement annulé',
|
||||
'LOST': 'Équipement perdu',
|
||||
'EQUIPMENT_MISSING': 'Équipement manquant',
|
||||
'DAMAGED': 'Équipement endommagé',
|
||||
'QUANTITY_MISMATCH': 'Quantité incorrecte',
|
||||
'MAINTENANCE_REMINDER': 'Maintenance requise',
|
||||
'STOCK_LOW': 'Stock faible',
|
||||
"EVENT_CREATED": "Nouvel événement créé",
|
||||
"EVENT_MODIFIED": "Événement modifié",
|
||||
"EVENT_CANCELLED": "Événement annulé",
|
||||
"LOST": "Équipement perdu",
|
||||
"EQUIPMENT_MISSING": "Équipement manquant",
|
||||
"DAMAGED": "Équipement endommagé",
|
||||
"QUANTITY_MISMATCH": "Quantité incorrecte",
|
||||
"MAINTENANCE_REMINDER": "Maintenance requise",
|
||||
"STOCK_LOW": "Stock faible",
|
||||
};
|
||||
|
||||
return titles[type] || 'Nouvelle alerte';
|
||||
return titles[type] || "Nouvelle alerte";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,17 +129,17 @@ function getAlertTitle(type) {
|
||||
async function renderTemplate(templateName, data) {
|
||||
try {
|
||||
// Lire le template de base
|
||||
const basePath = path.join(__dirname, '..', 'templates', 'base-template.html');
|
||||
const baseTemplate = await fs.readFile(basePath, 'utf8');
|
||||
const basePath = path.join(__dirname, "..", "templates", "base-template.html");
|
||||
const baseTemplate = await fs.readFile(basePath, "utf8");
|
||||
|
||||
// Lire le template de contenu
|
||||
const contentPath = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'templates',
|
||||
"..",
|
||||
"templates",
|
||||
`${templateName}.html`,
|
||||
);
|
||||
const contentTemplate = await fs.readFile(contentPath, 'utf8');
|
||||
const contentTemplate = await fs.readFile(contentPath, "utf8");
|
||||
|
||||
// Compiler les templates
|
||||
const compileContent = handlebars.compile(contentTemplate);
|
||||
|
||||
Reference in New Issue
Block a user