Compare commits

...

13 Commits

Author SHA1 Message Date
VivianDee 4d4e4fcd3e Update base_service.py 2025-04-25 11:33:05 +01:00
VivianDee 1cce111d1f Update offer.py 2025-04-25 11:30:43 +01:00
VivianDee b9b7988877 Update base_service.py 2025-04-25 11:12:23 +01:00
VivianDee 841393c470 [fix]: Loan fees 2025-04-25 10:59:24 +01:00
VivianDee bbb903b27c [update]: RACCheck 2025-04-24 18:44:31 +01:00
VivianDee c895cc36e0 [add]: Loan table extention 2025-04-24 18:29:38 +01:00
VivianDee 7d691db7a5 [update]: Select Offer 2025-04-23 20:59:07 +01:00
Vivian Dee 4b92c33d5a [fix]: loan charges and instalment amount 2025-04-23 18:57:22 +01:00
Vivian Dee 8cfa957cc0 [update]: Select Offer 2025-04-23 18:35:47 +01:00
ameye 5768b537b1 Merge branch 'charges_model' of DigiFi/digifi-BankToProductCore into master 2025-04-17 16:12:09 +00:00
ameye bc894c7856 Merge branch 'charges_model' of DigiFi/digifi-BankToProductCore into master 2025-04-17 15:13:48 +00:00
ameye 829bd976b2 Merge branch 'loan_charges_on_loans' of DigiFi/digifi-BankToProductCore into master 2025-04-17 10:45:18 +00:00
ameye e04f54bf83 Merge branch 'loan_charges_on_loans' of DigiFi/digifi-BankToProductCore into master 2025-04-17 10:03:16 +00:00
10 changed files with 323 additions and 23 deletions
+2
View File
@@ -49,6 +49,8 @@ class SimbrellaIntegration:
logger.error(f"This is Response: {str(response)}", exc_info=True)
return response
except Exception as e:
logger.error(f"RACCheck API call failed: {str(e)}", exc_info=True)
raise Exception(f"RACCheck API call failed: {str(e)}")
+88
View File
@@ -60,3 +60,91 @@ class BaseService:
def async_send_to_kafka(cls, loan_data, request_id, topic):
KafkaIntegration.send_loan_request(loan_data = loan_data, request_id = request_id, topic = topic)
KafkaIntegration.flush()
@classmethod
def calculate_charges(cls, offer, amount):
"""
Calculates and returns the charges for the given offer and amount.
Args:
offer (Offer): The offer object that contains the charges.
amount (float): The requested loan amount.
Returns:
dict: A dictionary containing the calculated charges.
"""
if not offer or not offer.charges:
logger.error(f"No charges found for offer ID {offer.id}")
return {"error": "No charges found for the offer"}
loan_charges = offer.charges
tenor = offer.tenor // 30 # Convert to months
interest = cls.get_charge_detail(charges = loan_charges, code = "INTEREST", amount = amount)
management = cls.get_charge_detail(charges = loan_charges, code = "MGTFEE", amount = amount)
insurance = cls.get_charge_detail(charges = loan_charges, code = "INSURANCE", amount = amount)
vat = cls.get_charge_detail(charges = loan_charges, code = "VAT", amount = amount, management_fee = management["fee"])
# Separate fees into upfront and postpaid
upfront_fees = [
fee["fee"]
for fee in [interest, management, insurance, vat]
if fee["due_days"] == 0
]
postpaid_fees = [
fee["fee"]
for fee in [interest, management, insurance, vat]
if fee["due_days"] != 0
]
# Up-front payment: (only those fees due immediately i.e due_days == 0)
upfront_payment = sum(upfront_fees)
# Repayment amount: (principal + only those fees not due immediately i.e due_days != 0)
repayment_amount = amount + (sum(postpaid_fees) * tenor)
# Total amount: (upfront_payment + repayment_amount)
total_amount = upfront_payment + repayment_amount
# Calculate the installment amount
installment_amount = repayment_amount / tenor
return {
"interest": interest,
"management": management,
"insurance": insurance,
"vat": vat,
"upfront_payment": round(upfront_payment, 2),
"repayment_amount": round(repayment_amount, 2),
"installment_amount": round(installment_amount, 2),
"total_amount": round(total_amount, 2)
}
@classmethod
def get_charge_detail(cls, charges, code, amount, management_fee=None):
"""
Get details for a specific charge code from a list of loan charges.
Returns default values if not found.
"""
for charge in charges:
if charge.code == code:
fee = (
management_fee * charge.percent / 100
if code == "VAT" and management_fee is not None
else amount * charge.percent / 100
)
return {
"rate": charge.percent,
"fee": round(fee, 2),
"due_days": charge.due
}
return {"rate": 0, "fee": 0, "due_days": 0}
+15 -2
View File
@@ -36,6 +36,8 @@ class ProvideLoanService(BaseService):
collection_type = validated_data.get('collectionType')
transaction_id = validated_data.get('transactionId')
offer_id = validated_data.get('offerId')
amount = validated_data.get("requestedAmount")
product_id = validated_data.get("productId")
customer = Customer.is_valid_customer(customer_id)
@@ -48,8 +50,7 @@ class ProvideLoanService(BaseService):
return jsonify({
"message": "Invalid Offer."
}), 400
# Log Transaction
transaction = ProvideLoanService.log_transaction(validated_data=validated_data)
@@ -58,6 +59,15 @@ class ProvideLoanService(BaseService):
return jsonify({
"message": "Failed to log transaction."
}), 400
db.session.flush()
charges = ProvideLoanService.calculate_charges(offer, amount)
upfront_fee = charges["upfront_payment"]
repayment_amount = charges["repayment_amount"]
installment_amount = charges["installment_amount"]
# Save the loan details
@@ -69,6 +79,9 @@ class ProvideLoanService(BaseService):
collection_type = collection_type,
transaction_id = validated_data.get('transactionId'),
initial_loan_amount = validated_data.get('requestedAmount'),
upfront_fee = upfront_fee,
repayment_amount = repayment_amount,
installment_amount = installment_amount,
status= LoanStatus.ACTIVE
)
+53 -17
View File
@@ -5,7 +5,9 @@ from app.api.enums import TransactionType
from app.utils.logger import logger
from app.api.schemas.select_offer import SelectOfferSchema
from app.extensions import db
from app.models import Offer
from datetime import date
from dateutil.relativedelta import relativedelta
class SelectOfferService(BaseService):
TRANSACTION_TYPE = TransactionType.SELECT_OFFER
@@ -28,6 +30,10 @@ class SelectOfferService(BaseService):
)
account_id = validated_data.get("accountId")
customer_id = validated_data.get("customerId")
amount = validated_data.get("requestedAmount")
product_id = validated_data.get("productId")
transaction_id = validated_data.get("transactionId")
request_id = validated_data.get("requestId")
if SelectOfferService.validate_account_ownership(
account_id=account_id, customer_id=customer_id
@@ -42,30 +48,59 @@ class SelectOfferService(BaseService):
else:
return jsonify({"message": "Invalid Customer or Account"}), 400
# Get the offer by product ID
offer = Offer.get_offer_by_product_id(product_id)
db.session.flush()
charges = SelectOfferService.calculate_charges(offer, amount)
upfront_payment = charges["upfront_payment"]
total_amount = charges["total_amount"]
installment_amount = charges["installment_amount"]
interest = charges["interest"]
management = charges["management"]
insurance = charges["insurance"]
vat = charges["vat"]
# Calculate the repayment dates
tenor = offer.tenor
start_date = date.today()
# Convert tenor to months
months = tenor // 30
recommended_repayment_dates = [
(start_date + relativedelta(months=i + 1)).isoformat()
for i in range(months)
]
offers = [
{
"offerId": "SAL90",
"productId": "2030",
"amount": 10000.0,
"upfrontPayment": 1000.0,
"interestRate": 3.0,
"managementRate": 1.0,
"managementFee": 1.0,
"insuranceRate": 1.0,
"insuranceFee": 100.0,
"VATRate": 7.5,
"VATAmount": 100.0,
"recommendedRepaymentDates": ["2022-11-30"],
"installmentAmount": 11000.0,
"totalRepaymentAmount": 11000.0,
"offerId": offer.id,
"productId": product_id,
"amount": amount,
"upfrontPayment": upfront_payment,
"interestRate": interest["rate"],
"managementRate": management["rate"],
"managementFee": management["fee"],
"insuranceRate": insurance["rate"],
"insuranceFee": insurance["fee"],
"VATRate": vat["rate"],
"VATAmount": vat["fee"],
"recommendedRepaymentDates": recommended_repayment_dates,
"installmentAmount": installment_amount,
"totalRepaymentAmount": total_amount,
}
]
# Business logic - selecting an offer
response_data = {
"outstandingDebtAmount": 0,
"requestId": "202111170001371256908",
"transactionId": transaction.id,
"requestId": request_id,
"transactionId": transaction_id,
"customerId": customer_id,
"accountId": account_id,
"loan": offers,
@@ -91,3 +126,4 @@ class SelectOfferService(BaseService):
logger.error(f"An error occurred: {str(e)}", exc_info=True)
db.session.rollback()
return jsonify({"message": "Internal Server Error"}), 500
+2 -1
View File
@@ -6,6 +6,7 @@ from .repayment import Repayment
from .loan_charge import LoanCharge
from .offer import Offer
from .charge import Charge
from .rac_checks import RACCheck
__all__ = ['Customer', 'Account', 'Loan', 'Transaction', 'Repayment', 'LoanCharge', 'Offer', 'Charge']
__all__ = ['Customer', 'Account', 'Loan', 'Transaction', 'Repayment', 'LoanCharge', 'Offer', 'Charge', 'RACCheck']
+20 -2
View File
@@ -25,6 +25,9 @@ class Loan(db.Model):
initial_loan_amount = db.Column(db.Float, nullable=False)
default_penalty_fee = db.Column(db.Float, default=0)
continuous_fee = db.Column(db.Float, default=0)
upfront_fee = db.Column(db.Float, nullable=True, default=0.0)
repayment_amount = db.Column(db.Float, nullable=True, default=0.0)
installment_amount = db.Column(db.Float, nullable=True, default=0.0)
status = db.Column(db.String(20), default='pending')
due_date = db.Column(db.DateTime, nullable=True)
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
@@ -45,8 +48,20 @@ class Loan(db.Model):
)
@classmethod
def create_loan(cls, customer_id, account_id, offer_id, product_id, initial_loan_amount, collection_type, transaction_id, status='pending'):
def create_loan(
cls,
customer_id,
account_id,
offer_id,
product_id,
initial_loan_amount,
collection_type,
transaction_id,
upfront_fee,
repayment_amount,
installment_amount,
status="pending",
):
# Check if customer exists
customer = Customer.is_valid_customer(customer_id)
if not customer:
@@ -64,6 +79,9 @@ class Loan(db.Model):
transaction_id = transaction_id,
initial_loan_amount = initial_loan_amount,
current_loan_amount = initial_loan_amount,
upfront_fee = upfront_fee,
repayment_amount = repayment_amount,
installment_amount = installment_amount,
due_date=now,
status = status
)
+27 -1
View File
@@ -11,6 +11,8 @@ class Offer(db.Model):
min_amount = db.Column(db.Float, nullable=False)
max_amount = db.Column(db.Float, nullable=False)
tenor = db.Column(db.Integer, nullable=False)
schedule = db.Column(db.Integer, nullable=True)
list_order = db.Column(db.Integer, nullable=True)
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))
@@ -40,6 +42,28 @@ class Offer(db.Model):
if not offer:
return False
return offer
@classmethod
def get_offer_by_id(cls, offer_id):
"""
Return an offer by its ID.
"""
offer = cls.query.filter_by(id=str(offer_id)).first()
if not offer:
raise ValueError(f"Offer with ID {offer_id} not found")
return offer
@classmethod
def get_offer_by_product_id(cls, product_id):
"""
Return an offer by its product ID.
"""
offer = cls.query.filter_by(product_id=str(product_id)).first()
if not offer:
raise ValueError(f"Offer with Product ID {product_id} not found")
return offer
def to_dict(self):
return {
@@ -47,7 +71,9 @@ class Offer(db.Model):
"productId": self.product_id,
"minAmount": self.min_amount,
"maxAmount": self.max_amount,
"tenor": self.tenor
"tenor": self.tenor,
"schedule": self.schedule,
"list_order": self.list_order
}
def __repr__(self):
+53
View File
@@ -0,0 +1,53 @@
from datetime import datetime, timezone
from app.extensions import db
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID
from uuid import uuid4
from sqlalchemy.types import JSON
class RACCheck(db.Model):
__tablename__ = 'rac_checks'
id = db.Column(db.String, primary_key=True)
transaction_id = db.Column(db.String(50), nullable=False)
customer_id = db.Column(db.String, nullable=False)
account_id = db.Column(db.String, nullable=False)
rac_response = db.Column(db.JSON, nullable=False)
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))
@classmethod
def get_all_rac_checks(cls):
"""
Return all RAC checks in dictionary format.
"""
rac_checks = cls.query.all()
if not rac_checks:
raise ValueError("No available RAC checks")
return rac_checks
@classmethod
def get_rac_check_by_id(cls, check_id):
"""
Return a RAC check by its ID.
"""
rac_check = cls.query.filter_by(id=check_id).first()
if not rac_check:
raise ValueError(f"RAC Check with ID {check_id} not found")
return rac_check
def to_dict(self):
return {
"id": str(self.id),
"transactionId": str(self.transaction_id),
"customerId": self.customer_id,
"accountId": self.account_id,
"racResponse": self.rac_response,
"createdAt": self.created_at.isoformat(),
"updatedAt": self.updated_at.isoformat() if self.updated_at else None
}
def __repr__(self):
return f'<RACCheck {self.id}>'
@@ -0,0 +1,59 @@
"""Migration on Thu Apr 24 17:42:25 UTC 2025
Revision ID: 1b2339f43824
Revises: de9ad96ba34e
Create Date: 2025-04-24 17:43:09.589626
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '1b2339f43824'
down_revision = 'de9ad96ba34e'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('rac_checks',
sa.Column('id', sa.String(), nullable=False),
sa.Column('transaction_id', sa.String(length=50), nullable=False),
sa.Column('customer_id', sa.String(), nullable=False),
sa.Column('account_id', sa.String(), nullable=False),
sa.Column('rac_response', sa.JSON(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('loan_charges', schema=None) as batch_op:
batch_op.alter_column('amount',
existing_type=sa.NUMERIC(precision=10, scale=2),
type_=sa.Float(),
existing_nullable=True)
with op.batch_alter_table('loans', schema=None) as batch_op:
batch_op.add_column(sa.Column('upfront_fee', sa.Float(), nullable=True))
batch_op.add_column(sa.Column('repayment_amount', sa.Float(), nullable=True))
batch_op.add_column(sa.Column('installment_amount', sa.Float(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('loans', schema=None) as batch_op:
batch_op.drop_column('installment_amount')
batch_op.drop_column('repayment_amount')
batch_op.drop_column('upfront_fee')
with op.batch_alter_table('loan_charges', schema=None) as batch_op:
batch_op.alter_column('amount',
existing_type=sa.Float(),
type_=sa.NUMERIC(precision=10, scale=2),
existing_nullable=True)
op.drop_table('rac_checks')
# ### end Alembic commands ###
+4
View File
@@ -35,3 +35,7 @@ flask-jwt-extended
# Kafka
confluent-kafka==1.9.2
python-dateutil