| import LocationHistory from "../model/locationHistory.model"; |
| import User from "../model/user.model"; |
| import { Request, Response } from "express"; |
|
|
| |
| const getTodayDateString = (): string => { |
| const now = new Date(); |
| return now.toISOString().split('T')[0]; |
| }; |
|
|
| |
| export const getAllUsersLatest = async (req: Request, res: Response) => { |
| try { |
| const users = await User.find({ isActive: true }).select('location lastActive username profilePhoto role'); |
| return res.json(users.filter(u => u.location && u.location.coordinates && u.location.coordinates.length === 2)); |
| } catch (err) { |
| console.error(err); |
| return res.status(500).json({ message: 'Server error' }); |
| } |
| }; |
|
|
| export const updateLocation = async (req: Request, res: Response) => { |
| try { |
| const userId = req.userId; |
| const { longitude, latitude } = req.body; |
|
|
| if (!longitude || !latitude) { |
| return res.status(400).json({ message: "Location missing" }); |
| } |
|
|
| const today = getTodayDateString(); |
| const coordinates: [number, number] = [longitude, latitude]; |
|
|
| |
| await User.findByIdAndUpdate(userId, { |
| location: { |
| type: "Point", |
| coordinates |
| }, |
| lastActive: new Date() |
| }); |
|
|
| |
| await LocationHistory.findOneAndUpdate( |
| { user: userId, date: today }, |
| { |
| $push: { |
| locations: { |
| coordinates, |
| timestamp: new Date() |
| } |
| }, |
| $set: { |
| lastLocation: { |
| type: "Point", |
| coordinates |
| } |
| } |
| }, |
| { upsert: true, new: true } |
| ); |
|
|
| return res.json({ message: "Location updated" }); |
| } catch (err) { |
| console.error(err); |
| return res.status(500).json({ message: "Server error" }); |
| } |
| }; |
|
|
| |
| export const getLocationHistory = async (req: Request, res: Response) => { |
| try { |
| const userId = req.userId; |
| const { date } = req.query; |
|
|
| if (!date || typeof date !== 'string') { |
| return res.status(400).json({ message: "Date required (YYYY-MM-DD)" }); |
| } |
|
|
| const history = await LocationHistory.findOne({ user: userId, date }); |
|
|
| if (!history) { |
| return res.status(404).json({ message: "No location data for this date" }); |
| } |
|
|
| return res.json({ |
| date: history.date, |
| locations: history.locations, |
| totalPoints: history.locations.length |
| }); |
| } catch (err) { |
| console.error(err); |
| return res.status(500).json({ message: "Server error" }); |
| } |
| }; |
|
|
| |
| export const getLocationHistoryRange = async (req: Request, res: Response) => { |
| try { |
| const userId = req.userId; |
| const { startDate, endDate } = req.query; |
|
|
| if (!startDate || !endDate || typeof startDate !== 'string' || typeof endDate !== 'string') { |
| return res.status(400).json({ message: "startDate and endDate required (YYYY-MM-DD)" }); |
| } |
|
|
| const history = await LocationHistory.find({ |
| user: userId, |
| date: { $gte: startDate, $lte: endDate } |
| }).sort({ date: 1 }); |
|
|
| return res.json({ |
| days: history.length, |
| history: history.map(h => ({ |
| date: h.date, |
| locationCount: h.locations.length, |
| lastLocation: h.lastLocation |
| })) |
| }); |
| } catch (err) { |
| console.error(err); |
| return res.status(500).json({ message: "Server error" }); |
| } |
| }; |
|
|
| |
| export const getNearbyUsers = async (req: Request, res: Response) => { |
| try { |
| const userId = req.userId; |
| const { maxDistance = 5000 } = req.query; |
|
|
| const user = await User.findById(userId); |
| if (!user?.location) { |
| return res.status(400).json({ message: "User location not set" }); |
| } |
|
|
| const nearbyUsers = await User.find({ |
| _id: { $ne: userId }, |
| location: { |
| $near: { |
| $geometry: user.location, |
| $maxDistance: Number(maxDistance) |
| } |
| } |
| }).limit(20).select('username profilePhoto location role'); |
|
|
| return res.json({ nearbyUsers }); |
| } catch (err) { |
| console.error(err); |
| return res.status(500).json({ message: "Server error" }); |
| } |
| }; |