File size: 3,193 Bytes
c701a3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const express = require('express');
const cors = require('cors');
const { initializeApp, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
require('dotenv').config();

const app = express();

// Middleware
app.use(cors());
app.use(express.json());

// --- FIREBASE ADMIN SETUP ---
// We check if we are in Production (HuggingFace) or Local
let serviceAccount;

if (process.env.FIREBASE_SERVICE_ACCOUNT) {
  // In Production: We will store the JSON inside an ENV variable
  serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT);
} else {
  // In Local: We read the file
  try {
    serviceAccount = require('./serviceAccountKey.json');
  } catch (e) {
    console.error("❌ Error: serviceAccountKey.json not found in server folder.");
  }
}

if (serviceAccount) {
  initializeApp({
    credential: cert(serviceAccount)
  });
  console.log("βœ… Firebase Admin Connected");
}

const db = getFirestore();

// --- ROUTES ---

// 1. Health Check (To verify server is running)
app.get('/', (req, res) => {
  res.send('House of Ruqa API is Running πŸ’Ž');
});

// 2. Get All Outfits
app.get('/api/outfits', async (req, res) => {
  try {
    const snapshot = await db.collection('outfits').get();
    if (snapshot.empty) {
      return res.json([]);
    }
    const outfits = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
    res.json(outfits);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 3. Get Single Outfit by ID
app.get('/api/outfits/:id', async (req, res) => {
  try {
    const docRef = db.collection('outfits').doc(req.params.id);
    const docSnap = await docRef.get();
    if (!docSnap.exists) {
      return res.status(404).json({ error: 'Outfit not found' });
    }
    res.json({ id: docSnap.id, ...docSnap.data() });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 4. Create Booking Request (Status: Pending)
app.post('/api/request-booking', async (req, res) => {
  try {
    const { outfitId, userId, pickupDate, returnDate, totalAmount, outfitTitle } = req.body;
    
    // Add to 'bookings' collection
    const bookingRef = await db.collection('bookings').add({
      outfitId,
      outfitTitle,
      userId,
      pickupDate,
      returnDate,
      totalAmount,
      status: 'pending', // Default status
      paymentVerified: false,
      timestamp: new Date().toISOString()
    });

    res.status(200).json({ success: true, bookingId: bookingRef.id });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 5. Admin Confirm Booking
app.post('/api/confirm-booking/:id', async (req, res) => {
  try {
    const bookingId = req.params.id;
    await db.collection('bookings').doc(bookingId).update({
      status: 'confirmed',
      paymentVerified: true
    });
    res.json({ success: true, message: "Booking confirmed" });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// --- SERVER START ---
const PORT = process.env.PORT || 7860; // 7860 is required for HuggingFace Spaces
app.listen(PORT, () => {
  console.log(`πŸš€ Server running on port ${PORT}`);
});