create user

This commit is contained in:
Ilham Dwi Pratama S 2021-12-08 11:14:12 +07:00
parent 10984f65a5
commit 988d545b64
8 changed files with 43 additions and 14 deletions

View File

@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ConfigurableController } from './configurable.controller'; import { ConfigurableController } from './configurable.controller';
import { ConfigurableService } from './configurable.service'; import { RoleService } from './roles.service';
describe('ConfigurableController', () => { describe('ConfigurableController', () => {
let controller: ConfigurableController; let controller: ConfigurableController;
@ -8,7 +8,7 @@ describe('ConfigurableController', () => {
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
controllers: [ConfigurableController], controllers: [ConfigurableController],
providers: [ConfigurableService], providers: [RoleService],
}).compile(); }).compile();
controller = module.get<ConfigurableController>(ConfigurableController); controller = module.get<ConfigurableController>(ConfigurableController);

View File

@ -9,14 +9,14 @@ import {
ParseUUIDPipe, ParseUUIDPipe,
HttpStatus, Query, HttpStatus, Query,
} from '@nestjs/common'; } from '@nestjs/common';
import { ConfigurableService } from './configurable.service'; import { RoleService } from './roles.service';
@Controller({ @Controller({
path: 'config', path: 'config',
version: '1', version: '1',
}) })
export class ConfigurableController { export class ConfigurableController {
constructor(private readonly usersService: ConfigurableService) {} constructor(private readonly usersService: RoleService) {}
@Get() @Get()
async findAll(@Query('page') page: number) { async findAll(@Query('page') page: number) {

View File

@ -2,11 +2,12 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Roles } from './entities/roles.entity'; import { Roles } from './entities/roles.entity';
import { ConfigurableController } from './configurable.controller'; import { ConfigurableController } from './configurable.controller';
import { ConfigurableService } from './configurable.service'; import { RoleService } from './roles.service';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Roles])], imports: [TypeOrmModule.forFeature([Roles])],
controllers: [ConfigurableController], controllers: [ConfigurableController],
providers: [ConfigurableService], providers: [RoleService],
exports: [RoleService]
}) })
export class ConfigurableModule {} export class ConfigurableModule {}

View File

@ -1,15 +1,15 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ConfigurableService } from './configurable.service'; import { RoleService } from './roles.service';
describe('ConfigurableService', () => { describe('RoleService', () => {
let service: ConfigurableService; let service: RoleService;
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [ConfigurableService], providers: [RoleService],
}).compile(); }).compile();
service = module.get<ConfigurableService>(ConfigurableService); service = module.get<RoleService>(RoleService);
}); });
it('should be defined', () => { it('should be defined', () => {

View File

@ -4,7 +4,7 @@ import { Roles } from './entities/roles.entity';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
@Injectable() @Injectable()
export class ConfigurableService { export class RoleService {
constructor( constructor(
@InjectRepository(Roles) @InjectRepository(Roles)
private rolesRepository: Repository<Roles>, private rolesRepository: Repository<Roles>,

View File

@ -23,6 +23,8 @@ export class UsersController {
@Post() @Post()
async create(@Body() createUserDto: CreateUserDto) { async create(@Body() createUserDto: CreateUserDto) {
return { return {
data: await this.usersService.create(createUserDto), data: await this.usersService.create(createUserDto),
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,

View File

@ -4,9 +4,10 @@ import { UsersService } from './users.service';
import { UsersController } from './users.controller'; import { UsersController } from './users.controller';
import { User } from './entities/user.entity'; import { User } from './entities/user.entity';
import { TransactionModule } from 'src/transaction/transaction.module'; import { TransactionModule } from 'src/transaction/transaction.module';
import { ConfigurableModule } from 'src/configurable/configurable.module';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([User]), TransactionModule], imports: [TypeOrmModule.forFeature([User]), TransactionModule, ConfigurableModule],
controllers: [UsersController], controllers: [UsersController],
providers: [UsersService], providers: [UsersService],
exports: [UsersService], exports: [UsersService],

View File

@ -8,21 +8,28 @@ import { randomStringGenerator } from '@nestjs/common/utils/random-string-genera
import { hashPassword } from '../helper/hash_password'; import { hashPassword } from '../helper/hash_password';
import { CoaService } from 'src/transaction/coa.service'; import { CoaService } from 'src/transaction/coa.service';
import { coaType } from 'src/helper/enum-list'; import { coaType } from 'src/helper/enum-list';
import { RoleService } from 'src/configurable/roles.service';
@Injectable() @Injectable()
export class UsersService { export class UsersService {
constructor( constructor(
@InjectRepository(User) @InjectRepository(User)
private usersRepository: Repository<User>, private usersRepository: Repository<User>,
private coaService: CoaService private coaService: CoaService,
private roleService: RoleService
) {} ) {}
async create(createUserDto: CreateUserDto) { async create(createUserDto: CreateUserDto) {
const roles = await this.roleService.findOne(createUserDto.roleId);
const superior = await this.findExist(createUserDto.superior);
const salt = randomStringGenerator(); const salt = randomStringGenerator();
const result = await this.usersRepository.insert({ const result = await this.usersRepository.insert({
username: createUserDto.username, username: createUserDto.username,
password: await hashPassword(createUserDto.password, salt), password: await hashPassword(createUserDto.password, salt),
salt, salt,
superior:superior,
roles:roles
}); });
return this.usersRepository.findOneOrFail(result.identifiers[0].id); 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) { async findOne(id: string) {
const coa = await this.coaService.findByUser(id,coaType.WALLET); const coa = await this.coaService.findByUser(id,coaType.WALLET);
try { try {