Untitled

 avatar
unknown
javascript
a year ago
1.7 kB
6
Indexable
export const getHoldRooms = async (req: Req, res: Response): Promise<Response> => {
  try {
    if (!req.query.date) return res.status(400).send({ status: 400, message: "Date is required" });
    const session = req.session;
    const dateKey = new Date(new Date(req.query.date as string)).toISOString().split("T")[0] + "T00:00:00.000Z";
    let roomsByDate = await redis.getValue(dateKey);
    if (roomsByDate) roomsByDate = JSON.parse(roomsByDate);
    else roomsByDate = [];
    let holds = roomsByDate.filter((h: string) => h !== null);
    holds = Array.from(new Set([...holds.map((h: string) => h)]));
    const holdExpirations = await Promise.all(
      roomsByDate.map(async (hold: string) => ({
        id: hold,
        expiration: await redis.getExpirationTime(hold)
      }))
    );
    holds = holdExpirations.filter(h => h.expiration > 0).map(h => h.id);
    holds = holds.filter((h: string) => roomsByDate.includes(h));
    let ownHold = await redis.getValue(session!);
    if (ownHold) {
      ownHold = JSON.parse(ownHold);
      ownHold = ownHold.filter((hold: string) => new Set(holds).has(hold));
      ownHold = await Promise.all(ownHold.map(async (h: string) => ({ id: h, time: secondsToDhms(await redis.getExpirationTime(h)) })));
      ownHold = ownHold.filter((hold: any) => roomsByDate.includes(hold.id));
    } else ownHold = [];
    return res.status(200).send({ status: 200, message: "Holds data retrieved", data: { totalHold: holds, holdByCurrentUser: ownHold } });
  } catch (err) {
    console.error(err);
    return res.status(500).send({ status: 500, message: "Internal Server Error" });
  }
};
Editor is loading...
Leave a Comment