Initial commit

This commit is contained in:
Torsten Schulz (notebook)
2024-08-16 16:34:23 +02:00
commit 31ca0979ce
4216 changed files with 499206 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import { register, activateUser, login, logout } from '../services/authService.js';
const registerUser = async (req, res, next) => {
try {
const { email, password } = req.body;
const user = await register(email, password);
res.status(201).json(user);
} catch (error) {
next(error);
}
};
const activate = async (req, res, next) => {
try {
const { activationCode } = req.params;
const user = await activateUser(activationCode);
res.status(200).json(user);
} catch (error) {
next(error);
}
};
const loginUser = async (req, res, next) => {
try {
const { email, password } = req.body;
const result = await login(email, password);
res.status(200).json(result);
} catch (error) {
next(error);
}
};
const logoutUser = async(req, res) => {
const { userid: userId, authtoken: authToken } = req.headers;
try {
logout(userId, authToken);
} catch (error) {
res.status(401).json({ msg: 'not found' });
}
res.status(200).json({ msg: 'loggedout' });
}
export { registerUser, activate, loginUser, logoutUser };

View File

@@ -0,0 +1,86 @@
import Club from '../models/Club.js';
import UserClub from '../models/UserClub.js';
import User from '../models/User.js';
import { Op, fn, where, col } from 'sequelize';
import { getUserByToken } from '../utils/userUtils.js';
const getClubs = async (req, res) => {
const clubs = await Club.findAll();
console.log(clubs);
res.status(200).json(clubs);
};
const addClub = async (req, res) => {
const { hashedId: token} = req.headers;
const { name: clubName } = req.body;
const club = await Club.findOne({
where: where(fn('LOWER', col('name')), 'LIKE', `%${clubName.toLowerCase()}%`)
});
const user = getUserByToken(token);
if (club) {
res.status(409).json({ error: "alreadyexists" });
return;
}
const newClub = Club.create({ name: clubName });
UserClub.create({userId: user.id, clubId: newClub.id });
res.status(200).json(newClub);
}
const getClub = async(req, res) => {
const { hashedId: token } = req.headers;
const { clubid: clubId } = req.params;
const user = getUserByToken(token);
const access = await UserClub.findAll({
where: {
userId: user.id,
clubId: clubId,
approved: true
}
});
if (access.length === 0) {
res.status(403).json({ error: "noaccess" });
return;
}
const club = await Club.findOne({ where: { id: clubId }});
res.status(200).json(club);
}
const approveClubAccess = async(req, res) => {
const { hashedId: token } = req.headers;
const { clubid: clubId, approveemail: toApproveEmail } = req.body;
const user = getUserByToken(token);
const access = await UserClub.findAll({
where: {
userId: user.id,
clubId: clubId,
approved: true
}
});
if (access.length === 0) {
res.status(403).json({ error: "noaccess" });
return;
}
const toApproveUser = await User.findOne({
where: {
email: toApproveEmail
}
});
if (!toApproveUser) {
res.status(404).json({ error: 'usernotfound' });
return;
}
const toApproveUserClub = UserClub.findAll({
where: {
userId: toApproveUser.id,
clubId: clubId
}
});
if (!toApproveUserClub) {
res.status(404).json({ error: 'norequest' });
return;
}
await toApproveUserClub.update({ approved: true });
res.status(200).json({ status: 'ok' });
}
export { getClubs, addClub, getClub, approveClubAccess };

View File

@@ -0,0 +1,19 @@
import MemberService from "../services/memberService";
const getClubMembers = async(req, res) => {
const { clubid: clubId } = req.body;
res.status(200).json(MemberService.getClubMembers(clubId));
}
const getWaitingApprovals = async(req, res) => {
try {
const { clubid: clubId } = req.body;
const { hashedId: userToken} = req.headers;
const waitingApprovals = MemberService.getApprovalRequests(userToken, clubId);
res.status(200).json(waitingApprovals);
} catch(error) {
res.status(403).json({ error: error });
}
}
export default { getClubMembers, getWaitingApprovals };