[add]: ref_id and ref_model for transactions. And db session and rollback for transactions

This commit is contained in:
VivianDee
2025-04-10 23:06:51 +01:00
parent e5320c075e
commit a196d4d3c4
14 changed files with 123 additions and 88 deletions
+14 -16
View File
@@ -7,7 +7,8 @@ from app.utils.logger import logger
from app.api.schemas.repayment import RepaymentSchema
from app.api.services.base_service import BaseService
from app.api.enums import TransactionType
from threading import Thread
from threading import Thread
from app.extensions import db
class RepaymentService(BaseService):
TRANSACTION_TYPE = TransactionType.REPAYMENT
@@ -26,20 +27,17 @@ class RepaymentService(BaseService):
try:
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
customer_id = validated_data.get('customerId')
customer = RepaymentService.get_or_create_customer(validated_data)
account = customer.accounts[0]
validated_data['accountId'] = account.id
request_id = validated_data.get('requestId')
loan_id = validated_data.get('debtId')
product_id = validated_data.get('productId')
if (RepaymentService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
# Save the repayment details
with db.session.begin():
# Save the repayment details
repayment = Repayment.create_repayment(
customer_id = customer_id,
loan_id = loan_id,
product_id = validated_data.get('productId')
product_id = product_id
)
@@ -49,6 +47,9 @@ class RepaymentService(BaseService):
"message": "Failed to save repayment details."
}), 400
validated_data['refId'] = repayment.id
validated_data['refModel'] = "repayment"
#Update Loan status
Loan.update_status(loan_id = loan_id, status = LoanStatus.REPAID)
@@ -59,16 +60,13 @@ class RepaymentService(BaseService):
return jsonify({
"message": "Failed to log transaction."
}), 400
else:
return jsonify({
"message": "Invalid Customer or Account"
}), 400
# Simulated processing logic
response_data = {
"customerId": "CN621868",
"productId": "101",
"debtId": "273194670",
"customerId": customer_id,
"productId": product_id,
"debtId": loan_id,
"resultCode": "00",
"resultDescription": "Successful"
}