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 };
|
||||
@@ -18,6 +18,9 @@ const Log = sequelize.define('Log', {
|
||||
key: 'id',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
underscored: true
|
||||
});
|
||||
|
||||
export default Log;
|
||||
|
||||
@@ -82,10 +82,12 @@ Member.init({
|
||||
}
|
||||
}
|
||||
}, {
|
||||
underscored: true,
|
||||
sequelize,
|
||||
modelName: 'Member',
|
||||
tableName: 'members',
|
||||
timestamps: true,
|
||||
|
||||
hooks: {
|
||||
beforeCreate: (member) => {
|
||||
member.hashedId = crypto.createHash('sha256').update(String(member.id)).digest('hex');
|
||||
|
||||
@@ -29,6 +29,8 @@ const User = sequelize.define('User', {
|
||||
allowNull: true
|
||||
}
|
||||
}, {
|
||||
underscored: true,
|
||||
table: 'user',
|
||||
hooks: {
|
||||
beforeCreate: async (user) => {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
|
||||
@@ -17,11 +17,13 @@ const UserClub = sequelize.define('UserClub', {
|
||||
model: Club,
|
||||
key: 'id',
|
||||
},
|
||||
},
|
||||
},
|
||||
approved: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
}, {
|
||||
underscored: true
|
||||
});
|
||||
|
||||
User.belongsToMany(Club, { through: UserClub, foreignKey: 'userId' });
|
||||
|
||||
1184
backend/node_modules/.package-lock.json
generated
vendored
1184
backend/node_modules/.package-lock.json
generated
vendored
File diff suppressed because it is too large
Load Diff
1188
backend/package-lock.json
generated
1188
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -13,6 +13,7 @@
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"bcrypt": "^5.1.1",
|
||||
"cors": "^2.8.5",
|
||||
"crypto": "^1.0.1",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import express from 'express';
|
||||
import { authenticate } from '../middleware/authMiddleware.js';
|
||||
import { getClubs, addClub, getClub } from '../controllers/clubsController.js';
|
||||
import { getClubs, addClub, getClub, requestClubAccess } from '../controllers/clubsController.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authenticate, getClubs);
|
||||
router.post('/', authenticate, addClub);
|
||||
router.get('/:clubid', authenticate, getClub);
|
||||
router.get('/request/:clubid', authenticate, requestClubAccess);
|
||||
|
||||
export default router;
|
||||
@@ -1,8 +1,8 @@
|
||||
import { getClubMembers, getWaitingApprovals } from '../controllers/memberController';
|
||||
import { getClubMembers, getWaitingApprovals } from '../controllers/memberController.js';
|
||||
import express from 'express';
|
||||
import { authenticate } from '../middleware/authMiddleware';
|
||||
import { authenticate } from '../middleware/authMiddleware.js';
|
||||
|
||||
const router = express.Router;
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', authenticate, getClubMembers);
|
||||
router.get('/notapproved/:id', authenticate, getWaitingApprovals);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { UserClub } from "../models";
|
||||
import { checkAccess } from "../utils/userUtils";
|
||||
import UserClub from "../models/UserClub.js";
|
||||
import { checkAccess } from "../utils/userUtils.js";
|
||||
|
||||
class MemberService {
|
||||
async getApprovalRequests(userToken, clubId) {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { User, UserClub } from "../models";
|
||||
import User from '../models/User.js'
|
||||
import UserClub from '../models/UserClub.js';
|
||||
|
||||
export const getUserByToken = async(token) => {
|
||||
const user = await User.findOne({
|
||||
where: [
|
||||
{hashedId: token}
|
||||
{hashed_id: token}
|
||||
]
|
||||
});
|
||||
return user;
|
||||
|
||||
Reference in New Issue
Block a user