Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
McNaBry committed Nov 12, 2024
1 parent 2b6c943 commit 2cdd900
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
30 changes: 24 additions & 6 deletions services/user/src/controller/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ import {
} from '../model/repository';
import { Request, Response } from 'express';
import { User } from '../model/user-model';
import { handleBadRequest, handleConflict, handleInternalError, handleNotFound, handleSuccess, handleUnauthorized } from '../utils/helper';
import { registrationSchema, updatePasswordSchema, updateUsernameAndEmailSchema, UserValidationErrors } from '../types/custom';
import {
handleBadRequest,
handleConflict,
handleInternalError,
handleNotFound,
handleSuccess,
handleUnauthorized,
} from '../utils/helper';
import {
registrationSchema,
updatePasswordSchema,
updateUsernameAndEmailSchema,
UserValidationErrors,
} from '../types/custom';

export async function createUser(req: Request, res: Response) {
try {
Expand Down Expand Up @@ -106,7 +118,7 @@ export async function updateUsernameAndEmail(req: Request, res: Response) {
handleNotFound(res, `User ${userId} not found`);
return;
}
const match = await bcrypt.compare(password, userById.password);
const match = await bcrypt.compare(password, userById.password);
if (!match) {
handleUnauthorized(res, 'Wrong password');
return;
Expand All @@ -115,7 +127,7 @@ export async function updateUsernameAndEmail(req: Request, res: Response) {
const updatedUser = (await _updateUserById(userId, username, email, userById.password)) as User;
handleSuccess(res, 200, `Updated data for user ${userId}`, formatUserResponse(updatedUser));
} else {
console.log(parseResult.error.errors)
console.log(parseResult.error.errors);
const required_errors = parseResult.error.errors.filter(
err => err.message == UserValidationErrors.REQUIRED,
);
Expand All @@ -126,6 +138,7 @@ export async function updateUsernameAndEmail(req: Request, res: Response) {
handleBadRequest(res, 'invalid username and/or email');
}
} catch (err) {
console.error(err);
handleInternalError(res, 'Unknown error when updating user!');
return;
}
Expand All @@ -147,7 +160,7 @@ export async function updatePassword(req: Request, res: Response) {
handleNotFound(res, `User ${userId} not found`);
return;
}
const match = await bcrypt.compare(oldPassword, userById.password);
const match = await bcrypt.compare(oldPassword, userById.password);
if (!match) {
handleUnauthorized(res, 'Wrong password');
return;
Expand All @@ -156,7 +169,12 @@ export async function updatePassword(req: Request, res: Response) {
const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(newPassword, salt);

const updatedUser = (await _updateUserById(userId, userById.username, userById.email, hashedPassword)) as User;
const updatedUser = (await _updateUserById(
userId,
userById.username,
userById.email,
hashedPassword,
)) as User;
handleSuccess(res, 200, `Updated data for user ${userId}`, formatUserResponse(updatedUser));
} else {
const required_errors = parseResult.error.errors.filter(
Expand Down
36 changes: 18 additions & 18 deletions services/user/src/types/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ export interface RequestUser {
}

export const usernameSchema = z
.string({
invalid_type_error: UserValidationErrors.INVALID,
required_error: UserValidationErrors.REQUIRED,
})
.regex(/^[a-zA-Z0-9._-]+$/, UserValidationErrors.INVALID);
.string({
invalid_type_error: UserValidationErrors.INVALID,
required_error: UserValidationErrors.REQUIRED,
})
.regex(/^[a-zA-Z0-9._-]+$/, UserValidationErrors.INVALID);

export const emailSchema = z
.string({
invalid_type_error: UserValidationErrors.INVALID,
required_error: UserValidationErrors.REQUIRED,
})
.email(UserValidationErrors.INVALID);
.string({
invalid_type_error: UserValidationErrors.INVALID,
required_error: UserValidationErrors.REQUIRED,
})
.email(UserValidationErrors.INVALID);

export const passwordSchema = z
.string({
invalid_type_error: UserValidationErrors.INVALID,
required_error: UserValidationErrors.REQUIRED,
})
.regex(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})(?=.*[!"#$%&'()*+,-.:;<=>?@\\/\\[\]^_`{|}~])/,
UserValidationErrors.INVALID,
);
.string({
invalid_type_error: UserValidationErrors.INVALID,
required_error: UserValidationErrors.REQUIRED,
})
.regex(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})(?=.*[!"#$%&'()*+,-.:;<=>?@\\/\\[\]^_`{|}~])/,
UserValidationErrors.INVALID,
);

export const registrationSchema = z.object({
username: usernameSchema,
Expand Down

0 comments on commit 2cdd900

Please sign in to comment.