diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index b3eb1bb..cbc862f 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,8 +1,7 @@ -import { Controller, Post, UseGuards, Request, Get } from '@nestjs/common'; +import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common'; import { LocalAuthGuard } from './local-auth.guard'; import { AuthService } from './auth.service'; -import { JwtAuthGuard } from './jwt-auth.guard'; -import {Public} from "./public.decorator"; +import { Public } from './public.decorator'; @Controller({ path: 'auth', @@ -18,9 +17,8 @@ export class AuthController { return this.authService.login(req.user); } - @UseGuards(JwtAuthGuard) @Get('profile') getProfile(@Request() req) { - return req.user; + return this.authService.getProfile(req.user.userId); } } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 3e76f1c..56f4b63 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -35,4 +35,8 @@ export class AuthService { access_token: this.jwtService.sign(payload), }; } + + getProfile = async (userId: string) => { + return this.usersService.findOne(userId); + }; } 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/dto/update-supplier.dto.ts b/src/users/dto/update-supplier.dto.ts new file mode 100644 index 0000000..40a7218 --- /dev/null +++ b/src/users/dto/update-supplier.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/mapped-types'; +import { CreateSupplierDto } from './create-supplier.dto'; + +export class UpdateSupplierDto extends PartialType(CreateSupplierDto) {} diff --git a/src/users/partner/partner.service.ts b/src/users/partner/partner.service.ts index 78ca16d..8d3b23b 100644 --- a/src/users/partner/partner.service.ts +++ b/src/users/partner/partner.service.ts @@ -6,13 +6,15 @@ import { 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 +40,7 @@ export class PartnerService { throw new HttpException( { statusCode: HttpStatus.NOT_ACCEPTABLE, - error: 'N Already Exist', + error: 'NPWP Already Exist', }, HttpStatus.NOT_FOUND, ); @@ -67,6 +69,69 @@ 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; + } + + setStatus = async (id: string, type: string) => { + const partnerData = await this.partnerRepository.findOne({ + id: id, + }); + + if (type === 'active') { + partnerData.status = true; + } else { + partnerData.status = false; + } + + await this.connection.transaction(async (manager) => { + await manager.update(Partner, { id: id }, partnerData); + }); + + return partnerData; + }; + findAllPartner(page) { return this.partnerRepository.findAndCount({ skip: page * 10, diff --git a/src/users/supplier/supplier.service.ts b/src/users/supplier/supplier.service.ts index 3bff728..491b858 100644 --- a/src/users/supplier/supplier.service.ts +++ b/src/users/supplier/supplier.service.ts @@ -6,13 +6,14 @@ import { Injectable, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Connection, EntityNotFoundError, Repository } from 'typeorm'; +import { Connection, EntityNotFoundError, Not, Repository } from 'typeorm'; import { Supplier } from '../entities/supplier.entity'; import { InputCoaDto } from '../../transaction/dto/input-coa.dto'; import { balanceType, coaType } from '../../helper/enum-list'; import { CreateSupplierDto } from '../dto/create-supplier.dto'; import { CoaService } from '../../transaction/coa.service'; import * as uuid from 'uuid'; +import { UpdateSupplierDto } from '../dto/update-supplier.dto'; @Injectable() export class SupplierService { @@ -84,6 +85,50 @@ export class SupplierService { return supplierData; } + async update(id: string, updateSupplierDto: UpdateSupplierDto) { + const check = await this.supplierRepository.findOne({ + code: updateSupplierDto.code, + id: Not(id), + }); + + if (check) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_ACCEPTABLE, + error: 'Supplier Already Exist', + }, + HttpStatus.NOT_FOUND, + ); + } + + const supplierData = new Supplier(); + + supplierData.name = updateSupplierDto.name; + supplierData.status = true; + + await this.connection.transaction(async (manager) => { + await manager.update(Supplier, { id: id }, supplierData); + }); + + return supplierData; + } + + setStatus = async (id: string, type: string) => { + const supplierData = new Supplier(); + + if (type === 'active') { + supplierData.status = true; + } else { + supplierData.status = false; + } + + await this.connection.transaction(async (manager) => { + await manager.update(Supplier, { id: id }, supplierData); + }); + + return supplierData; + }; + findAllSupplier(page) { return this.supplierRepository.findAndCount({ skip: page * 10, diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index e6a77ff..4c15b31 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,8 @@ 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'; +import { UpdateSupplierDto } from './dto/update-supplier.dto'; @Controller({ path: 'users', @@ -49,6 +51,30 @@ export class UsersController { }; } + @Get('supplier/:id/:type') + async updateSupplier( + @Param('id', ParseUUIDPipe) id: string, + @Param('type') type: string, + ) { + return { + data: await this.supplierService.setStatus(id, type), + statusCode: HttpStatus.CREATED, + message: 'success', + }; + } + + @Put('supplier/:id') + async setStatusSupplier( + @Param('id', ParseUUIDPipe) id: string, + @Body() updatePartnerDto: UpdateSupplierDto, + ) { + return { + data: await this.supplierService.update(id, updatePartnerDto), + statusCode: HttpStatus.CREATED, + message: 'success', + }; + } + @Post('partner') async createPartner( @Request() req, @@ -61,6 +87,31 @@ export class UsersController { }; } + @Get('partner/:id/:type') + async setStatusPartner( + @Param('id', ParseUUIDPipe) id: string, + @Param('type') type: string, + ) { + return { + data: await this.partnerService.setStatus(id, type), + statusCode: HttpStatus.CREATED, + message: 'success', + }; + } + + @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( @@ -140,13 +191,26 @@ export class UsersController { }; } + @Get(':id/:type') + async setStatusMembership( + @Param('id', ParseUUIDPipe) id: string, + @Param('type') type: string, + ) { + return { + data: await this.usersService.setStatus(id, type), + statusCode: HttpStatus.CREATED, + message: 'success', + }; + } + @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 c95af73..74edf69 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,11 +233,50 @@ 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; } + setStatus = async (id: string, type: string) => { + const userData = new User(); + + if (type === 'active') { + userData.isActive = true; + } else { + userData.isActive = false; + } + + await this.connection.transaction(async (manager) => { + await manager.update(User, { id: id }, userData); + }); + + return userData; + }; + async remove(id: string) { try { await this.usersRepository.findOneOrFail(id); @@ -266,4 +305,27 @@ export class UsersService { relations: ['roles', 'partner'], }); } + + 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; + } + } + } } diff --git a/yarn.lock b/yarn.lock index 5de4fe2..4e706e9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4671,6 +4671,23 @@ pino@^7.0.5: sonic-boom "^2.2.1" thread-stream "^0.13.0" +pino@^7.5.1: + version "7.5.1" + resolved "https://registry.yarnpkg.com/pino/-/pino-7.5.1.tgz#6e4cd3389e9365caf64039d4b83dcfa84ed43fee" + integrity sha512-Wzo2G7CLaRHKOz3+Ex006LC5Xi0xEUm+mwm/h0NKzuKZONdekcbmjXg7vWDoO8hVTGX+1RuUy2fwlzvZ24EI5A== + dependencies: + fast-redact "^3.0.0" + fastify-warning "^0.2.0" + get-caller-file "^2.0.5" + on-exit-leak-free "^0.2.0" + pino-abstract-transport v0.5.0 + pino-std-serializers "^4.0.0" + quick-format-unescaped "^4.0.3" + real-require "^0.1.0" + safe-stable-stringify "^2.1.0" + sonic-boom "^2.2.1" + thread-stream "^0.13.0" + pirates@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"