From 6b1352e671d1735147ace6a6acdc89b656bc34d7 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Tue, 14 Dec 2021 15:18:25 +0700 Subject: [PATCH] feat: add update partner endpoint --- src/users/dto/update-partner.dto.ts | 5 +++ src/users/partner/partner.service.ts | 59 +++++++++++++++++++++++----- src/users/users.controller.ts | 29 ++++++++++---- src/users/users.service.ts | 52 ++++++++++++++++++++++-- 4 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 src/users/dto/update-partner.dto.ts diff --git a/src/users/dto/update-partner.dto.ts b/src/users/dto/update-partner.dto.ts new file mode 100644 index 0000000..2e2f4c1 --- /dev/null +++ b/src/users/dto/update-partner.dto.ts @@ -0,0 +1,5 @@ +import { PartialType } from '@nestjs/mapped-types'; +import { CreatePartnerDto } from './create-partner.dto'; + +export class UpdatePartnerDto extends PartialType(CreatePartnerDto) { +} diff --git a/src/users/partner/partner.service.ts b/src/users/partner/partner.service.ts index 78ca16d..bbb715b 100644 --- a/src/users/partner/partner.service.ts +++ b/src/users/partner/partner.service.ts @@ -1,18 +1,14 @@ -import { - forwardRef, - HttpException, - HttpStatus, - Inject, - Injectable, -} from '@nestjs/common'; +import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Connection, EntityNotFoundError, Repository } from 'typeorm'; +import { Connection, Not, Repository } from 'typeorm'; import { CoaService } from '../../transaction/coa.service'; import { CreatePartnerDto } from '../dto/create-partner.dto'; import { Partner } from '../entities/partner.entity'; import * as uuid from 'uuid'; import { UsersService } from '../users.service'; import { CreateUserDto } from '../dto/create-user.dto'; +import { UpdatePartnerDto } from '../dto/update-partner.dto'; +import { UpdateUserDto } from '../dto/update-user.dto'; @Injectable() export class PartnerService { @@ -38,7 +34,7 @@ export class PartnerService { throw new HttpException( { statusCode: HttpStatus.NOT_ACCEPTABLE, - error: 'N Already Exist', + error: 'NPWP Already Exist', }, HttpStatus.NOT_FOUND, ); @@ -67,6 +63,51 @@ export class PartnerService { return partnerData; } + async update( + id: string, + updatePartnerDto: UpdatePartnerDto, + currentUser: any, + ) { + const check = await this.partnerRepository.findOne({ + npwp: updatePartnerDto.npwp, + id: Not(id), + }); + + if (check) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_ACCEPTABLE, + error: 'NPWP Already Exist', + }, + HttpStatus.NOT_FOUND, + ); + } + + const partnerData = new Partner(); + + partnerData.id = id; + partnerData.name = updatePartnerDto.name; + partnerData.address = updatePartnerDto.address; + + if (updatePartnerDto.npwp) { + partnerData.npwp = updatePartnerDto.npwp; + } + + await this.connection.transaction(async (manager) => { + await manager.update(Partner, { id: id }, partnerData); + }); + + const dataUser = new UpdateUserDto(); + const userData = await this.userService.findOneByPartner(id); + + dataUser.username = `admin_${partnerData.name}`; + dataUser.partner = partnerData; + + await this.userService.update(userData.id, dataUser, currentUser); + + return partnerData; + } + findAllPartner(page) { return this.partnerRepository.findAndCount({ skip: page * 10, diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index e6a77ff..02e4313 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -1,13 +1,13 @@ import { - Controller, - Get, - Post, Body, - Put, - Param, + Controller, Delete, - ParseUUIDPipe, + Get, HttpStatus, + Param, + ParseUUIDPipe, + Post, + Put, Query, Request, } from '@nestjs/common'; @@ -19,6 +19,7 @@ import { CreateSupplierDto } from './dto/create-supplier.dto'; import { SupplierService } from './supplier/supplier.service'; import { PartnerService } from './partner/partner.service'; import { CreatePartnerDto } from './dto/create-partner.dto'; +import { UpdatePartnerDto } from './dto/update-partner.dto'; @Controller({ path: 'users', @@ -61,6 +62,19 @@ export class UsersController { }; } + @Put('partner/:id') + async updatePartner( + @Param('id', ParseUUIDPipe) id: string, + @Request() req, + @Body() updatePartnerDto: UpdatePartnerDto, + ) { + return { + data: await this.partnerService.update(id, updatePartnerDto, req.user), + statusCode: HttpStatus.CREATED, + message: 'success', + }; + } + @Get() async findAll(@Request() req, @Query('page') page: number) { const [data, count] = await this.usersService.findAll( @@ -143,10 +157,11 @@ export class UsersController { @Put(':id') async update( @Param('id', ParseUUIDPipe) id: string, + @Request() req, @Body() updateUserDto: UpdateUserDto, ) { return { - data: await this.usersService.update(id, updateUserDto), + data: await this.usersService.update(id, updateUserDto, req.user), statusCode: HttpStatus.OK, message: 'success', }; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index e94e43f..743e04c 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -216,7 +216,7 @@ export class UsersService { } } - async update(id: string, updateUserDto: UpdateUserDto) { + async update(id: string, updateUserDto: UpdateUserDto, currentUser: any) { try { await this.usersRepository.findOneOrFail(id); } catch (e) { @@ -233,9 +233,32 @@ export class UsersService { } } - // const result = await this.usersRepository.update(id, updateUserDto); + const check = await this.usersRepository.findOne({ + username: updateUserDto.username, + id: Not(id), + }); - return this.usersRepository.findOneOrFail(id); + if (check) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_ACCEPTABLE, + error: 'Username Already Exist', + }, + HttpStatus.NOT_FOUND, + ); + } + + const userData = new User(); + + userData.id = id; + userData.username = updateUserDto.username; + userData.partner = updateUserDto.partner; + + await this.connection.transaction(async (manager) => { + const result = await manager.update(User, { id: id }, userData); + }); + + return userData; } async remove(id: string) { @@ -266,4 +289,27 @@ export class UsersService { relations: ['roles'], }); } + + async findOneByPartner(partnerId: string) { + try { + return this.usersRepository.findOneOrFail({ + relations: ['roles'], + where: { + partner: partnerId, + }, + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Data not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } }