如何处理两个用户同时预约?

编程入门 行业动态 更新时间:2024-10-07 16:20:51

<a href=https://www.elefans.com/category/jswz/34/1771197.html style=如何处理两个用户同时预约?"/>

如何处理两个用户同时预约?

我正在构建一个医生预约 MERN 堆栈应用程序,并且我已经编写了用于检查预约可用性的代码。在我的代码中,我实现了任何两个约会都不应该冲突的逻辑(每个约会都是 1 小时)。

现在我正在尝试实现处理不同用户同时预订的两个约会的代码。

这是我的代码:

router.post("/check-booking-availability", AuthMiddleware, async (req, res) => {
    try {
      const date = moment(req.body.date, "DD-MM-YYYY").toISOString();
      const test = req.body['time']
      const [hours, minutes] = test.split(':');
      const dateObj = new Date();
      dateObj.setHours(hours);
      dateObj.setMinutes(minutes);
      dateObj.setHours(dateObj.getHours() + 1);
      const newHours = String(dateObj.getHours()).padStart(2, '0');
      const newMinutes = String(dateObj.getMinutes()).padStart(2, '0');
      const newTimeString = `${newHours}:${newMinutes}`;
      // console.log(newTimeString);

      const formattedTime = test
      const boundaries = await Doctor.find({_id : req.body.doctorId}).catch((e)=> console.log(e))
      const timings = boundaries[0].timings
      if(formattedTime < timings[0] || formattedTime > timings[1] || newTimeString> timings[1])
      {
        return res.status(200).send({
          message: "Appointments not available!",
          success: false,
        });
      }
      const fromTime = moment(req.body.time, "HH:mm")
        .subtract(1, "hours")
        .toISOString();
      const toTime = moment(req.body.time, "HH:mm").add(1, "hours").toISOString();
      const doctorId = req.body.doctorId;
      const appointments = await Appointment.find({
        doctorId,
        date,
        time: { $gte: fromTime, $lte: toTime },
      });
      if (appointments.length > 0) {
        return res.status(200).send({
          message: "Appointments not available",
          success: false,
        });
      } else {
        return res.status(200).send({
          message: "Appointments available",
          success: true,
        });
      }
    } catch (error) {
      console.log(error);
      res.status(500).send({
        message: "Error booking appointment",
        success: false,
        error,
      });
    }
  });

formattedTime
是检查用户时间是否在医院时间之间。
newTimeString
是检查所有约会是否都是1小时。 请帮助我构建请求的逻辑以及如何实现它。

回答如下:

更多推荐

如何处理两个用户同时预约?

本文发布于:2024-05-30 09:26:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   两个   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!