Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHE-199] Create Tests and Error Handling for userController registerUser #156

142 changes: 0 additions & 142 deletions __tests__/userController.tests.ts

This file was deleted.

67 changes: 1 addition & 66 deletions server/controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
import User from '../models/userModel';
import Profile from '../models/profileModel';
import generateToken from '../utils/generateToken';
import { Request, Response, NextFunction } from 'express';
import { UserType } from '../types/user';
import GraduateInvitation from '../models/graduateInvitationModel';

// ENDPOINT POST api/users/register
// PURPOSE Register a new user
// ACCESS Public
const registerUser = async (req: Request, res: Response, next: NextFunction) => {
const { email, password } = req.body;
const { token } = req.query;

try {
const isValidEmail = email.match(/[\w\d.]+@[a-z]+.[\w]+$/gim);
if (!isValidEmail) {
return res.status(400).json('Invalid Email');
}

const invitation = await GraduateInvitation.findOne({
email,
token,
tokenExpiry: { $gt: new Date() },
isRegistered: false,
});

//TODO Needs better error handling - this can trigger with situaions other than bad or missing token
if (!invitation) {
return res.status(400).json({ message: 'Invalid or expired registration token' });
}
const userExists: UserType | null = await User.findOne({ email });
if (userExists) {
return res.status(400).json({ message: 'User already exists!' });
}
const user: UserType = await User.create({
firstName: invitation.firstName,
lastName: invitation.lastName,
email,
password,
});

if (user) {
invitation.isRegistered = true;
await invitation?.save();
await Profile.create({
user: user._id,
firstName: invitation.firstName,
lastName: invitation.lastName,
cohort: invitation.cohort,
});
res.locals.user = {
_id: user._id,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
};

res.cookie('token', generateToken(user._id.toString()));
return res.status(201).json(res.locals.user);
}
} catch (error) {
console.error('Error during user signup:', error);
return next({
log: 'Express error in createUser Middleware',
status: 503,
message: { err: 'An error occurred during sign-up' },
});
}
};
// ENDPOINT POST api/users/login
// PURPOSE Authenticate User and get token
// ACCESS Public
Expand Down Expand Up @@ -165,4 +100,4 @@ const deleteUserByEmail = async (req: Request, res: Response, next: NextFunction
}
};

export { registerUser, authUser, getUserById, deleteUserByEmail };
export { authUser, getUserById, deleteUserByEmail };
3 changes: 3 additions & 0 deletions server/controllers/userController/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import registerUser from './registerUser/registerUser';

export { registerUser };
Loading
Loading