Skip to content

Commit

Permalink
Merge pull request #156 from Code-Hammers/CHE-199/subtask/Create-Test…
Browse files Browse the repository at this point in the history
…s-and-Error-Handling-for-userController-registerUser

[CHE-199] Create Tests and Error Handling for userController registerUser
  • Loading branch information
brok3turtl3 authored Jul 1, 2024
2 parents 1666003 + 1e7096f commit f53b887
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 209 deletions.
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

0 comments on commit f53b887

Please sign in to comment.