-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Brints/update-user
feat: update user
- Loading branch information
Showing
6 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { CreateUserDto } from '../../auth/dto/create-user.dto'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class UpdateUserDto extends PartialType(CreateUserDto) {} | ||
export class UpdateUserDto extends PartialType(CreateUserDto) { | ||
@ApiProperty({ | ||
description: 'The id of the user', | ||
example: '60b7f3a8d8e9a7e4d8f9b1a7', | ||
}) | ||
@IsNotEmpty() | ||
@IsString() | ||
id: string; | ||
} |
63 changes: 61 additions & 2 deletions
63
brints-estate-api/src/users/providers/update-user.provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,63 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpStatus, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { User } from '../entities/user.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { UpdateUserDto } from '../dto/update-user.dto'; | ||
import { CustomException } from 'src/exceptions/custom.exception'; | ||
import { UploadToAwsProvider } from 'src/uploads/providers/upload-to-aws.provider'; | ||
import { IActiveUser } from 'src/auth/interfaces/active-user.interface'; | ||
|
||
@Injectable() | ||
export class UpdateUserProvider {} | ||
export class UpdateUserProvider { | ||
constructor( | ||
@InjectRepository(User) | ||
private readonly userRepository: Repository<User>, | ||
|
||
private uploadToAwsProvider: UploadToAwsProvider, | ||
) {} | ||
|
||
public async update( | ||
updateUserDto: UpdateUserDto, | ||
activeUser: IActiveUser, | ||
file: Express.Multer.File, | ||
): Promise<User> { | ||
const user = await this.userRepository.findOneBy({ id: updateUserDto.id }); | ||
|
||
if (!user) | ||
throw new CustomException(HttpStatus.NOT_FOUND, 'User not found'); | ||
|
||
if (user.id !== activeUser.sub) | ||
throw new CustomException( | ||
HttpStatus.FORBIDDEN, | ||
'You are not allowed to perform this action', | ||
); | ||
|
||
if (file) { | ||
const fileUrl = await this.uploadToAwsProvider.fileUpload(file); | ||
user.image_url = fileUrl; | ||
//await this.uploadToAwsProvider.deleteFile(user.image_url); | ||
await this.userRepository.update(user.id, { image_url: fileUrl }); | ||
} | ||
|
||
if (updateUserDto.phone_number) { | ||
const phoneNumberExists = await this.userRepository.findOneBy({ | ||
phone_number: updateUserDto.phone_number, | ||
}); | ||
if (phoneNumberExists) | ||
throw new CustomException( | ||
HttpStatus.CONFLICT, | ||
'Phone number already in use.', | ||
); | ||
} | ||
|
||
user.first_name = updateUserDto.first_name ?? user.first_name; | ||
user.last_name = updateUserDto.last_name ?? user.last_name; | ||
user.phone_number = updateUserDto.phone_number ?? user.phone_number; | ||
user.gender = updateUserDto.gender ?? user.gender; | ||
user.marketing = updateUserDto.marketing ?? user.marketing; | ||
|
||
await this.userRepository.update(user.id, user); | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters