[add]: db sessions
This commit is contained in:
@@ -23,13 +23,13 @@ class CustomerConsentService(BaseService):
|
|||||||
dict: A standardized response.
|
dict: A standardized response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with db.session.begin():
|
||||||
validated_data = CustomerConsentService.validate_data(data, CustomerConsentSchema())
|
validated_data = CustomerConsentService.validate_data(data, CustomerConsentSchema())
|
||||||
account_id = validated_data.get('accountId')
|
account_id = validated_data.get('accountId')
|
||||||
customer_id = validated_data.get('customerId')
|
customer_id = validated_data.get('customerId')
|
||||||
|
|
||||||
if(CustomerConsentService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
if(CustomerConsentService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||||
with db.session.begin():
|
|
||||||
transaction = CustomerConsentService.log_transaction(validated_data = validated_data)
|
transaction = CustomerConsentService.log_transaction(validated_data = validated_data)
|
||||||
|
|
||||||
if not transaction:
|
if not transaction:
|
||||||
@@ -49,11 +49,13 @@ class CustomerConsentService(BaseService):
|
|||||||
"resultDescription": "Request is received"
|
"resultDescription": "Request is received"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
return response_data
|
return response_data
|
||||||
|
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
|
|
||||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Validation exception"
|
"message": "Validation exception"
|
||||||
@@ -61,6 +63,7 @@ class CustomerConsentService(BaseService):
|
|||||||
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": str(err)
|
"message": str(err)
|
||||||
@@ -68,6 +71,7 @@ class CustomerConsentService(BaseService):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Internal Server Error"
|
"message": "Internal Server Error"
|
||||||
}) , 500
|
}) , 500
|
||||||
@@ -22,6 +22,7 @@ class EligibilityCheckService(BaseService):
|
|||||||
dict: A standardized response.
|
dict: A standardized response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with db.session.begin():
|
||||||
|
|
||||||
validated_data = EligibilityCheckService.validate_data(data, EligibilityCheckSchema())
|
validated_data = EligibilityCheckService.validate_data(data, EligibilityCheckSchema())
|
||||||
account_id = validated_data.get('accountId')
|
account_id = validated_data.get('accountId')
|
||||||
@@ -32,7 +33,7 @@ class EligibilityCheckService(BaseService):
|
|||||||
customer = EligibilityCheckService.get_or_create_customer(validated_data = validated_data)
|
customer = EligibilityCheckService.get_or_create_customer(validated_data = validated_data)
|
||||||
|
|
||||||
if (EligibilityCheckService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
if (EligibilityCheckService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||||
with db.session.begin():
|
|
||||||
transaction = EligibilityCheckService.log_transaction(validated_data = validated_data)
|
transaction = EligibilityCheckService.log_transaction(validated_data = validated_data)
|
||||||
|
|
||||||
if not transaction:
|
if not transaction:
|
||||||
@@ -40,6 +41,7 @@ class EligibilityCheckService(BaseService):
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Failed to log transaction."
|
"message": "Failed to log transaction."
|
||||||
}), 400
|
}), 400
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Invalid Customer or Account"
|
"message": "Invalid Customer or Account"
|
||||||
@@ -88,6 +90,7 @@ class EligibilityCheckService(BaseService):
|
|||||||
}
|
}
|
||||||
|
|
||||||
return response_data
|
return response_data
|
||||||
|
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
|
|
||||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||||
|
|||||||
@@ -22,13 +22,10 @@ class LoanStatusService(BaseService):
|
|||||||
dict: A standardized response.
|
dict: A standardized response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with db.session.begin():
|
||||||
validated_data = LoanStatusService.validate_data(data, LoanStatusSchema())
|
validated_data = LoanStatusService.validate_data(data, LoanStatusSchema())
|
||||||
customer_id = validated_data.get('customerId')
|
customer_id = validated_data.get('customerId')
|
||||||
customer = LoanStatusService.get_or_create_customer(validated_data)
|
|
||||||
account = customer.accounts[0]
|
|
||||||
|
|
||||||
if (LoanStatusService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
|
|
||||||
with db.session.begin():
|
|
||||||
transaction = LoanStatusService.log_transaction(validated_data = validated_data)
|
transaction = LoanStatusService.log_transaction(validated_data = validated_data)
|
||||||
|
|
||||||
if not transaction:
|
if not transaction:
|
||||||
@@ -36,10 +33,6 @@ class LoanStatusService(BaseService):
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Failed to log transaction."
|
"message": "Failed to log transaction."
|
||||||
}), 400
|
}), 400
|
||||||
else:
|
|
||||||
return jsonify({
|
|
||||||
"message": "Invalid Customer or Account"
|
|
||||||
}), 400
|
|
||||||
|
|
||||||
|
|
||||||
loans = [
|
loans = [
|
||||||
@@ -65,12 +58,13 @@ class LoanStatusService(BaseService):
|
|||||||
"resultDescription": "Successful"
|
"resultDescription": "Successful"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
return response_data
|
return response_data
|
||||||
|
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
|
|
||||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Validation exception"
|
"message": "Validation exception"
|
||||||
@@ -78,6 +72,7 @@ class LoanStatusService(BaseService):
|
|||||||
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": str(err)
|
"message": str(err)
|
||||||
@@ -85,6 +80,7 @@ class LoanStatusService(BaseService):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Internal Server Error"
|
"message": "Internal Server Error"
|
||||||
}) , 500
|
}) , 500
|
||||||
@@ -26,6 +26,7 @@ class ProvideLoanService(BaseService):
|
|||||||
dict: A standardized response.
|
dict: A standardized response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with db.session.begin():
|
||||||
validated_data = ProvideLoanService.validate_data(data, ProvideLoanSchema())
|
validated_data = ProvideLoanService.validate_data(data, ProvideLoanSchema())
|
||||||
account_id = validated_data.get('accountId')
|
account_id = validated_data.get('accountId')
|
||||||
customer_id = validated_data.get('customerId')
|
customer_id = validated_data.get('customerId')
|
||||||
@@ -34,7 +35,7 @@ class ProvideLoanService(BaseService):
|
|||||||
|
|
||||||
if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||||
|
|
||||||
with db.session.begin():
|
|
||||||
# Save the loan details
|
# Save the loan details
|
||||||
loan = Loan.create_loan(
|
loan = Loan.create_loan(
|
||||||
customer_id=customer_id,
|
customer_id=customer_id,
|
||||||
@@ -50,6 +51,7 @@ class ProvideLoanService(BaseService):
|
|||||||
"message": "Failed to save loan details."
|
"message": "Failed to save loan details."
|
||||||
}), 400
|
}), 400
|
||||||
|
|
||||||
|
db.session.flush()
|
||||||
validated_data['refId'] = loan.id
|
validated_data['refId'] = loan.id
|
||||||
validated_data['refModel'] = "loan"
|
validated_data['refModel'] = "loan"
|
||||||
|
|
||||||
@@ -86,11 +88,13 @@ class ProvideLoanService(BaseService):
|
|||||||
thread = Thread(target=ProvideLoanService.async_send_to_kafka, args=(response_data, request_id, "PROCESS_PAYMENT"))
|
thread = Thread(target=ProvideLoanService.async_send_to_kafka, args=(response_data, request_id, "PROCESS_PAYMENT"))
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
return response_data
|
return response_data
|
||||||
|
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
|
|
||||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Validation exception"
|
"message": "Validation exception"
|
||||||
@@ -98,6 +102,7 @@ class ProvideLoanService(BaseService):
|
|||||||
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": str(err)
|
"message": str(err)
|
||||||
@@ -105,6 +110,7 @@ class ProvideLoanService(BaseService):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Internal Server Error"
|
"message": "Internal Server Error"
|
||||||
}) , 500
|
}) , 500
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class RepaymentService(BaseService):
|
|||||||
dict: A standardized response.
|
dict: A standardized response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with db.session.begin():
|
||||||
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
||||||
customer_id = validated_data.get('customerId')
|
customer_id = validated_data.get('customerId')
|
||||||
request_id = validated_data.get('requestId')
|
request_id = validated_data.get('requestId')
|
||||||
@@ -32,7 +33,7 @@ class RepaymentService(BaseService):
|
|||||||
product_id = validated_data.get('productId')
|
product_id = validated_data.get('productId')
|
||||||
|
|
||||||
|
|
||||||
with db.session.begin():
|
|
||||||
# Save the repayment details
|
# Save the repayment details
|
||||||
repayment = Repayment.create_repayment(
|
repayment = Repayment.create_repayment(
|
||||||
customer_id = customer_id,
|
customer_id = customer_id,
|
||||||
@@ -47,6 +48,8 @@ class RepaymentService(BaseService):
|
|||||||
"message": "Failed to save repayment details."
|
"message": "Failed to save repayment details."
|
||||||
}), 400
|
}), 400
|
||||||
|
|
||||||
|
db.session.flush()
|
||||||
|
|
||||||
validated_data['refId'] = repayment.id
|
validated_data['refId'] = repayment.id
|
||||||
validated_data['refModel'] = "repayment"
|
validated_data['refModel'] = "repayment"
|
||||||
|
|
||||||
@@ -80,11 +83,13 @@ class RepaymentService(BaseService):
|
|||||||
thread = Thread(target=RepaymentService.async_send_to_kafka, args=(response_data, request_id, "LOAN_REPAYMENT"))
|
thread = Thread(target=RepaymentService.async_send_to_kafka, args=(response_data, request_id, "LOAN_REPAYMENT"))
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
return response_data
|
return response_data
|
||||||
|
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
|
|
||||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Validation exception"
|
"message": "Validation exception"
|
||||||
@@ -92,6 +97,7 @@ class RepaymentService(BaseService):
|
|||||||
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": str(err)
|
"message": str(err)
|
||||||
@@ -99,6 +105,7 @@ class RepaymentService(BaseService):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Internal Server Error"
|
"message": "Internal Server Error"
|
||||||
}) , 500
|
}) , 500
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from app.utils.logger import logger
|
|||||||
from app.api.schemas.select_offer import SelectOfferSchema
|
from app.api.schemas.select_offer import SelectOfferSchema
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
|
|
||||||
|
|
||||||
class SelectOfferService(BaseService):
|
class SelectOfferService(BaseService):
|
||||||
TRANSACTION_TYPE = TransactionType.SELECT_OFFER
|
TRANSACTION_TYPE = TransactionType.SELECT_OFFER
|
||||||
|
|
||||||
@@ -21,23 +22,25 @@ class SelectOfferService(BaseService):
|
|||||||
dict: A standardized response.
|
dict: A standardized response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
validated_data = SelectOfferService.validate_data(data, SelectOfferSchema())
|
|
||||||
account_id = validated_data.get('accountId')
|
|
||||||
customer_id = validated_data.get('customerId')
|
|
||||||
|
|
||||||
if (SelectOfferService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
|
||||||
with db.session.begin():
|
with db.session.begin():
|
||||||
transaction = SelectOfferService.log_transaction(validated_data = validated_data)
|
validated_data = SelectOfferService.validate_data(
|
||||||
|
data, SelectOfferSchema()
|
||||||
|
)
|
||||||
|
account_id = validated_data.get("accountId")
|
||||||
|
customer_id = validated_data.get("customerId")
|
||||||
|
|
||||||
|
if SelectOfferService.validate_account_ownership(
|
||||||
|
account_id=account_id, customer_id=customer_id
|
||||||
|
):
|
||||||
|
transaction = SelectOfferService.log_transaction(
|
||||||
|
validated_data=validated_data
|
||||||
|
)
|
||||||
|
|
||||||
if not transaction:
|
if not transaction:
|
||||||
logger.error(f"Failed to log transaction")
|
logger.error(f"Failed to log transaction")
|
||||||
return jsonify({
|
return jsonify({"message": "Failed to log transaction."}), 400
|
||||||
"message": "Failed to log transaction."
|
|
||||||
}), 400
|
|
||||||
else:
|
else:
|
||||||
return jsonify({
|
return jsonify({"message": "Invalid Customer or Account"}), 400
|
||||||
"message": "Invalid Customer or Account"
|
|
||||||
}), 400
|
|
||||||
|
|
||||||
offers = [
|
offers = [
|
||||||
{
|
{
|
||||||
@@ -54,7 +57,7 @@ class SelectOfferService(BaseService):
|
|||||||
"VATAmount": 100.0,
|
"VATAmount": 100.0,
|
||||||
"recommendedRepaymentDates": ["2022-11-30"],
|
"recommendedRepaymentDates": ["2022-11-30"],
|
||||||
"installmentAmount": 11000.0,
|
"installmentAmount": 11000.0,
|
||||||
"totalRepaymentAmount": 11000.0
|
"totalRepaymentAmount": 11000.0,
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -67,29 +70,24 @@ class SelectOfferService(BaseService):
|
|||||||
"accountId": account_id,
|
"accountId": account_id,
|
||||||
"loan": offers,
|
"loan": offers,
|
||||||
"resultCode": "00",
|
"resultCode": "00",
|
||||||
"resultDescription": "Successful"
|
"resultDescription": "Successful",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
return response_data
|
return response_data
|
||||||
|
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
|
|
||||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({
|
return jsonify({"message": "Validation exception"}), 422
|
||||||
"message": "Validation exception"
|
|
||||||
}) , 422
|
|
||||||
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({
|
return jsonify({"message": str(err)}), 400
|
||||||
"message": str(err)
|
|
||||||
}) , 400
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||||
return jsonify({
|
db.session.rollback()
|
||||||
"message": "Internal Server Error"
|
return jsonify({"message": "Internal Server Error"}), 500
|
||||||
}) , 500
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Repayment(db.Model):
|
|||||||
|
|
||||||
repayment = cls(
|
repayment = cls(
|
||||||
customer_id=customer_id,
|
customer_id=customer_id,
|
||||||
loan_id=loan.id,
|
loan_id=loan_id,
|
||||||
product_id=product_id,
|
product_id=product_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user