add: create supplier
This commit is contained in:
15
src/users/dto/create-partner.dto.ts
Normal file
15
src/users/dto/create-partner.dto.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class CreatePartnerDto {
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
address: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
owner: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
npwp: string;
|
||||
}
|
||||
9
src/users/dto/create-supplier.dto.ts
Normal file
9
src/users/dto/create-supplier.dto.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class CreateSupplierDto {
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
}
|
||||
@@ -4,10 +4,16 @@ import { BaseModel } from '../../config/basemodel.entity';
|
||||
import { hashPassword } from '../../helper/hash_password';
|
||||
|
||||
@Entity()
|
||||
export class User extends BaseModel {
|
||||
export class Supplier extends BaseModel {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
code: string;
|
||||
|
||||
@Column()
|
||||
status: boolean;
|
||||
}
|
||||
18
src/users/supplier.service.spec.ts
Normal file
18
src/users/supplier.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SupplierService } from './supplier.service';
|
||||
|
||||
describe('PartnerService', () => {
|
||||
let service: SupplierService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [SupplierService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<SupplierService>(SupplierService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
114
src/users/supplier.service.ts
Normal file
114
src/users/supplier.service.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import {
|
||||
forwardRef,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Inject,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Connection, EntityNotFoundError, 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';
|
||||
|
||||
@Injectable()
|
||||
export class SupplierService {
|
||||
constructor(
|
||||
@InjectRepository(Supplier)
|
||||
private supplierRepository: Repository<Supplier>,
|
||||
@Inject(
|
||||
forwardRef(() => {
|
||||
return CoaService;
|
||||
}),
|
||||
)
|
||||
private coaService: CoaService,
|
||||
private connection: Connection,
|
||||
) {}
|
||||
|
||||
async create(createSupplierDto: CreateSupplierDto) {
|
||||
const check = await this.supplierRepository.findOne({
|
||||
code: createSupplierDto.code,
|
||||
});
|
||||
|
||||
if (check) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
||||
error: 'Supplier Already Exist',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const supplierData = new Supplier();
|
||||
supplierData.id = uuid.v4();
|
||||
supplierData.name = createSupplierDto.name;
|
||||
supplierData.code = createSupplierDto.code;
|
||||
supplierData.status = true;
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
const result = await manager.insert(Supplier, supplierData);
|
||||
|
||||
const dataCoaInventory = new InputCoaDto();
|
||||
dataCoaInventory.supplier = supplierData;
|
||||
dataCoaInventory.balanceType = balanceType.DEBIT;
|
||||
dataCoaInventory.type = coaType.INVENTORY;
|
||||
dataCoaInventory.coaEntityManager = manager;
|
||||
await this.coaService.create(dataCoaInventory);
|
||||
|
||||
const dataCoaCostOfSales = new InputCoaDto();
|
||||
dataCoaCostOfSales.supplier = supplierData;
|
||||
dataCoaCostOfSales.balanceType = balanceType.DEBIT;
|
||||
dataCoaCostOfSales.type = coaType.COST_OF_SALES;
|
||||
dataCoaCostOfSales.coaEntityManager = manager;
|
||||
await this.coaService.create(dataCoaCostOfSales);
|
||||
|
||||
const dataCoaBudget = new InputCoaDto();
|
||||
dataCoaBudget.supplier = supplierData;
|
||||
dataCoaBudget.balanceType = balanceType.DEBIT;
|
||||
dataCoaBudget.type = coaType.BUDGET;
|
||||
dataCoaBudget.coaEntityManager = manager;
|
||||
await this.coaService.create(dataCoaBudget);
|
||||
|
||||
const dataCoaContraBudget = new InputCoaDto();
|
||||
dataCoaContraBudget.supplier = supplierData;
|
||||
dataCoaContraBudget.balanceType = balanceType.CREDIT;
|
||||
dataCoaContraBudget.type = coaType.CONTRA_BUDGET;
|
||||
dataCoaContraBudget.coaEntityManager = manager;
|
||||
await this.coaService.create(dataCoaContraBudget);
|
||||
});
|
||||
|
||||
return supplierData;
|
||||
}
|
||||
|
||||
findAllSupplier(page) {
|
||||
return this.supplierRepository.findAndCount({
|
||||
skip: page * 10,
|
||||
take: 10,
|
||||
order: {
|
||||
version: 'DESC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
try {
|
||||
return await this.supplierRepository.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,13 +15,18 @@ import { UsersService } from './users.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { Public } from '../auth/public.decorator';
|
||||
import { CreateSupplierDto } from './dto/create-supplier.dto';
|
||||
import { SupplierService } from './supplier.service';
|
||||
|
||||
@Controller({
|
||||
path: 'users',
|
||||
version: '1',
|
||||
})
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
constructor(
|
||||
private readonly usersService: UsersService,
|
||||
private readonly supplierService: SupplierService,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
async create(@Request() req, @Body() createUserDto: CreateUserDto) {
|
||||
@@ -32,6 +37,15 @@ export class UsersController {
|
||||
};
|
||||
}
|
||||
|
||||
@Post('supplier')
|
||||
async createPartner(@Body() createPartnerDto: CreateSupplierDto) {
|
||||
return {
|
||||
data: await this.supplierService.create(createPartnerDto),
|
||||
statusCode: HttpStatus.CREATED,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get()
|
||||
async findAll(@Query('page') page: number) {
|
||||
|
||||
@@ -5,11 +5,17 @@ import { UsersController } from './users.controller';
|
||||
import { User } from './entities/user.entity';
|
||||
import { TransactionModule } from 'src/transaction/transaction.module';
|
||||
import { ConfigurableModule } from 'src/configurable/configurable.module';
|
||||
import { SupplierService } from './supplier.service';
|
||||
import { Supplier } from './entities/supplier.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User]), forwardRef(() => TransactionModule), ConfigurableModule],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([User, Supplier]),
|
||||
forwardRef(() => TransactionModule),
|
||||
ConfigurableModule,
|
||||
],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
providers: [UsersService, SupplierService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
||||
Reference in New Issue
Block a user