File size: 6,650 Bytes
e3fefe8 | 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | const ProfessionalModel = require('../models/professionalModel');
const UserModel = require('../models/userModel');
const BookingModel = require('../models/bookingModel');
const { dbGet } = require('../config/firebase');
const GeolocationController = {
/**
* Get nearby professionals based on customer location and filters.
* Requires: lat, lng in query/body
* Optional: radiusKm (default 10), serviceType, minRating, maxPrice
*/
async getNearbyProfessionals(req, res) {
try {
const { lat, lng, radiusKm = 10, serviceType, minRating, maxPrice } = req.query || req.body;
if (!lat || !lng) {
return res.status(400).json({
success: false,
message: 'lat and lng are required.',
});
}
const latNum = parseFloat(lat);
const lngNum = parseFloat(lng);
if (isNaN(latNum) || isNaN(lngNum)) {
return res.status(400).json({
success: false,
message: 'lat and lng must be valid numbers.',
});
}
const radiusNum = radiusKm ? parseFloat(radiusKm) : 10;
const filters = {
lat: latNum,
lng: lngNum,
radiusKm: radiusNum,
};
if (serviceType) filters.serviceType = serviceType;
if (minRating) filters.minRating = parseFloat(minRating);
if (maxPrice) filters.maxPrice = parseFloat(maxPrice);
const nearby = await ProfessionalModel.getNearby(filters);
return res.status(200).json({
success: true,
data: {
professionals: nearby,
count: nearby.length,
},
});
} catch (error) {
console.error('getNearbyProfessionals error:', error);
return res.status(500).json({
success: false,
message: 'Failed to fetch nearby professionals: ' + error.message,
});
}
},
/**
* Get professional location and phone ONLY if booking is confirmed (payment done).
* Requires: bookingId (or booking can have professionalId + customerId)
* Returns: professional location, phone (only if booking is confirmed)
*/
async getProfessionalLocationForBooking(req, res) {
try {
const { uid: customerId } = req.user;
const { bookingId, professionalId } = req.body;
if (!bookingId && !professionalId) {
return res.status(400).json({
success: false,
message: 'bookingId or professionalId is required.',
});
}
let booking = null;
let profId = professionalId;
if (bookingId) {
// Fetch booking to verify customer and check payment
booking = await BookingModel.getById(bookingId);
if (!booking) {
return res.status(404).json({ success: false, message: 'Booking not found.' });
}
// Verify customer owns this booking
if (booking.customerId !== customerId) {
return res.status(403).json({
success: false,
message: 'You are not authorized to view this booking.',
});
}
// Check if booking is confirmed (payment done)
if (!['confirmed', 'in_progress', 'completed'].includes(booking.status)) {
return res.status(403).json({
success: false,
message: `Professional location is only available after booking is confirmed. Current status: ${booking.status}`,
});
}
profId = booking.professionalId;
} else {
// If professionalId is provided directly, try to find a confirmed booking between them
const bookingPath = `bookings`;
const allBookings = await dbGet(bookingPath);
if (allBookings && typeof allBookings === 'object') {
for (const bid in allBookings) {
const b = allBookings[bid];
if (b.customerId === customerId && b.professionalId === profId && ['confirmed', 'in_progress', 'completed'].includes(b.status)) {
booking = { ...b, id: bid };
break;
}
}
}
if (!booking) {
return res.status(403).json({
success: false,
message: 'No confirmed booking found with this professional.',
});
}
}
// Fetch professional and user info
const professional = await ProfessionalModel.getById(profId);
const user = await UserModel.getById(profId, true);
if (!professional || !user) {
return res.status(404).json({
success: false,
message: 'Professional not found.',
});
}
return res.status(200).json({
success: true,
data: {
professional: {
uid: professional.uid,
displayName: user.displayName || '',
photoURL: user.photoURL || '',
phoneNumber: user.phoneNumber || 'Not provided', // Revealed only after confirmed booking
lat: user.location?.lat || null,
lng: user.location?.lng || null,
address: user.address || '',
rating: user.rating || 0,
totalRatings: user.totalRatings || 0,
},
booking: {
id: booking.id || bookingId,
status: booking.status,
price: booking.price,
serviceType: booking.serviceType,
},
},
});
} catch (error) {
console.error('getProfessionalLocationForBooking error:', error);
return res.status(500).json({
success: false,
message: 'Failed to fetch professional location: ' + error.message,
});
}
},
/**
* Update professional location (for real-time tracking during active booking).
* Called by professional periodically during job (in_progress status).
*/
async updateProfessionalLocation(req, res) {
try {
const { uid: professionalId } = req.user;
const { lat, lng, address } = req.body;
if (!lat || !lng) {
return res.status(400).json({
success: false,
message: 'lat and lng are required.',
});
}
// Update user location
await UserModel.updateLocation(professionalId, lat, lng, address || '');
return res.status(200).json({
success: true,
data: {
message: 'Location updated successfully.',
location: { lat, lng, address: address || '' },
},
});
} catch (error) {
console.error('updateProfessionalLocation error:', error);
return res.status(500).json({
success: false,
message: 'Failed to update location: ' + error.message,
});
}
},
};
module.exports = GeolocationController;
|