add: change implement enum and distribution
This commit is contained in:
		
							
								
								
									
										25
									
								
								src/helper/enum-list.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/helper/enum-list.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | |||||||
|  | export enum statusTransaction { | ||||||
|  |   PENDING, | ||||||
|  |   SUCCESS, | ||||||
|  |   FAILED, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export enum typeTransaction { | ||||||
|  |   DISTRIBUTION, | ||||||
|  |   ORDER, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export enum productType { | ||||||
|  |   NORMAL, | ||||||
|  |   PROMO, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export enum coaType { | ||||||
|  |   SYSTEM_BANk, | ||||||
|  |   INCOME, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export enum balanceType { | ||||||
|  |   DEBIT, | ||||||
|  |   CREDIT, | ||||||
|  | } | ||||||
| @@ -6,11 +6,7 @@ import { | |||||||
| } from 'typeorm'; | } from 'typeorm'; | ||||||
| import { Product } from './product.entity'; | import { Product } from './product.entity'; | ||||||
| import { BaseModel } from '../../config/basemodel.entity'; | import { BaseModel } from '../../config/basemodel.entity'; | ||||||
|  | import { productType } from '../../helper/enum-list'; | ||||||
| enum Type { |  | ||||||
|   NORMAL, |  | ||||||
|   PROMO, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @Entity() | @Entity() | ||||||
| export class ProductHistoryPrice extends BaseModel { | export class ProductHistoryPrice extends BaseModel { | ||||||
| @@ -30,5 +26,5 @@ export class ProductHistoryPrice extends BaseModel { | |||||||
|   endDate: Date; |   endDate: Date; | ||||||
|  |  | ||||||
|   @Column('text') |   @Column('text') | ||||||
|   type: Type; |   type: productType; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,11 +6,7 @@ import { CreateProductDto } from '../product/dto/product/create-product.dto'; | |||||||
| import { ProductSubCategoriesService } from './product-sub-categories.service'; | import { ProductSubCategoriesService } from './product-sub-categories.service'; | ||||||
| import { UpdateProductDto } from './dto/product/update-product.dto'; | import { UpdateProductDto } from './dto/product/update-product.dto'; | ||||||
| import { ProductHistoryPrice } from './entities/product-history-price.entity'; | import { ProductHistoryPrice } from './entities/product-history-price.entity'; | ||||||
|  | import { productType } from '../helper/enum-list'; | ||||||
| enum Type { |  | ||||||
|   NORMAL, |  | ||||||
|   PROMO, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| export class ProductService { | export class ProductService { | ||||||
|   constructor( |   constructor( | ||||||
| @@ -36,7 +32,7 @@ export class ProductService { | |||||||
|  |  | ||||||
|     await this.productHistoryPrice.insert({ |     await this.productHistoryPrice.insert({ | ||||||
|       product: result.identifiers[0], |       product: result.identifiers[0], | ||||||
|       type: Type.NORMAL, |       type: productType.NORMAL, | ||||||
|       startDate: new Date(), |       startDate: new Date(), | ||||||
|       endDate: null, |       endDate: null, | ||||||
|     }); |     }); | ||||||
|   | |||||||
							
								
								
									
										29
									
								
								src/transaction/coa.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/transaction/coa.service.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||||||
|  | import { EntityNotFoundError, Repository } from 'typeorm'; | ||||||
|  | import { InjectRepository } from '@nestjs/typeorm'; | ||||||
|  | import { COA } from './entities/coa.entity'; | ||||||
|  |  | ||||||
|  | export class CoaService { | ||||||
|  |   constructor( | ||||||
|  |     @InjectRepository(COA) | ||||||
|  |     private coaRepository: Repository<COA>, | ||||||
|  |   ) {} | ||||||
|  |  | ||||||
|  |   async findByUser(id: string) { | ||||||
|  |     try { | ||||||
|  |       return await this.coaRepository.findOneOrFail({ user: id }); | ||||||
|  |     } catch (e) { | ||||||
|  |       if (e instanceof EntityNotFoundError) { | ||||||
|  |         throw new HttpException( | ||||||
|  |           { | ||||||
|  |             statusCode: HttpStatus.NOT_FOUND, | ||||||
|  |             error: 'Data not found', | ||||||
|  |           }, | ||||||
|  |           HttpStatus.NOT_FOUND, | ||||||
|  |         ); | ||||||
|  |       } else { | ||||||
|  |         throw e; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @@ -1,23 +1,9 @@ | |||||||
| import { | import { | ||||||
|   Entity, |   Entity, | ||||||
|   Column, |   Column, | ||||||
|   PrimaryGeneratedColumn, |  | ||||||
|   UpdateDateColumn, |  | ||||||
|   DeleteDateColumn, |  | ||||||
|   VersionColumn, |  | ||||||
|   CreateDateColumn, |  | ||||||
|   ManyToOne, |  | ||||||
|   ManyToMany, |  | ||||||
|   JoinTable, |  | ||||||
| } from 'typeorm'; | } from 'typeorm'; | ||||||
| import { Product } from '../../product/entities/product.entity'; |  | ||||||
| import { User } from '../../users/entities/user.entity'; |  | ||||||
| import { BaseModel } from '../../config/basemodel.entity'; | import { BaseModel } from '../../config/basemodel.entity'; | ||||||
|  | import { balanceType } from '../../helper/enum-list'; | ||||||
| enum balanceType { |  | ||||||
|   DEBIT, |  | ||||||
|   CREDIT, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @Entity() | @Entity() | ||||||
| export class CoaType extends BaseModel { | export class CoaType extends BaseModel { | ||||||
| @@ -26,8 +12,4 @@ export class CoaType extends BaseModel { | |||||||
|  |  | ||||||
|   @Column('text') |   @Column('text') | ||||||
|   normalBalance: balanceType; |   normalBalance: balanceType; | ||||||
|  |  | ||||||
|   @ManyToMany(() => User) |  | ||||||
|   @JoinTable() |  | ||||||
|   user: User[]; |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,28 +1,9 @@ | |||||||
| import { | import { | ||||||
|   Entity, |   Entity, | ||||||
|   Column, |   Column, | ||||||
|   PrimaryGeneratedColumn, |  | ||||||
|   UpdateDateColumn, |  | ||||||
|   DeleteDateColumn, |  | ||||||
|   VersionColumn, |  | ||||||
|   CreateDateColumn, |  | ||||||
|   ManyToOne, |  | ||||||
|   ManyToMany, |  | ||||||
|   JoinTable, |  | ||||||
| } from 'typeorm'; | } from 'typeorm'; | ||||||
| import { Product } from '../../product/entities/product.entity'; |  | ||||||
| import { User } from '../../users/entities/user.entity'; |  | ||||||
| import { BaseModel } from '../../config/basemodel.entity'; | import { BaseModel } from '../../config/basemodel.entity'; | ||||||
|  | import { coaType, balanceType } from '../../helper/enum-list'; | ||||||
| enum type { |  | ||||||
|   SYSTEM_BANk, |  | ||||||
|   INCOME, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| enum balanceType { |  | ||||||
|   DEBIT, |  | ||||||
|   CREDIT, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @Entity() | @Entity() | ||||||
| export class COA extends BaseModel { | export class COA extends BaseModel { | ||||||
| @@ -30,7 +11,7 @@ export class COA extends BaseModel { | |||||||
|   name: string; |   name: string; | ||||||
|  |  | ||||||
|   @Column('text') |   @Column('text') | ||||||
|   type: type; |   type: coaType; | ||||||
|  |  | ||||||
|   @Column('text') |   @Column('text') | ||||||
|   balanceType: balanceType; |   balanceType: balanceType; | ||||||
| @@ -38,7 +19,6 @@ export class COA extends BaseModel { | |||||||
|   @Column() |   @Column() | ||||||
|   amount: number; |   amount: number; | ||||||
|  |  | ||||||
|   @ManyToMany(() => User) |   @Column() | ||||||
|   @JoinTable() |   user: string; | ||||||
|   user: User[]; |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,44 +1,52 @@ | |||||||
| import { | import { | ||||||
|   Entity, |   Entity, | ||||||
|   Column, |   Column, | ||||||
|   PrimaryGeneratedColumn, |   ManyToOne, | ||||||
|   UpdateDateColumn, |   ManyToMany, | ||||||
|   DeleteDateColumn, |   JoinTable, | ||||||
|   VersionColumn, |   OneToOne, | ||||||
|   CreateDateColumn, ManyToOne, ManyToMany, JoinTable, OneToOne, |  | ||||||
| } from 'typeorm'; | } from 'typeorm'; | ||||||
| import { Product } from '../../product/entities/product.entity'; |  | ||||||
| import { User } from '../../users/entities/user.entity'; |  | ||||||
| import { BaseModel } from '../../config/basemodel.entity'; | import { BaseModel } from '../../config/basemodel.entity'; | ||||||
| import { ProductCategories } from '../../product/entities/product-category.entity'; |  | ||||||
| import { COA } from './coa.entity'; | import { COA } from './coa.entity'; | ||||||
| import { Transactions } from './transactions.entity'; | import { Transactions } from './transactions.entity'; | ||||||
| import { TransactionType } from './transaction-type.entity'; | import { TransactionType } from './transaction-type.entity'; | ||||||
|  | import { balanceType } from '../../helper/enum-list'; | ||||||
| enum type { |  | ||||||
|   SYSTEM_BANk, |  | ||||||
|   INCOME, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| enum balanceType { |  | ||||||
|   DEBIT, |  | ||||||
|   CREDIT, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @Entity() | @Entity() | ||||||
| export class TransactionJournal extends BaseModel { | export class TransactionJournal extends BaseModel { | ||||||
|   @Column('text') |   @Column('text') | ||||||
|   type: type; |   type: balanceType; | ||||||
|  |  | ||||||
|   @Column() |   @Column() | ||||||
|   amount: number; |   amount: number; | ||||||
|  |  | ||||||
|   @OneToOne(() => Transactions, (trans) => trans.id) |   @OneToOne( | ||||||
|  |     () => { | ||||||
|  |       return Transactions; | ||||||
|  |     }, | ||||||
|  |     (trans) => { | ||||||
|  |       return trans.id; | ||||||
|  |     }, | ||||||
|  |   ) | ||||||
|   transaction: Transactions; |   transaction: Transactions; | ||||||
|  |  | ||||||
|   @ManyToOne(() => COA, (coa) => coa.id) |   @ManyToOne( | ||||||
|  |     () => { | ||||||
|  |       return COA; | ||||||
|  |     }, | ||||||
|  |     (coa) => { | ||||||
|  |       return coa.id; | ||||||
|  |     }, | ||||||
|  |   ) | ||||||
|   coa: COA; |   coa: COA; | ||||||
|  |  | ||||||
|   @ManyToOne(() => TransactionType, (transType) => transType.id) |   @ManyToOne( | ||||||
|  |     () => { | ||||||
|  |       return TransactionType; | ||||||
|  |     }, | ||||||
|  |     (transType) => { | ||||||
|  |       return transType.id; | ||||||
|  |     }, | ||||||
|  |   ) | ||||||
|   transactionType: TransactionType; |   transactionType: TransactionType; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -10,17 +10,8 @@ import { | |||||||
|   ManyToMany, |   ManyToMany, | ||||||
|   JoinTable, |   JoinTable, | ||||||
| } from 'typeorm'; | } from 'typeorm'; | ||||||
| import { Product } from '../../product/entities/product.entity'; |  | ||||||
| import { User } from '../../users/entities/user.entity'; |  | ||||||
| import { BaseModel } from '../../config/basemodel.entity'; | import { BaseModel } from '../../config/basemodel.entity'; | ||||||
| import { COA } from './coa.entity'; | import { statusTransaction, typeTransaction } from '../../helper/enum-list'; | ||||||
| import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity'; |  | ||||||
|  |  | ||||||
| enum status { |  | ||||||
|   PENDING, |  | ||||||
|   SUCCESS, |  | ||||||
|   FAILED, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| @Entity() | @Entity() | ||||||
| export class Transactions extends BaseModel { | export class Transactions extends BaseModel { | ||||||
| @@ -28,8 +19,16 @@ export class Transactions extends BaseModel { | |||||||
|   amount: number; |   amount: number; | ||||||
|  |  | ||||||
|   @Column() |   @Column() | ||||||
|   status: status; |   status: statusTransaction; | ||||||
|  |  | ||||||
|   @ManyToOne(() => User, (user) => user.id) |   @Column() | ||||||
|   user: User; |   type: typeTransaction; | ||||||
|  |  | ||||||
|  |   @Column() | ||||||
|  |   user: string; | ||||||
|  |  | ||||||
|  |   @Column({ | ||||||
|  |     nullable: true, | ||||||
|  |   }) | ||||||
|  |   userDestination: string; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -8,6 +8,7 @@ import { CoaType } from './entities/coa-type.entity'; | |||||||
| import { TransactionType } from './entities/transaction-type.entity'; | import { TransactionType } from './entities/transaction-type.entity'; | ||||||
| import { TransactionJournal } from './entities/transaction-journal.entity'; | import { TransactionJournal } from './entities/transaction-journal.entity'; | ||||||
| import { Transactions } from './entities/transactions.entity'; | import { Transactions } from './entities/transactions.entity'; | ||||||
|  | import { CoaService } from './coa.service'; | ||||||
|  |  | ||||||
| @Module({ | @Module({ | ||||||
|   imports: [ |   imports: [ | ||||||
| @@ -20,6 +21,6 @@ import { Transactions } from './entities/transactions.entity'; | |||||||
|     ]), |     ]), | ||||||
|   ], |   ], | ||||||
|   controllers: [TransactionController, PpobCallbackController], |   controllers: [TransactionController, PpobCallbackController], | ||||||
|   providers: [TransactionService], |   providers: [TransactionService, CoaService], | ||||||
| }) | }) | ||||||
| export class TransactionModule {} | export class TransactionModule {} | ||||||
|   | |||||||
| @@ -3,11 +3,12 @@ import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; | |||||||
| import { UpdateTransactionDto } from './dto/update-transaction.dto'; | import { UpdateTransactionDto } from './dto/update-transaction.dto'; | ||||||
| import { InjectRepository } from '@nestjs/typeorm'; | import { InjectRepository } from '@nestjs/typeorm'; | ||||||
| import { Transactions } from './entities/transactions.entity'; | import { Transactions } from './entities/transactions.entity'; | ||||||
| import { Repository } from 'typeorm'; | import { Connection, Repository } from 'typeorm'; | ||||||
| import { User } from '../users/entities/user.entity'; |  | ||||||
| import { COA } from './entities/coa.entity'; | import { COA } from './entities/coa.entity'; | ||||||
| import { TransactionType } from './entities/transaction-type.entity'; | import { TransactionType } from './entities/transaction-type.entity'; | ||||||
| import { TransactionJournal } from './entities/transaction-journal.entity'; | import { TransactionJournal } from './entities/transaction-journal.entity'; | ||||||
|  | import { CoaService } from './coa.service'; | ||||||
|  | import { statusTransaction } from '../helper/enum-list'; | ||||||
|  |  | ||||||
| @Injectable() | @Injectable() | ||||||
| export class TransactionService { | export class TransactionService { | ||||||
| @@ -19,31 +20,50 @@ export class TransactionService { | |||||||
|     @InjectRepository(TransactionJournal) |     @InjectRepository(TransactionJournal) | ||||||
|     private transactionJournalRepository: Repository<TransactionJournal>, |     private transactionJournalRepository: Repository<TransactionJournal>, | ||||||
|     @InjectRepository(COA) |     @InjectRepository(COA) | ||||||
|     private coaRepository: Repository<COA> |     private coaRepository: Repository<COA>, | ||||||
|  |     private coaService: CoaService, | ||||||
|  |     private connection: Connection, | ||||||
|   ) {} |   ) {} | ||||||
|  |  | ||||||
|   async create(distributeTransactionDto: DistributeTransactionDto) { |   async create(distributeTransactionDto: DistributeTransactionDto) { | ||||||
|     // GET USER |  | ||||||
|     // GET COA |     // GET COA | ||||||
|  |     const coaSender = await this.coaService.findByUser('id_user'); | ||||||
|  |     const coaReciever = await this.coaService.findByUser('id_user'); | ||||||
|  |  | ||||||
|     // GET TYPE TRANSAKSI |     // GET TYPE TRANSAKSI | ||||||
|  |  | ||||||
|  |     await this.connection.transaction(async (manager) => { | ||||||
|  |       //INSERT TRANSACTION | ||||||
|  |       const transactionSaved = await manager.insert(Transactions, { | ||||||
|  |         amount: distributeTransactionDto.amount, | ||||||
|  |         user: 'id_user', | ||||||
|  |         userDestination: distributeTransactionDto.destination, | ||||||
|  |         status: statusTransaction.SUCCESS, | ||||||
|  |       }); | ||||||
|  |  | ||||||
|  |       //INSERT TRANSACTION JOURNAL FOR SENDER | ||||||
|  |       const journalSender = await manager.insert(TransactionJournal, { | ||||||
|  |         amount: distributeTransactionDto.amount, | ||||||
|  |         transaction: transactionSaved.identifiers[0], | ||||||
|  |         coa: coaSender, | ||||||
|  |       }); | ||||||
|  |  | ||||||
|  |       //INSERT TRANSACTION JOURNAL FOR RECEIVER | ||||||
|  |       const journalReceiver = await manager.insert(TransactionJournal, { | ||||||
|  |         amount: distributeTransactionDto.amount, | ||||||
|  |         transaction: transactionSaved.identifiers[0], | ||||||
|  |         coa: coaReciever, | ||||||
|  |       }); | ||||||
|  |  | ||||||
|  |       //UPDATE AMOUNT COA SENDER | ||||||
|  |       coaSender.amount = coaSender.amount - distributeTransactionDto.amount; | ||||||
|  |       await manager.save(coaSender); | ||||||
|  |  | ||||||
|  |       coaReciever.amount = coaReciever.amount + distributeTransactionDto.amount; | ||||||
|  |       await manager.save(coaReciever); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|     //ADD USER IN INSERT |     //ADD USER IN INSERT | ||||||
|     const transactionSaved = await this.transactionRepository.insert({ |  | ||||||
|       amount: distributeTransactionDto.amount, |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     //INSERT TRANSACTION JOURNAL FOR SEND |  | ||||||
|     await this.transactionJournalRepository.insert({ |  | ||||||
|       amount: distributeTransactionDto.amount, |  | ||||||
|       transaction: transactionSaved.identifiers[0], |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     //INSERT TRANSACTION JOURNAL FOR RECIEVE |  | ||||||
|     await this.transactionJournalRepository.insert({ |  | ||||||
|       amount: distributeTransactionDto.amount, |  | ||||||
|       transaction: transactionSaved.identifiers[0], |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     // await this.coaRepository.update({ |     // await this.coaRepository.update({ | ||||||
|     //   amount:1 |     //   amount:1 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user