176 lines
4.4 KiB
TypeScript
176 lines
4.4 KiB
TypeScript
import express from 'express';
|
|
import Sessions from './Schema/sessions.schema.js'
|
|
import Teachers from '../Teachers/Schema/Teachers.schema.js';
|
|
import { Types } from 'mongoose';
|
|
|
|
const SessionsRoutes = express.Router();
|
|
|
|
// Get All Sessions
|
|
SessionsRoutes.get('/', async(req, res) => {
|
|
|
|
})
|
|
|
|
// Add New User for teacher
|
|
SessionsRoutes.post('/', async(req, res) => {
|
|
const {teacherId, startTime, endTime } = req.body;
|
|
|
|
//Check for teacher existance
|
|
const existingTeacher = await Teachers.findOne({_id: teacherId});
|
|
|
|
console.log('existingTeacher', existingTeacher)
|
|
if(!existingTeacher){
|
|
return res.status(400).send({message: 'Teacher Does not exist please provide valid teacherId.'})
|
|
}
|
|
|
|
// Check endTime must be greater then startTime
|
|
if(new Date(endTime) <= new Date(startTime)) {
|
|
return res.status(400).send({message: 'End time must be greater then start time.'})
|
|
}
|
|
|
|
const newSession = new Sessions({teacherId, startTime, endTime});
|
|
const result = await newSession.save();
|
|
|
|
if(result) {
|
|
return res.status(201).send({message: 'Session Added Successfully', result})
|
|
}
|
|
|
|
})
|
|
|
|
// Get Available Sessions
|
|
SessionsRoutes.get('/available', async(req: any, res) => {
|
|
const dateTimestamp = req.query.dateTimestamp;
|
|
|
|
const dateString = dateTimestamp.slice(0, 10);
|
|
|
|
// us standard dates we used here for simplicity
|
|
const startOfDay = new Date(`${dateString}T00:00:00.000Z`);
|
|
const endOfDay = new Date(`${dateString}T23:59:59.999Z`);
|
|
|
|
const availableSessions = await Sessions.aggregate([
|
|
{
|
|
$match: {
|
|
startTime: { $lte: endOfDay },
|
|
endTime: { $gte: startOfDay },
|
|
status: "AVAILABLE",
|
|
}
|
|
}
|
|
]);
|
|
|
|
return res.status(200).send({message: 'Available Session', availableSessions})
|
|
|
|
})
|
|
|
|
|
|
// Book Available Sessions
|
|
SessionsRoutes.post('/:id/book', async(req: any, res) => {
|
|
|
|
const {id} = req.params;
|
|
const {userId} = req.body;
|
|
|
|
const existingSession = await Sessions.findOne({_id: id});
|
|
|
|
if(!existingSession) {
|
|
return res.status(400).send({message: 'Session does not exist.'})
|
|
}
|
|
if(existingSession.status !== 'AVAILABLE') {
|
|
return res.status(400).send({message: 'This session is not Available'});
|
|
}
|
|
|
|
const result = await Sessions.findByIdAndUpdate(
|
|
id,
|
|
{ status: "BOOKED", userId: userId },
|
|
{ new: true }
|
|
);
|
|
|
|
// console.log('re-------', result)
|
|
|
|
if(result?.status === 'BOOKED') {
|
|
return res.status(200).send({message: 'Session Booked', result})
|
|
} else {
|
|
return res.status(400).send({message: 'Error during Booking session.'})
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
// Update Available Sessions To Complete : User Id also needed to update
|
|
SessionsRoutes.patch('/:id/complete', async(req: any, res) => {
|
|
|
|
const {id} = req.params;
|
|
const {userId} = req.body;
|
|
|
|
const existingSession = await Sessions.findOne({_id: id, });
|
|
|
|
if(!existingSession) {
|
|
return res.status(400).send({message: 'Session does not exist.'})
|
|
}
|
|
if(existingSession.status !== 'BOOKED') {
|
|
return res.status(400).send({message: 'This session is not BOOKED'});
|
|
}
|
|
|
|
const result = await Sessions.findByIdAndUpdate(
|
|
id,
|
|
{ status: "COMPLETED", completedAt: Date.now() },
|
|
{ new: true }
|
|
);
|
|
|
|
// console.log('re-------', result)
|
|
|
|
if(result?.status === 'COMPLETED') {
|
|
return res.status(200).send({message: 'Session Marked Completed', result})
|
|
} else {
|
|
return res.status(400).send({message: 'Error during updating session.'})
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
// Get Sessions for User : With Seperation
|
|
SessionsRoutes.get('/:id/sessions', async(req,res) => {
|
|
|
|
let userId = new Types.ObjectId(req.params.id);
|
|
console.log('userId', userId)
|
|
|
|
const result = await Sessions.aggregate([
|
|
{
|
|
$match: {
|
|
userId: userId
|
|
}
|
|
},
|
|
{
|
|
$facet: {
|
|
upcomingSessions: [
|
|
{
|
|
$match: {
|
|
status: 'BOOKED'
|
|
}
|
|
}
|
|
],
|
|
completedSessions: [
|
|
{
|
|
$match: {
|
|
status: 'COMPLETED'
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
data: result[0] ?? {
|
|
upcomingSessions: [],
|
|
completedSessions: []
|
|
}
|
|
});
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
export default SessionsRoutes; |