Merge branch 'master' of https://gitlab.chiefsoft.net/DigiFi/digifi-BankToProductCore
This commit is contained in:
@@ -8,8 +8,8 @@ class ProvideLoanSchema(Schema):
|
|||||||
customerId = fields.Str(required=True)
|
customerId = fields.Str(required=True)
|
||||||
accountId = fields.Str(required=True)
|
accountId = fields.Str(required=True)
|
||||||
msisdn = fields.Str(required=False)
|
msisdn = fields.Str(required=False)
|
||||||
# productId = fields.Str(required=True)
|
# productId = fields.Str(required=True)
|
||||||
# lienAmount = fields.Float(required=True)
|
# lienAmount = fields.Float(required=True)
|
||||||
requestedAmount = fields.Float(required=True)
|
requestedAmount = fields.Float(required=True)
|
||||||
collectionType = fields.Int(required=True)
|
collectionType = fields.Int(required=True)
|
||||||
offerId = fields.Int(required=True)
|
offerId = fields.Int(required=True)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from app.api.enums import TransactionType
|
|||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
from app.api.schemas.provide_loan import ProvideLoanSchema
|
from app.api.schemas.provide_loan import ProvideLoanSchema
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
from app.models.loan import Loan
|
||||||
|
|
||||||
|
|
||||||
class ProvideLoanService(BaseService):
|
class ProvideLoanService(BaseService):
|
||||||
@@ -31,6 +32,7 @@ class ProvideLoanService(BaseService):
|
|||||||
transaction_id = validated_data.get('transactionId')
|
transaction_id = validated_data.get('transactionId')
|
||||||
|
|
||||||
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)):
|
||||||
|
|
||||||
transaction = ProvideLoanService.log_transaction(validated_data = validated_data)
|
transaction = ProvideLoanService.log_transaction(validated_data = validated_data)
|
||||||
|
|
||||||
if not transaction:
|
if not transaction:
|
||||||
@@ -38,6 +40,25 @@ class ProvideLoanService(BaseService):
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Failed to log transaction."
|
"message": "Failed to log transaction."
|
||||||
}), 400
|
}), 400
|
||||||
|
|
||||||
|
# Save the loan details
|
||||||
|
loan_id = f"loan_{transaction_id}"
|
||||||
|
|
||||||
|
loan = Loan.create_loan(
|
||||||
|
customer_id=customer_id,
|
||||||
|
account_id=account_id,
|
||||||
|
offer_id=validated_data.get('offerId'),
|
||||||
|
principal_amount=validated_data.get('requestedAmount'),
|
||||||
|
status="active"
|
||||||
|
)
|
||||||
|
|
||||||
|
if not loan:
|
||||||
|
logger.error(f"Failed to save loan details")
|
||||||
|
return jsonify({
|
||||||
|
"message": "Failed to save loan details."
|
||||||
|
}), 400
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"message": "Invalid Customer or Account"
|
"message": "Invalid Customer or Account"
|
||||||
@@ -86,4 +107,3 @@ class ProvideLoanService(BaseService):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ class RepaymentService(BaseService):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
||||||
account_id = validated_data.get('accountId')
|
|
||||||
customer_id = validated_data.get('customerId')
|
customer_id = validated_data.get('customerId')
|
||||||
customer = RepaymentService.get_or_create_customer(validated_data)
|
customer = RepaymentService.get_or_create_customer(validated_data)
|
||||||
account = customer.accounts[0]
|
account = customer.accounts[0]
|
||||||
@@ -30,7 +29,7 @@ class RepaymentService(BaseService):
|
|||||||
request_id = validated_data.get('requestId')
|
request_id = validated_data.get('requestId')
|
||||||
|
|
||||||
|
|
||||||
if (RepaymentService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
if (RepaymentService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
|
||||||
transaction = RepaymentService.log_transaction(validated_data = validated_data)
|
transaction = RepaymentService.log_transaction(validated_data = validated_data)
|
||||||
|
|
||||||
if not transaction:
|
if not transaction:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from marshmallow import ValidationError
|
|||||||
from app.api.services.base_service import BaseService
|
from app.api.services.base_service import BaseService
|
||||||
from app.api.enums import TransactionType
|
from app.api.enums import TransactionType
|
||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
from app.api.schemas.select_offer import SelectOfferSchema
|
from app.api.schemas.select_offer import SelectOfferSchema
|
||||||
|
|
||||||
class SelectOfferService(BaseService):
|
class SelectOfferService(BaseService):
|
||||||
TRANSACTION_TYPE = TransactionType.SELECT_OFFER
|
TRANSACTION_TYPE = TransactionType.SELECT_OFFER
|
||||||
|
|||||||
@@ -20,11 +20,11 @@ class Customer(db.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_eligible(cls, customer_id):
|
def is_valid_customer(cls, customer_id):
|
||||||
customer = cls.query.filter_by(id=customer_id).first()
|
customer = cls.query.filter_by(id=customer_id).first()
|
||||||
if not customer:
|
if not customer:
|
||||||
return False, "Customer not found"
|
return False
|
||||||
return True, "Customer is eligible"
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_customer(cls, id, msisdn, country_code, account_id, account_type='savings'):
|
def create_customer(cls, id, msisdn, country_code, account_id, account_type='savings'):
|
||||||
|
|||||||
+38
-4
@@ -1,20 +1,54 @@
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
|
from app.models.customer import Customer
|
||||||
|
from app.models.account import Account
|
||||||
|
|
||||||
|
|
||||||
class Loan(db.Model):
|
class Loan(db.Model):
|
||||||
__tablename__ = 'loans'
|
__tablename__ = 'loans'
|
||||||
|
|
||||||
id = db.Column(db.String(50), primary_key=True)
|
id = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
primary_key=True,
|
||||||
|
autoincrement=True,
|
||||||
|
)
|
||||||
customer_id = db.Column(db.String(50), nullable=False)
|
customer_id = db.Column(db.String(50), nullable=False)
|
||||||
account_id = db.Column(db.String(50), nullable=False)
|
account_id = db.Column(db.String(50), nullable=False)
|
||||||
product_id = db.Column(db.String(20), nullable=False)
|
offer_id = db.Column(db.String(20), nullable=False)
|
||||||
principal_amount = db.Column(db.Float, nullable=False)
|
principal_amount = db.Column(db.Float, nullable=False)
|
||||||
status = db.Column(db.String(20), default='pending')
|
status = db.Column(db.String(20), default='pending')
|
||||||
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
|
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
|
||||||
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
|
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_loan(cls, customer_id, account_id, offer_id, principal_amount, status='pending'):
|
||||||
|
|
||||||
|
# Check if customer exists
|
||||||
|
is_valid = Customer.is_valid_customer(customer_id)
|
||||||
|
if not is_valid:
|
||||||
|
raise ValueError("Customer does not exist")
|
||||||
|
|
||||||
|
# # Check for active loans
|
||||||
|
# has_active_loans = cls.has_active_loans(customer_id)
|
||||||
|
# if has_active_loans:
|
||||||
|
# raise ValueError("Customer has active loans")
|
||||||
|
|
||||||
|
|
||||||
|
# Create and save the loan
|
||||||
|
loan = cls(
|
||||||
|
customer_id=customer_id,
|
||||||
|
account_id=account_id,
|
||||||
|
offer_id=offer_id,
|
||||||
|
principal_amount=principal_amount,
|
||||||
|
status=status
|
||||||
|
)
|
||||||
|
|
||||||
|
db.session.add(loan)
|
||||||
|
db.session.commit()
|
||||||
|
return loan
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def has_active_loans(cls, customer_id):
|
def has_active_loans(cls, customer_id):
|
||||||
active_loans = cls.query.filter_by(
|
active_loans = cls.query.filter_by(
|
||||||
@@ -23,8 +57,8 @@ class Loan(db.Model):
|
|||||||
).count()
|
).count()
|
||||||
|
|
||||||
if active_loans > 0:
|
if active_loans > 0:
|
||||||
return False, "Customer has active loans"
|
return False
|
||||||
return True, "No active loans"
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_customer_loan(cls, loan_id, customer_id):
|
def get_customer_loan(cls, loan_id, customer_id):
|
||||||
|
|||||||
@@ -21,15 +21,6 @@
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"example": "3451342"
|
"example": "3451342"
|
||||||
},
|
},
|
||||||
"productId": {
|
|
||||||
"type": "string",
|
|
||||||
"example": "101"
|
|
||||||
},
|
|
||||||
"lienAmount": {
|
|
||||||
"type": "number",
|
|
||||||
"format": "decimal",
|
|
||||||
"example": 400
|
|
||||||
},
|
|
||||||
"requestedAmount": {
|
"requestedAmount": {
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"format": "decimal",
|
"format": "decimal",
|
||||||
|
|||||||
Reference in New Issue
Block a user