Browse Source

added expected participant count to appointment

liontix 9 months ago
parent
commit
07ae4a82c0
2 changed files with 44 additions and 7 deletions
  1. 40 7
      routes/members.js
  2. 4 0
      schemas/appointmentSchema.js

+ 40 - 7
routes/members.js

@@ -30,9 +30,9 @@ router.use((req, res, next) => {
  */
 router.post('/create', function(req, res, next) {
     const user = req.userId;
-    const { title, description, dueDate, place, duration, times, inputs, timeSpans } = req.body;
+    const { title, description, dueDate, place, duration, times, inputs, timeSpans, participants } = req.body;
 
-    if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times || !inputs || !timeSpans) {
+    if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times || !inputs || !timeSpans || !participants) {
         res.status(400).json({ 'message': 'Empty parameter' });
         return;
     }
@@ -53,7 +53,7 @@ router.post('/create', function(req, res, next) {
         return;
     }
 
-    const newTask = new Appointment({ user, title, description, dueDate, place, duration, times, inputs, timeSpans });
+    const newTask = new Appointment({ user, title, description, dueDate, place, duration, times, inputs, timeSpans, participants });
 
     newTask.save()
         .then(doc => res.json({ "id": doc._id }))
@@ -66,8 +66,8 @@ router.post('/create', function(req, res, next) {
 router.post('/modify', function (req, res, next) {
     const user = req.userId;
 
-    const { appointmentId, title, description, dueDate, place, times, inputs, timeSpans } = req.body;
-    if (!title || !dueDate || !description || !times || !inputs || !timeSpans) {
+    const { appointmentId, title, description, dueDate, place, times, inputs, timeSpans, participants } = req.body;
+    if (!title || !dueDate || !description || !times || !inputs || !timeSpans || !participants) {
         res.status(400).json({ 'error': 'Empty parameter' });
         return;
     }
@@ -79,7 +79,7 @@ router.post('/modify', function (req, res, next) {
 
     Appointment.updateOne(
         { _id: appointmentId, user: user },
-        { $set: { title, description, dueDate, place, times, inputs, timeSpans } }
+        { $set: { title, description, dueDate, place, times, inputs, timeSpans, participants } }
     )
         .then(result => {
             if (result.modifiedCount === 1) {
@@ -165,10 +165,39 @@ router.get('/appointments', function (req, res, next) {
         });
 });
 
+/**
+ * Count the participants of an appointment
+ */
+router.get('/bookings/count', async function (req, res, next) {
+    const user = req.userId;
+    
+    let data = [];
+
+    Appointment.find({ user: user })
+        .then(async appointments => {
+            console.log(appointments);
+            for (const element of appointments) {
+                const count = await Booking.countDocuments({appointment: element._id});
+                console.log(count);
+                data.push({appointmentId: element._id, count: count}); 
+            }
+        })
+        .catch(err => {
+            res.status(404).json({'message': "Appointments not found"});
+        });
+        res.json(data);
+});
+
+async function findAppointment(userId, appointmentId) {
+    const resp = await Appointment.find({ user: userId, _id: appointmentId });
+    return resp.length > 0;
+}
+
 /**
  * Get all bookings for a specific appointment.
  */
-router.get('/bookings', function (req, res, next) {
+router.get('/bookings', async function (req, res, next) {
+    const user = req.userId;
     const { appointmentId } = req.query;
 
     if (!appointmentId) {
@@ -176,6 +205,10 @@ router.get('/bookings', function (req, res, next) {
         return;
     }
 
+    if (!await findAppointment(user, appointmentId)) {
+        return res.status(400).json({'message': 'Not your appointment'});
+    }
+
     Booking.find({ appointment: appointmentId })
         .then(bookings => {
             if (bookings === null) {

+ 4 - 0
schemas/appointmentSchema.js

@@ -71,6 +71,10 @@ const appointmentSchema = new mongoose.Schema({
         type: String,
         required: true
     },
+    participants: {
+        type: Number,
+        required: true
+    },
     description: {
         type: String
     },