Prashikshak / API /src /controller /locationHistory.controller.ts
Abhisingh-18's picture
Initial commit: Prashikshak - disaster management training platform
9a92a42
Raw
History Blame Contribute Delete
4.39 kB
import LocationHistory from "../model/locationHistory.model";
import User from "../model/user.model";
import { Request, Response } from "express";
// Helper to get current date in YYYY-MM-DD format
const getTodayDateString = (): string => {
const now = new Date();
return now.toISOString().split('T')[0];
};
// GET /location/users/latest
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];
// 1) Update latest location in User model
await User.findByIdAndUpdate(userId, {
location: {
type: "Point",
coordinates
},
lastActive: new Date()
});
// 2) Upsert location history for today
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" });
}
};
// Get user's location history for a specific date
export const getLocationHistory = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { date } = req.query; // Expected format: YYYY-MM-DD
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" });
}
};
// Get user's location history for a date range
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" });
}
};
// Get nearby users based on their last known location
export const getNearbyUsers = async (req: Request, res: Response) => {
try {
const userId = req.userId;
const { maxDistance = 5000 } = req.query; // default 5km
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" });
}
};