feat: add update partner endpoint
This commit is contained in:
parent
26e7401163
commit
6b1352e671
5
src/users/dto/update-partner.dto.ts
Normal file
5
src/users/dto/update-partner.dto.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
|
import { CreatePartnerDto } from './create-partner.dto';
|
||||||
|
|
||||||
|
export class UpdatePartnerDto extends PartialType(CreatePartnerDto) {
|
||||||
|
}
|
|
@ -1,18 +1,14 @@
|
||||||
import {
|
import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
|
||||||
forwardRef,
|
|
||||||
HttpException,
|
|
||||||
HttpStatus,
|
|
||||||
Inject,
|
|
||||||
Injectable,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Connection, EntityNotFoundError, Repository } from 'typeorm';
|
import { Connection, Not, Repository } from 'typeorm';
|
||||||
import { CoaService } from '../../transaction/coa.service';
|
import { CoaService } from '../../transaction/coa.service';
|
||||||
import { CreatePartnerDto } from '../dto/create-partner.dto';
|
import { CreatePartnerDto } from '../dto/create-partner.dto';
|
||||||
import { Partner } from '../entities/partner.entity';
|
import { Partner } from '../entities/partner.entity';
|
||||||
import * as uuid from 'uuid';
|
import * as uuid from 'uuid';
|
||||||
import { UsersService } from '../users.service';
|
import { UsersService } from '../users.service';
|
||||||
import { CreateUserDto } from '../dto/create-user.dto';
|
import { CreateUserDto } from '../dto/create-user.dto';
|
||||||
|
import { UpdatePartnerDto } from '../dto/update-partner.dto';
|
||||||
|
import { UpdateUserDto } from '../dto/update-user.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PartnerService {
|
export class PartnerService {
|
||||||
|
@ -38,7 +34,7 @@ export class PartnerService {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
{
|
{
|
||||||
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
||||||
error: 'N Already Exist',
|
error: 'NPWP Already Exist',
|
||||||
},
|
},
|
||||||
HttpStatus.NOT_FOUND,
|
HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
|
@ -67,6 +63,51 @@ export class PartnerService {
|
||||||
return partnerData;
|
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) {
|
findAllPartner(page) {
|
||||||
return this.partnerRepository.findAndCount({
|
return this.partnerRepository.findAndCount({
|
||||||
skip: page * 10,
|
skip: page * 10,
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import {
|
import {
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
Post,
|
|
||||||
Body,
|
Body,
|
||||||
Put,
|
Controller,
|
||||||
Param,
|
|
||||||
Delete,
|
Delete,
|
||||||
ParseUUIDPipe,
|
Get,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
|
Param,
|
||||||
|
ParseUUIDPipe,
|
||||||
|
Post,
|
||||||
|
Put,
|
||||||
Query,
|
Query,
|
||||||
Request,
|
Request,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
|
@ -19,6 +19,7 @@ import { CreateSupplierDto } from './dto/create-supplier.dto';
|
||||||
import { SupplierService } from './supplier/supplier.service';
|
import { SupplierService } from './supplier/supplier.service';
|
||||||
import { PartnerService } from './partner/partner.service';
|
import { PartnerService } from './partner/partner.service';
|
||||||
import { CreatePartnerDto } from './dto/create-partner.dto';
|
import { CreatePartnerDto } from './dto/create-partner.dto';
|
||||||
|
import { UpdatePartnerDto } from './dto/update-partner.dto';
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
path: 'users',
|
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()
|
@Get()
|
||||||
async findAll(@Request() req, @Query('page') page: number) {
|
async findAll(@Request() req, @Query('page') page: number) {
|
||||||
const [data, count] = await this.usersService.findAll(
|
const [data, count] = await this.usersService.findAll(
|
||||||
|
@ -143,10 +157,11 @@ export class UsersController {
|
||||||
@Put(':id')
|
@Put(':id')
|
||||||
async update(
|
async update(
|
||||||
@Param('id', ParseUUIDPipe) id: string,
|
@Param('id', ParseUUIDPipe) id: string,
|
||||||
|
@Request() req,
|
||||||
@Body() updateUserDto: UpdateUserDto,
|
@Body() updateUserDto: UpdateUserDto,
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
data: await this.usersService.update(id, updateUserDto),
|
data: await this.usersService.update(id, updateUserDto, req.user),
|
||||||
statusCode: HttpStatus.OK,
|
statusCode: HttpStatus.OK,
|
||||||
message: 'success',
|
message: 'success',
|
||||||
};
|
};
|
||||||
|
|
|
@ -216,7 +216,7 @@ export class UsersService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, updateUserDto: UpdateUserDto) {
|
async update(id: string, updateUserDto: UpdateUserDto, currentUser: any) {
|
||||||
try {
|
try {
|
||||||
await this.usersRepository.findOneOrFail(id);
|
await this.usersRepository.findOneOrFail(id);
|
||||||
} catch (e) {
|
} 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) {
|
async remove(id: string) {
|
||||||
|
@ -266,4 +289,27 @@ export class UsersService {
|
||||||
relations: ['roles'],
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user