fix: get tranasction history by user
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/product/history-price/history-price.service.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/product/history-price/history-price.service.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| import { Test, TestingModule } from '@nestjs/testing'; | ||||
| import { HistoryPriceService } from './history-price.service'; | ||||
|  | ||||
| describe('HistoryPriceService', () => { | ||||
|   let service: HistoryPriceService; | ||||
|  | ||||
|   beforeEach(async () => { | ||||
|     const module: TestingModule = await Test.createTestingModule({ | ||||
|       providers: [HistoryPriceService], | ||||
|     }).compile(); | ||||
|  | ||||
|     service = module.get<HistoryPriceService>(HistoryPriceService); | ||||
|   }); | ||||
|  | ||||
|   it('should be defined', () => { | ||||
|     expect(service).toBeDefined(); | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										37
									
								
								src/product/history-price/history-price.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/product/history-price/history-price.service.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||||
| import { EntityNotFoundError, IsNull, Repository } from 'typeorm'; | ||||
| import { InjectRepository } from '@nestjs/typeorm'; | ||||
| import { ProductCategories } from '../entities/product-category.entity'; | ||||
| import { ProductHistoryPrice } from '../entities/product-history-price.entity'; | ||||
|  | ||||
| @Injectable() | ||||
| export class ProductHistoryPriceService { | ||||
|   constructor( | ||||
|     @InjectRepository(ProductHistoryPrice) | ||||
|     private productHistoryPriceService: Repository<ProductHistoryPrice>, | ||||
|   ) {} | ||||
|  | ||||
|   async findOne(product: string, partner: string) { | ||||
|     try { | ||||
|       return await this.productHistoryPriceService.findOneOrFail({ | ||||
|         where: { | ||||
|           product: product, | ||||
|           endDate: IsNull(), | ||||
|           partner: partner ? partner : IsNull(), | ||||
|         }, | ||||
|       }); | ||||
|     } catch (e) { | ||||
|       if (e instanceof EntityNotFoundError) { | ||||
|         throw new HttpException( | ||||
|           { | ||||
|             statusCode: HttpStatus.NOT_FOUND, | ||||
|             error: 'Data not found', | ||||
|           }, | ||||
|           HttpStatus.NOT_FOUND, | ||||
|         ); | ||||
|       } else { | ||||
|         throw e; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @@ -12,6 +12,8 @@ import { | ||||
| } from 'typeorm'; | ||||
| import { BaseModel } from '../../config/basemodel.entity'; | ||||
| import { statusTransaction, typeTransaction } from '../../helper/enum-list'; | ||||
| import { Partner } from '../../users/entities/partner.entity'; | ||||
| import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity'; | ||||
|  | ||||
| @Entity() | ||||
| export class Transactions extends BaseModel { | ||||
| @@ -31,4 +33,9 @@ export class Transactions extends BaseModel { | ||||
|     nullable: true, | ||||
|   }) | ||||
|   user_destination: string; | ||||
|  | ||||
|   @ManyToOne(() => ProductHistoryPrice, (product) => product.id) | ||||
|   product_price: ProductHistoryPrice; | ||||
|  | ||||
|   mark_up_price: number; | ||||
| } | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import { | ||||
|   Delete, | ||||
|   Request, | ||||
|   HttpStatus, | ||||
|   Query, | ||||
| } from '@nestjs/common'; | ||||
| import { TransactionService } from './transaction.service'; | ||||
| import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; | ||||
| @@ -73,4 +74,18 @@ export class TransactionController { | ||||
|       req.user, | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   @Get('history') | ||||
|   async findByCategories(@Query('page') page: number, @Request() req) { | ||||
|     const data = await this.transactionService.transactionHistoryByUser( | ||||
|       page, | ||||
|       req.user.userId, | ||||
|     ); | ||||
|  | ||||
|     return { | ||||
|       ...data, | ||||
|       statusCode: HttpStatus.OK, | ||||
|       message: 'success', | ||||
|     }; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -123,6 +123,7 @@ export class TransactionService { | ||||
|     const userData = await this.userService.findByUsername( | ||||
|       currentUser.username, | ||||
|     ); | ||||
|  | ||||
|     if (userData.roles.name != 'Admin') { | ||||
|       throw new HttpException( | ||||
|         { | ||||
| @@ -289,11 +290,13 @@ export class TransactionService { | ||||
|  | ||||
|     let supervisorData = []; | ||||
|  | ||||
|     const profit = product_price.mark_up_price - product_price.price; | ||||
|  | ||||
|     if (!userData.partner) { | ||||
|       //GET SALES | ||||
|       supervisorData = await this.calculateCommission( | ||||
|         supervisorData, | ||||
|         product_price.mark_up_price - product_price.price, | ||||
|         profit, | ||||
|         userData, | ||||
|       ); | ||||
|     } | ||||
| @@ -339,6 +342,7 @@ export class TransactionService { | ||||
|         transactionData.user = userData.id; | ||||
|         transactionData.status = statusTransaction.SUCCESS; | ||||
|         transactionData.type = typeTransaction.ORDER; | ||||
|         transactionData.product_price = product_price; | ||||
|         await manager.insert(Transactions, transactionData); | ||||
|  | ||||
|         await this.accountingTransaction({ | ||||
| @@ -365,9 +369,7 @@ export class TransactionService { | ||||
|             }, | ||||
|             { | ||||
|               coa_id: coaExpense.id, | ||||
|               credit: userData.partner | ||||
|                 ? 0 | ||||
|                 : product_price.mark_up_price - product_price.price, | ||||
|               credit: userData.partner ? 0 : profit, | ||||
|             }, | ||||
|           ].concat(supervisorData), | ||||
|         }); | ||||
| @@ -379,8 +381,39 @@ export class TransactionService { | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   async transactionHistoryByUser(page: number, user: string) { | ||||
|     const baseQuery = this.transactionRepository | ||||
|       .createQueryBuilder('transaction') | ||||
|       .select('transaction.id', 'id') | ||||
|       .addSelect('transaction.created_at', 'created_at') | ||||
|       .where('transaction.user = :id and transaction.type = 1', { | ||||
|         id: user, | ||||
|       }) | ||||
|       .leftJoin('transaction.product_price', 'product_price') | ||||
|       .leftJoin('product_price.product', 'product') | ||||
|       .addSelect('product_price.mark_up_price', 'mark_up_price') | ||||
|       .addSelect('product.name', 'name') | ||||
|       .addSelect('product.id', 'product_id'); | ||||
|  | ||||
|     // .leftJoinAndSelect('transaction.product_price', 'product_price') | ||||
|     // .leftJoinAndSelect('product_price.product', 'product'); | ||||
|  | ||||
|     const data = await baseQuery | ||||
|       .skip(page * 10) | ||||
|       .take(10) | ||||
|       .getRawMany(); | ||||
|  | ||||
|     const totalData = await baseQuery.getCount(); | ||||
|  | ||||
|     return { | ||||
|       data, | ||||
|       count: totalData, | ||||
|     }; | ||||
|   } | ||||
|  | ||||
|   async calculateCommission(data, totalPrice, userData) { | ||||
|     let supervisorData = []; | ||||
|     const supervisorData = []; | ||||
|  | ||||
|     supervisorData.push( | ||||
|       await this.userService.findByUsername(userData.superior.username), | ||||
|     ); | ||||
| @@ -498,17 +531,20 @@ export class TransactionService { | ||||
|             return a.plus(b); | ||||
|           }, new Decimal(0)); | ||||
|  | ||||
|         let coa = coas.find( | ||||
|           (it) => it.id.toLowerCase() === coaId.toLowerCase(), | ||||
|         ); | ||||
|         const coa = coas.find((it) => { | ||||
|           return it.id.toLowerCase() === coaId.toLowerCase(); | ||||
|         }); | ||||
|  | ||||
|         let balance = new Decimal(coa.amount); | ||||
|  | ||||
|         if (coa.balanceType == balanceType.DEBIT) { | ||||
|           balance = balance.plus(debitSum.minus(creditSum)); | ||||
|         } else if (coa.balanceType == balanceType.CREDIT) { | ||||
|           balance = balance.plus(creditSum.minus(debitSum)); | ||||
|         } | ||||
|  | ||||
|         const diff = balance.minus(new Decimal(coa.amount)); | ||||
|  | ||||
|         return createJournalDto.transactionalEntityManager | ||||
|           .createQueryBuilder() | ||||
|           .update(COA) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user