aboutsummaryrefslogtreecommitdiffstats
path: root/server/group.ts
blob: be71cc3d12fd92ad038819779d6a4882aa5d2c48 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Router } from "express";
import {
  createGroup,
  getGroupDailyStatus,
  getGroupByJoinCode,
  recordGroupJoin,
} from "./db/groups";
import { getUtcDate } from "./shared";

export const groupRouter = Router();

groupRouter.post("/create-group", (req, res) => {
  const body = req.body as {
    name?: unknown;
    username?: unknown;
  };

  if (typeof body.name !== "string" || body.name.trim().length === 0) {
    res.status(400).json({ error: "Group name is required." });
    return;
  }

  try {
    const group = createGroup(body.name);

    if (typeof body.username === "string" && body.username.trim()) {
      recordGroupJoin(group.id, body.username);
    }

    res.json({
      group: {
        id: group.id,
        name: group.name,
        joinToken: group.joinCode,
      },
    });
  } catch (error) {
    res
      .status(500)
      .json({ error: "Group already exists, choose a different name" });
  }
});

groupRouter.post("/join-group", (req, res) => {
  const body = req.body as {
    joinToken?: unknown;
    username?: unknown;
  };

  // validate request
  if (typeof body.joinToken !== "string" || body.joinToken.trim().length === 0) {
    res.status(400).json({ error: "joinToken is required." });
    return;
  }
  if (typeof body.username !== "string" || body.username.trim().length === 0) {
    res.status(400).json({ error: "username is required." });
    return;
  }
  const group = getGroupByJoinCode(body.joinToken);
  if (!group) {
    res.status(404).json({ error: "Group not found for that token." });
    return;
  }
  try {
    recordGroupJoin(group.id, body.username);
  } catch {
    res.status(500).json({ error: "Unable to join group." });
    return;
  }

  res.json({
    groupId: group.id,
    groupName: group.name,
    joinToken: group.joinCode,
    username: body.username.trim(),
  });
});

groupRouter.get("/group-status", (req, res) => {
  const groupId = req.query.groupId;
  const date = req.query.date;
  const mode = req.query.mode;

  if (typeof groupId !== "string" || groupId.trim().length === 0) {
    res.status(400).json({ error: "groupId is required." });
    return;
  }

  const targetDate = typeof date === "string" && date.trim() ? date : getUtcDate();
  const normalizedMode = mode === "mv" ? "mv" : "daily";
  const status = getGroupDailyStatus(groupId, targetDate, normalizedMode);

  if (!status) {
    res.status(404).json({ error: "Group not found." });
    return;
  }

  res.json(status);
});
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage