Advertisement
bokoness

recurring event

Mar 15th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const router = require("express").Router();
  2. require("dotenv").config();
  3. const Bagel = require("@bageldb/bagel-db");
  4. const keys = require("../../secret.js");
  5. let db = new Bagel(keys.ADMIN_TOKEN);
  6.  
  7. const dayjs = require("dayjs");
  8. const isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
  9. dayjs.extend(isSameOrBefore);
  10.  
  11. /**
  12.  * Recurring
  13.  * This route is a WebHook for BagelDb
  14.  * It creates Recurring event in Bagel, events with repeat option, of month or week
  15.  * Whenever a reservation collection is created, Bagel will call to this route
  16.  * @param req.body.item the wanted item
  17.  * @param req.body.item.isRecurring is the field that activates the functionality of this route, if it eqauls to false - the request will be ignored
  18.  */
  19. router.post("/recurring", async (req, res) => {
  20.     //if item is not recurring - ignore
  21.     if (!req.body.item.isRecurring || req.body.item.recurringOriginalRecordId) {
  22.         return res.sendStatus(200);
  23.     }
  24.     //deleting the record id, so it will duplicate the record without it, and bagel will create a new id to each record
  25.     delete req.body.item._id;
  26.     req.body.item.recurringOriginalRecordId = req.body.itemID;
  27.     let curDate = dayjs(req.body.item.start);
  28.     let endingDate = dayjs(req.body.item.recurringContinueUntill);
  29.     try {
  30.         while (curDate.isSameOrBefore(endingDate)) {
  31.             curDate = curDate.add(
  32.                 1,
  33.                 req.body.item.recurringCycle._id == "c13i96a23akg00bvnj90"
  34.                     ? "M"
  35.                     : "w"
  36.             );
  37.             req.body.item.start = curDate;
  38.             // req.body.item.end = dayjs(curDate).add(req.body.item.hours, "h");
  39.             await db.collection("reservation").post(req.body.item);
  40.         }
  41.         return res.sendStatus(200);
  42.     } catch (error) {
  43.         console.log(error);
  44.         return res.sendStatus(500);
  45.     }
  46. });
  47.  
  48. module.exports = router;
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement