Skip to content

Commit

Permalink
fix(lint-errors): fix linting errors
Browse files Browse the repository at this point in the history
-fix linting errors by making appropriate changes

[Fixes #34]
  • Loading branch information
jkarenzi committed May 6, 2024
1 parent 11ce5ea commit 13fc390
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 38 deletions.
13 changes: 6 additions & 7 deletions src/__test__/roles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,13 @@ describe('CRUD roles test', () => {

it('should change user role successfully', async () => {
const createRoleformData = {
name: 'Buyer',
permissions: ['test-permission1', 'test-permission2', 'test-permission3'],
name: 'Buyer',
permissions: ['test-permission1', 'test-permission2', 'test-permission3'],
};

const createRoleResponse = await request(app)
.post('/api/v1/roles/create_role')
.send(createRoleformData);

await request(app)
.post('/api/v1/roles/create_role')
.send(createRoleformData);

const userData = {
firstName: 'Test',
Expand Down Expand Up @@ -171,7 +170,7 @@ describe('CRUD roles test', () => {
it('should return a 404 if user is not found on change user role operation', async () => {
const formData = {
userId: 100,
newRoleId: "some id",
newRoleId: 'some id',
};

const response = await request(app)
Expand Down
5 changes: 4 additions & 1 deletion src/__test__/userController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ describe('User Registration Tests', () => {
expect(response.body.user).toHaveProperty('firstName', userData.firstName);
expect(response.body.user).toHaveProperty('lastName', userData.lastName);
expect(response.body.user).toHaveProperty('email', userData.email);
expect(response.body.user).toHaveProperty('userType', response.body.user.userType);
expect(response.body.user).toHaveProperty(
'userType',
response.body.user.userType
);
});

it('should return a 400 status code if validation fails', async () => {
Expand Down
9 changes: 4 additions & 5 deletions src/controller/roleController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,17 @@ class roleController {
}

const role = await roleRepository.findOneBy({
id:formData.newRoleId
})
id: formData.newRoleId,
});

if(!role){
return res.status(404).json({ msg: 'Role not found'})
if (!role) {
return res.status(404).json({ msg: 'Role not found' });
}

user.userType = role;
await userRepository.save(user);
return res.status(200).json({ msg: 'User role successfully updated' });
} catch (err: any) {
console.log(err.message);
res.status(500).json({ msg: 'Internal Server Error' });
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { check, validationResult } from 'express-validator';
import bcrypt from 'bcryptjs';
import dbConnection from '../database';
import { UserModel } from '../database/models/userModel';
import { Role } from '../database/models/roleEntity'
import { Role } from '../database/models/roleEntity';

// Assuming dbConnection.getRepository(UserModel) returns a repository instance
const userRepository = dbConnection.getRepository(UserModel);
const roleRepository = dbConnection.getRepository(Role)
const roleRepository = dbConnection.getRepository(Role);

interface CreateUserRequestBody {
firstName: string;
Expand Down Expand Up @@ -44,19 +44,19 @@ export const registerUser = [
}
const hashedPassword = await bcrypt.hash(password, 10);

const userRole = (userType == 'vendor')?
(await roleRepository.findOneBy({name:'Vendor'}))!:
(await roleRepository.findOneBy({name:'Buyer'}))!

const userRole =
userType == 'vendor'
? (await roleRepository.findOneBy({ name: 'Vendor' }))!
: (await roleRepository.findOneBy({ name: 'Buyer' }))!;

const newUser = new UserModel({
firstName: firstName,
lastName: lastName,
email: email,
password: hashedPassword,
userType: userRole
userType: userRole,
});


const savedUser = await userRepository.save(newUser);
res.status(201).json({
message: 'User successfully registered',
Expand All @@ -74,8 +74,8 @@ export const registerUser = [
export const getAllUsers = async (req: Request, res: Response) => {
try {
const users = await userRepository.find({
select: ['id', 'firstName', 'lastName', 'email','userType'],
relations: ['userType']
select: ['id', 'firstName', 'lastName', 'email', 'userType'],
relations: ['userType'],
});
res.status(200).json(users);
} catch (error) {
Expand Down
10 changes: 8 additions & 2 deletions src/database/models/userModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn } from 'typeorm';
import {
Entity,
PrimaryGeneratedColumn,
Column,
OneToOne,
JoinColumn,
} from 'typeorm';
import { Role } from './roleEntity';

@Entity()
Expand All @@ -20,7 +26,7 @@ export class UserModel {

@OneToOne(() => Role)
@JoinColumn()
userType: Role
userType: Role;

constructor(user: Partial<UserModel>) {
Object.assign(this, user);
Expand Down
27 changes: 16 additions & 11 deletions src/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { Request, Response, NextFunction } from 'express';

type MiddlewareFunction = (req: Request, res: Response, next: NextFunction) => Promise<void>;
type MiddlewareFunction = (
req: Request,
res: Response,
next: NextFunction
) => Promise<void>;

function errorHandler(func: MiddlewareFunction) {
return async (req: Request, res: Response, next: NextFunction) => {
try {
await func(req, res, next);
} catch (error) { // Removed the type annotation from the catch clause variable because it caused liting errors
const message = error.detail || 'Internal Server Error';
res.status(500).send(message);
}
};
}
return async (req: Request, res: Response, next: NextFunction) => {
try {
await func(req, res, next);
} catch (error) {
// Removed the type annotation from the catch clause variable because it caused liting errors
const message = error.detail || 'Internal Server Error';
res.status(500).send(message);
}
};
}

export default errorHandler;
export default errorHandler;
2 changes: 1 addition & 1 deletion src/middlewares/roleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export const updateRoleSchema = Joi.object({
export const changeRoleSchema = Joi.object({
userId: Joi.number().integer().positive().required(),
newRoleId: Joi.number().integer().positive().required(),
});
});
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

// router.use('/roles', roleRoutes)

// export default router
// export default router

0 comments on commit 13fc390

Please sign in to comment.