116 lines
5.0 KiB
Kotlin
116 lines
5.0 KiB
Kotlin
package net.ipksindia
|
|
|
|
import net.ipksindia.dao.TellerDao
|
|
import model.TransactionRequest
|
|
import net.ipksindia.config.AppConfig
|
|
import net.ipksindia.dao.TransactionDao
|
|
import net.ipksindia.model.NeftTransaction
|
|
import net.ipksindia.model.OutwardNeftResponse
|
|
import net.ipksindia.model.TransferTransaction
|
|
import org.ipks.model.Transaction
|
|
import org.slf4j.Logger
|
|
import org.slf4j.LoggerFactory
|
|
import response.TransactionFailureResponse
|
|
import response.TransactionResponse
|
|
import response.TransactionSuccessResponse
|
|
|
|
class NeftRequestProcessor {
|
|
|
|
companion object {
|
|
private val logger: Logger = LoggerFactory.getLogger(NeftRequestProcessor::class.java)
|
|
private val migratedDCCBCodes = AppConfig.bankCodes
|
|
|
|
/**
|
|
* Process the transaction request based on the transaction number.
|
|
* @param transactionNumber The transaction number to be processed.
|
|
* @return A pair of transferQueueNumber and neftQueueNumber if successful, null otherwise.
|
|
*/
|
|
fun process(transactionNumber: String): OutwardNeftResponse {
|
|
return try {
|
|
val transactionRequest = fetchTransactionRequest(transactionNumber)
|
|
val dccbCode = transactionRequest.dccbCode.padStart(4, '0')
|
|
val branchCode = transactionRequest.branchCode.padStart(5, '0')
|
|
|
|
if (!isDCCBCodeMigrated(dccbCode)) {
|
|
logDCCBCodeNotMigrated(transactionNumber)
|
|
return OutwardNeftResponse(0, "dccb code not migrated", null, null)
|
|
}
|
|
|
|
val teller = TellerDao().getTeller(dccbCode, branchCode)
|
|
val transactionPair = TransactionFactory(transactionRequest, teller)
|
|
.createTransactionPair()
|
|
|
|
executeAndProcessTransaction(transactionNumber, transactionRequest, transactionPair)
|
|
} catch (e: Exception) {
|
|
logger.error("TXN: #{} FAILED REASON: {}", transactionNumber, e.toString())
|
|
OutwardNeftResponse(0, e.message ?: "", null, null)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fetch the transaction request using the transaction number.
|
|
*/
|
|
private fun fetchTransactionRequest(transactionNumber: String): TransactionRequest {
|
|
val transactionRequest = TransactionDao().getTransactionRequest(transactionNumber)
|
|
logger.info("TXN: #{} FOUND", transactionNumber)
|
|
return transactionRequest
|
|
}
|
|
|
|
/**
|
|
* Check if the DCCB code is not migrated.
|
|
*/
|
|
private fun isDCCBCodeMigrated(dccbCode: String)= dccbCode in migratedDCCBCodes
|
|
|
|
/**
|
|
* Log an error if the DCCB code is not migrated.
|
|
*/
|
|
private fun logDCCBCodeNotMigrated(transactionNumber: String) {
|
|
logger.error("TXN: #{} FAILED REASON: DCCB Code not migrated", transactionNumber)
|
|
}
|
|
|
|
|
|
/**
|
|
* Execute the transaction pair and process the results.
|
|
*/
|
|
private fun executeAndProcessTransaction(
|
|
transactionNumber: String,
|
|
transactionRequest: TransactionRequest,
|
|
transactionPair: Pair<TransferTransaction, NeftTransaction>
|
|
): OutwardNeftResponse {
|
|
val (transferResponse, neftResponse) = TransactionExecutor().executePair(transactionPair)
|
|
|
|
return if (isSuccess(transferResponse, neftResponse)) {
|
|
val transferQueueNumber = (transferResponse as TransactionSuccessResponse).queueNumber
|
|
val neftQueueNumber = (neftResponse as TransactionSuccessResponse).queueNumber
|
|
TransactionDao().updateSuccessTransaction(transactionRequest, transferQueueNumber, neftQueueNumber)
|
|
|
|
logger.info("TXN: #{} UPDATED RESULTS SUCCESSFULLY", transactionNumber)
|
|
OutwardNeftResponse(1, "transaction successful", transferQueueNumber, neftQueueNumber)
|
|
} else {
|
|
logTransactionFailure(transactionNumber, transferResponse, neftResponse)
|
|
OutwardNeftResponse(0, "transaction failed", null, null)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if both transfer and NEFT responses are successful.
|
|
*/
|
|
private fun isSuccess(transferResponse: TransactionResponse, neftResponse: TransactionResponse) = transferResponse is TransactionSuccessResponse && neftResponse is TransactionSuccessResponse
|
|
|
|
/**
|
|
* Log errors if the transaction fails.
|
|
*/
|
|
private fun logTransactionFailure(
|
|
transactionNumber: String,
|
|
transferResponse: Any,
|
|
neftResponse: Any
|
|
) {
|
|
val transferErrorMsg = (transferResponse as? TransactionFailureResponse)?.errorMsg ?: "Unknown Error"
|
|
val neftErrorMsg = (neftResponse as? TransactionFailureResponse)?.errorMsg ?: "Unknown Error"
|
|
|
|
logger.error("TXN: #{} TRANSFER TXN FAILED DUE TO: {}", transactionNumber, transferErrorMsg)
|
|
logger.error("TXN: #{} NEFT TXN FAILED DUE TO: {}", transactionNumber, neftErrorMsg)
|
|
}
|
|
}
|
|
}
|