Some fixes
This commit is contained in:
@@ -21,6 +21,7 @@ const activate = async (req, res, next) => {
|
||||
};
|
||||
|
||||
const loginUser = async (req, res, next) => {
|
||||
console.log('login');
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
const result = await login(email, password);
|
||||
|
||||
@@ -6,49 +6,72 @@ 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 addClub = async (req, res) => {
|
||||
const { authcode: 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);
|
||||
const user = await 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 });
|
||||
UserClub.create({ userId: user.id, clubId: newClub.id, approved: true });
|
||||
res.status(200).json(newClub);
|
||||
}
|
||||
|
||||
const getClub = async(req, res) => {
|
||||
const { hashedId: token } = req.headers;
|
||||
const getClub = async (req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubid: clubId } = req.params;
|
||||
const user = getUserByToken(token);
|
||||
console.log('load club', token)
|
||||
const user = await getUserByToken(token);
|
||||
console.log(user);
|
||||
const access = await UserClub.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
clubId: clubId,
|
||||
approved: true
|
||||
clubId: clubId,
|
||||
}
|
||||
});
|
||||
if (access.length === 0) {
|
||||
res.status(403).json({ error: "noaccess" });
|
||||
if (access.length === 0 || !access[0].approved) {
|
||||
res.status(403).json({ error: "noaccess", status: access.length === 0 ? "notrequested" : "requested" });
|
||||
return;
|
||||
}
|
||||
const club = await Club.findOne({ where: { id: clubId }});
|
||||
res.status(200).json(club);
|
||||
try {
|
||||
const club = await Club.findByPk(clubId, {
|
||||
include: [
|
||||
{
|
||||
model: Member,
|
||||
as: 'Members',
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: 'Users',
|
||||
through: {
|
||||
attributes: []
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!club) {
|
||||
return res.status(404).json({ message: 'Club not found' });
|
||||
}
|
||||
res.status(200).json(club);
|
||||
} catch (error) {
|
||||
console.error('err', error);
|
||||
res.status(500).json({ message: 'Server error' });
|
||||
}
|
||||
}
|
||||
|
||||
const approveClubAccess = async(req, res) => {
|
||||
const { hashedId: token } = req.headers;
|
||||
const approveClubAccess = async (req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubid: clubId, approveemail: toApproveEmail } = req.body;
|
||||
const user = getUserByToken(token);
|
||||
const user = await getUserByToken(token);
|
||||
const access = await UserClub.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
@@ -83,4 +106,34 @@ const approveClubAccess = async(req, res) => {
|
||||
res.status(200).json({ status: 'ok' });
|
||||
}
|
||||
|
||||
export { getClubs, addClub, getClub, approveClubAccess };
|
||||
const requestClubAccess = async(req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubid: clubId } = req.params;
|
||||
const user = await getUserByToken(token);
|
||||
console.log(user);
|
||||
const access = await UserClub.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
clubId: clubId,
|
||||
}
|
||||
});
|
||||
if (access.length > 0) {
|
||||
res.status(409).json({ err: "alreadyrequested"});
|
||||
return;
|
||||
}
|
||||
const club = Club.findOne({ where: {
|
||||
id: clubId
|
||||
}});
|
||||
if (!club) {
|
||||
res.status(404).json({ err: "clubnotfound" });
|
||||
return;
|
||||
}
|
||||
UserClub.create({
|
||||
userId: user.id,
|
||||
clubId: clubId,
|
||||
approved: false
|
||||
});
|
||||
res.status(200).json({});
|
||||
}
|
||||
|
||||
export { getClubs, addClub, getClub, approveClubAccess, requestClubAccess };
|
||||
@@ -1,4 +1,4 @@
|
||||
import MemberService from "../services/memberService";
|
||||
import MemberService from "../services/memberService.js";
|
||||
|
||||
const getClubMembers = async(req, res) => {
|
||||
const { clubid: clubId } = req.body;
|
||||
@@ -8,7 +8,7 @@ const getClubMembers = async(req, res) => {
|
||||
const getWaitingApprovals = async(req, res) => {
|
||||
try {
|
||||
const { clubid: clubId } = req.params;
|
||||
const { hashedId: userToken} = req.headers;
|
||||
const { authcode: userToken} = req.headers;
|
||||
const waitingApprovals = MemberService.getApprovalRequests(userToken, clubId);
|
||||
res.status(200).json(waitingApprovals);
|
||||
} catch(error) {
|
||||
@@ -16,4 +16,4 @@ const getWaitingApprovals = async(req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
export default { getClubMembers, getWaitingApprovals };
|
||||
export { getClubMembers, getWaitingApprovals };
|
||||
Reference in New Issue
Block a user