|
|
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(); |
|
|
|
|
|
|
|
|
app.use(cors()); |
|
|
app.use(express.json()); |
|
|
|
|
|
|
|
|
|
|
|
let serviceAccount; |
|
|
|
|
|
if (process.env.FIREBASE_SERVICE_ACCOUNT) { |
|
|
|
|
|
serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT); |
|
|
} else { |
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get('/', (req, res) => { |
|
|
res.send('House of Ruqa API is Running π'); |
|
|
}); |
|
|
|
|
|
|
|
|
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 }); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
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 }); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.post('/api/request-booking', async (req, res) => { |
|
|
try { |
|
|
const { outfitId, userId, pickupDate, returnDate, totalAmount, outfitTitle } = req.body; |
|
|
|
|
|
|
|
|
const bookingRef = await db.collection('bookings').add({ |
|
|
outfitId, |
|
|
outfitTitle, |
|
|
userId, |
|
|
pickupDate, |
|
|
returnDate, |
|
|
totalAmount, |
|
|
status: 'pending', |
|
|
paymentVerified: false, |
|
|
timestamp: new Date().toISOString() |
|
|
}); |
|
|
|
|
|
res.status(200).json({ success: true, bookingId: bookingRef.id }); |
|
|
} catch (error) { |
|
|
res.status(500).json({ error: error.message }); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
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 }); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
const PORT = process.env.PORT || 7860; |
|
|
app.listen(PORT, () => { |
|
|
console.log(`π Server running on port ${PORT}`); |
|
|
}); |
|
|
|