aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/admin/broadcast/index.js
blob: 470d61d34c17a6828c06ecf2317770958564b6d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { rateLimitStrict, redis } from "@/lib/redis";
import { getServerSession } from "next-auth";
import { authOptions } from "pages/api/auth/[...nextauth]";

export default async function handler(req, res) {
  // Check if the custom header "X-Your-Custom-Header" is present and has a specific value
  const sessions = await getServerSession(req, res, authOptions);

  const admin = sessions?.user?.name === process.env.ADMIN_USERNAME;
  // if req.method === POST and admin === false return 401
  if (!admin && req.method === "DELETE") {
    return res.status(401).json({ message: "Unauthorized" });
  }

  const customHeaderValue = req.headers["x-broadcast-key"];

  if (customHeaderValue !== "get-broadcast") {
    return res.status(401).json({ message: "Unauthorized" });
  }

  try {
    if (redis) {
      try {
        const ipAddress = req.socket.remoteAddress;
        await rateLimitStrict.consume(ipAddress);
      } catch (error) {
        return res.status(429).json({
          error: `Too Many Requests, retry after ${error.msBeforeNext / 1000}`,
        });
      }

      if (req.method === "POST") {
        const { message, startAt = undefined, show = false } = req.body;
        if (!message) {
          return res.status(400).json({ message: "Message is required" });
        }

        const broadcastContent = {
          message,
          startAt,
          show,
        };
        await redis.set(`broadcasts`, JSON.stringify(broadcastContent));
        return res.status(200).json({ message: "Broadcast created" });
      } else if (req.method === "DELETE") {
        const br = await redis.get(`broadcasts`);
        // set broadcast show as false
        if (br) {
          const broadcast = JSON.parse(br);
          broadcast.show = false;
          await redis.set(`broadcasts`, JSON.stringify(broadcast));
        }
        return res.status(200).json({ message: "Broadcast deleted" });
      } else if (req.method === "GET") {
        const getId = await redis.get(`broadcasts`);
        if (getId) {
          const broadcast = JSON.parse(getId);
          return res.status(200).json({
            message: broadcast.message,
            startAt: broadcast.startAt,
            show: broadcast.show,
          });
        } else {
          return res.status(200).json({ message: "No broadcast" });
        }
      }
    }

    return res.status(200).json({ message: "redis is not defined" });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: err.message });
  }
}