feat: implement comprehensive Firebase Functions backend for equipment management and migrate core repository services
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
/**
|
||||
* Utilitaires d'authentification et d'autorisation
|
||||
*/
|
||||
const admin = require('firebase-admin');
|
||||
const logger = require('firebase-functions/logger');
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
|
||||
/**
|
||||
* Vérifie le token Firebase et retourne l'utilisateur
|
||||
*/
|
||||
async function authenticateUser(req) {
|
||||
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
|
||||
throw new Error('Unauthorized: No token provided');
|
||||
if (!req.headers.authorization || !req.headers.authorization.startsWith("Bearer ")) {
|
||||
throw new Error("Unauthorized: No token provided");
|
||||
}
|
||||
|
||||
const idToken = req.headers.authorization.split('Bearer ')[1];
|
||||
const idToken = req.headers.authorization.split("Bearer ")[1];
|
||||
try {
|
||||
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
||||
return decodedToken;
|
||||
} catch (e) {
|
||||
logger.error("Error verifying Firebase ID token:", e);
|
||||
throw new Error('Unauthorized: Invalid token');
|
||||
throw new Error("Unauthorized: Invalid token");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ async function authenticateUser(req) {
|
||||
* Récupère les données utilisateur depuis Firestore
|
||||
*/
|
||||
async function getUserData(uid) {
|
||||
const userDoc = await admin.firestore().collection('users').doc(uid).get();
|
||||
const userDoc = await admin.firestore().collection("users").doc(uid).get();
|
||||
if (!userDoc.exists) {
|
||||
return null;
|
||||
}
|
||||
return { uid, ...userDoc.data() };
|
||||
return {uid, ...userDoc.data()};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +40,7 @@ async function getRolePermissions(roleRef) {
|
||||
if (!roleRef) return [];
|
||||
|
||||
let roleId;
|
||||
if (typeof roleRef === 'string') {
|
||||
if (typeof roleRef === "string") {
|
||||
roleId = roleRef;
|
||||
} else if (roleRef.id) {
|
||||
roleId = roleRef.id;
|
||||
@@ -48,7 +48,7 @@ async function getRolePermissions(roleRef) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const roleDoc = await admin.firestore().collection('roles').doc(roleId).get();
|
||||
const roleDoc = await admin.firestore().collection("roles").doc(roleId).get();
|
||||
if (!roleDoc.exists) return [];
|
||||
|
||||
return roleDoc.data().permissions || [];
|
||||
@@ -74,7 +74,7 @@ async function isAdmin(uid) {
|
||||
|
||||
let roleId;
|
||||
const roleField = userData.role;
|
||||
if (typeof roleField === 'string') {
|
||||
if (typeof roleField === "string") {
|
||||
roleId = roleField;
|
||||
} else if (roleField && roleField.id) {
|
||||
roleId = roleField.id;
|
||||
@@ -82,22 +82,22 @@ async function isAdmin(uid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return roleId === 'ADMIN';
|
||||
return roleId === "ADMIN";
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si l'utilisateur est assigné à un événement
|
||||
*/
|
||||
async function isAssignedToEvent(uid, eventId) {
|
||||
const eventDoc = await admin.firestore().collection('events').doc(eventId).get();
|
||||
const eventDoc = await admin.firestore().collection("events").doc(eventId).get();
|
||||
if (!eventDoc.exists) return false;
|
||||
|
||||
const eventData = eventDoc.data();
|
||||
const workforce = eventData.workforce || [];
|
||||
|
||||
// workforce contient des références DocumentReference
|
||||
return workforce.some(ref => {
|
||||
if (typeof ref === 'string') return ref === uid;
|
||||
return workforce.some((ref) => {
|
||||
if (typeof ref === "string") return ref === uid;
|
||||
if (ref && ref.id) return ref.id === uid;
|
||||
return false;
|
||||
});
|
||||
@@ -113,7 +113,7 @@ async function authMiddleware(req, res, next) {
|
||||
req.uid = decodedToken.uid;
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(401).json({ error: error.message });
|
||||
res.status(401).json({error: error.message});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,12 +125,12 @@ function requirePermission(permission) {
|
||||
try {
|
||||
const hasAccess = await hasPermission(req.uid, permission);
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({ error: `Forbidden: Requires permission '${permission}'` });
|
||||
res.status(403).json({error: `Forbidden: Requires permission '${permission}'`});
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
res.status(403).json({error: error.message});
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -142,12 +142,12 @@ async function requireAdmin(req, res, next) {
|
||||
try {
|
||||
const adminAccess = await isAdmin(req.uid);
|
||||
if (!adminAccess) {
|
||||
res.status(403).json({ error: 'Forbidden: Admin access required' });
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
res.status(403).json({error: error.message});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
// Pour configurer : Définir SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS dans .env ou Firebase
|
||||
const getSmtpConfig = () => {
|
||||
return {
|
||||
host: process.env.SMTP_HOST || 'mail.em2events.fr',
|
||||
port: parseInt(process.env.SMTP_PORT || '465'),
|
||||
host: process.env.SMTP_HOST || "mail.em2events.fr",
|
||||
port: parseInt(process.env.SMTP_PORT || "465"),
|
||||
secure: true, // true pour port 465, false pour autres ports
|
||||
auth: {
|
||||
user: process.env.SMTP_USER || 'notify@em2events.fr',
|
||||
pass: process.env.SMTP_PASS || '',
|
||||
user: process.env.SMTP_USER || "notify@em2events.fr",
|
||||
pass: process.env.SMTP_PASS || "",
|
||||
},
|
||||
tls: {
|
||||
// Ne pas échouer sur certificats invalides
|
||||
@@ -24,12 +24,12 @@ const getSmtpConfig = () => {
|
||||
// Configuration email par défaut
|
||||
const EMAIL_CONFIG = {
|
||||
from: {
|
||||
name: 'EM2 Events',
|
||||
address: 'notify@em2events.fr',
|
||||
name: "EM2 Events",
|
||||
address: "notify@em2events.fr",
|
||||
},
|
||||
replyTo: 'contact@em2events.fr',
|
||||
replyTo: "contact@em2events.fr",
|
||||
// URL de l'application pour les liens
|
||||
appUrl: process.env.APP_URL || 'https://app.em2events.fr',
|
||||
appUrl: process.env.APP_URL || "https://app.em2events.fr",
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Helpers pour la manipulation de données Firestore
|
||||
*/
|
||||
const admin = require('firebase-admin');
|
||||
const admin = require("firebase-admin");
|
||||
|
||||
/**
|
||||
* Convertit les Timestamps Firestore en ISO strings pour JSON
|
||||
@@ -19,7 +19,7 @@ function serializeTimestamps(data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
for (const key in result) {
|
||||
const value = result[key];
|
||||
@@ -29,31 +29,31 @@ function serializeTimestamps(data) {
|
||||
}
|
||||
|
||||
// Gérer les Timestamps Firestore
|
||||
if (value.toDate && typeof value.toDate === 'function') {
|
||||
if (value.toDate && typeof value.toDate === "function") {
|
||||
result[key] = value.toDate().toISOString();
|
||||
}
|
||||
// Gérer les DocumentReference
|
||||
else if (value.path && value.id && typeof value.path === 'string') {
|
||||
else if (value.path && value.id && typeof value.path === "string") {
|
||||
result[key] = value.path;
|
||||
}
|
||||
// Gérer les GeoPoint
|
||||
else if (value.latitude !== undefined && value.longitude !== undefined) {
|
||||
result[key] = {
|
||||
latitude: value.latitude,
|
||||
longitude: value.longitude
|
||||
longitude: value.longitude,
|
||||
};
|
||||
}
|
||||
// Gérer les tableaux
|
||||
else if (Array.isArray(value)) {
|
||||
result[key] = value.map(item => {
|
||||
if (!item || typeof item !== 'object') return item;
|
||||
result[key] = value.map((item) => {
|
||||
if (!item || typeof item !== "object") return item;
|
||||
|
||||
// DocumentReference dans un tableau
|
||||
if (item.path && item.id) {
|
||||
return item.path;
|
||||
}
|
||||
// Timestamp dans un tableau
|
||||
if (item.toDate && typeof item.toDate === 'function') {
|
||||
if (item.toDate && typeof item.toDate === "function") {
|
||||
return item.toDate().toISOString();
|
||||
}
|
||||
// Objet normal
|
||||
@@ -61,7 +61,7 @@ function serializeTimestamps(data) {
|
||||
});
|
||||
}
|
||||
// Gérer les objets imbriqués (mais pas les objets Firestore)
|
||||
else if (typeof value === 'object' && !value._firestore && !value._path) {
|
||||
else if (typeof value === "object" && !value._firestore && !value._path) {
|
||||
result[key] = serializeTimestamps(value);
|
||||
}
|
||||
}
|
||||
@@ -75,10 +75,10 @@ function serializeTimestamps(data) {
|
||||
function deserializeTimestamps(data, timestampFields = []) {
|
||||
if (!data) return data;
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
for (const field of timestampFields) {
|
||||
if (result[field] && typeof result[field] === 'string') {
|
||||
if (result[field] && typeof result[field] === "string") {
|
||||
result[field] = admin.firestore.Timestamp.fromDate(new Date(result[field]));
|
||||
}
|
||||
}
|
||||
@@ -92,15 +92,15 @@ function deserializeTimestamps(data, timestampFields = []) {
|
||||
function serializeReferences(data) {
|
||||
if (!data) return data;
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
for (const key in result) {
|
||||
if (result[key] && result[key].path && typeof result[key].path === 'string') {
|
||||
if (result[key] && result[key].path && typeof result[key].path === "string") {
|
||||
// C'est une DocumentReference
|
||||
result[key] = result[key].id;
|
||||
} else if (Array.isArray(result[key])) {
|
||||
result[key] = result[key].map(item => {
|
||||
if (item && item.path && typeof item.path === 'string') {
|
||||
result[key] = result[key].map((item) => {
|
||||
if (item && item.path && typeof item.path === "string") {
|
||||
return item.id;
|
||||
}
|
||||
return item;
|
||||
@@ -117,7 +117,7 @@ function serializeReferences(data) {
|
||||
function maskSensitiveFields(data, canViewSensitive) {
|
||||
if (canViewSensitive) return data;
|
||||
|
||||
const masked = { ...data };
|
||||
const masked = {...data};
|
||||
|
||||
// Masquer les prix si pas de permission manage_equipment
|
||||
delete masked.purchasePrice;
|
||||
@@ -143,34 +143,34 @@ function paginate(query, limit = 50, startAfter = null) {
|
||||
* Filtre les événements annulés
|
||||
*/
|
||||
function filterCancelledEvents(events) {
|
||||
return events.filter(event => event.status !== 'CANCELLED');
|
||||
return events.filter((event) => event.status !== "CANCELLED");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertit les IDs en DocumentReference pour maintenir la compatibilité avec l'ancien format
|
||||
* @param {Object} data - Données de l'événement
|
||||
* @returns {Object} - Données avec DocumentReference
|
||||
* @return {Object} - Données avec DocumentReference
|
||||
*/
|
||||
function convertIdsToReferences(data) {
|
||||
if (!data) return data;
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
// Convertir EventType (ID → DocumentReference)
|
||||
if (result.EventType && typeof result.EventType === 'string' && !result.EventType.includes('/')) {
|
||||
result.EventType = admin.firestore().collection('eventTypes').doc(result.EventType);
|
||||
if (result.EventType && typeof result.EventType === "string" && !result.EventType.includes("/")) {
|
||||
result.EventType = admin.firestore().collection("eventTypes").doc(result.EventType);
|
||||
}
|
||||
|
||||
// Convertir customer (ID → DocumentReference)
|
||||
if (result.customer && typeof result.customer === 'string' && !result.customer.includes('/')) {
|
||||
result.customer = admin.firestore().collection('customers').doc(result.customer);
|
||||
if (result.customer && typeof result.customer === "string" && !result.customer.includes("/")) {
|
||||
result.customer = admin.firestore().collection("customers").doc(result.customer);
|
||||
}
|
||||
|
||||
// Convertir workforce (IDs → DocumentReference)
|
||||
if (Array.isArray(result.workforce)) {
|
||||
result.workforce = result.workforce.map(item => {
|
||||
if (typeof item === 'string' && !item.includes('/')) {
|
||||
return admin.firestore().collection('users').doc(item);
|
||||
result.workforce = result.workforce.map((item) => {
|
||||
if (typeof item === "string" && !item.includes("/")) {
|
||||
return admin.firestore().collection("users").doc(item);
|
||||
}
|
||||
return item;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user