fix: rollback

This commit is contained in:
ilham 2022-06-29 18:07:07 +07:00
parent 104067f263
commit fe7702836e
2 changed files with 33 additions and 43 deletions

View File

@ -132,9 +132,9 @@ export class TransactionController {
}; };
} }
@Get('rollback-jurnal/:trxId') @Post('rollback-jurnal')
async rollbackJurnal(@Request() req, @Param('trxId') trxId: string) { async rollbackJurnal(@Body() request, @Request() req) {
const data = await this.transactionService.rollbackJurnal(trxId); const data = await this.transactionService.rollbackJurnal(request.trxId);
return { return {
data, data,
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,

View File

@ -3,7 +3,7 @@ import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
import { OrderTransactionDto } from './dto/order-transaction.dto'; import { OrderTransactionDto } from './dto/order-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 { Between, Connection, EntityNotFoundError, Repository } from 'typeorm'; import { Between, Connection, EntityNotFoundError, In, Repository } from 'typeorm';
import { COA } from './entities/coa.entity'; import { COA } from './entities/coa.entity';
import { TransactionJournal } from './entities/transaction-journal.entity'; import { TransactionJournal } from './entities/transaction-journal.entity';
import { CoaService } from './coa.service'; import { CoaService } from './coa.service';
@ -1317,35 +1317,25 @@ export class TransactionService {
return res; return res;
} }
async rollbackJurnal(trxId: string) { async rollbackJurnal(trxId: string[]) {
const dataTransaction = await this.transactionRepository.findOne({ // const dataTransaction = await this.transactionRepository.findOne({
where: { // where: {
id: trxId, // id: trxId,
}, // },
relations: ['product_price'], // relations: ['product_price'],
}); // });
let dataTransactionJurnal; if (trxId.length % 2 != 0) {
if (dataTransaction.type == typeTransaction.ORDER) { throw Error("Not Balance")
dataTransactionJurnal = await this.transactionJournalRepository.find({
where: {
transaction_head: trxId,
},
relations: ['coa'],
skip: 4,
order: {
createdAt: 'ASC',
},
});
} else {
dataTransactionJurnal = await this.transactionJournalRepository.find({
where: {
transaction_head: trxId,
},
relations: ['coa'],
});
} }
const dataTransactionJurnal = await this.transactionJournalRepository.find({
where: {
id: In(trxId),
},
relations: ['coa'],
});
let dataRollbackJurnal = []; let dataRollbackJurnal = [];
dataTransactionJurnal.map((it) => { dataTransactionJurnal.map((it) => {
@ -1362,20 +1352,20 @@ export class TransactionService {
dataRollbackJurnal.push(data); dataRollbackJurnal.push(data);
}); });
if (dataRollbackJurnal.length > 0) { const dataTransaction = new Transactions();
try {
await this.connection.transaction(async (manager) => { try {
await this.accountingTransaction({ await this.connection.transaction(async (manager) => {
createTransaction: false, await this.accountingTransaction({
transactionalEntityManager: manager, createTransaction: false,
transaction: dataTransaction, transactionalEntityManager: manager,
amount: dataTransaction.amount, transaction: dataTransaction,
journals: dataRollbackJurnal, amount: dataTransaction.amount,
}); journals: dataRollbackJurnal,
}); });
} catch (e) { });
throw e; } catch (e) {
} throw e;
} }
} }