feat: add update partner endpoint
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user