最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin46浏览0评论

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

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

我正在构建一个医生预约 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小时。 请帮助我构建请求的逻辑以及如何实现它。

回答如下:
发布评论

评论列表(0)

  1. 暂无评论