feat: implement comprehensive Firebase Functions backend for equipment management and migrate core repository services
This commit is contained in:
@@ -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