Skip to content

Commit

Permalink
fix: 🐛 fix user api response
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Jan 2, 2023
1 parent 2851b6e commit 849a952
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 5 deletions.
3 changes: 3 additions & 0 deletions apps/api/src/user/dto/changePassword.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class ChangePasswordDto {
password: string;
}
15 changes: 15 additions & 0 deletions apps/api/src/user/interface/changePassword.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { UserRole } from '@prisma/client';

export interface ChangePasswordRes {
message: string;
user: UserRes;
}

export interface UserRes {
id: string;
name: string;
email: string;
created_at: Date;
updated_at: Date;
userRoles: UserRole[];
}
24 changes: 23 additions & 1 deletion apps/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Controller, Post, Get, Body, Param } from '@nestjs/common';
import { Controller, Post, Get, Body, Param, Patch } from '@nestjs/common';
import { UserService } from './user.service';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import { SignupDto } from './dto/signup.dto';
import { LoginDto } from './dto/login.dto';
import { ChangePasswordDto } from './dto/changePassword.dto';
import { SignupRes } from './interface/signup.interface';
import { LoginRes } from './interface/login.interface';
import { GetNormalUsersRes } from './interface/getNormalUsers.interface';
import { GetUserByIdRes } from './interface/getUserById.interface';
import { ChangePasswordRes } from './interface/changePassword.interface';

@Controller('users')
export class UserController {
Expand Down Expand Up @@ -95,4 +97,24 @@ export class UserController {

return response;
}

@Patch('/:id/changePassword')
async changePassword(
@Param('id') id: string,
@Body() changePasswordDto: ChangePasswordDto
): Promise<ChangePasswordRes> {
let response: ChangePasswordRes;

const password = changePasswordDto.password;

const user = await this.userService.changePassword(id, password);
if (user) {
response = {
message: 'change password',
user: user,
};
}

return response;
}
}
15 changes: 15 additions & 0 deletions apps/api/src/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { UserRole } from '@prisma/client';
import { PrismaService } from '../prisma.service';
import bcrypt from 'bcryptjs';

@Injectable()
export class UserRepository {
Expand Down Expand Up @@ -55,4 +56,18 @@ export class UserRepository {
});
return user;
}

async changePassword(id: string, password: string) {
const salt = bcrypt.genSaltSync(10);

const user = await this.prisma.user.update({
where: {
id: id,
},
data: {
password: bcrypt.hashSync(password, salt),
},
});
return user;
}
}
51 changes: 49 additions & 2 deletions apps/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,59 @@ export class UserService {
}

async getNormalUsers() {
let normalUsers = [];

const users = await this.userRepository.findNormalUsers();
return users;
if (users) {
normalUsers = users.map((user) => {
const data = {
id: user.id,
name: user.name,
email: user.email,
created_at: user.created_at,
updated_at: user.updated_at,
userRoles: user.userRoles,
};
return data;
});
}

return normalUsers;
}

async getUserById(id: string) {
const user = await this.userRepository.findUserById(id);
let user = null;

const userFromDB = await this.userRepository.findUserById(id);
if (userFromDB) {
user = {
id: userFromDB.id,
name: userFromDB.name,
email: userFromDB.email,
created_at: userFromDB.created_at,
updated_at: userFromDB.updated_at,
userRoles: userFromDB.userRoles,
};
}

return user;
}

async changePassword(id: string, password: string) {
let user = null;

const userFromDB = await this.userRepository.changePassword(id, password);
if (userFromDB) {
user = {
id: userFromDB.id,
name: userFromDB.name,
email: userFromDB.email,
created_at: userFromDB.created_at,
updated_at: userFromDB.updated_at,
userRoles: userFromDB.userRoles,
};
}

return user;
}
}
37 changes: 35 additions & 2 deletions apps/web/src/components/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import Container from '@mui/material/Container';
import Typography from '@mui/material/Typography';
import Button from '@mui/material/Button';
import CustomBreadcrumbs from '../customBreadcrumbs/CustomBreadcrumbs';
import CustomSnackBar from '../customSnackBar/CustomSnackBar';
import * as userService from '../../services/userService';

function Settings() {
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');

const [snackbarText, setSnackbarText] = useState('');
const [snackbarOpen, setSnackbarOpen] = useState(false);

const handlePasswordChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
Expand All @@ -23,8 +28,30 @@ function Settings() {
setConfirmPassword(e.target.value);
};

const handleChangePasswordClick = () => {
console.log('change password api');
const handleChangePasswordClick = async () => {
const token = localStorage.getItem('token');
const userId = localStorage.getItem('userId');
if (token && userId && password && confirmPassword) {
if (password === confirmPassword) {
const response = await userService.changePassword(
token,
userId,
password
);
console.log('response = ', response);

if (response) {
const responseData = response.data;
if (responseData) {
setSnackbarOpen(true);
setSnackbarText('Change password');

setPassword('');
setConfirmPassword('');
}
}
}
}
};

const renderSettingsView = () => {
Expand Down Expand Up @@ -85,6 +112,12 @@ function Settings() {
</div>

{renderSettingsView()}

<CustomSnackBar
type={snackbarText}
open={snackbarOpen}
setOpen={setSnackbarOpen}
/>
</>
);
}
Expand Down
20 changes: 20 additions & 0 deletions apps/web/src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,23 @@ export const getUserById = async (token: string, id: string) => {
});
return response;
};

export const changePassword = async (
token: string,
id: string,
password: string
) => {
const data = {
password: password,
};
const response = await axios.patch(
`${rootUrl}/users/${id}/changePassword`,
data,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
return response;
};

0 comments on commit 849a952

Please sign in to comment.