-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_auth.controller.ts
164 lines (132 loc) · 4.12 KB
/
user_auth.controller.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Request, Response } from "express";
import prisma from "../lib/prisma";
import { mailClient } from "../lib/redis";
import { nanoid } from "nanoid";
import { verificationMail } from "../lib/mail";
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
import { generateTeamleaderJWT, requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth";
import { createRolesForTeam } from "./role.controller";
import { any, z } from "zod";
export const TeamBody = z.object({
teamName: z.string().min(1),
leaderEmail: z.string().email(),
disciplineId: z.string().uuid(),
partFirstName: z.string().min(1),
partLastName: z.string().min(1),
partGroupId: z.string().uuid(),
});
interface CreateTeamBody {
teamName: string;
leaderEmail: string;
disciplineId: string;
partFirstName: string;
partLastName: string;
partGroupId: string;
}
export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Response) => {
const result = TeamBody.safeParse(req.body);
if (result.success === false) {
return res.status(400).json(
generateInvalidBodyError(
{
teamName: DataType.STRING,
leaderEmail: DataType.STRING,
disciplineId: DataType.UUID,
partFirstName: DataType.STRING,
partLastName: DataType.STRING,
partGroupId: DataType.UUID,
},
result.error
)
);
}
const body = result.data;
const group = prisma.group.findUnique({ where: { pid: body.partGroupId } });
if (typeof group == null) {
return res.status(404).json(generateError("Specified group was not found!"));
}
const discipline = prisma.discipline.findUnique({ where: { pid: body.disciplineId } });
if (typeof discipline == null) {
return res.status(404).json(generateError("Specified discipline was not found!"));
}
const team = await prisma.team.create({
data: {
leaderEmail: body.leaderEmail,
name: body.teamName,
roles: undefined,
discipline: { connect: { pid: body.disciplineId } },
participants: {
create: {
firstName: body.partFirstName,
lastName: body.partLastName,
relevance: "TEAMLEADER",
group: { connect: { pid: body.partGroupId } },
},
},
},
select: {
pid: true,
name: true,
discipline: { select: { pid: true } },
},
});
//TODO: maybe use returned amount of created use?
createRolesForTeam(team.pid);
const usid = nanoid();
(await mailClient).set(usid, team.pid);
verificationMail(req.body.leaderEmail, "eventname", usid);
res.status(201).json({ type: "success", payload: { team } });
};
export const requestToken = async (req: Request, res: Response) => {
const { teamId } = req.body || {};
if (!(typeof teamId === "string")) {
return res.status(400).json(generateInvalidBodyError({ teamId: DataType.STRING }));
}
const team = await prisma.team.findUnique({
where: {
pid: teamId,
},
});
if (!team) {
return res.status(404).json(generateError("Team does not exist!"));
}
const usid = nanoid();
(await mailClient).set(usid, team.pid);
verificationMail(team.leaderEmail, "eventname", usid);
res.status(200).json({ type: "sucess", payload: { message: "Email sent!" } });
};
export const verifyEmail = async (req: Request, res: Response) => {
const { code } = req.params || {};
if (!(typeof code === "string")) {
return res.status(400).json({
type: "error",
payload: {
message: "Invalid Request parameter",
},
});
}
const acc = await mailClient.get(code);
if (!acc) {
return res.status(404).json({
type: "error",
payload: {
message: "Invalid token. It might be expired",
},
});
}
const team = await prisma.team.update({
where: {
pid: acc,
},
data: {
verified: true,
},
});
mailClient.set(code, "");
const token = generateTeamleaderJWT(team);
res.cookie("teamLeaderToken", token, {
path: "/",
maxAge: 1000 * 60 * 60 * 24 * 4,
});
res.status(200).json({ type: "succes", payload: { token } });
};