Add functionality to edit user profile in user module

master
nicekid1 2 months ago
parent fa026ac2f3
commit 54531c1c63
  1. 33
      src/users/dto/update-user.dto.ts
  2. 4
      src/users/entities/user.entity.ts
  3. 10
      src/users/users.controller.ts
  4. 20
      src/users/users.service.ts

@ -0,0 +1,33 @@
import { IsOptional, IsString, IsEmail, IsEnum } from 'class-validator';
import {Gender } from '../entities/user.entity';
export class UpdateUserDto {
@IsOptional()
@IsString()
username?: string;
@IsOptional()
@IsEmail()
email?: string;
@IsOptional()
@IsString()
password?: string;
@IsOptional()
@IsString()
firstName?: string;
@IsOptional()
@IsString()
lastName?: string;
@IsOptional()
@IsString()
phoneNumber?: string;
@IsOptional()
@IsEnum(Gender)
gender?: Gender;
}

@ -29,3 +29,7 @@ export class User extends Model<User> {
})
gender: string;
}
export enum Gender {
Male = 'male',
Female = 'female',
}

@ -1,9 +1,10 @@
import { Controller, Post, Body, UseGuards, Get,Request} from "@nestjs/common";
import { Controller, Post, Body, UseGuards, Get, Request, Put } from "@nestjs/common";
import { UsersService } from "./users.service";
import { User } from "./entities/user.entity";
import { CreateUserDto } from "./dto/create-user.dto";
import { LoginUserDto } from "./dto/login-user.dto";
import { JwtAuthGuard } from "src/guard/auth.guard";
import { UpdateUserDto } from "./dto/update-user.dto";
@Controller("user")
export class UsersController {
@ -26,5 +27,10 @@ export class UsersController {
const userId = req.user.id;
return this.usersService.getProfile(userId);
}
@UseGuards(JwtAuthGuard)
@Put()
async editProfile(@Request() req, @Body() updateUserDto: UpdateUserDto): Promise<User> {
const userId = req.user.id;
return this.usersService.editProfile(userId, updateUserDto);
}
}

@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable, UnauthorizedException, BadRequestException } from "@nestjs/common";
import { HttpException, HttpStatus, Injectable, UnauthorizedException, BadRequestException, NotFoundException } from "@nestjs/common";
import { InjectModel } from "@nestjs/sequelize";
import { User } from "./entities/user.entity";
import * as bcrypt from "bcrypt";
@ -6,6 +6,7 @@ import { JwtService } from "@nestjs/jwt";
import { ConfigService } from "@nestjs/config";
import { CreateUserDto } from "./dto/create-user.dto";
import { LoginUserDto } from "./dto/login-user.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
@Injectable()
export class UsersService {
@ -66,7 +67,7 @@ export class UsersService {
return { token };
}
//get information user method
async getProfile(userId: number): Promise<User> {
const user = await this.userModel.findOne({
where: { id: userId },
@ -77,6 +78,21 @@ export class UsersService {
throw new Error('User not found');
}
return user;
}
async editProfile(userId:number, updateUserDto:UpdateUserDto){
const user = await this.userModel.findOne({ where: { id:userId } });
if (!user) {
throw new NotFoundException('User not found');
}
if (updateUserDto.password) {
updateUserDto.password = await bcrypt.hash(updateUserDto.password, 10);
}
await user.update(updateUserDto);
return user;
}
}

Loading…
Cancel
Save