Skip to content

Commit

Permalink
feat: update user with roles
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyoptimist committed Jun 3, 2024
1 parent 5a515c9 commit 849c1af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/modules/user/role.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { Exclude } from 'class-transformer';

@Entity({
name: 'roles',
})
export class Role {
@PrimaryGeneratedColumn()
@Exclude()
id: number;

@Column({ length: 50 })
Expand Down
12 changes: 11 additions & 1 deletion src/modules/user/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Allow } from 'class-validator';
import { Allow, IsEnum, ValidateIf } from 'class-validator';
import { RoleEnum } from './role.entity';

export class UpdateUserDto {
@ApiPropertyOptional()
Expand All @@ -9,4 +10,13 @@ export class UpdateUserDto {
@ApiPropertyOptional()
@Allow()
lastName: string;

@ApiPropertyOptional({
enum: RoleEnum,
isArray: true,
default: [RoleEnum.User],
})
@ValidateIf((obj: UpdateUserDto) => obj.roles && obj.roles.length != 0)
@IsEnum(RoleEnum, { each: true })
roles: RoleEnum[];
}
33 changes: 31 additions & 2 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { In, Repository, SelectQueryBuilder } from 'typeorm';
import { User } from './user.entity';
import { SignupDto } from '@modules/auth/dto/signup.dto';
import { UpdateUserDto } from './user.dto';
Expand All @@ -13,12 +13,15 @@ import {
PaginationParam,
SortParam,
} from '@app/utils/query-param.util';
import { Role } from './role.entity';

@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
@InjectRepository(Role)
private readonly roleRepository: Repository<Role>,
) {}

async create(signupDto: SignupDto) {
Expand Down Expand Up @@ -107,7 +110,33 @@ export class UserService {
}

async update(id: number, dto: UpdateUserDto) {
return await this.userRepository.update(id, dto);
const isUserExists = await this.userRepository.exists({ where: { id } });
if (!isUserExists) {
throw new NotFoundException();
}

let { roles: roleNames, ...partialEntity } = dto;

let roles: Role[] = [];
if (dto.roles && dto.roles.length > 0) {
roles = await this.roleRepository.findBy({
name: In(roleNames),
});
}

const updateDto =
roles.length > 0
? {
...partialEntity,
roles,
id,
}
: {
...partialEntity,
id,
};

return await this.userRepository.save(updateDto);
}

async delete(id: number) {
Expand Down

0 comments on commit 849c1af

Please sign in to comment.