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'; | ||||
| import { Product } from './product.entity'; | ||||
| import { BaseModel } from '../../config/basemodel.entity'; | ||||
|  | ||||
| enum Type { | ||||
|   NORMAL, | ||||
|   PROMO, | ||||
| } | ||||
| import { productType } from '../../helper/enum-list'; | ||||
|  | ||||
| @Entity() | ||||
| export class ProductHistoryPrice extends BaseModel { | ||||
| @@ -30,5 +26,5 @@ export class ProductHistoryPrice extends BaseModel { | ||||
|   endDate: Date; | ||||
|  | ||||
|   @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 { UpdateProductDto } from './dto/product/update-product.dto'; | ||||
| import { ProductHistoryPrice } from './entities/product-history-price.entity'; | ||||
|  | ||||
| enum Type { | ||||
|   NORMAL, | ||||
|   PROMO, | ||||
| } | ||||
| import { productType } from '../helper/enum-list'; | ||||
|  | ||||
| export class ProductService { | ||||
|   constructor( | ||||
| @@ -36,7 +32,7 @@ export class ProductService { | ||||
|  | ||||
|     await this.productHistoryPrice.insert({ | ||||
|       product: result.identifiers[0], | ||||
|       type: Type.NORMAL, | ||||
|       type: productType.NORMAL, | ||||
|       startDate: new Date(), | ||||
|       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 { | ||||
|   Entity, | ||||
|   Column, | ||||
|   PrimaryGeneratedColumn, | ||||
|   UpdateDateColumn, | ||||
|   DeleteDateColumn, | ||||
|   VersionColumn, | ||||
|   CreateDateColumn, | ||||
|   ManyToOne, | ||||
|   ManyToMany, | ||||
|   JoinTable, | ||||
| } from 'typeorm'; | ||||
| import { Product } from '../../product/entities/product.entity'; | ||||
| import { User } from '../../users/entities/user.entity'; | ||||
| import { BaseModel } from '../../config/basemodel.entity'; | ||||
|  | ||||
| enum balanceType { | ||||
|   DEBIT, | ||||
|   CREDIT, | ||||
| } | ||||
| import { balanceType } from '../../helper/enum-list'; | ||||
|  | ||||
| @Entity() | ||||
| export class CoaType extends BaseModel { | ||||
| @@ -26,8 +12,4 @@ export class CoaType extends BaseModel { | ||||
|  | ||||
|   @Column('text') | ||||
|   normalBalance: balanceType; | ||||
|  | ||||
|   @ManyToMany(() => User) | ||||
|   @JoinTable() | ||||
|   user: User[]; | ||||
| } | ||||
|   | ||||
| @@ -1,28 +1,9 @@ | ||||
| import { | ||||
|   Entity, | ||||
|   Column, | ||||
|   PrimaryGeneratedColumn, | ||||
|   UpdateDateColumn, | ||||
|   DeleteDateColumn, | ||||
|   VersionColumn, | ||||
|   CreateDateColumn, | ||||
|   ManyToOne, | ||||
|   ManyToMany, | ||||
|   JoinTable, | ||||
| } from 'typeorm'; | ||||
| import { Product } from '../../product/entities/product.entity'; | ||||
| import { User } from '../../users/entities/user.entity'; | ||||
| import { BaseModel } from '../../config/basemodel.entity'; | ||||
|  | ||||
| enum type { | ||||
|   SYSTEM_BANk, | ||||
|   INCOME, | ||||
| } | ||||
|  | ||||
| enum balanceType { | ||||
|   DEBIT, | ||||
|   CREDIT, | ||||
| } | ||||
| import { coaType, balanceType } from '../../helper/enum-list'; | ||||
|  | ||||
| @Entity() | ||||
| export class COA extends BaseModel { | ||||
| @@ -30,7 +11,7 @@ export class COA extends BaseModel { | ||||
|   name: string; | ||||
|  | ||||
|   @Column('text') | ||||
|   type: type; | ||||
|   type: coaType; | ||||
|  | ||||
|   @Column('text') | ||||
|   balanceType: balanceType; | ||||
| @@ -38,7 +19,6 @@ export class COA extends BaseModel { | ||||
|   @Column() | ||||
|   amount: number; | ||||
|  | ||||
|   @ManyToMany(() => User) | ||||
|   @JoinTable() | ||||
|   user: User[]; | ||||
|   @Column() | ||||
|   user: string; | ||||
| } | ||||
|   | ||||
| @@ -1,44 +1,52 @@ | ||||
| import { | ||||
|   Entity, | ||||
|   Column, | ||||
|   PrimaryGeneratedColumn, | ||||
|   UpdateDateColumn, | ||||
|   DeleteDateColumn, | ||||
|   VersionColumn, | ||||
|   CreateDateColumn, ManyToOne, ManyToMany, JoinTable, OneToOne, | ||||
|   ManyToOne, | ||||
|   ManyToMany, | ||||
|   JoinTable, | ||||
|   OneToOne, | ||||
| } from 'typeorm'; | ||||
| import { Product } from '../../product/entities/product.entity'; | ||||
| import { User } from '../../users/entities/user.entity'; | ||||
| import { BaseModel } from '../../config/basemodel.entity'; | ||||
| import { ProductCategories } from '../../product/entities/product-category.entity'; | ||||
| import { COA } from './coa.entity'; | ||||
| import { Transactions } from './transactions.entity'; | ||||
| import { TransactionType } from './transaction-type.entity'; | ||||
|  | ||||
| enum type { | ||||
|   SYSTEM_BANk, | ||||
|   INCOME, | ||||
| } | ||||
|  | ||||
| enum balanceType { | ||||
|   DEBIT, | ||||
|   CREDIT, | ||||
| } | ||||
| import { balanceType } from '../../helper/enum-list'; | ||||
|  | ||||
| @Entity() | ||||
| export class TransactionJournal extends BaseModel { | ||||
|   @Column('text') | ||||
|   type: type; | ||||
|   type: balanceType; | ||||
|  | ||||
|   @Column() | ||||
|   amount: number; | ||||
|  | ||||
|   @OneToOne(() => Transactions, (trans) => trans.id) | ||||
|   @OneToOne( | ||||
|     () => { | ||||
|       return Transactions; | ||||
|     }, | ||||
|     (trans) => { | ||||
|       return trans.id; | ||||
|     }, | ||||
|   ) | ||||
|   transaction: Transactions; | ||||
|  | ||||
|   @ManyToOne(() => COA, (coa) => coa.id) | ||||
|   @ManyToOne( | ||||
|     () => { | ||||
|       return COA; | ||||
|     }, | ||||
|     (coa) => { | ||||
|       return coa.id; | ||||
|     }, | ||||
|   ) | ||||
|   coa: COA; | ||||
|  | ||||
|   @ManyToOne(() => TransactionType, (transType) => transType.id) | ||||
|   @ManyToOne( | ||||
|     () => { | ||||
|       return TransactionType; | ||||
|     }, | ||||
|     (transType) => { | ||||
|       return transType.id; | ||||
|     }, | ||||
|   ) | ||||
|   transactionType: TransactionType; | ||||
| } | ||||
|   | ||||
| @@ -10,17 +10,8 @@ import { | ||||
|   ManyToMany, | ||||
|   JoinTable, | ||||
| } from 'typeorm'; | ||||
| import { Product } from '../../product/entities/product.entity'; | ||||
| import { User } from '../../users/entities/user.entity'; | ||||
| import { BaseModel } from '../../config/basemodel.entity'; | ||||
| import { COA } from './coa.entity'; | ||||
| import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity'; | ||||
|  | ||||
| enum status { | ||||
|   PENDING, | ||||
|   SUCCESS, | ||||
|   FAILED, | ||||
| } | ||||
| import { statusTransaction, typeTransaction } from '../../helper/enum-list'; | ||||
|  | ||||
| @Entity() | ||||
| export class Transactions extends BaseModel { | ||||
| @@ -28,8 +19,16 @@ export class Transactions extends BaseModel { | ||||
|   amount: number; | ||||
|  | ||||
|   @Column() | ||||
|   status: status; | ||||
|   status: statusTransaction; | ||||
|  | ||||
|   @ManyToOne(() => User, (user) => user.id) | ||||
|   user: User; | ||||
|   @Column() | ||||
|   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 { TransactionJournal } from './entities/transaction-journal.entity'; | ||||
| import { Transactions } from './entities/transactions.entity'; | ||||
| import { CoaService } from './coa.service'; | ||||
|  | ||||
| @Module({ | ||||
|   imports: [ | ||||
| @@ -20,6 +21,6 @@ import { Transactions } from './entities/transactions.entity'; | ||||
|     ]), | ||||
|   ], | ||||
|   controllers: [TransactionController, PpobCallbackController], | ||||
|   providers: [TransactionService], | ||||
|   providers: [TransactionService, CoaService], | ||||
| }) | ||||
| export class TransactionModule {} | ||||
|   | ||||
| @@ -3,11 +3,12 @@ import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; | ||||
| import { UpdateTransactionDto } from './dto/update-transaction.dto'; | ||||
| import { InjectRepository } from '@nestjs/typeorm'; | ||||
| import { Transactions } from './entities/transactions.entity'; | ||||
| import { Repository } from 'typeorm'; | ||||
| import { User } from '../users/entities/user.entity'; | ||||
| 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'; | ||||
|  | ||||
| @Injectable() | ||||
| export class TransactionService { | ||||
| @@ -19,31 +20,50 @@ export class TransactionService { | ||||
|     @InjectRepository(TransactionJournal) | ||||
|     private transactionJournalRepository: Repository<TransactionJournal>, | ||||
|     @InjectRepository(COA) | ||||
|     private coaRepository: Repository<COA> | ||||
|     private coaRepository: Repository<COA>, | ||||
|     private coaService: CoaService, | ||||
|     private connection: Connection, | ||||
|   ) {} | ||||
|  | ||||
|   async create(distributeTransactionDto: DistributeTransactionDto) { | ||||
|     // GET USER | ||||
|     // GET COA | ||||
|     const coaSender = await this.coaService.findByUser('id_user'); | ||||
|     const coaReciever = await this.coaService.findByUser('id_user'); | ||||
|  | ||||
|     // 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 | ||||
|     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({ | ||||
|     //   amount:1 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user