From 988d545b6422a1a20cb81bb4eea7e662023bfd04 Mon Sep 17 00:00:00 2001 From: Ilham Dwi Pratama S Date: Wed, 8 Dec 2021 11:14:12 +0700 Subject: [PATCH] create user --- .../configurable.controller.spec.ts | 4 +-- src/configurable/configurable.controller.ts | 4 +-- src/configurable/configurable.module.ts | 5 ++-- src/configurable/configurable.service.spec.ts | 10 +++---- ...nfigurable.service.ts => roles.service.ts} | 2 +- src/users/users.controller.ts | 2 ++ src/users/users.module.ts | 3 ++- src/users/users.service.ts | 27 ++++++++++++++++++- 8 files changed, 43 insertions(+), 14 deletions(-) rename src/configurable/{configurable.service.ts => roles.service.ts} (96%) diff --git a/src/configurable/configurable.controller.spec.ts b/src/configurable/configurable.controller.spec.ts index 579b37e..2d26cbd 100644 --- a/src/configurable/configurable.controller.spec.ts +++ b/src/configurable/configurable.controller.spec.ts @@ -1,6 +1,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ConfigurableController } from './configurable.controller'; -import { ConfigurableService } from './configurable.service'; +import { RoleService } from './roles.service'; describe('ConfigurableController', () => { let controller: ConfigurableController; @@ -8,7 +8,7 @@ describe('ConfigurableController', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [ConfigurableController], - providers: [ConfigurableService], + providers: [RoleService], }).compile(); controller = module.get(ConfigurableController); diff --git a/src/configurable/configurable.controller.ts b/src/configurable/configurable.controller.ts index 21d4c01..8d4d3ca 100644 --- a/src/configurable/configurable.controller.ts +++ b/src/configurable/configurable.controller.ts @@ -9,14 +9,14 @@ import { ParseUUIDPipe, HttpStatus, Query, } from '@nestjs/common'; -import { ConfigurableService } from './configurable.service'; +import { RoleService } from './roles.service'; @Controller({ path: 'config', version: '1', }) export class ConfigurableController { - constructor(private readonly usersService: ConfigurableService) {} + constructor(private readonly usersService: RoleService) {} @Get() async findAll(@Query('page') page: number) { diff --git a/src/configurable/configurable.module.ts b/src/configurable/configurable.module.ts index da4f11e..5325245 100644 --- a/src/configurable/configurable.module.ts +++ b/src/configurable/configurable.module.ts @@ -2,11 +2,12 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Roles } from './entities/roles.entity'; import { ConfigurableController } from './configurable.controller'; -import { ConfigurableService } from './configurable.service'; +import { RoleService } from './roles.service'; @Module({ imports: [TypeOrmModule.forFeature([Roles])], controllers: [ConfigurableController], - providers: [ConfigurableService], + providers: [RoleService], + exports: [RoleService] }) export class ConfigurableModule {} diff --git a/src/configurable/configurable.service.spec.ts b/src/configurable/configurable.service.spec.ts index be931a1..bced459 100644 --- a/src/configurable/configurable.service.spec.ts +++ b/src/configurable/configurable.service.spec.ts @@ -1,15 +1,15 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { ConfigurableService } from './configurable.service'; +import { RoleService } from './roles.service'; -describe('ConfigurableService', () => { - let service: ConfigurableService; +describe('RoleService', () => { + let service: RoleService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [ConfigurableService], + providers: [RoleService], }).compile(); - service = module.get(ConfigurableService); + service = module.get(RoleService); }); it('should be defined', () => { diff --git a/src/configurable/configurable.service.ts b/src/configurable/roles.service.ts similarity index 96% rename from src/configurable/configurable.service.ts rename to src/configurable/roles.service.ts index 109c824..9404376 100644 --- a/src/configurable/configurable.service.ts +++ b/src/configurable/roles.service.ts @@ -4,7 +4,7 @@ import { Roles } from './entities/roles.entity'; import { InjectRepository } from '@nestjs/typeorm'; @Injectable() -export class ConfigurableService { +export class RoleService { constructor( @InjectRepository(Roles) private rolesRepository: Repository, diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 06e5902..f3389a6 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -23,6 +23,8 @@ export class UsersController { @Post() async create(@Body() createUserDto: CreateUserDto) { + + return { data: await this.usersService.create(createUserDto), statusCode: HttpStatus.CREATED, diff --git a/src/users/users.module.ts b/src/users/users.module.ts index 2d718ca..fd4e301 100644 --- a/src/users/users.module.ts +++ b/src/users/users.module.ts @@ -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], diff --git a/src/users/users.service.ts b/src/users/users.service.ts index af76258..4e8ae5e 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -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, - 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 {