create user
This commit is contained in:
@@ -23,6 +23,8 @@ export class UsersController {
|
||||
|
||||
@Post()
|
||||
async create(@Body() createUserDto: CreateUserDto) {
|
||||
|
||||
|
||||
return {
|
||||
data: await this.usersService.create(createUserDto),
|
||||
statusCode: HttpStatus.CREATED,
|
||||
|
||||
@@ -4,9 +4,10 @@ import { UsersService } from './users.service';
|
||||
import { UsersController } from './users.controller';
|
||||
import { User } from './entities/user.entity';
|
||||
import { TransactionModule } from 'src/transaction/transaction.module';
|
||||
import { ConfigurableModule } from 'src/configurable/configurable.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User]), TransactionModule],
|
||||
imports: [TypeOrmModule.forFeature([User]), TransactionModule, ConfigurableModule],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
|
||||
@@ -8,21 +8,28 @@ import { randomStringGenerator } from '@nestjs/common/utils/random-string-genera
|
||||
import { hashPassword } from '../helper/hash_password';
|
||||
import { CoaService } from 'src/transaction/coa.service';
|
||||
import { coaType } from 'src/helper/enum-list';
|
||||
import { RoleService } from 'src/configurable/roles.service';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private usersRepository: Repository<User>,
|
||||
private coaService: CoaService
|
||||
private coaService: CoaService,
|
||||
private roleService: RoleService
|
||||
) {}
|
||||
|
||||
async create(createUserDto: CreateUserDto) {
|
||||
const roles = await this.roleService.findOne(createUserDto.roleId);
|
||||
const superior = await this.findExist(createUserDto.superior);
|
||||
|
||||
const salt = randomStringGenerator();
|
||||
const result = await this.usersRepository.insert({
|
||||
username: createUserDto.username,
|
||||
password: await hashPassword(createUserDto.password, salt),
|
||||
salt,
|
||||
superior:superior,
|
||||
roles:roles
|
||||
});
|
||||
|
||||
return this.usersRepository.findOneOrFail(result.identifiers[0].id);
|
||||
@@ -51,6 +58,24 @@ export class UsersService {
|
||||
});
|
||||
}
|
||||
|
||||
async findExist(id: string) {
|
||||
try {
|
||||
return await this.usersRepository.findOneOrFail(id);
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'Data not found',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const coa = await this.coaService.findByUser(id,coaType.WALLET);
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user