Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const router = require("express").Router();
- require("dotenv").config();
- const Bagel = require("@bageldb/bagel-db");
- const keys = require("../../secret.js");
- let db = new Bagel(keys.ADMIN_TOKEN);
- const dayjs = require("dayjs");
- const isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
- dayjs.extend(isSameOrBefore);
- /**
- * Recurring
- * This route is a WebHook for BagelDb
- * It creates Recurring event in Bagel, events with repeat option, of month or week
- * Whenever a reservation collection is created, Bagel will call to this route
- * @param req.body.item the wanted item
- * @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
- */
- router.post("/recurring", async (req, res) => {
- //if item is not recurring - ignore
- if (!req.body.item.isRecurring || req.body.item.recurringOriginalRecordId) {
- return res.sendStatus(200);
- }
- //deleting the record id, so it will duplicate the record without it, and bagel will create a new id to each record
- delete req.body.item._id;
- req.body.item.recurringOriginalRecordId = req.body.itemID;
- let curDate = dayjs(req.body.item.start);
- let endingDate = dayjs(req.body.item.recurringContinueUntill);
- try {
- while (curDate.isSameOrBefore(endingDate)) {
- curDate = curDate.add(
- 1,
- req.body.item.recurringCycle._id == "c13i96a23akg00bvnj90"
- ? "M"
- : "w"
- );
- req.body.item.start = curDate;
- // req.body.item.end = dayjs(curDate).add(req.body.item.hours, "h");
- await db.collection("reservation").post(req.body.item);
- }
- return res.sendStatus(200);
- } catch (error) {
- console.log(error);
- return res.sendStatus(500);
- }
- });
- module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement