diff --git a/src/product/history-price/history-price.service.ts b/src/product/history-price/history-price.service.ts index 694dedd..698b369 100644 --- a/src/product/history-price/history-price.service.ts +++ b/src/product/history-price/history-price.service.ts @@ -42,7 +42,12 @@ export class ProductHistoryPriceService { async findById(id: string) { try { - return await this.productHistoryPriceService.findOneOrFail(id); + return await this.productHistoryPriceService.findOneOrFail({ + where: { + id: id, + }, + relations: ['product'], + }); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 55573ef..e4bcfb0 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -1,5 +1,5 @@ import { HttpException, HttpStatus } from '@nestjs/common'; -import { EntityNotFoundError, Repository } from 'typeorm'; +import { EntityNotFoundError, IsNull, Repository } from 'typeorm'; import { Product } from './entities/product.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { CreateProductDto } from './dto/product/create-product.dto'; @@ -87,27 +87,37 @@ export class ProductService { partner: partnerData.id, }, }); + + } else { dataHistoryPrice = await this.productHistoryPrice.findOne({ product: productData, + partner: IsNull(), }); } if (!dataHistoryPrice) { - return; + await this.productHistoryPrice.insert({ + product: productData, + mark_up_price: it[4], + price: it[3], + type: productType.NORMAL, + startDate: new Date(), + partner: it[6] != '-' ? partnerData : null, + }); + } else { + dataHistoryPrice.endDate = new Date(); + await this.productHistoryPrice.save(dataHistoryPrice); + + await this.productHistoryPrice.insert({ + product: productData, + mark_up_price: it[4], + price: it[3], + type: productType.NORMAL, + startDate: new Date(), + partner: it[6] != '-' ? partnerData : null, + }); } - - dataHistoryPrice.endDate = new Date(); - await this.productHistoryPrice.save(dataHistoryPrice); - - await this.productHistoryPrice.insert({ - product: productData, - mark_up_price: it[4], - price: it[3], - type: productType.NORMAL, - startDate: new Date(), - partner: it[6] != '-' ? partnerData : null, - }); } else { let partnerData; if (it[6] != '-' && it[6] != '') { diff --git a/src/transaction/entities/transactions.entity.ts b/src/transaction/entities/transactions.entity.ts index c9b4c74..ba5c342 100644 --- a/src/transaction/entities/transactions.entity.ts +++ b/src/transaction/entities/transactions.entity.ts @@ -58,6 +58,16 @@ export class Transactions extends BaseModel { }) phone_number: string; + @Column({ + nullable: true, + }) + request_json: string; + + @Column({ + nullable: true, + }) + callback_json: string; + mark_up_price: number; userData: UserDetail; diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index e7064ec..f208388 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -13,6 +13,7 @@ import { import { TransactionService } from './transaction.service'; import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; import { FastifyRequest } from 'fastify'; +import { Public } from '../auth/public.decorator'; @Controller({ path: 'ppob_callback', @@ -23,6 +24,7 @@ export class PpobCallbackController { constructor(private readonly transactionService: TransactionService) {} + @Public() @Get() async get(@Req() request: FastifyRequest) { const response = request.query; @@ -30,12 +32,16 @@ export class PpobCallbackController { if (response['statuscode'] == 2) { //TODO: UPDATE GAGAL const updateTransaction = - await this.transactionService.callbackOrderFailed(response['clientid']); + await this.transactionService.callbackOrderFailed( + response['clientid'], + response, + ); } else { //TODO: UPDATE BERHASIL const updateTransaction = await this.transactionService.callbackOrderSuccess( response['clientid'], + response, ); } this.logger.log({ diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index dc64cee..bbcfcc3 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -319,25 +319,8 @@ export class TransactionService { ); let supervisorData = []; - let profit = product_price.mark_up_price; - if (!userData.partner) { - //GET SALES - supervisorData = await this.calculateCommission( - supervisorData, - profit, - userData, - ); - profit = supervisorData - .map((item) => { - return item.credit; - }) - .reduce((prev, curr) => { - return prev + curr; - }, 0); - } - //GET COA const coaAccount = await this.coaService.findByUser( userData.id, @@ -360,6 +343,29 @@ export class TransactionService { `${coaType[coaType.EXPENSE]}-SYSTEM`, ); + if (!userData.partner) { + //GET SALES + supervisorData = await this.calculateCommission( + supervisorData, + profit, + userData, + ); + profit = supervisorData + .map((item) => { + return item.credit; + }) + .reduce((prev, curr) => { + return prev + curr; + }, 0); + + supervisorData = supervisorData.concat([ + { + coa_id: coaExpense.id, + debit: profit, + }, + ]); + } + if (coaAccount.amount <= product.price) { throw new HttpException( { @@ -406,10 +412,6 @@ export class TransactionService { coa_id: coaSales.id, credit: product_price.mark_up_price + product_price.price, }, - { - coa_id: coaExpense.id, - debit: userData.partner ? 0 : profit, - }, ].concat(supervisorData), }); }); @@ -645,24 +647,27 @@ export class TransactionService { return transactionData; } - async callbackOrderFailed(supplier_trx_id: string) { + async callbackOrderFailed(supplier_trx_id: string, callback: any) { const dataTransaction = await this.transactionRepository.findOne({ where: { supplier_trx_id: supplier_trx_id, }, }); dataTransaction.status = statusTransaction.FAILED; + dataTransaction.callback_json = callback; await this.transactionRepository.save(dataTransaction); } - async callbackOrderSuccess(supplier_trx_id: string) { + async callbackOrderSuccess(supplier_trx_id: string, callback: any) { const dataTransaction = await this.transactionRepository.findOne({ where: { supplier_trx_id: supplier_trx_id, }, + relations: ['product_price'], }); dataTransaction.status = statusTransaction.FAILED; + dataTransaction.callback_json = callback; const userData = await this.userService.findExist(dataTransaction.user); @@ -678,22 +683,6 @@ export class TransactionService { let profit = product_price.mark_up_price; - if (!userData.partner) { - //GET SALES - supervisorData = await this.calculateCommission( - supervisorData, - profit, - userData, - ); - profit = supervisorData - .map((item) => { - return item.credit; - }) - .reduce((prev, curr) => { - return prev + curr; - }, 0); - } - //GET COA const coaAccount = await this.coaService.findByUser( userData.id, @@ -716,6 +705,29 @@ export class TransactionService { `${coaType[coaType.EXPENSE]}-SYSTEM`, ); + if (userData.partner) { + //GET SALES + supervisorData = await this.calculateCommission( + supervisorData, + profit, + userData, + ); + profit = supervisorData + .map((item) => { + return item.credit; + }) + .reduce((prev, curr) => { + return prev + curr; + }, 0); + + supervisorData = supervisorData.concat([ + { + coa_id: coaExpense.id, + debit: profit, + }, + ]); + } + try { await this.connection.transaction(async (manager) => { await manager.save(dataTransaction); @@ -742,10 +754,6 @@ export class TransactionService { coa_id: coaSales.id, credit: product_price.mark_up_price + product_price.price, }, - { - coa_id: coaExpense.id, - debit: userData.partner ? 0 : profit, - }, ].concat(supervisorData), }); }); @@ -768,7 +776,7 @@ export class TransactionService { }) .leftJoin('transaction.product_price', 'product_price') .leftJoin('product_price.product', 'product') - .addSelect('product_price.mark_up_price', 'mark_up_price') + .addSelect('transaction.amount', 'mark_up_price') .addSelect('product.name', 'name') .addSelect('product.id', 'product_id'); diff --git a/src/users/partner/partner.service.ts b/src/users/partner/partner.service.ts index cf2422f..c2ae800 100644 --- a/src/users/partner/partner.service.ts +++ b/src/users/partner/partner.service.ts @@ -62,7 +62,7 @@ export class PartnerService { const dataUser = new CreateUserDto(); - dataUser.username = `admin_${partnerData.name}`; + dataUser.username = `admin_${partnerData.code}`; dataUser.name = partnerData.name; dataUser.phone_number = partnerData.phone_number; dataUser.roleId = '21dceea2-416e-4b55-b74c-12605e1f8d1b'; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index a127b27..ba68801 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -230,7 +230,20 @@ export class UsersController { @Body() updateUserDto: UpdateUserDto, ) { return { - data: await this.usersService.updatePassword(id, updateUserDto, req.user), + data: await this.usersService.updatePassword(id, updateUserDto), + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Put('change-password-partner/:id') + async updatePasswordPartner( + @Param('id', ParseUUIDPipe) id: string, + @Request() req, + @Body() updateUserDto: UpdateUserDto, + ) { + return { + data: await this.usersService.updatePassword(id, updateUserDto), statusCode: HttpStatus.OK, message: 'success', }; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 197d5e9..939af19 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -220,7 +220,12 @@ export class UsersService { async findExist(id: string) { try { - return await this.usersRepository.findOneOrFail(id); + return await this.usersRepository.findOneOrFail({ + where: { + id: id, + }, + relations: ['superior'], + }); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( @@ -358,11 +363,7 @@ export class UsersService { return userData; } - async updatePassword( - id: string, - updateUserDto: UpdateUserDto, - currentUser: any, - ) { + async updatePassword(id: string, updateUserDto: UpdateUserDto) { try { const dataUser = await this.usersRepository.findOneOrFail(id); dataUser.password = await hashPassword( @@ -386,6 +387,34 @@ export class UsersService { } } + async updatePasswordPartner(id: string, updateUserDto: UpdateUserDto) { + try { + const dataUser = await this.usersRepository.findOneOrFail({ + where: { + partner: id, + }, + }); + dataUser.password = await hashPassword( + updateUserDto.password, + dataUser.salt, + ); + const result = await this.usersRepository.save(dataUser); + return dataUser; + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'User not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } + setStatus = async (id: string, type: string) => { const userData = new User();