auth
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import {
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AuthUser } from '../auth/interfaces/auth-user.interface';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { User } from './entities/user.entity';
|
||||
import { UserRole } from './enums/user-role.enum';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
@@ -34,11 +37,25 @@ export class UsersService {
|
||||
return user;
|
||||
}
|
||||
|
||||
async update(id: string, updateUserDto: UpdateUserDto): Promise<User> {
|
||||
async findByMobile(mobile: string): Promise<User | null> {
|
||||
return this.usersRepository.findOneBy({ mobile });
|
||||
}
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
updateUserDto: UpdateUserDto,
|
||||
currentUser?: AuthUser,
|
||||
): Promise<User> {
|
||||
const user = await this.findOne(id);
|
||||
if (updateUserDto.mobile) {
|
||||
await this.ensureMobileIsUnique(updateUserDto.mobile, id);
|
||||
}
|
||||
if (
|
||||
updateUserDto.role !== undefined &&
|
||||
currentUser?.role !== UserRole.ADMIN
|
||||
) {
|
||||
throw new ForbiddenException('Only admin can change user role');
|
||||
}
|
||||
Object.assign(user, updateUserDto);
|
||||
return this.usersRepository.save(user);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user