From 2e52ae494e3a38299cdad3afa1456430b549b3c1 Mon Sep 17 00:00:00 2001 From: ilham Date: Wed, 8 Dec 2021 09:16:43 +0700 Subject: [PATCH] add: product price --- src/helper/enum-list.ts | 2 +- src/product/dto/product/create-product.dto.ts | 3 ++ .../dto/product/update-price-product.dto.ts | 17 +++++++++++ src/product/dto/product/update-product.dto.ts | 6 ++-- .../entities/product-history-price.entity.ts | 3 ++ src/product/product.service.ts | 23 ++++++++++++++- src/transaction/coa.service.ts | 4 +-- src/transaction/transaction.controller.ts | 18 ------------ src/transaction/transaction.service.ts | 29 +++++++++++++------ 9 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 src/product/dto/product/update-price-product.dto.ts diff --git a/src/helper/enum-list.ts b/src/helper/enum-list.ts index 27b3276..68e79ca 100644 --- a/src/helper/enum-list.ts +++ b/src/helper/enum-list.ts @@ -15,7 +15,7 @@ export enum productType { } export enum coaType { - SYSTEM_BANk, + WALLET, INCOME, } diff --git a/src/product/dto/product/create-product.dto.ts b/src/product/dto/product/create-product.dto.ts index 4c93970..be1ab0c 100644 --- a/src/product/dto/product/create-product.dto.ts +++ b/src/product/dto/product/create-product.dto.ts @@ -13,6 +13,9 @@ export class CreateProductDto { @IsNotEmpty() price: number; + @IsNotEmpty() + markUpPrice: number; + @IsUUID() subCategoriesId: string; } diff --git a/src/product/dto/product/update-price-product.dto.ts b/src/product/dto/product/update-price-product.dto.ts new file mode 100644 index 0000000..9a51190 --- /dev/null +++ b/src/product/dto/product/update-price-product.dto.ts @@ -0,0 +1,17 @@ +import { IsNotEmpty } from 'class-validator'; +import { productType } from '../../../helper/enum-list'; + +export class UpdatePriceProductDto { + @IsNotEmpty() + price: number; + + @IsNotEmpty() + markUpPrice: number; + + @IsNotEmpty() + type: productType; + + startDate: Date; + + endDate: Date; +} diff --git a/src/product/dto/product/update-product.dto.ts b/src/product/dto/product/update-product.dto.ts index e2d43fc..30534c5 100644 --- a/src/product/dto/product/update-product.dto.ts +++ b/src/product/dto/product/update-product.dto.ts @@ -1,4 +1,6 @@ -import { PartialType } from '@nestjs/mapped-types'; +import { OmitType, PartialType } from '@nestjs/mapped-types'; import { CreateProductDto } from './create-product.dto'; -export class UpdateProductDto extends PartialType(CreateProductDto) {} +export class UpdateProductDto extends PartialType( + OmitType(CreateProductDto, ['price'] as const), +) {} diff --git a/src/product/entities/product-history-price.entity.ts b/src/product/entities/product-history-price.entity.ts index 4107952..3b28ccb 100644 --- a/src/product/entities/product-history-price.entity.ts +++ b/src/product/entities/product-history-price.entity.ts @@ -19,6 +19,9 @@ export class ProductHistoryPrice extends BaseModel { @Column() price: number; + @Column() + markUpPrice: number; + @Column({ type: 'date' }) startDate: Date; diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 620f0b4..cf45007 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -7,6 +7,7 @@ import { ProductSubCategoriesService } from './product-sub-categories.service'; import { UpdateProductDto } from './dto/product/update-product.dto'; import { ProductHistoryPrice } from './entities/product-history-price.entity'; import { productType } from '../helper/enum-list'; +import { UpdatePriceProductDto } from './dto/product/update-price-product.dto'; export class ProductService { constructor( @@ -33,6 +34,8 @@ export class ProductService { await this.productHistoryPrice.insert({ product: result.identifiers[0], type: productType.NORMAL, + price: createProductDto.price, + markUpPrice: createProductDto.markUpPrice, startDate: new Date(), endDate: null, }); @@ -84,6 +87,7 @@ export class ProductService { throw e; } } + const subCategories = await this.productSubCategoriesService.findOne( updateProductDto.subCategoriesId, ); @@ -93,12 +97,29 @@ export class ProductService { code: updateProductDto.code, status: updateProductDto.status, subCategories: subCategories, - price: updateProductDto.price, }); return this.productRepository.findOneOrFail(id); } + async updatePrice( + code: string, + updatePriceProductDto: UpdatePriceProductDto, + ) { + const product = await this.findOne(code); + + await this.productHistoryPrice.insert({ + product: product, + type: updatePriceProductDto.type, + price: updatePriceProductDto.price, + markUpPrice: updatePriceProductDto.markUpPrice, + startDate: updatePriceProductDto.startDate, + endDate: updatePriceProductDto.endDate, + }); + + return + } + async remove(id: string) { try { await this.productRepository.findOneOrFail(id); diff --git a/src/transaction/coa.service.ts b/src/transaction/coa.service.ts index e51d36f..a5ce977 100644 --- a/src/transaction/coa.service.ts +++ b/src/transaction/coa.service.ts @@ -10,9 +10,9 @@ export class CoaService { private coaRepository: Repository, ) {} - async findByUser(id: string, type: coaType) { + async findByUser(id: string, typeOfCoa: coaType) { try { - return await this.coaRepository.findOneOrFail({ user: id, type: type }); + return await this.coaRepository.findOneOrFail({ user: id, type: typeOfCoa }); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 93734f8..8822465 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -28,22 +28,4 @@ export class TransactionController { orderTransaction(@Body() orderTransactionDto: OrderTransactionDto) { return this.transactionService.orderTransaction(orderTransactionDto); } - - @Get(':id') - findOne(@Param('id') id: string) { - return this.transactionService.findOne(+id); - } - - @Patch(':id') - update( - @Param('id') id: string, - @Body() updateTransactionDto: UpdateTransactionDto, - ) { - return this.transactionService.update(+id, updateTransactionDto); - } - - @Delete(':id') - remove(@Param('id') id: string) { - return this.transactionService.remove(+id); - } } diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index e5ced1f..a99e55a 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -1,18 +1,21 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; import { OrderTransactionDto } from './dto/order-transaction.dto'; -import { UpdateTransactionDto } from './dto/update-transaction.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { Transactions } from './entities/transactions.entity'; -import { Connection, EntityNotFoundError, Repository } from 'typeorm'; +import { Connection, Repository } from 'typeorm'; import { COA } from './entities/coa.entity'; import { TransactionType } from './entities/transaction-type.entity'; import { TransactionJournal } from './entities/transaction-journal.entity'; import { CoaService } from './coa.service'; -import { statusTransaction } from '../helper/enum-list'; +import { + balanceType, + coaType, + statusTransaction, + typeTransaction, +} from '../helper/enum-list'; import { ProductService } from '../product/product.service'; import * as irsService from '../helper/irs-service'; -import { balanceType, typeTransaction } from '../helper/enum-list'; @Injectable() export class TransactionService { @@ -32,8 +35,14 @@ export class TransactionService { async distributeDeposit(distributeTransactionDto: DistributeTransactionDto) { // GET COA - const coaSender = await this.coaService.findByUser('id_user'); - const coaReciever = await this.coaService.findByUser('id_user'); + const coaSender = await this.coaService.findByUser( + 'id_user', + coaType.WALLET, + ); + const coaReciever = await this.coaService.findByUser( + 'id_user', + coaType.WALLET, + ); await this.connection.transaction(async (manager) => { //INSERT TRANSACTION @@ -73,7 +82,10 @@ export class TransactionService { } async orderTransaction(orderTransactionDto: OrderTransactionDto) { - const coaAccount = await this.coaService.findByUser('id_user'); + const coaAccount = await this.coaService.findByUser( + 'id_user', + coaType.WALLET, + ); //GET PRODUCT const product = await this.productService.findOne( @@ -90,8 +102,7 @@ export class TransactionService { ); } - - try { + try { const orderIRS = await irsService.createTransaction( orderTransactionDto.productCode, orderTransactionDto.destination,