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')
async rollbackJurnal(@Request() req, @Param('trxId') trxId: string) {
const data = await this.transactionService.rollbackJurnal(trxId);
@Post('rollback-jurnal')
async rollbackJurnal(@Body() request, @Request() req) {
const data = await this.transactionService.rollbackJurnal(request.trxId);
return {
data,
statusCode: HttpStatus.OK,

View File

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