2 Commits

Author SHA1 Message Date
Chinenye Nmoh 93d2659462 added extra query 2026-03-17 09:42:18 +01:00
Chinenye Nmoh 569d4c45d7 added extra query 2026-03-17 09:37:08 +01:00
49 changed files with 4518 additions and 4688 deletions
+15 -15
View File
@@ -1,16 +1,16 @@
KAFKA_TIMEOUT=1000.0 KAFKA_TIMEOUT=1000.0
KAFKA_BROKER="10.20.30.50:9092" KAFKA_BROKER="10.20.30.50:9092"
KAFKA_TOPICS=PROCESS_PAYMENT,LOAN_REPAYMENT KAFKA_TOPICS=PROCESS_PAYMENT,LOAN_REPAYMENT
# DATABASE_USER=firstadvance # DATABASE_USER=firstadvance
# DATABASE_PASSWORD=FirstAdvance! # DATABASE_PASSWORD=FirstAdvance!
# DATABASE_HOST=10.20.30.60 # DATABASE_HOST=10.20.30.60
# DATABASE_PORT=5432 # DATABASE_PORT=5432
# DATABASE_NAME=firstadvancedev # DATABASE_NAME=firstadvancedev
DATABASE_USER=system DATABASE_USER=system
DATABASE_PASSWORD=FIRSTADV_PASS DATABASE_PASSWORD=FIRSTADV_PASS
DATABASE_HOST=10.10.33.65 DATABASE_HOST=10.10.33.65
DATABASE_PORT=1521 DATABASE_PORT=1521
DATABASE_SID=FREE DATABASE_SID=FREE
+15 -15
View File
@@ -1,16 +1,16 @@
KAFKA_TIMEOUT=1000.0 KAFKA_TIMEOUT=1000.0
KAFKA_BROKER="dev-events.simbrellang.net:9085" KAFKA_BROKER="dev-events.simbrellang.net:9085"
KAFKA_TOPICS=PROCESS_PAYMENT,LOAN_REPAYMENT KAFKA_TOPICS=PROCESS_PAYMENT,LOAN_REPAYMENT
# DATABASE_USER=firstadvance # DATABASE_USER=firstadvance
# DATABASE_PASSWORD=FirstAdvance! # DATABASE_PASSWORD=FirstAdvance!
# DATABASE_HOST=dev-data.simbrellang.net # DATABASE_HOST=dev-data.simbrellang.net
# DATABASE_PORT=10532 # DATABASE_PORT=10532
# DATABASE_NAME=firstadvancedev # DATABASE_NAME=firstadvancedev
DATABASE_USER=system DATABASE_USER=system
DATABASE_PASSWORD=FIRSTADV_PASS DATABASE_PASSWORD=FIRSTADV_PASS
DATABASE_HOST=10.10.33.65 DATABASE_HOST=10.10.33.65
DATABASE_PORT=1521 DATABASE_PORT=1521
DATABASE_SID=FREE DATABASE_SID=FREE
+5 -5
View File
@@ -1,6 +1,6 @@
.vscode/ .vscode/
__pycache__/ __pycache__/
*/__pycache__/ */__pycache__/
.env .env
app.log app.log
.idea/ .idea/
+21 -21
View File
@@ -1,21 +1,21 @@
# Use Python base image # Use Python base image
FROM python:3.10 FROM python:3.10
# Set working directory # Set working directory
WORKDIR /app WORKDIR /app
# Copy files to container # Copy files to container
COPY . /app COPY . /app
# Install dependencies # Install dependencies
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# Expose port 5000 # Expose port 5000
EXPOSE 5000 EXPOSE 5000
# Set environment variables # Set environment variables
ENV FLASK_APP=app.py ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0 ENV FLASK_RUN_HOST=0.0.0.0
# Run the application # Run the application
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "--timeout", "120", "wsgi:wsgi_app"] CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "--timeout", "120", "wsgi:wsgi_app"]
+75 -75
View File
@@ -1,75 +1,75 @@
# Flask API with Swagger in Docker # Flask API with Swagger in Docker
This repository contains a Flask API with Swagger documentation, running inside Docker containers. This repository contains a Flask API with Swagger documentation, running inside Docker containers.
## Getting Started ## Getting Started
### Prerequisites ### Prerequisites
Ensure you have the following installed: Ensure you have the following installed:
- [Docker](https://www.docker.com/get-started) - [Docker](https://www.docker.com/get-started)
- [Docker Compose](https://docs.docker.com/compose/install/) - [Docker Compose](https://docs.docker.com/compose/install/)
### Clone the Repository ### Clone the Repository
```sh ```sh
git clone <your-repo-url> git clone <your-repo-url>
cd <your-repo-folder> cd <your-repo-folder>
``` ```
### Setup and Run the Application ### Setup and Run the Application
#### 1. Build and Start the Containers #### 1. Build and Start the Containers
```sh ```sh
docker-compose up --build -d docker-compose up --build -d
``` ```
This will: This will:
- Build the Flask API container. - Build the Flask API container.
- Start the Swagger UI container separately. - Start the Swagger UI container separately.
#### 2. Verify the Containers are Running #### 2. Verify the Containers are Running
```sh ```sh
docker ps docker ps
``` ```
#### 3. Access the API #### 3. Access the API
- The **Flask API** will be available at: - The **Flask API** will be available at:
``` ```
http://localhost:5000 http://localhost:5000
``` ```
- The **Swagger UI** will be available at: - The **Swagger UI** will be available at:
``` ```
http://localhost:9000 http://localhost:9000
``` ```
You can interact with the API documentation and test endpoints from here. You can interact with the API documentation and test endpoints from here.
## Making API Requests ## Making API Requests
You can send requests using tools like `curl` or Postman: You can send requests using tools like `curl` or Postman:
```sh ```sh
curl -X GET http://localhost:5000/hello curl -X GET http://localhost:5000/hello
``` ```
## Stopping the Application ## Stopping the Application
To stop the running containers, use: To stop the running containers, use:
```sh ```sh
docker-compose down docker-compose down
``` ```
## Troubleshooting ## Troubleshooting
- If there are permission issues, try running Docker commands with `sudo`. - If there are permission issues, try running Docker commands with `sudo`.
- If ports are in use, change them in `docker-compose.yml`. - If ports are in use, change them in `docker-compose.yml`.
- To rebuild the containers, use: - To rebuild the containers, use:
```sh ```sh
docker-compose up --build -d docker-compose up --build -d
``` ```
+7 -7
View File
@@ -1,7 +1,7 @@
from app import create_app from app import create_app
app = create_app() app = create_app()
if __name__ == "__main__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True) app.run(host="0.0.0.0", port=5000, debug=True)
+36 -36
View File
@@ -1,36 +1,36 @@
from flask import Flask from flask import Flask
from flask_mail import Mail from flask_mail import Mail
from flask_cors import CORS from flask_cors import CORS
from app.config import Config from app.config import Config
from app.routes import auth_bp, autocall_bp from app.routes import auth_bp, autocall_bp
from app.response import (method_not_allowed, unsupported_media_type, not_found, bad_request) from app.response import (method_not_allowed, unsupported_media_type, not_found, bad_request)
from app.extensions import db, mail from app.extensions import db, mail
def create_app(): def create_app():
"""Factory function to create a Flask app instance""" """Factory function to create a Flask app instance"""
app = Flask(__name__) app = Flask(__name__)
# Load configuration # Load configuration
app.config.from_object(Config) app.config.from_object(Config)
# Setup CORS # Setup CORS
CORS(app) CORS(app)
# Initialize Flask-Mail # Initialize Flask-Mail
mail.init_app(app) mail.init_app(app)
# Register blueprints # Register blueprints
app.register_blueprint(auth_bp) app.register_blueprint(auth_bp)
app.register_blueprint(autocall_bp, url_prefix="/autocall") app.register_blueprint(autocall_bp, url_prefix="/autocall")
# Error Handlers # Error Handlers
app.register_error_handler(405, method_not_allowed) app.register_error_handler(405, method_not_allowed)
app.register_error_handler(415, unsupported_media_type) app.register_error_handler(415, unsupported_media_type)
app.register_error_handler(404, not_found) app.register_error_handler(404, not_found)
app.register_error_handler(400, bad_request) app.register_error_handler(400, bad_request)
# Database # Database
db.init_app(app) db.init_app(app)
return app return app
+83 -91
View File
@@ -1,91 +1,83 @@
import os import os
from datetime import timedelta from datetime import timedelta
class Config: class Config:
"""Base configuration for Flask app""" """Base configuration for Flask app"""
SECRET_KEY = os.getenv("SECRET_KEY", "supersecretkey") SECRET_KEY = os.getenv("SECRET_KEY", "supersecretkey")
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "your_jwt_secret") JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "your_jwt_secret")
DEBUG = True DEBUG = True
KAFKA_BROKER = os.getenv("KAFKA_BROKER", "dev-events.simbrellang.net:9085") KAFKA_BROKER = os.getenv("KAFKA_BROKER", "dev-events.simbrellang.net:9085")
KAFKA_TOPICS = [topic.strip() for topic in os.getenv("KAFKA_TOPICS", "PROCESS_PAYMENT,LOAN_REPAYMENT").split(",") if topic.strip()] KAFKA_TOPICS = [topic.strip() for topic in os.getenv("KAFKA_TOPICS", "PROCESS_PAYMENT,LOAN_REPAYMENT").split(",") if topic.strip()]
KAFKA_TIMEOUT = float( os.getenv("KAFKA_TIMEOUT", 1000.0) ) KAFKA_TIMEOUT = float( os.getenv("KAFKA_TIMEOUT", 1000.0) )
JWT_ACCESS_TOKEN_EXPIRES = os.getenv("JWT_ACCESS_TOKEN_EXPIRES", timedelta(hours=1)) JWT_ACCESS_TOKEN_EXPIRES = os.getenv("JWT_ACCESS_TOKEN_EXPIRES", timedelta(hours=1))
JWT_REFRESH_TOKEN_EXPIRES = os.getenv( JWT_REFRESH_TOKEN_EXPIRES = os.getenv(
"JWT_REFRESH_TOKEN_EXPIRES", timedelta(days=30) "JWT_REFRESH_TOKEN_EXPIRES", timedelta(days=30)
) )
BANK_CALL_APP_ID = os.getenv("BANK_CALL_APP_ID", "app1") BANK_CALL_APP_ID = os.getenv("BANK_CALL_APP_ID", "app1")
BANK_CALL_API_KEY = os.getenv("BANK_CALL_API_KEY", "testtest-api-key-12345") BANK_CALL_API_KEY = os.getenv("BANK_CALL_API_KEY", "testtest-api-key-12345")
BANK_CALL_BASIC_AUTH_USERNAME = os.environ.get( BANK_CALL_BASIC_AUTH_USERNAME = os.environ.get(
"BANK_CALL_BASIC_AUTH_USERNAME", "simbrella" "BANK_CALL_BASIC_AUTH_USERNAME", "simbrella"
) )
BANK_CALL_BASIC_AUTH_PASSWORD = os.environ.get( BANK_CALL_BASIC_AUTH_PASSWORD = os.environ.get(
"BANK_CALL_BASIC_AUTH_PASSWORD", "G7$k9@pL2!qR" "BANK_CALL_BASIC_AUTH_PASSWORD", "G7$k9@pL2!qR"
) )
DATABASE_USER = os.getenv("DATABASE_USER") DATABASE_USER = os.getenv("DATABASE_USER")
DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD") DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")
DATABASE_HOST = os.getenv("DATABASE_HOST") DATABASE_HOST = os.getenv("DATABASE_HOST")
DATABASE_NAME = os.getenv("DATABASE_NAME") DATABASE_NAME = os.getenv("DATABASE_NAME")
DATABASE_PORT = os.getenv("DATABASE_PORT", 10532) DATABASE_PORT = os.getenv("DATABASE_PORT", 10532)
DATABASE_SID = os.environ.get("DATABASE_SID", "FREE") DATABASE_SID = os.environ.get("DATABASE_SID", "FREE")
DNS = f"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={DATABASE_HOST})(PORT={DATABASE_PORT}))(CONNECT_DATA=(SID={DATABASE_SID})))" DNS = f"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={DATABASE_HOST})(PORT={DATABASE_PORT}))(CONNECT_DATA=(SID={DATABASE_SID})))"
SQLALCHEMY_DATABASE_URI_INTERNAL = (f"oracle+oracledb://{DATABASE_USER}:{DATABASE_PASSWORD}@{DNS}") SQLALCHEMY_DATABASE_URI_INTERNAL = (f"oracle+oracledb://{DATABASE_USER}:{DATABASE_PASSWORD}@{DNS}")
#SQLALCHEMY_DATABASE_URI_INTERNAL = (f"oracle+oracledb://{DATABASE_USER}:{DATABASE_PASSWORD}@{DNS}") #SQLALCHEMY_DATABASE_URI_INTERNAL = (f"oracle+oracledb://{DATABASE_USER}:{DATABASE_PASSWORD}@{DNS}")
SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI_FULL", SQLALCHEMY_DATABASE_URI_INTERNAL) SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI_FULL", SQLALCHEMY_DATABASE_URI_INTERNAL)
# SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}" # SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
# SQLALCHEMY_ECHO = True # SQLALCHEMY_ECHO = True
OVERRIDE_COLLECTION_TRANCATION_ID = int(os.getenv("OVERRIDE_COLLECTION_TRANCATION_ID", 100)) OVERRIDE_COLLECTION_TRANCATION_ID = int(os.getenv("OVERRIDE_COLLECTION_TRANCATION_ID", 100))
MAIL_SERVER = os.getenv('MAIL_SERVER','smtp.zoho.com') MAIL_SERVER = os.getenv('MAIL_SERVER','smtp.zoho.com')
MAIL_PORT = os.getenv('MAIL_PORT', 587) MAIL_PORT = os.getenv('MAIL_PORT', 587)
MAIL_USERNAME = os.getenv('MAIL_USERNAME', 'firstadvance@dynamikservices.tech') MAIL_USERNAME = os.getenv('MAIL_USERNAME', 'firstadvance@dynamikservices.tech')
MAIL_PASSWORD = os.getenv('MAIL_PASSWORD') MAIL_PASSWORD = os.getenv('MAIL_PASSWORD')
MAIL_USE_TLS = os.getenv('MAIL_USE_TLS', 'True').lower() in ('true', '1', 'yes') MAIL_USE_TLS = os.getenv('MAIL_USE_TLS', 'True').lower() in ('true', '1', 'yes')
MAIL_USE_SSL = os.getenv('MAIL_USE_SSL', 'False').lower() in ('true', '1', 'yes') MAIL_USE_SSL = os.getenv('MAIL_USE_SSL', 'False').lower() in ('true', '1', 'yes')
MAIL_DEFAULT_SENDER = ('FirstAdvance', 'firstadvance@dynamikservices.tech') MAIL_DEFAULT_SENDER = ('FirstAdvance', 'firstadvance@dynamikservices.tech')
MAIL_RECEIVER= os.getenv('MAIL_RECEIVER', 'chinenyeumeaku@gmail.com,umeakuchinenye@gmail.com') MAIL_RECEIVER= os.getenv('MAIL_RECEIVER', 'chinenyeumeaku@gmail.com,umeakuchinenye@gmail.com')
# Processing Overdue LOANS sections # Processing Overdue LOANS sections
OVERDUE_LOAN_BATCH_SIZE = int(os.getenv("OVERDUE_LOAN_BATCH_SIZE", 10)) OVERDUE_LOAN_BATCH_SIZE = int(os.getenv("OVERDUE_LOAN_BATCH_SIZE", 10))
OVERDUE_LOAN_DELAY_SECONDS = int(os.getenv("OVERDUE_LOAN_DELAY_SECONDS", 5)) OVERDUE_LOAN_DELAY_SECONDS = int(os.getenv("OVERDUE_LOAN_DELAY_SECONDS", 5))
OVERDUE_LOAN_BATCH_DELAY_SECONDS = int( OVERDUE_LOAN_BATCH_DELAY_SECONDS = int(
os.getenv("OVERDUE_LOAN_BATCH_DELAY_SECONDS", 5) os.getenv("OVERDUE_LOAN_BATCH_DELAY_SECONDS", 5)
) )
OVERDUE_GRACE_PERIOD_DAYS = int(os.getenv("OVERDUE_GRACE_PERIOD_DAYS", 30)) OVERDUE_GRACE_PERIOD_DAYS = int(os.getenv("OVERDUE_GRACE_PERIOD_DAYS", 30))
OVERDUE_PROCESSING_LIST_LIMIT = int(os.getenv("OVERDUE_PROCESSING_LIST_LIMIT", 100)) OVERDUE_PROCESSING_LIST_LIMIT = int(os.getenv("OVERDUE_PROCESSING_LIST_LIMIT", 100))
PENAL_CHARGE_PERCENTAGE = os.getenv("PENAL_CHARGE_PERCENTAGE", 1) PENAL_CHARGE_PERCENTAGE = os.getenv("PENAL_CHARGE_PERCENTAGE", 1)
PENAL_CHARGE_INTERVAL_DAYS = os.getenv("PENAL_CHARGE_INTERVAL_DAYS", 30) PENAL_CHARGE_INTERVAL_DAYS = os.getenv("PENAL_CHARGE_INTERVAL_DAYS", 30)
PENAL_CHARGE_MAXIMUM_COUNT = os.getenv("PENAL_CHARGE_MAXIMUM_COUNT", 6) PENAL_CHARGE_MAXIMUM_COUNT = os.getenv("PENAL_CHARGE_MAXIMUM_COUNT", 6)
#processing failed disbursement sections
FAILED_DISBURSEMENT_BATCH_SIZE = int(os.getenv("FAILED_DISBURSEMENT_BATCH_SIZE", 10)) BANK_CALL_API_TIME_OUT = os.getenv("BANK_CALL_API_TIME_OUT", 100)
FAILED_DISBURSEMENT_DELAY_SECONDS = int(os.getenv("FAILED_DISBURSEMENT_DELAY_SECONDS", 5)) BANK_CALL_BASE_URL = os.getenv("BANK_CALL_BASE_URL", "https://bank-emulator.dev.simbrellang.net/api")
FAILED_DISBURSEMENT_BATCH_DELAY_SECONDS = int( BANK_CALL_SMS_BASE_URL= os.getenv("BANK_CALL_SMS_BASE_URL","https://first-advance-middleware-develop.fbn-devops-dev-asenv.appserviceenvironment.net/SMS")
os.getenv("FAILED_DISBURSEMENT_BATCH_DELAY_SECONDS", 5) BANK_CALL_DISBURSE_LOAN_ENDPOINT = os.getenv("BANK_CALL_DISBURSE_LOAN_ENDPOINT","/DisburseLoan")
) BANK_CALL_COLLECT_LOAN_ENDPOINT = os.getenv("BANK_CALL_COLLECT_LOAN_ENDPOINT","/CollectLoan")
FAILED_RETRY_TIME_INTERVAL_SECONDS = int(os.getenv("FAILED_RETRY_TIME_INTERVAL_SECONDS", 86400)) #24 hours = 86400 seconds BANK_CALL_TRANSACTION_VERIFY = os.getenv("BANK_CALL_TRANSACTION_VERIFY", "/TransactionVerify")
BANK_HEALTH_CHECK_ENDPOINT = os.getenv("BANK_HEALTH_CHECK_ENDPOINT", "/system-health-check")
BANK_CALL_AUTH_ENDPOINT = os.getenv("BANK_CALL_AUTH_ENDPOINT", "/Auth/generate-token")
BANK_CALL_API_TIME_OUT = os.getenv("BANK_CALL_API_TIME_OUT", 100) BANK_GRANT_TYPE = os.getenv("BANK_GRANT_TYPE", "password")
BANK_CALL_BASE_URL = os.getenv("BANK_CALL_BASE_URL", "https://bank-emulator.dev.simbrellang.net/api") TEST_NO = os.getenv("TEST_NO", "2347038224367")
BANK_CALL_SMS_BASE_URL= os.getenv("BANK_CALL_SMS_BASE_URL","https://first-advance-middleware-develop.fbn-devops-dev-asenv.appserviceenvironment.net/SMS")
BANK_CALL_DISBURSE_LOAN_ENDPOINT = os.getenv("BANK_CALL_DISBURSE_LOAN_ENDPOINT","/DisburseLoan") settings = Config()
BANK_CALL_COLLECT_LOAN_ENDPOINT = os.getenv("BANK_CALL_COLLECT_LOAN_ENDPOINT","/CollectLoan")
BANK_CALL_TRANSACTION_VERIFY = os.getenv("BANK_CALL_TRANSACTION_VERIFY", "/TransactionVerify")
BANK_HEALTH_CHECK_ENDPOINT = os.getenv("BANK_HEALTH_CHECK_ENDPOINT", "/system-health-check")
BANK_CALL_AUTH_ENDPOINT = os.getenv("BANK_CALL_AUTH_ENDPOINT", "/Auth/generate-token")
BANK_GRANT_TYPE = os.getenv("BANK_GRANT_TYPE", "password")
TEST_NO = os.getenv("TEST_NO", "2347038224367")
settings = Config()
+3 -3
View File
@@ -1,3 +1,3 @@
from .transaction_type import TransactionType from .transaction_type import TransactionType
from .loan_status import LoanStatus from .loan_status import LoanStatus
from .repayment_schedule_status import RepaymentScheduleStatus from .repayment_schedule_status import RepaymentScheduleStatus
+8 -9
View File
@@ -1,9 +1,8 @@
from enum import Enum from enum import Enum
class LoanStatus(str, Enum): class LoanStatus(str, Enum):
PENDING = "pending" PENDING = "pending"
ACTIVE = "active" ACTIVE = "active"
ACTIVE_PARTIAL = "active_partial" ACTIVE_PARTIAL = "active_partial"
START_REPAY = "start_repay" START_REPAY = "start_repay"
REPAID = "repaid" REPAID = "repaid"
FAILED = "failed"
+6 -6
View File
@@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
class RepaymentScheduleStatus(str, Enum): class RepaymentScheduleStatus(str, Enum):
PARTIALLY_PAID = "partially_paid" PARTIALLY_PAID = "partially_paid"
REPAID = "repaid" REPAID = "repaid"
ACTIVE = "active" ACTIVE = "active"
OVERDUE = "overdue" OVERDUE = "overdue"
+10 -10
View File
@@ -1,10 +1,10 @@
from enum import Enum from enum import Enum
class TransactionType(str, Enum): class TransactionType(str, Enum):
ELIGIBILITY_CHECK = "eligibility_check" ELIGIBILITY_CHECK = "eligibility_check"
CUSTOMER_CONSENT = "customer_consent" CUSTOMER_CONSENT = "customer_consent"
LOAN_STATUS = "loan_status" LOAN_STATUS = "loan_status"
NOTIFICATION_CALLBACK = "notification_callback" NOTIFICATION_CALLBACK = "notification_callback"
PROVIDE_LOAN = "provide_loan" PROVIDE_LOAN = "provide_loan"
REPAYMENT = "repayment" REPAYMENT = "repayment"
SELECT_OFFER = "select_offer" SELECT_OFFER = "select_offer"
+4 -4
View File
@@ -1,5 +1,5 @@
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail from flask_mail import Mail
mail = Mail() mail = Mail()
db = SQLAlchemy() db = SQLAlchemy()
+62 -62
View File
@@ -1,63 +1,63 @@
import random import random
import string import string
from app.services.repayment import RepaymentService from app.services.repayment import RepaymentService
from app.services.loan import LoanService from app.services.loan import LoanService
from app.helpers.response_helper import ResponseHelper from app.helpers.response_helper import ResponseHelper
from app.utils.logger import logger from app.utils.logger import logger
from app.config import settings from app.config import settings
OVERRIDE_COLLECTION_TRANCATION_ID = settings.OVERRIDE_COLLECTION_TRANCATION_ID OVERRIDE_COLLECTION_TRANCATION_ID = settings.OVERRIDE_COLLECTION_TRANCATION_ID
class CollectLoanHelper: class CollectLoanHelper:
@staticmethod @staticmethod
def _validate_repayment_and_loan(data): def _validate_repayment_and_loan(data):
repayment = RepaymentService.get_repayment_by_id(id=data['Id']) repayment = RepaymentService.get_repayment_by_id(id=data['Id'])
if not repayment: if not repayment:
logger.info(f"Repayment id: {data['Id']}, was not found") logger.info(f"Repayment id: {data['Id']}, was not found")
return None, None, ResponseHelper.error("Repayment not found") return None, None, ResponseHelper.error("Repayment not found")
repayment_data = repayment.to_dict() repayment_data = repayment.to_dict()
loan = LoanService.get_loan_by_loan_id(loan_id=int(repayment_data['loanId'])) loan = LoanService.get_loan_by_loan_id(loan_id=int(repayment_data['loanId']))
if not loan: if not loan:
logger.info(f"Loan id: {repayment_data['loanId']}, was not found") logger.info(f"Loan id: {repayment_data['loanId']}, was not found")
return None, None, ResponseHelper.error("Loan not found") return None, None, ResponseHelper.error("Loan not found")
loan loan
return repayment_data, loan, None return repayment_data, loan, None
@staticmethod @staticmethod
def _build_collect_loan_payload(loan_data, repayment_data, data, collectionMethod): def _build_collect_loan_payload(loan_data, repayment_data, data, collectionMethod):
logger.info(f"building CollectLoan endpoint with data: {loan_data}") logger.info(f"building CollectLoan endpoint with data: {loan_data}")
debtId = str(loan_data.get('debtId', "")).strip().zfill(6) debtId = str(loan_data.get('debtId', "")).strip().zfill(6)
#this can be overridden based on config #this can be overridden based on config
t_id = ''.join(random.choices(string.ascii_uppercase, k=22)) t_id = ''.join(random.choices(string.ascii_uppercase, k=22))
if OVERRIDE_COLLECTION_TRANCATION_ID == 100: if OVERRIDE_COLLECTION_TRANCATION_ID == 100:
t_id = loan_data['transactionId'] t_id = loan_data['transactionId']
return { return {
"transactionId": t_id, "transactionId": t_id,
"fbnTransactionId": loan_data['transactionId'], "fbnTransactionId": loan_data['transactionId'],
"debtId": debtId, "debtId": debtId,
"customerId": repayment_data['customerId'], "customerId": repayment_data['customerId'],
"accountId": loan_data['accountId'], "accountId": loan_data['accountId'],
"productId": repayment_data['productId'], "productId": repayment_data['productId'],
"collectAmount": ( "collectAmount": (
data['overdueLoanScheduleAmount'] data['overdueLoanScheduleAmount']
if data.get('overdueLoanScheduleAmount') if data.get('overdueLoanScheduleAmount')
is not None else loan_data.get('balance', 0) is not None else loan_data.get('balance', 0)
), ),
"penalCharge": 0, "penalCharge": 0,
"channel": "USSD", "channel": "USSD",
"collectionMethod": collectionMethod, "collectionMethod": collectionMethod,
"lienAmount": 0, "lienAmount": 0,
"countryId": "NG", "countryId": "NG",
"comment": "COLLECT LOAN" "comment": "COLLECT LOAN"
} }
@staticmethod @staticmethod
def chunk_list(data, chunk_size): def chunk_list(data, chunk_size):
"""Yield successive chunk_size chunks from data.""" """Yield successive chunk_size chunks from data."""
for i in range(0, len(data), chunk_size): for i in range(0, len(data), chunk_size):
yield data[i:i + chunk_size] yield data[i:i + chunk_size]
+250 -250
View File
@@ -1,251 +1,251 @@
from flask import jsonify from flask import jsonify
from typing import List, Dict, Union, Optional, Any from typing import List, Dict, Union, Optional, Any
class ResponseHelper: class ResponseHelper:
""" """
A helper class for building standardized JSON responses in Flask. A helper class for building standardized JSON responses in Flask.
""" """
@staticmethod @staticmethod
def build_response( def build_response(
status: bool, status: bool,
message: str, message: str,
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
status_code: int = 200, status_code: int = 200,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Build a standardized JSON response. Build a standardized JSON response.
Args: Args:
status (bool): Indicates whether the request was successful. status (bool): Indicates whether the request was successful.
message (str): A message describing the result of the request. message (str): A message describing the result of the request.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
status_code (int): The HTTP status code for the response. status_code (int): The HTTP status code for the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
response = { response = {
"status": status, "status": status,
"statusCode": status_code, "statusCode": status_code,
"message": message, "message": message,
"data": data if data is not None else {}, "data": data if data is not None else {},
"error": error if error is not None else {}, "error": error if error is not None else {},
} }
return jsonify(response), status_code return jsonify(response), status_code
@staticmethod @staticmethod
def success( def success(
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
message: str = "Successful", message: str = "Successful",
status_code: int = 200, status_code: int = 200,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a success response. Return a success response.
Args: Args:
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
message (str): A message describing the result of the request. message (str): A message describing the result of the request.
status_code (int): The HTTP status code for the response. status_code (int): The HTTP status code for the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(True, message, data, status_code, error) return ResponseHelper.build_response(True, message, data, status_code, error)
@staticmethod @staticmethod
def error( def error(
message: str = "An error occurred", message: str = "An error occurred",
status_code: int = 400, status_code: int = 400,
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return an error response. Return an error response.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
status_code (int): The HTTP status code for the response. status_code (int): The HTTP status code for the response.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, status_code, error) return ResponseHelper.build_response(False, message, data, status_code, error)
@staticmethod @staticmethod
def created( def created(
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
message: str = "Resource created successfully", message: str = "Resource created successfully",
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for a created resource. Return a response for a created resource.
Args: Args:
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
message (str): A message describing the result of the request. message (str): A message describing the result of the request.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(True, message, data, 201, error) return ResponseHelper.build_response(True, message, data, 201, error)
@staticmethod @staticmethod
def updated( def updated(
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
message: str = "Resource updated successfully", message: str = "Resource updated successfully",
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for an updated resource. Return a response for an updated resource.
Args: Args:
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
message (str): A message describing the result of the request. message (str): A message describing the result of the request.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(True, message, data, 200, error) return ResponseHelper.build_response(True, message, data, 200, error)
@staticmethod @staticmethod
def internal_server_error( def internal_server_error(
message: str = "Internal Server Error", message: str = "Internal Server Error",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for an internal server error. Return a response for an internal server error.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 500, error) return ResponseHelper.build_response(False, message, data, 500, error)
@staticmethod @staticmethod
def unauthorized( def unauthorized(
message: str = "Unauthorized", message: str = "Unauthorized",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for an unauthorized request. Return a response for an unauthorized request.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 401, error) return ResponseHelper.build_response(False, message, data, 401, error)
@staticmethod @staticmethod
def forbidden( def forbidden(
message: str = "Forbidden", message: str = "Forbidden",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for a forbidden request. Return a response for a forbidden request.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 403, error) return ResponseHelper.build_response(False, message, data, 403, error)
@staticmethod @staticmethod
def not_found( def not_found(
message: str = "Resource not found", message: str = "Resource not found",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for a not found resource. Return a response for a not found resource.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 404, error) return ResponseHelper.build_response(False, message, data, 404, error)
@staticmethod @staticmethod
def unprocessable_entity( def unprocessable_entity(
message: str = "Unprocessable entity", message: str = "Unprocessable entity",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for an unprocessable entity. Return a response for an unprocessable entity.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 422, error) return ResponseHelper.build_response(False, message, data, 422, error)
@staticmethod @staticmethod
def method_not_allowed( def method_not_allowed(
message: str = "Method Not Allowed", message: str = "Method Not Allowed",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for a method not allowed error. Return a response for a method not allowed error.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 405, error) return ResponseHelper.build_response(False, message, data, 405, error)
@staticmethod @staticmethod
def bad_request( def bad_request(
message: str = "Bad Request", message: str = "Bad Request",
data: Optional[Union[Dict, List, str]] = None, data: Optional[Union[Dict, List, str]] = None,
error: Optional[Union[Dict, str]] = None, error: Optional[Union[Dict, str]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Return a response for a bad request error. Return a response for a bad request error.
Args: Args:
message (str): A message describing the error. message (str): A message describing the error.
data (Optional[Union[Dict, List, str]]): The data to return in the response. data (Optional[Union[Dict, List, str]]): The data to return in the response.
error (Optional[Union[Dict, str]]): Any error details to include in the response. error (Optional[Union[Dict, str]]): Any error details to include in the response.
Returns: Returns:
Dict[str, Any]: A dictionary representing the JSON response. Dict[str, Any]: A dictionary representing the JSON response.
""" """
return ResponseHelper.build_response(False, message, data, 400, error) return ResponseHelper.build_response(False, message, data, 400, error)
+2 -2
View File
@@ -1,3 +1,3 @@
from .kafka import KafkaIntegration from .kafka import KafkaIntegration
from .simbrella import SimbrellaClient from .simbrella import SimbrellaClient
from .bank_service import BankService from .bank_service import BankService
+24 -24
View File
@@ -1,25 +1,25 @@
import requests import requests
from app.config import settings from app.config import settings
from app.utils.auth import get_headers from app.utils.auth import get_headers
from app.utils.logger import logger from app.utils.logger import logger
class BankService: class BankService:
BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL
BANK_HEALTH_CHECK_ENDPOINT = settings.BANK_HEALTH_CHECK_ENDPOINT BANK_HEALTH_CHECK_ENDPOINT = settings.BANK_HEALTH_CHECK_ENDPOINT
BANK_CALL_APP_ID = settings.BANK_CALL_APP_ID BANK_CALL_APP_ID = settings.BANK_CALL_APP_ID
@staticmethod @staticmethod
def health_check(): def health_check():
api_url = f"{BankService.BANK_CALL_BASE_URL}{BankService.BANK_HEALTH_CHECK_ENDPOINT}" api_url = f"{BankService.BANK_CALL_BASE_URL}{BankService.BANK_HEALTH_CHECK_ENDPOINT}"
logger.info(f"Calling Health Check endpoint: {api_url}") logger.info(f"Calling Health Check endpoint: {api_url}")
try: try:
response = requests.get(api_url, timeout=5, headers=get_headers()) response = requests.get(api_url, timeout=5, headers=get_headers())
logger.info(f"Health Check response status code: {response.status_code}") logger.info(f"Health Check response status code: {response.status_code}")
return response.json() return response.json()
except Exception as e: except Exception as e:
logger.error(f"Health Check API call failed: {str(e)}", exc_info=True) logger.error(f"Health Check API call failed: {str(e)}", exc_info=True)
raise raise
+156 -156
View File
@@ -1,156 +1,156 @@
from confluent_kafka import Consumer, Producer from confluent_kafka import Consumer, Producer
import json import json
from app.utils.logger import logger from app.utils.logger import logger
from app.config import settings from app.config import settings
import requests import requests
from app.integrations.simbrella import SimbrellaClient from app.integrations.simbrella import SimbrellaClient
class KafkaIntegration: class KafkaIntegration:
BASE_URL = settings.BANK_CALL_BASE_URL BASE_URL = settings.BANK_CALL_BASE_URL
_consumer = None _consumer = None
_consumer_config = { _consumer_config = {
"bootstrap.servers": settings.KAFKA_BROKER, "bootstrap.servers": settings.KAFKA_BROKER,
"group.id": "loan-service-consumer", "group.id": "loan-service-consumer",
"auto.offset.reset": "earliest", "auto.offset.reset": "earliest",
"enable.auto.commit": True, "enable.auto.commit": True,
} }
@staticmethod @staticmethod
def _get_consumer(): def _get_consumer():
"""Kafka consumer""" """Kafka consumer"""
if not KafkaIntegration._consumer: if not KafkaIntegration._consumer:
KafkaIntegration._consumer = Consumer(KafkaIntegration._consumer_config) KafkaIntegration._consumer = Consumer(KafkaIntegration._consumer_config)
logger.info( logger.info(
f"Consumer connected to Kafka broker at {KafkaIntegration._consumer_config['bootstrap.servers']}" f"Consumer connected to Kafka broker at {KafkaIntegration._consumer_config['bootstrap.servers']}"
) )
return KafkaIntegration._consumer return KafkaIntegration._consumer
@staticmethod @staticmethod
def delivery_report(err, msg): def delivery_report(err, msg):
"""Called once for each message produced""" """Called once for each message produced"""
if err is not None: if err is not None:
logger.error(f"Message delivery failed: {err}") logger.error(f"Message delivery failed: {err}")
raise RuntimeError(f"Message delivery failed: {err}") raise RuntimeError(f"Message delivery failed: {err}")
else: else:
logger.debug( logger.debug(
f"Message delivered to {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}" f"Message delivered to {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}"
) )
@staticmethod @staticmethod
def receive_messages(topics, timeout): def receive_messages(topics, timeout):
""" """
Receive messages from a Kafka topic. Receive messages from a Kafka topic.
:param topics: The Kafka topics to subscribe to :param topics: The Kafka topics to subscribe to
:param timeout: Time to wait for a message (in seconds) :param timeout: Time to wait for a message (in seconds)
:return: The message value (decoded) or None if no message is received :return: The message value (decoded) or None if no message is received
""" """
consumer = KafkaIntegration._get_consumer() consumer = KafkaIntegration._get_consumer()
consumer.subscribe(topics) consumer.subscribe(topics)
logger.info( logger.info(
f"Waiting for messages from topic {topics} with this timeout: {timeout}..." f"Waiting for messages from topic {topics} with this timeout: {timeout}..."
) )
message = [] message = []
try: try:
msg = consumer.poll(timeout=timeout) msg = consumer.poll(timeout=timeout)
logger.info(str(msg.value)) logger.info(str(msg.value))
logger.info( logger.info(
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: " f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: "
) )
if msg is None: if msg is None:
logger.info(f"No message received from topic {topics} within timeout") logger.info(f"No message received from topic {topics} within timeout")
return None return None
if msg.error(): if msg.error():
logger.info(f"Consumer error: {msg.error()}") logger.info(f"Consumer error: {msg.error()}")
raise RuntimeError(f"Consumer error: {msg.error()}") raise RuntimeError(f"Consumer error: {msg.error()}")
# Decode and return the message value # Decode and return the message value
message_value= {"value":''} message_value= {"value":''}
if msg.value() != "": if msg.value() != "":
message_value = msg.value().decode("utf-8") message_value = msg.value().decode("utf-8")
logger.info( logger.info(
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message_value}" f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message_value}"
) )
# Invalid Json will stop this process # Invalid Json will stop this process
try: try:
message = json.loads(message_value) if message_value else None message = json.loads(message_value) if message_value else None
except ValueError as e: except ValueError as e:
return message # Invalid JSON JUST TURN BACK HERE return message # Invalid JSON JUST TURN BACK HERE
else: else:
pass # valid json pass # valid json
# Call the endpoint if provided # Call the endpoint if provided
if message: if message:
logger.info( logger.info(
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message}" f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message}"
) )
current_topic = msg.topic() current_topic = msg.topic()
if current_topic=="PROCESS_PAYMENT": if current_topic=="PROCESS_PAYMENT":
KafkaIntegration._call_disbursement_service(message) KafkaIntegration._call_disbursement_service(message)
if current_topic=="LOAN_REPAYMENT": if current_topic=="LOAN_REPAYMENT":
KafkaIntegration._call_collect_loan_service(message) KafkaIntegration._call_collect_loan_service(message)
logger.info( logger.info(
f"Loan Repayment message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message}" f"Loan Repayment message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message}"
) )
return message return message
except Exception as e: except Exception as e:
logger.error(f"Error while receiving message: {e}") logger.error(f"Error while receiving message: {e}")
raise raise
#return [] #return []
@staticmethod @staticmethod
def close_consumer(): def close_consumer():
"""Shutdown consumer""" """Shutdown consumer"""
consumer = KafkaIntegration._get_consumer() consumer = KafkaIntegration._get_consumer()
consumer.close() consumer.close()
logger.info("Kafka consumer closed") logger.info("Kafka consumer closed")
@staticmethod @staticmethod
def _call_disbursement_service(message): def _call_disbursement_service(message):
"""Call the disbursement service with the received message""" """Call the disbursement service with the received message"""
logger.info(f"Calling disbursement service with message: {message}") logger.info(f"Calling disbursement service with message: {message}")
try: try:
response = SimbrellaClient.disburse_loan(message) response = SimbrellaClient.disburse_loan(message)
logger.info( logger.info(
f"Successfully sent message to disbursement service: {response}" f"Successfully sent message to disbursement service: {response}"
) )
# LoanService.set_disbursement_date(loan_id=loan_data['debtId'], # LoanService.set_disbursement_date(loan_id=loan_data['debtId'],
# customer_id=customerId) # must mark it on way out # customer_id=customerId) # must mark it on way out
# #
except Exception as e: except Exception as e:
logger.info(f"Failed to call disbursement service: {e}") logger.info(f"Failed to call disbursement service: {e}")
#raise #raise
@staticmethod @staticmethod
def _call_collect_loan_service(message): def _call_collect_loan_service(message):
"""Call the collect loan service with the received message""" """Call the collect loan service with the received message"""
logger.info(f"Calling collect_loan service with message: {message}") logger.info(f"Calling collect_loan service with message: {message}")
try: try:
#Calling CollectLoan endpoint with data: {'transactionId': 'TRCVIC85641527829', 'customerId': 'ZX48440946', 'productId': 'AMPC', 'loanRef': 'TRCVIC85641527829USSDAMPC', 'debtId': '014231'} #Calling CollectLoan endpoint with data: {'transactionId': 'TRCVIC85641527829', 'customerId': 'ZX48440946', 'productId': 'AMPC', 'loanRef': 'TRCVIC85641527829USSDAMPC', 'debtId': '014231'}
response = SimbrellaClient.collect_loan_user_initiated(message) response = SimbrellaClient.collect_loan_user_initiated(message)
logger.info( logger.info(
f"Successfully sent message to collect_loan service: {response}" f"Successfully sent message to collect_loan service: {response}"
) )
except Exception as e: except Exception as e:
logger.error(f"Failed to call collect_loan service: {e}") logger.error(f"Failed to call collect_loan service: {e}")
# raise # raise
File diff suppressed because it is too large Load Diff
+9 -9
View File
@@ -1,10 +1,10 @@
from .transactions import Transaction from .transactions import Transaction
from .repayment import Repayment from .repayment import Repayment
from .loan import Loan from .loan import Loan
from .loan_charge import LoanCharge from .loan_charge import LoanCharge
from .customer import Customer from .customer import Customer
from .account import Account from .account import Account
from .repayments_data import RepaymentsData from .repayments_data import RepaymentsData
from .salary import Salary from .salary import Salary
__all__ = ['Transaction', 'Repayment', 'Loan', 'LoanCharge', 'Customer', 'Account', 'RepaymentsData','Salary'] __all__ = ['Transaction', 'Repayment', 'Loan', 'LoanCharge', 'Customer', 'Account', 'RepaymentsData','Salary']
+25 -25
View File
@@ -1,25 +1,25 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from app.extensions import db from app.extensions import db
class Account(db.Model): class Account(db.Model):
__tablename__ = 'accounts' __tablename__ = 'accounts'
id = db.Column(db.String(50), primary_key=True) id = db.Column(db.String(50), primary_key=True)
customer_id = db.Column(db.String(50), nullable=False) customer_id = db.Column(db.String(50), nullable=False)
account_type = db.Column(db.String(50)) account_type = db.Column(db.String(50))
status = db.Column(db.String(20), default='active') status = db.Column(db.String(20), default='active')
lien_amount = db.Column(db.Float, default=0.0) lien_amount = db.Column(db.Float, default=0.0)
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))
customer = relationship( customer = relationship(
"Customer", "Customer",
primaryjoin="Customer.id == Account.customer_id", primaryjoin="Customer.id == Account.customer_id",
foreign_keys=[customer_id], foreign_keys=[customer_id],
back_populates="accounts", back_populates="accounts",
) )
def __repr__(self): def __repr__(self):
return f'<Account {self.id}>' return f'<Account {self.id}>'
+41 -41
View File
@@ -1,41 +1,41 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from app.extensions import db from app.extensions import db
class Customer(db.Model): class Customer(db.Model):
__tablename__ = 'customers' __tablename__ = 'customers'
id = db.Column(db.String(50), primary_key=True) id = db.Column(db.String(50), primary_key=True)
msisdn = db.Column(db.String(20), unique=True, nullable=False) msisdn = db.Column(db.String(20), unique=True, nullable=False)
country_code = db.Column(db.String(3), nullable=False) country_code = db.Column(db.String(3), nullable=False)
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))
accounts = relationship( accounts = relationship(
"Account", "Account",
primaryjoin="Customer.id == Account.customer_id", primaryjoin="Customer.id == Account.customer_id",
foreign_keys="Account.customer_id", foreign_keys="Account.customer_id",
back_populates="customer", back_populates="customer",
) )
loans = relationship( loans = relationship(
"Loan", "Loan",
primaryjoin="Customer.id == Loan.customer_id", primaryjoin="Customer.id == Loan.customer_id",
foreign_keys="Loan.customer_id", foreign_keys="Loan.customer_id",
back_populates="customer", back_populates="customer",
) )
@classmethod @classmethod
def get_customer(cls, customer_id): def get_customer(cls, customer_id):
""" """
Get customer by ID. Get customer by 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:
raise ValueError(f"Customer does not exist") raise ValueError(f"Customer does not exist")
return customer return customer
def __repr__(self): def __repr__(self):
return f'<Customer {self.id}>' return f'<Customer {self.id}>'
+415 -451
View File
@@ -1,451 +1,415 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from datetime import timedelta from datetime import timedelta
import logging import logging
from sqlalchemy import and_, or_, not_ from sqlalchemy import and_, or_, not_
from sqlalchemy.sql import func from sqlalchemy.sql import func
from app.utils.logger import logger from app.utils.logger import logger
from app.extensions import db from app.extensions import db
from decimal import Decimal, ROUND_HALF_UP from decimal import Decimal, ROUND_HALF_UP
from datetime import datetime, timezone from datetime import datetime, timezone
from app.config import settings
class Loan(db.Model):
class Loan(db.Model): __tablename__ = "loans"
__tablename__ = "loans"
id = db.Column(
id = db.Column( db.Integer,
db.Integer, primary_key=True,
primary_key=True, autoincrement=True,
autoincrement=True, )
) customer_id = db.Column(db.String(50), nullable=False)
customer_id = db.Column(db.String(50), nullable=False) transaction_id = db.Column(db.String(50), nullable=True)
transaction_id = db.Column(db.String(50), nullable=True) original_transaction = db.Column(db.String(50), nullable=True)
original_transaction = db.Column(db.String(50), nullable=True) account_id = db.Column(db.String(50), nullable=False)
account_id = db.Column(db.String(50), nullable=False) offer_id = db.Column(db.String(20), nullable=False)
offer_id = db.Column(db.String(20), nullable=False) product_id = db.Column(db.String(20), nullable=True)
product_id = db.Column(db.String(20), nullable=True) collection_type = db.Column(db.String(20), nullable=True)
collection_type = db.Column(db.String(20), nullable=True) current_loan_amount = db.Column(db.Float, nullable=True)
current_loan_amount = db.Column(db.Float, nullable=True) initial_loan_amount = db.Column(db.Float, nullable=False)
initial_loan_amount = db.Column(db.Float, nullable=False) default_penalty_fee = db.Column(db.Float, default=0)
default_penalty_fee = db.Column(db.Float, default=0) continuous_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)
upfront_fee = db.Column(db.Float, nullable=True, default=0.0) repayment_amount = db.Column(db.Float, nullable=True, default=0.0)
repayment_amount = db.Column(db.Float, nullable=True, default=0.0) balance = db.Column(db.Float, nullable=True, default=0.0)
balance = db.Column(db.Float, nullable=True, default=0.0) installment_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')
status = db.Column(db.String(20), default='pending') tenor = db.Column(db.Integer, nullable=True)
tenor = db.Column(db.Integer, nullable=True) due_date = db.Column(db.DateTime, nullable=True)
due_date = db.Column(db.DateTime, nullable=True) created_at = db.Column(db.DateTime(timezone=True), server_default=func.now())
created_at = db.Column(db.DateTime(timezone=True), server_default=func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
updated_at = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) eligible_amount = db.Column(db.Float, nullable=True, default=0.0)
eligible_amount = db.Column(db.Float, nullable=True, default=0.0) disburse_date = db.Column(db.DateTime, nullable=True)
disburse_date = db.Column(db.DateTime, nullable=True) disburse_verify = db.Column(db.DateTime, nullable=True)
disburse_verify = db.Column(db.DateTime, nullable=True) disburse_result = db.Column(db.String(10), nullable=True)
disburse_result = db.Column(db.String(10), nullable=True) disburse_description = db.Column(db.String(100), nullable=True)
disburse_description = db.Column(db.String(100), nullable=True) verify_result = db.Column(db.String(10), nullable=True)
verify_result = db.Column(db.String(10), nullable=True) verify_description = db.Column(db.String(100), nullable=True)
verify_description = db.Column(db.String(100), nullable=True) reference = db.Column(db.String(50), nullable=True)
reference = db.Column(db.String(50), nullable=True)
total_penal_charge = db.Column(db.Float, default=0.0) total_penal_charge = db.Column(db.Float, default=0.0)
last_penal_date = db.Column(db.DateTime, nullable=True) last_penal_date = db.Column(db.DateTime, nullable=True)
customer = relationship( customer = relationship(
"Customer", "Customer",
primaryjoin="Customer.id == Loan.customer_id", primaryjoin="Customer.id == Loan.customer_id",
foreign_keys=[customer_id], foreign_keys=[customer_id],
back_populates="loans", back_populates="loans",
) )
loan_charges = relationship( loan_charges = relationship(
"LoanCharge", "LoanCharge",
primaryjoin="Loan.id == LoanCharge.loan_id", primaryjoin="Loan.id == LoanCharge.loan_id",
foreign_keys="LoanCharge.loan_id", foreign_keys="LoanCharge.loan_id",
back_populates="loan", back_populates="loan",
) )
def __repr__(self): def __repr__(self):
return f"<Loan {self.id}>" return f"<Loan {self.id}>"
def to_dict(self): def to_dict(self):
""" """
Convert the Loan object to a dictionary format for JSON serialization. Convert the Loan object to a dictionary format for JSON serialization.
""" """
return { return {
'debtId': self.id, 'debtId': self.id,
"customerId": self.customer_id, "customerId": self.customer_id,
'initialLoanAmount': self.initial_loan_amount, 'initialLoanAmount': self.initial_loan_amount,
'currentLoanAmount': self.current_loan_amount, 'currentLoanAmount': self.current_loan_amount,
'defaultPenaltyFee': self.default_penalty_fee, 'defaultPenaltyFee': self.default_penalty_fee,
'continuousFee': self.continuous_fee, 'continuousFee': self.continuous_fee,
'collectionType': self.collection_type, 'collectionType': self.collection_type,
'repaymentAmount':self.repayment_amount, 'repaymentAmount':self.repayment_amount,
'status': self.status, 'status': self.status,
'productId': self.product_id, 'productId': self.product_id,
'disburseResult': self.disburse_result, 'disburseResult': self.disburse_result,
'disburseDescription': self.disburse_description, 'disburseDescription': self.disburse_description,
'verifyResult': self.verify_result, 'verifyResult': self.verify_result,
'verifyDescription': self.verify_description, 'verifyDescription': self.verify_description,
'transactionId': self.transaction_id, 'transactionId': self.transaction_id,
'accountId':self.account_id, 'accountId':self.account_id,
'dueDate': self.due_date.isoformat() if self.due_date else None, 'dueDate': self.due_date.isoformat() if self.due_date else None,
'loanDate': self.created_at.isoformat() if self.created_at else None, 'loanDate': self.created_at.isoformat() if self.created_at else None,
'disburseDate': self.disburse_date.isoformat() if self.disburse_date else None, 'disburseDate': self.disburse_date.isoformat() if self.disburse_date else None,
'disburseVerify': self.disburse_verify.isoformat() if self.disburse_verify else None, 'disburseVerify': self.disburse_verify.isoformat() if self.disburse_verify else None,
'reference': self.reference, 'reference': self.reference,
'balance': self.balance, 'balance': self.balance,
'tenor': self.tenor, 'tenor': self.tenor,
'totalPenalCharge': self.total_penal_charge, 'totalPenalCharge': self.total_penal_charge,
'lastPenalDate': self.last_penal_date 'lastPenalDate': self.last_penal_date
} }
@classmethod @classmethod
def get_loan_by_transaction_id(cls, transaction_id): def get_loan_by_transaction_id(cls, transaction_id):
return cls.query.filter_by(transaction_id=transaction_id).first() return cls.query.filter_by(transaction_id=transaction_id).first()
#update loan status @classmethod
@classmethod def get_loan_by_loan_id(cls, loan_id):
def update_status(cls, loan_id, status): return cls.query.filter_by(id=loan_id).first()
loan = cls.query.get(loan_id)
if loan: @classmethod
loan.status = status def set_disbursement_date(cls, loan_id, customer_id):
db.session.commit() """
return loan.to_dict() Update the disburse date of the loan with the given loan_id.
else: """
raise ValueError(f"Loan with ID {loan_id} not found.") # Retrieve loan
loan = cls.query.get(loan_id)
if not loan:
@classmethod raise ValueError(f"Loan with ID {loan_id} does not exist.")
def get_loan_by_loan_id(cls, loan_id):
return cls.query.filter_by(id=loan_id).first() # Check if customer_id matches
if loan.customer_id != customer_id:
@classmethod raise ValueError(f"Customer ID {customer_id} does not match the loan's customer ID.")
def set_disbursement_date(cls, loan_id, customer_id):
""" current_time = datetime.now()
Update the disburse date of the loan with the given loan_id. logger.info(f"What is now ======= ==== ==> : {current_time}")
""" # Update loan disburse_date
# Retrieve loan loan.disburse_date = current_time
loan = cls.query.get(loan_id)
# Commit changes to database
if not loan: try:
raise ValueError(f"Loan with ID {loan_id} does not exist.") logger.info(f"Updating disburse date for loan ID {loan_id} to {current_time}")
db.session.commit()
# Check if customer_id matches except Exception as e:
if loan.customer_id != customer_id: db.session.rollback()
raise ValueError(f"Customer ID {customer_id} does not match the loan's customer ID.") logger.error(f"Failed to update disburse date: {e}")
raise
current_time = datetime.now() @classmethod
logger.info(f"What is now ======= ==== ==> : {current_time}") def set_disburse_verify_date(cls, loan_id, customer_id):
# Update loan disburse_date """
loan.disburse_date = current_time Update the disburse verify date of the loan with the given loan_id.
"""
# Commit changes to database # Retrieve loan
try: loan = cls.query.get(loan_id)
logger.info(f"Updating disburse date for loan ID {loan_id} to {current_time}")
db.session.commit() if not loan:
except Exception as e: raise ValueError(f"Loan with ID {loan_id} does not exist.")
db.session.rollback()
logger.error(f"Failed to update disburse date: {e}") # Check if customer_id matches
raise if loan.customer_id != customer_id:
@classmethod raise ValueError(f"Customer ID {customer_id} does not match the loan's customer ID.")
def set_disburse_verify_date(cls, loan_id, customer_id):
""" current_time = datetime.now()
Update the disburse verify date of the loan with the given loan_id. logger.info(f"What is now ======= ==== ==> : {current_time}")
""" # Update loan verify_date
# Retrieve loan loan.disburse_verify = current_time
loan = cls.query.get(loan_id)
# Commit changes to database
if not loan: try:
raise ValueError(f"Loan with ID {loan_id} does not exist.") logger.info(f"Updating disburse verify date for loan ID {loan_id} to {current_time}")
db.session.commit()
# Check if customer_id matches except Exception as e:
if loan.customer_id != customer_id: db.session.rollback()
raise ValueError(f"Customer ID {customer_id} does not match the loan's customer ID.") logger.error(f"Failed to update disburse verify date: {e}")
raise
current_time = datetime.now()
logger.info(f"What is now ======= ==== ==> : {current_time}") @classmethod
# Update loan verify_date def set_disbursement_message(cls, loan_id, description):
loan.disburse_verify = current_time """
Update the disburse result and description of the loan with the given loan_id.
# Commit changes to database """
try: # Retrieve loan
logger.info(f"Updating disburse verify date for loan ID {loan_id} to {current_time}") loan = cls.query.get(loan_id)
db.session.commit()
except Exception as e: if not loan:
db.session.rollback() raise ValueError(f"Loan with ID {loan_id} does not exist.")
logger.error(f"Failed to update disburse verify date: {e}")
raise # Update disburse description only
loan.disburse_description = description
@classmethod
def set_disbursement_message(cls, loan_id, description): # Commit changes to database
""" try:
Update the disburse result and description of the loan with the given loan_id. logger.info(f"Updating disburse result for loan ID {loan_id} with description {description}")
""" db.session.commit()
# Retrieve loan except Exception as e:
loan = cls.query.get(loan_id) db.session.rollback()
logger.error(f"Failed to update disbursement result: {e}")
if not loan: raise
raise ValueError(f"Loan with ID {loan_id} does not exist.")
# Update disburse description only @classmethod
loan.disburse_description = description def set_disbursement_result(cls, loan_id, result, description):
"""
# Commit changes to database Update the disburse result and description of the loan with the given loan_id.
try: """
logger.info(f"Updating disburse result for loan ID {loan_id} with description {description}") # Retrieve loan
db.session.commit() loan = cls.query.get(loan_id)
except Exception as e:
db.session.rollback() if not loan:
logger.error(f"Failed to update disbursement result: {e}") raise ValueError(f"Loan with ID {loan_id} does not exist.")
raise
# Update disburse result and description
loan.disburse_result = result
@classmethod loan.disburse_description = description
def set_disbursement_result(cls, loan_id, result, description):
""" # Commit changes to database
Update the disburse result and description of the loan with the given loan_id. try:
""" logger.info(f"Updating disburse result for loan ID {loan_id} to {result} with description {description}")
# Retrieve loan db.session.commit()
loan = cls.query.get(loan_id) except Exception as e:
db.session.rollback()
if not loan: logger.error(f"Failed to update disbursement result: {e}")
raise ValueError(f"Loan with ID {loan_id} does not exist.") raise
# Update disburse result and description
loan.disburse_result = result @classmethod
loan.disburse_description = description def set_disburse_verify_result(cls, loan_id, result, description):
"""
# Commit changes to database Update the verify result and description of the loan with the given loan_id.
try: """
logger.info(f"Updating disburse result for loan ID {loan_id} to {result} with description {description}") # Retrieve loan
db.session.commit() loan = cls.query.get(loan_id)
except Exception as e:
db.session.rollback() if not loan:
logger.error(f"Failed to update disbursement result: {e}") raise ValueError(f"Loan with ID {loan_id} does not exist.")
raise
# Update disburse result and description
loan.verify_result = result
@classmethod loan.verify_description = description
def set_disburse_verify_result(cls, loan_id, result, description):
""" # Commit changes to database
Update the verify result and description of the loan with the given loan_id. try:
""" logger.info(f"Updating verify result for loan ID {loan_id} to {result} with description {description}")
# Retrieve loan db.session.commit()
loan = cls.query.get(loan_id) except Exception as e:
db.session.rollback()
if not loan: logger.error(f"Failed to update verify result: {e}")
raise ValueError(f"Loan with ID {loan_id} does not exist.") raise
@classmethod
# Update disburse result and description def get_latest_loan_without_disburse_date(cls):
loan.verify_result = result """
loan.verify_description = description Get the latest loan without a disbursement date.
"""
# Commit changes to database logger.info("Fetching latest loan without disburse date")
try:
logger.info(f"Updating verify result for loan ID {loan_id} to {result} with description {description}") try:
db.session.commit() return cls.query.filter(
except Exception as e: cls.disburse_date.is_(None)
db.session.rollback() ).order_by(cls.created_at.desc()).first()
logger.error(f"Failed to update verify result: {e}") except Exception as e:
raise logger.error(f"Error fetching latest loan without disburse date: {e}")
@classmethod raise
def get_latest_loan_without_disburse_date(cls):
""" @classmethod
Get the latest loan without a disbursement date. def get_latest_loan_with_disburse_date(cls):
""" """
logger.info("Fetching latest loan without disburse date") Get the latest loan with a disbursement date and no verification date.
"""
try: return cls.query.filter(
return cls.query.filter( cls.disburse_date.isnot(None),
cls.disburse_date.is_(None) cls.disburse_verify.is_(None)
).order_by(cls.created_at.desc()).first() ).order_by(cls.created_at.desc()).first()
except Exception as e:
logger.error(f"Error fetching latest loan without disburse date: {e}") @classmethod
raise def get_customer_loans(cls, customer_id):
"""
@classmethod Get customer's active loans and sum by customer_id.
def get_failed_disbursements(cls): """
""" customer_loans = cls.query.filter_by( customer_id = customer_id).all()
Get all loans with failed disbursement. if not customer_loans:
""" raise ValueError(f"Customer with Id {customer_id} does not have any loan.")
try:
last_time_interval = settings.FAILED_RETRY_TIME_INTERVAL_SECONDS or 0 total_amount = (
failed_loans = cls.query.filter( cls.query.with_entities(func.coalesce(func.sum(cls.balance), 0.0))
cls.disburse_date.is_(None), .filter_by(customer_id=customer_id)
cls.created_at >= datetime.utcnow() - timedelta(seconds=last_time_interval) .scalar()
).all() )
if not failed_loans: logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}")
logger.info("No failed disbursements found.")
return [] return customer_loans, total_amount
@classmethod
logger.info(f"Found {len(failed_loans)} failed disbursements.") def get_customer_active_loans(cls, customer_id):
return failed_loans """
Get customer's active loans and sum by customer_id.
except Exception as e: """
logger.error(f"Error fetching failed disbursements: {e}") customer_loans = cls.query.filter(
return [] cls.customer_id == customer_id,
cls.status != 'repaid'
@classmethod ).all()
def get_latest_loan_with_disburse_date(cls):
""" if not customer_loans:
Get the latest loan with a disbursement date and no verification date. raise ValueError(f"Customer with Id {customer_id} does not have any active loan.")
"""
return cls.query.filter( total_amount = (
cls.disburse_date.isnot(None), cls.query
cls.disburse_verify.is_(None) .with_entities(func.coalesce(func.sum(cls.balance), 0.0))
).order_by(cls.created_at.desc()).first() .filter(
cls.customer_id == customer_id,
@classmethod cls.status != 'repaid'
def get_customer_loans(cls, customer_id): )
""" .scalar()
Get customer's active loans and sum by customer_id. )
"""
customer_loans = cls.query.filter_by( customer_id = customer_id).all() logger.info(f"Found {len(customer_loans)} active loans for customer ID: {customer_id} with total amount: {total_amount}")
if not customer_loans:
raise ValueError(f"Customer with Id {customer_id} does not have any loan.") return customer_loans, total_amount
total_amount = (
cls.query.with_entities(func.coalesce(func.sum(cls.balance), 0.0)) @classmethod
.filter_by(customer_id=customer_id) def update_status(cls, loan_id, status):
.scalar() """
) Update the status of the loan record with the given loan_id.
"""
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}") try:
# Retrieve loan record
return customer_loans, total_amount loan = cls.query.get(loan_id)
@classmethod
def get_customer_active_loans(cls, customer_id): if not loan:
""" raise ValueError(f"Loan with ID {loan_id} does not exist.")
Get customer's active loans and sum by customer_id.
""" if loan.status == status:
customer_loans = cls.query.filter( return loan.to_dict() # Still return the current state if no change
cls.customer_id == customer_id,
cls.status != 'repaid' # Update status and timestamp
).all() loan.status = status
loan.updated_at = datetime.now(timezone.utc)
if not customer_loans: db.session.commit()
raise ValueError(f"Customer with Id {customer_id} does not have any active loan.")
logger.info("Loan status updated and committed.")
total_amount = ( return loan.to_dict()
cls.query
.with_entities(func.coalesce(func.sum(cls.balance), 0.0)) except Exception as e:
.filter( db.session.rollback()
cls.customer_id == customer_id, logger.error(f"Error updating loan status: {e}")
cls.status != 'repaid' raise Exception(f"Error updating loan status: {str(e)}")
)
.scalar()
) @classmethod
def update_loan_balance(cls, loan_id, amount_collected):
logger.info(f"Found {len(customer_loans)} active loans for customer ID: {customer_id} with total amount: {total_amount}") """
Update the balance of a loan after successful repayment.
return customer_loans, total_amount """
try:
# Fetch the loan record
@classmethod loan = cls.query.get(loan_id)
def update_status(cls, loan_id, status):
""" if not loan:
Update the status of the loan record with the given loan_id. raise ValueError(f"Loan with ID {loan_id} does not exist.")
"""
try: # Convert to Decimal and round to 2 decimal places
# Retrieve loan record amount_collected = Decimal(str(amount_collected)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
loan = cls.query.get(loan_id) balance = Decimal(str(loan.balance or 0)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
if not loan: # Ensure valid repayment amount
raise ValueError(f"Loan with ID {loan_id} does not exist.") if amount_collected <= Decimal("0.00"):
logger.info(f"Repayment amount is less than or equal to 0: {amount_collected}. Must be greater than 0.00")
if loan.status == status: if balance <= Decimal("0.00"):
return loan.to_dict() # Still return the current state if no change raise ValueError("There is no balance for this loan.")
if amount_collected > balance:
# Update status and timestamp # allow tiny rounding diff
loan.status = status if abs(amount_collected - balance) <= Decimal("0.01"):
loan.updated_at = datetime.now(timezone.utc) amount_collected = balance
db.session.commit() else:
raise ValueError("Repayment amount exceeds current loan balance.")
logger.info("Loan status updated and committed.")
return loan.to_dict()
# Deduct the amount from the current balance
except Exception as e: new_balance = balance - amount_collected
db.session.rollback() loan.balance = float(new_balance)
logger.error(f"Error updating loan status: {e}") loan.updated_at = datetime.now(timezone.utc)
raise Exception(f"Error updating loan status: {str(e)}") db.session.commit()
logger.info(f"Loan balance updated for loan ID {loan_id}. New balance: {loan.balance}")
@classmethod return loan.to_dict()
def update_loan_balance(cls, loan_id, amount_collected):
""" except Exception as e:
Update the balance of a loan after successful repayment. db.session.rollback()
""" logger.error(f"Error updating loan balance: {e}")
try: raise Exception(f"Error updating loan balance: {str(e)}")
# Fetch the loan record
loan = cls.query.get(loan_id) @classmethod
def get_overdue_loans(cls):
if not loan: """
raise ValueError(f"Loan with ID {loan_id} does not exist.") Get all overdue loans.
"""
# Convert to Decimal and round to 2 decimal places try:
amount_collected = Decimal(str(amount_collected)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) overdue_loans = cls.query.filter(
balance = Decimal(str(loan.balance or 0)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) cls.due_date < datetime.now(timezone.utc),
cls.status != 'repaid'
# Ensure valid repayment amount ).all()
if amount_collected <= Decimal("0.00"):
logger.info(f"Repayment amount is less than or equal to 0: {amount_collected}. Must be greater than 0.00") if not overdue_loans:
if balance <= Decimal("0.00"): logger.info("No overdue loans found.")
raise ValueError("There is no balance for this loan.") return []
if amount_collected > balance:
# allow tiny rounding diff logger.info(f"Found {len(overdue_loans)} overdue loans.")
if abs(amount_collected - balance) <= Decimal("0.01"): return overdue_loans
amount_collected = balance except Exception as e:
else: logger.error(f"Error fetching overdue loans: {e}")
raise ValueError("Repayment amount exceeds current loan balance.") return []
@classmethod
# Deduct the amount from the current balance def apply_penal_to_loan(cls, loan_id, penal_amount):
new_balance = balance - amount_collected
loan.balance = float(new_balance) loan = cls.query.get(loan_id)
loan.updated_at = datetime.now(timezone.utc)
db.session.commit() if not loan:
raise ValueError("Loan not found")
logger.info(f"Loan balance updated for loan ID {loan_id}. New balance: {loan.balance}") penal_amount = Decimal(str(penal_amount))
return loan.to_dict()
loan.total_penal_charge = Decimal(str(loan.total_penal_charge or 0)) + penal_amount
except Exception as e: loan.last_penal_date = datetime.now(timezone.utc)
db.session.rollback()
logger.error(f"Error updating loan balance: {e}") db.session.commit()
raise Exception(f"Error updating loan balance: {str(e)}")
@classmethod
def get_overdue_loans(cls):
"""
Get all overdue loans.
"""
try:
overdue_loans = cls.query.filter(
cls.due_date < datetime.now(timezone.utc),
cls.status != 'repaid'
).all()
if not overdue_loans:
logger.info("No overdue loans found.")
return []
logger.info(f"Found {len(overdue_loans)} overdue loans.")
return overdue_loans
except Exception as e:
logger.error(f"Error fetching overdue loans: {e}")
return []
@classmethod
def apply_penal_to_loan(cls, loan_id, penal_amount):
loan = cls.query.get(loan_id)
if not loan:
raise ValueError("Loan not found")
penal_amount = Decimal(str(penal_amount))
loan.total_penal_charge = Decimal(str(loan.total_penal_charge or 0)) + penal_amount
loan.last_penal_date = datetime.now(timezone.utc)
db.session.commit()
+126 -126
View File
@@ -1,127 +1,127 @@
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
from os.path import devnull from os.path import devnull
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from app.extensions import db from app.extensions import db
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
class LoanCharge(db.Model): class LoanCharge(db.Model):
__tablename__ = 'loan_charges' __tablename__ = 'loan_charges'
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
loan_id = db.Column(db.Integer, nullable=False) loan_id = db.Column(db.Integer, nullable=False)
transaction_id = db.Column(db.String(50), nullable=True) transaction_id = db.Column(db.String(50), nullable=True)
code = db.Column(db.String(50), nullable=False) code = db.Column(db.String(50), nullable=False)
amount = db.Column(db.Float, default=0.0) amount = db.Column(db.Float, default=0.0)
percent = db.Column(db.Float, default=0.0) percent = db.Column(db.Float, default=0.0)
description = db.Column(db.Text, nullable=True) description = db.Column(db.Text, nullable=True)
due = db.Column(db.Integer, nullable=False) due = db.Column(db.Integer, nullable=False)
due_date = db.Column(db.DateTime, nullable=True) due_date = db.Column(db.DateTime, nullable=True)
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))
loan = relationship( loan = relationship(
"Loan", "Loan",
primaryjoin="LoanCharge.loan_id == Loan.id", primaryjoin="LoanCharge.loan_id == Loan.id",
foreign_keys=[loan_id], foreign_keys=[loan_id],
back_populates="loan_charges", back_populates="loan_charges",
) )
def __repr__(self): def __repr__(self):
return f"<LoanCharge {self.id} - Loan {self.loan_id} - {self.code}>" return f"<LoanCharge {self.id} - Loan {self.loan_id} - {self.code}>"
def to_dict(self): def to_dict(self):
""" """
Convert the Loan charge object to a dictionary format for JSON serialization. Convert the Loan charge object to a dictionary format for JSON serialization.
""" """
return { return {
'id': self.id, 'id': self.id,
'loanId': self.loan_id, 'loanId': self.loan_id,
'transactionId': self.transaction_id, 'transactionId': self.transaction_id,
'code': self.code, 'code': self.code,
'amount': self.amount, 'amount': self.amount,
'percent': self.percent, 'percent': self.percent,
'description': self.description, 'description': self.description,
'due': self.due 'due': self.due
} }
#get last penal #get last penal
@classmethod @classmethod
def get_last_penal_no(cls, loan_id): def get_last_penal_no(cls, loan_id):
""" """
Returns the last penal number created for a loan. Returns the last penal number created for a loan.
Example: Example:
PENAL1 -> returns 1 PENAL1 -> returns 1
PENAL3 -> returns 3 PENAL3 -> returns 3
If none exists, returns 0. If none exists, returns 0.
""" """
last_penal = ( last_penal = (
cls.query cls.query
.filter(cls.loan_id == loan_id) .filter(cls.loan_id == loan_id)
.filter(cls.code.like("PENAL%")) .filter(cls.code.like("PENAL%"))
.order_by(cls.id.desc()) .order_by(cls.id.desc())
.first() .first()
) )
if not last_penal: if not last_penal:
return 0 return 0
try: try:
return int(last_penal.code.replace("PENAL", "")) return int(last_penal.code.replace("PENAL", ""))
except ValueError: except ValueError:
return 0 return 0
@classmethod @classmethod
def get_penal_charges_by_loan_id(cls, loan_id): def get_penal_charges_by_loan_id(cls, loan_id):
""" """
Returns all penal charges for a specific loan. Returns all penal charges for a specific loan.
""" """
return cls.query.filter( return cls.query.filter(
cls.loan_id == loan_id, cls.loan_id == loan_id,
cls.code.like("PENAL%") cls.code.like("PENAL%")
).all() ).all()
@classmethod @classmethod
def get_loan_charge_by_debt_id(cls, debt_id): def get_loan_charge_by_debt_id(cls, debt_id):
return cls.query.filter_by(loan_id=debt_id) return cls.query.filter_by(loan_id=debt_id)
#create penal charge #create penal charge
@classmethod @classmethod
def create_penal_charges_for_loan(cls, loan_id, transaction_id, percent, penal_no, schedule_number, penal_amount=0.0): def create_penal_charges_for_loan(cls, loan_id, transaction_id, percent, penal_no, schedule_number, penal_amount=0.0):
""" """
Create a penal charge for a given loan and schedule. Create a penal charge for a given loan and schedule.
""" """
if loan_id is None: if loan_id is None:
raise ValueError("loan_id cannot be None") raise ValueError("loan_id cannot be None")
code = f"PENAL{penal_no:02d}-SCHEDULE{schedule_number:02d}" code = f"PENAL{penal_no:02d}-SCHEDULE{schedule_number:02d}"
# Check if this penal charge already exists # Check if this penal charge already exists
existing = cls.query.filter_by( existing = cls.query.filter_by(
loan_id=loan_id, loan_id=loan_id,
code=code code=code
).first() ).first()
if existing: if existing:
return existing return existing
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
penal_charge = cls( penal_charge = cls(
loan_id=loan_id, loan_id=loan_id,
transaction_id=transaction_id, transaction_id=transaction_id,
code=code, code=code,
amount=penal_amount, amount=penal_amount,
percent=percent, percent=percent,
description=f"Penal Charge {penal_no} for loan {loan_id} schedule {schedule_number}", description=f"Penal Charge {penal_no} for loan {loan_id} schedule {schedule_number}",
due=True, due=True,
due_date=now due_date=now
) )
try: try:
db.session.add(penal_charge) db.session.add(penal_charge)
db.session.commit() db.session.commit()
except IntegrityError as err: except IntegrityError as err:
db.session.rollback() db.session.rollback()
raise ValueError(f"Database integrity error: {err}") raise ValueError(f"Database integrity error: {err}")
return penal_charge return penal_charge
+347 -347
View File
@@ -1,348 +1,348 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from app.extensions import db from app.extensions import db
from app.utils.logger import logger from app.utils.logger import logger
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import or_ from sqlalchemy import or_
from app.enums.repayment_schedule_status import RepaymentScheduleStatus from app.enums.repayment_schedule_status import RepaymentScheduleStatus
from app.config import settings from app.config import settings
from decimal import Decimal, ROUND_HALF_UP from decimal import Decimal, ROUND_HALF_UP
# from dateutil.relativedelta import relativedelta # from dateutil.relativedelta import relativedelta
class LoanRepaymentSchedule(db.Model): class LoanRepaymentSchedule(db.Model):
__tablename__ = 'loan_repayment_schedules' __tablename__ = 'loan_repayment_schedules'
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
loan_id = db.Column(db.Integer, nullable=False) loan_id = db.Column(db.Integer, nullable=False)
transaction_id = db.Column(db.String(50), nullable=True) transaction_id = db.Column(db.String(50), nullable=True)
product_id = db.Column(db.String(20), nullable=True) product_id = db.Column(db.String(20), nullable=True)
installment_number = db.Column(db.Integer, nullable=False) installment_number = db.Column(db.Integer, nullable=False)
due_date = db.Column(db.DateTime, nullable=False) due_date = db.Column(db.DateTime, nullable=False)
installment_amount= db.Column(db.Float, default=0.0) installment_amount= db.Column(db.Float, default=0.0)
total_repayment_amount = db.Column(db.Float, default=0.0) total_repayment_amount = db.Column(db.Float, default=0.0)
paid = db.Column(db.Boolean, default=False) paid = db.Column(db.Boolean, default=False)
paid_at = db.Column(db.DateTime, nullable=True) paid_at = db.Column(db.DateTime, nullable=True)
due_process_date = db.Column(db.DateTime, nullable=True) due_process_date = db.Column(db.DateTime, nullable=True)
due_process_count = db.Column(db.Integer, default=0) due_process_count = db.Column(db.Integer, default=0)
paid_status = db.Column(db.String(20), nullable=True) paid_status = db.Column(db.String(20), nullable=True)
repay_description = db.Column(db.String(255), nullable=True) repay_description = db.Column(db.String(255), nullable=True)
partial_balance = db.Column(db.Float, default=0.0) partial_balance = db.Column(db.Float, default=0.0)
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))
penal_charge = db.Column(db.Float, default=0.0) penal_charge = db.Column(db.Float, default=0.0)
penal_count = db.Column(db.Integer, default=0) penal_count = db.Column(db.Integer, default=0)
last_penal_date = db.Column(db.DateTime, nullable=True) last_penal_date = db.Column(db.DateTime, nullable=True)
def to_dict(self): def to_dict(self):
return { return {
'id': self.id, 'id': self.id,
'loan_id': self.loan_id, 'loan_id': self.loan_id,
'product_id': self.product_id, 'product_id': self.product_id,
'transaction_id': self.transaction_id, 'transaction_id': self.transaction_id,
'installment_number': self.installment_number, 'installment_number': self.installment_number,
'due_date': self.due_date.isoformat() if self.due_date else None, 'due_date': self.due_date.isoformat() if self.due_date else None,
'installment_amount': self.installment_amount, 'installment_amount': self.installment_amount,
'total_repayment_amount': self.total_repayment_amount, 'total_repayment_amount': self.total_repayment_amount,
'paid': self.paid, 'paid': self.paid,
'due_process_date': self.due_process_date.isoformat() if self.due_process_date else None, 'due_process_date': self.due_process_date.isoformat() if self.due_process_date else None,
'due_process_count': self.due_process_count, 'due_process_count': self.due_process_count,
'paid_status': self.paid_status, 'paid_status': self.paid_status,
'repay_description': self.repay_description, 'repay_description': self.repay_description,
'partial_balance': self.partial_balance, 'partial_balance': self.partial_balance,
'paid_at': self.paid_at.isoformat() if self.paid_at else None, 'paid_at': self.paid_at.isoformat() if self.paid_at else None,
'created_at': self.created_at.isoformat() if self.created_at else None, 'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None, 'updated_at': self.updated_at.isoformat() if self.updated_at else None,
'penal_charge': self.penal_charge, 'penal_charge': self.penal_charge,
'penal_count': self.penal_count, 'penal_count': self.penal_count,
'last_penal_date': self.last_penal_date.isoformat() if self.last_penal_date else None 'last_penal_date': self.last_penal_date.isoformat() if self.last_penal_date else None
} }
def __repr__(self): def __repr__(self):
return f'<LoanRepaymentSchedule Loan:{self.loan_id} Installment:{self.installment_number}>' return f'<LoanRepaymentSchedule Loan:{self.loan_id} Installment:{self.installment_number}>'
@classmethod @classmethod
def get_repayment_schedule_by_loan_id(cls, loan_id, include_paid=True): def get_repayment_schedule_by_loan_id(cls, loan_id, include_paid=True):
""" """
Get repayment schedules by loan ID. Get repayment schedules by loan ID.
:param loan_id: Loan ID to filter by :param loan_id: Loan ID to filter by
:param include_paid: If True, include all schedules. If False, only unpaid ones. :param include_paid: If True, include all schedules. If False, only unpaid ones.
:return: List of repayment schedules ordered by due_date :return: List of repayment schedules ordered by due_date
""" """
try: try:
query = cls.query.filter_by(loan_id=loan_id) query = cls.query.filter_by(loan_id=loan_id)
if not include_paid: if not include_paid:
query = query.filter_by(paid=False) query = query.filter_by(paid=False)
schedules = query.order_by(cls.due_date.asc()).all() schedules = query.order_by(cls.due_date.asc()).all()
return schedules return schedules
except Exception as e: except Exception as e:
logger.error(f"Error fetching repayment schedules for loan {loan_id}: {e}") logger.error(f"Error fetching repayment schedules for loan {loan_id}: {e}")
raise raise
@classmethod @classmethod
def get_repayment_schedule_by_id_and_transaction_id(cls, id, transaction_id): def get_repayment_schedule_by_id_and_transaction_id(cls, id, transaction_id):
""" """
Get repayment schedule by ID and transaction ID Get repayment schedule by ID and transaction ID
""" """
try: try:
return cls.query.filter_by(id=id, transaction_id=transaction_id).first() return cls.query.filter_by(id=id, transaction_id=transaction_id).first()
except Exception as e: except Exception as e:
logger.error(f"Error fetching repayment schedule for id={id}, transaction_id={transaction_id}: {e}") logger.error(f"Error fetching repayment schedule for id={id}, transaction_id={transaction_id}: {e}")
return None return None
@classmethod @classmethod
def get_overdue_repayment_schedule(cls): def get_overdue_repayment_schedule(cls):
""" """
Get all overdue repayment schedules that are not repaid. Get all overdue repayment schedules that are not repaid.
""" """
try: try:
return cls.query.filter(cls.due_date < datetime.now(timezone.utc), cls.paid == False).order_by(cls.due_date.asc()).all() return cls.query.filter(cls.due_date < datetime.now(timezone.utc), cls.paid == False).order_by(cls.due_date.asc()).all()
except Exception as e: except Exception as e:
logger.error(f"Error fetching overdue repayment schedules: {e}") logger.error(f"Error fetching overdue repayment schedules: {e}")
return [] return []
@classmethod @classmethod
def get_active_overdue_repayment_schedule(cls): def get_active_overdue_repayment_schedule(cls):
""" """
Get all overdue repayment schedules that are active. Get all overdue repayment schedules that are active.
""" """
try: try:
return ( return (
cls.query cls.query
.filter( .filter(
cls.due_date < datetime.now(timezone.utc), cls.due_date < datetime.now(timezone.utc),
cls.paid_status == RepaymentScheduleStatus.ACTIVE cls.paid_status == RepaymentScheduleStatus.ACTIVE
) )
.order_by(cls.due_date.asc()) .order_by(cls.due_date.asc())
.all() .all()
) )
except Exception as e: except Exception as e:
logger.error(f"Error fetching active overdue repayment schedules: {e}") logger.error(f"Error fetching active overdue repayment schedules: {e}")
return [] return []
@classmethod @classmethod
def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days, limit=None): def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days, limit=None):
try: try:
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
grace_period_date = now - timedelta(days=grace_period_days) grace_period_date = now - timedelta(days=grace_period_days)
penal_interval = timedelta(days=settings.PENAL_CHARGE_INTERVAL_DAYS) penal_interval = timedelta(days=settings.PENAL_CHARGE_INTERVAL_DAYS)
return cls.query.filter( return cls.query.filter(
cls.due_date < grace_period_date, cls.due_date < grace_period_date,
cls.paid == False, cls.paid == False,
or_( or_(
cls.last_penal_date == None, # never penalized before cls.last_penal_date == None, # never penalized before
cls.last_penal_date < now - penal_interval cls.last_penal_date < now - penal_interval
) )
).order_by(cls.due_date.asc()).limit(limit).all() ).order_by(cls.due_date.asc()).limit(limit).all()
except Exception as e: except Exception as e:
logger.error(f"Error fetching overdue repayment schedules with grace period: {e}") logger.error(f"Error fetching overdue repayment schedules with grace period: {e}")
return [] return []
@classmethod @classmethod
def get_partially_paid_overdue_repayment_schedule(cls): def get_partially_paid_overdue_repayment_schedule(cls):
""" """
Get all overdue repayment schedules that are partially paid. Get all overdue repayment schedules that are partially paid.
""" """
try: try:
return ( return (
cls.query cls.query
.filter( .filter(
cls.due_date < datetime.now(timezone.utc), cls.due_date < datetime.now(timezone.utc),
cls.paid_status == RepaymentScheduleStatus.PARTIALLY_PAID cls.paid_status == RepaymentScheduleStatus.PARTIALLY_PAID
) )
.order_by(cls.due_date.asc()) .order_by(cls.due_date.asc())
.all() .all()
) )
except Exception as e: except Exception as e:
logger.error(f"Error fetching partially paid overdue repayment schedules: {e}") logger.error(f"Error fetching partially paid overdue repayment schedules: {e}")
return [] return []
@classmethod @classmethod
def get_repayment_schedule_by_transaction_id(cls, transaction_id): def get_repayment_schedule_by_transaction_id(cls, transaction_id):
""" """
Get repayment schedule by transaction ID Get repayment schedule by transaction ID
""" """
return cls.query.filter_by(transaction_id=transaction_id).all() return cls.query.filter_by(transaction_id=transaction_id).all()
@classmethod @classmethod
def update_repayment_schedule_description(cls, schedule_id, description): def update_repayment_schedule_description(cls, schedule_id, description):
""" """
Update the repayment description for a specific schedule. Update the repayment description for a specific schedule.
""" """
try: try:
schedule = cls.query.get(schedule_id) schedule = cls.query.get(schedule_id)
if not schedule: if not schedule:
raise ValueError(f"Schedule with ID {schedule_id} does not exist.") raise ValueError(f"Schedule with ID {schedule_id} does not exist.")
schedule.repay_description = description schedule.repay_description = description
schedule.updated_at = datetime.now(timezone.utc) schedule.updated_at = datetime.now(timezone.utc)
db.session.commit() db.session.commit()
logger.info(f"Updated repayment description for schedule ID {schedule_id}") logger.info(f"Updated repayment description for schedule ID {schedule_id}")
return schedule.to_dict() return schedule.to_dict()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error updating repayment description for schedule {schedule_id}: {e}") logger.error(f"Error updating repayment description for schedule {schedule_id}: {e}")
raise raise
@classmethod @classmethod
def update_repayment_schedule_status(cls, schedule_id): def update_repayment_schedule_status(cls, schedule_id):
""" """
Mark a repayment schedule as fully repaid when the parent loan is fully repaid. Mark a repayment schedule as fully repaid when the parent loan is fully repaid.
This function does not take amount_collected because the loan is already cleared. This function does not take amount_collected because the loan is already cleared.
""" """
try: try:
# Fetch schedule # Fetch schedule
schedule = cls.query.get(schedule_id) schedule = cls.query.get(schedule_id)
if not schedule: if not schedule:
raise ValueError(f"Schedule with ID {schedule_id} does not exist.") raise ValueError(f"Schedule with ID {schedule_id} does not exist.")
# Force balance to 0 # Force balance to 0
schedule.partial_balance = 0.0 schedule.partial_balance = 0.0
schedule.paid_status = RepaymentScheduleStatus.REPAID schedule.paid_status = RepaymentScheduleStatus.REPAID
schedule.paid = True schedule.paid = True
schedule.paid_at = datetime.now(timezone.utc) schedule.paid_at = datetime.now(timezone.utc)
# Track due processing # Track due processing
if schedule.due_process_count is None: if schedule.due_process_count is None:
schedule.due_process_count = 0 schedule.due_process_count = 0
schedule.due_process_count += 1 schedule.due_process_count += 1
schedule.due_process_date = datetime.now(timezone.utc) schedule.due_process_date = datetime.now(timezone.utc)
# Update timestamp # Update timestamp
schedule.updated_at = datetime.now(timezone.utc) schedule.updated_at = datetime.now(timezone.utc)
# Commit changes # Commit changes
db.session.commit() db.session.commit()
logger.info(f"Schedule {schedule_id} marked as REPAID since parent loan is fully repaid.") logger.info(f"Schedule {schedule_id} marked as REPAID since parent loan is fully repaid.")
return schedule.to_dict() return schedule.to_dict()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error updating repayment schedule {schedule_id} after loan repayment: {e}") logger.error(f"Error updating repayment schedule {schedule_id} after loan repayment: {e}")
raise raise
@classmethod @classmethod
def update_repayment_schedule_status_to_active(cls, schedule_id): def update_repayment_schedule_status_to_active(cls, schedule_id):
""" """
Update repayment schedule status to ACTIVE. Update repayment schedule status to ACTIVE.
""" """
try: try:
schedule = cls.query.get(schedule_id) schedule = cls.query.get(schedule_id)
if not schedule: if not schedule:
raise ValueError(f"Schedule with ID {schedule_id} does not exist.") raise ValueError(f"Schedule with ID {schedule_id} does not exist.")
schedule.paid_status = RepaymentScheduleStatus.ACTIVE schedule.paid_status = RepaymentScheduleStatus.ACTIVE
schedule.updated_at = datetime.now(timezone.utc) schedule.updated_at = datetime.now(timezone.utc)
db.session.commit() db.session.commit()
logger.info(f"Updated repayment schedule ID {schedule_id} status to ACTIVE") logger.info(f"Updated repayment schedule ID {schedule_id} status to ACTIVE")
return schedule.to_dict() return schedule.to_dict()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error updating repayment schedule status for schedule {schedule_id}: {e}") logger.error(f"Error updating repayment schedule status for schedule {schedule_id}: {e}")
raise raise
@classmethod @classmethod
def update_repayment_schedule_balance(cls, schedule_id, amount_collected): def update_repayment_schedule_balance(cls, schedule_id, amount_collected):
""" """
Apply repayment to a loan schedule: Apply repayment to a loan schedule:
- Deduct from partial balance if partially paid. - Deduct from partial balance if partially paid.
- Otherwise deduct from installment amount. - Otherwise deduct from installment amount.
- Update partial balance, paid status, timestamps, etc. - Update partial balance, paid status, timestamps, etc.
""" """
try: try:
schedule = cls.query.get(schedule_id) schedule = cls.query.get(schedule_id)
if not schedule: if not schedule:
raise ValueError(f"Schedule with ID {schedule_id} does not exist.") raise ValueError(f"Schedule with ID {schedule_id} does not exist.")
# Normalize amount # Normalize amount
amount_collected = Decimal(str(amount_collected)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) amount_collected = Decimal(str(amount_collected)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
if amount_collected <= Decimal("0.00"): if amount_collected <= Decimal("0.00"):
logger.info("Repayment amount must be greater than zero.") logger.info("Repayment amount must be greater than zero.")
return schedule.to_dict() return schedule.to_dict()
# Determine current balance # Determine current balance
if schedule.paid_status == RepaymentScheduleStatus.PARTIALLY_PAID and (schedule.partial_balance or 0) > 0: if schedule.paid_status == RepaymentScheduleStatus.PARTIALLY_PAID and (schedule.partial_balance or 0) > 0:
balance = Decimal(str(schedule.partial_balance)) balance = Decimal(str(schedule.partial_balance))
else: else:
balance = Decimal(str(schedule.installment_amount)) balance = Decimal(str(schedule.installment_amount))
# Deduct repayment # Deduct repayment
new_balance = balance - amount_collected new_balance = balance - amount_collected
if new_balance < 0: if new_balance < 0:
new_balance = Decimal("0.00") # prevent negatives new_balance = Decimal("0.00") # prevent negatives
# Update schedule fields # Update schedule fields
schedule.partial_balance = float(new_balance) if new_balance > 0 else 0.0 schedule.partial_balance = float(new_balance) if new_balance > 0 else 0.0
schedule.updated_at = datetime.now(timezone.utc) schedule.updated_at = datetime.now(timezone.utc)
if new_balance == 0: if new_balance == 0:
schedule.paid_status = RepaymentScheduleStatus.REPAID schedule.paid_status = RepaymentScheduleStatus.REPAID
schedule.paid = True schedule.paid = True
schedule.paid_at = datetime.now(timezone.utc) schedule.paid_at = datetime.now(timezone.utc)
else: else:
schedule.paid_status = RepaymentScheduleStatus.PARTIALLY_PAID schedule.paid_status = RepaymentScheduleStatus.PARTIALLY_PAID
schedule.paid = False # not fully paid yet schedule.paid = False # not fully paid yet
# Track due processing # Track due processing
if schedule.due_process_count is None: if schedule.due_process_count is None:
schedule.due_process_count = 0 schedule.due_process_count = 0
schedule.due_process_count += 1 schedule.due_process_count += 1
schedule.due_process_date = datetime.now(timezone.utc) schedule.due_process_date = datetime.now(timezone.utc)
# Commit # Commit
db.session.commit() db.session.commit()
logger.info(f"Repayment applied for schedule ID {schedule_id}. Remaining balance: {schedule.partial_balance}") logger.info(f"Repayment applied for schedule ID {schedule_id}. Remaining balance: {schedule.partial_balance}")
return schedule.to_dict() return schedule.to_dict()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error applying repayment for schedule {schedule_id}: {e}") logger.error(f"Error applying repayment for schedule {schedule_id}: {e}")
raise raise
from decimal import Decimal from decimal import Decimal
@classmethod @classmethod
def apply_penal_to_schedule(cls, schedule_id, penal_amount): def apply_penal_to_schedule(cls, schedule_id, penal_amount):
schedule = cls.query.get(schedule_id) schedule = cls.query.get(schedule_id)
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
penal_amount = Decimal(str(penal_amount)) penal_amount = Decimal(str(penal_amount))
current_penal = Decimal(str(schedule.penal_charge)) if schedule.penal_charge else Decimal("0") current_penal = Decimal(str(schedule.penal_charge)) if schedule.penal_charge else Decimal("0")
schedule.penal_count = (schedule.penal_count or 0) + 1 schedule.penal_count = (schedule.penal_count or 0) + 1
schedule.penal_charge = current_penal + penal_amount schedule.penal_charge = current_penal + penal_amount
schedule.last_penal_date = now schedule.last_penal_date = now
schedule.due_process_date = now schedule.due_process_date = now
schedule.updated_at = now schedule.updated_at = now
db.session.commit() db.session.commit()
# Calculate penal charge # Calculate penal charge
@classmethod @classmethod
def calculate_penal_charge(cls, schedule): def calculate_penal_charge(cls, schedule):
if schedule.paid_status == RepaymentScheduleStatus.PARTIALLY_PAID: if schedule.paid_status == RepaymentScheduleStatus.PARTIALLY_PAID:
outstanding = Decimal(str(schedule.partial_balance)) outstanding = Decimal(str(schedule.partial_balance))
else: else:
outstanding = Decimal(str(schedule.installment_amount)) outstanding = Decimal(str(schedule.installment_amount))
rate = Decimal(str(settings.PENAL_CHARGE_PERCENTAGE)) / 100 rate = Decimal(str(settings.PENAL_CHARGE_PERCENTAGE)) / 100
penal_charge = (outstanding * rate).quantize( penal_charge = (outstanding * rate).quantize(
Decimal("0.01"), Decimal("0.01"),
rounding=ROUND_HALF_UP rounding=ROUND_HALF_UP
) )
return penal_charge return penal_charge
+259 -259
View File
@@ -1,260 +1,260 @@
from app.extensions import db from app.extensions import db
from datetime import datetime, timezone from datetime import datetime, timezone
from app.utils.logger import logger from app.utils.logger import logger
from app.enums.loan_status import LoanStatus from app.enums.loan_status import LoanStatus
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
class Repayment(db.Model): class Repayment(db.Model):
__tablename__ = "repayments" __tablename__ = "repayments"
id = db.Column( id = db.Column(
db.Integer, db.Integer,
primary_key=True, primary_key=True,
autoincrement=True, autoincrement=True,
) )
loan_id = db.Column(db.String(50), nullable=False) loan_id = db.Column(db.String(50), nullable=False)
customer_id = db.Column(db.String(50), nullable=False) customer_id = db.Column(db.String(50), nullable=False)
product_id = db.Column(db.String(20), nullable=True) product_id = db.Column(db.String(20), nullable=True)
transaction_id = db.Column(db.String(50), nullable=False) transaction_id = db.Column(db.String(50), nullable=False)
initiated_by = db.Column(db.String(50), nullable=True) initiated_by = db.Column(db.String(50), nullable=True)
salary_amount = db.Column(db.Float, nullable=True, default=0.0) salary_amount = db.Column(db.Float, nullable=True, default=0.0)
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))
repay_date = db.Column(db.DateTime, nullable=True) repay_date = db.Column(db.DateTime, nullable=True)
verify_date = db.Column(db.DateTime, nullable=True) verify_date = db.Column(db.DateTime, nullable=True)
repay_result = db.Column(db.String(10), nullable=True) repay_result = db.Column(db.String(10), nullable=True)
repay_description = db.Column(db.String(100), nullable=True) repay_description = db.Column(db.String(100), nullable=True)
verify_result = db.Column(db.String(10), nullable=True) verify_result = db.Column(db.String(10), nullable=True)
verify_description = db.Column(db.String(100), nullable=True) verify_description = db.Column(db.String(100), nullable=True)
def __repr__(self): def __repr__(self):
return f'<Repayment {self.id}>' return f'<Repayment {self.id}>'
def to_dict(self): def to_dict(self):
""" """
Convert the Repayment object to a dictionary format for JSON serialization. Convert the Repayment object to a dictionary format for JSON serialization.
""" """
return { return {
'Id': self.id, 'Id': self.id,
"customerId": self.customer_id, "customerId": self.customer_id,
'loanId': self.loan_id, 'loanId': self.loan_id,
'productId': self.product_id, 'productId': self.product_id,
'repayResult': self.repay_result, 'repayResult': self.repay_result,
'repayDescription': self.repay_description, 'repayDescription': self.repay_description,
'verifyResult': self.verify_result, 'verifyResult': self.verify_result,
'verifyDescription': self.verify_description, 'verifyDescription': self.verify_description,
'transactionId': self.transaction_id, 'transactionId': self.transaction_id,
'initiatedBy':self.initiated_by, 'initiatedBy':self.initiated_by,
'salaryAmount':self.salary_amount, 'salaryAmount':self.salary_amount,
'repayDate': self.repay_date.isoformat() if self.repay_date else None, 'repayDate': self.repay_date.isoformat() if self.repay_date else None,
'VerifyDate': self.verify_date.isoformat() if self.verify_date else None, 'VerifyDate': self.verify_date.isoformat() if self.verify_date else None,
} }
@classmethod @classmethod
def create_repayment(cls, repayment_data): def create_repayment(cls, repayment_data):
if repayment_data["LoanStatus"] not in [LoanStatus.ACTIVE, LoanStatus.START_REPAY,LoanStatus.ACTIVE_PARTIAL]: if repayment_data["LoanStatus"] not in [LoanStatus.ACTIVE, LoanStatus.START_REPAY,LoanStatus.ACTIVE_PARTIAL]:
raise ValueError(f"Repayment cannot be processed. Loan status: ({repayment_data['LoanStatus']})") raise ValueError(f"Repayment cannot be processed. Loan status: ({repayment_data['LoanStatus']})")
repayment = cls( repayment = cls(
customer_id=repayment_data["customerId"], customer_id=repayment_data["customerId"],
loan_id=repayment_data["loanId"], loan_id=repayment_data["loanId"],
product_id=repayment_data["productId"], product_id=repayment_data["productId"],
transaction_id=repayment_data["transactionId"], transaction_id=repayment_data["transactionId"],
created_at=datetime.now(timezone.utc), created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc), updated_at=datetime.now(timezone.utc),
initiated_by= repayment_data["initiatedBy"], initiated_by= repayment_data["initiatedBy"],
salary_amount=repayment_data["salaryAmount"] salary_amount=repayment_data["salaryAmount"]
) )
try: try:
db.session.add(repayment) db.session.add(repayment)
db.session.commit() db.session.commit()
logger.info("Repayment record committed.") logger.info("Repayment record committed.")
return repayment return repayment
except IntegrityError as err: except IntegrityError as err:
logger.error(f"Database integrity error: {err}") logger.error(f"Database integrity error: {err}")
return {"error": "Integrity error", "details": str(err)} return {"error": "Integrity error", "details": str(err)}
@classmethod @classmethod
def add_repayment(cls, data: dict): def add_repayment(cls, data: dict):
""" """
Create and persist a new repayment record. Create and persist a new repayment record.
""" """
logger.info(f"Received repayment data: {data}") logger.info(f"Received repayment data: {data}")
try: try:
new_repayment = cls( new_repayment = cls(
loan_id=data["loanId"], loan_id=data["loanId"],
customer_id=data["customerId"], customer_id=data["customerId"],
product_id=data.get("productId"), product_id=data.get("productId"),
transaction_id=data["transactionId"], transaction_id=data["transactionId"],
initiated_by=data.get("initiatedBy"), initiated_by=data.get("initiatedBy"),
salary_amount=float(data.get("salaryAmount", 0.0)), salary_amount=float(data.get("salaryAmount", 0.0)),
repay_date=( repay_date=(
datetime.strptime(data["repayDate"], "%Y-%m-%d") datetime.strptime(data["repayDate"], "%Y-%m-%d")
.replace(tzinfo=timezone.utc) .replace(tzinfo=timezone.utc)
if data.get("repayDate") if data.get("repayDate")
else None else None
), ),
repay_result=data.get("repayResult"), repay_result=data.get("repayResult"),
repay_description=data.get("repayDescription"), repay_description=data.get("repayDescription"),
verify_result=data.get("verifyResult"), verify_result=data.get("verifyResult"),
verify_description=data.get("verifyDescription"), verify_description=data.get("verifyDescription"),
verify_date=( verify_date=(
datetime.strptime(data["verifyDate"], "%Y-%m-%d") datetime.strptime(data["verifyDate"], "%Y-%m-%d")
.replace(tzinfo=timezone.utc) .replace(tzinfo=timezone.utc)
if data.get("verifyDate") if data.get("verifyDate")
else None else None
), ),
) )
db.session.add(new_repayment) db.session.add(new_repayment)
db.session.commit() db.session.commit()
logger.info("Repayment record committed.") logger.info("Repayment record committed.")
return new_repayment return new_repayment
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error adding repayment data: {e}") logger.error(f"Error adding repayment data: {e}")
raise raise
@classmethod @classmethod
def get_repayment_by_transaction_id(cls, transaction_id): def get_repayment_by_transaction_id(cls, transaction_id):
return cls.query.filter_by(transaction_id=transaction_id).first() return cls.query.filter_by(transaction_id=transaction_id).first()
@classmethod @classmethod
def get_repayment_by_id(cls, id): def get_repayment_by_id(cls, id):
return cls.query.filter_by(id=id).first() return cls.query.filter_by(id=id).first()
@classmethod @classmethod
def set_repay_date(cls, repayment_id, customer_id): def set_repay_date(cls, repayment_id, customer_id):
""" """
Update the repay date of the loan with the given loan_id. Update the repay date of the loan with the given loan_id.
""" """
# Retrieve repayment # Retrieve repayment
repayment = cls.query.get(repayment_id) repayment = cls.query.get(repayment_id)
if not repayment: if not repayment:
raise ValueError(f"repayment with ID {repayment_id} does not exist.") raise ValueError(f"repayment with ID {repayment_id} does not exist.")
# Check if customer_id matches # Check if customer_id matches
if repayment.customer_id != customer_id: if repayment.customer_id != customer_id:
raise ValueError(f"Customer ID {customer_id} does not match the repayment's customer ID.") raise ValueError(f"Customer ID {customer_id} does not match the repayment's customer ID.")
current_time = datetime.now() current_time = datetime.now()
logger.info(f"What is now ======= ==== ==> : {current_time}") logger.info(f"What is now ======= ==== ==> : {current_time}")
# Update repayment date # Update repayment date
repayment.repay_date = current_time repayment.repay_date = current_time
# Commit changes to database # Commit changes to database
try: try:
logger.info(f"Updating repay date for repayment ID {repayment_id} to {current_time}") logger.info(f"Updating repay date for repayment ID {repayment_id} to {current_time}")
db.session.commit() db.session.commit()
return repayment.to_dict() return repayment.to_dict()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Failed to update repay date: {e}") logger.error(f"Failed to update repay date: {e}")
raise e raise e
@classmethod @classmethod
def set_repay_verify_date(cls, repayment_id, customer_id): def set_repay_verify_date(cls, repayment_id, customer_id):
""" """
Update the repayment verify date of the loan with the given repayment_id. Update the repayment verify date of the loan with the given repayment_id.
""" """
# Retrieve repayment # Retrieve repayment
repayment = cls.query.get(repayment_id) repayment = cls.query.get(repayment_id)
if not repayment: if not repayment:
raise ValueError(f"repayment with ID {repayment_id} does not exist.") raise ValueError(f"repayment with ID {repayment_id} does not exist.")
# Check if customer_id matches # Check if customer_id matches
if repayment.customer_id != customer_id: if repayment.customer_id != customer_id:
raise ValueError(f"Customer ID {customer_id} does not match the repayment's customer ID.") raise ValueError(f"Customer ID {customer_id} does not match the repayment's customer ID.")
current_time = datetime.now() current_time = datetime.now()
logger.info(f"What is now ======= ==== ==> : {current_time}") logger.info(f"What is now ======= ==== ==> : {current_time}")
# Update repayment verify_date # Update repayment verify_date
repayment.verify_date = current_time repayment.verify_date = current_time
# Commit changes to database # Commit changes to database
try: try:
logger.info(f"Updating repay verify date for repayment ID {repayment_id} to {current_time}") logger.info(f"Updating repay verify date for repayment ID {repayment_id} to {current_time}")
db.session.commit() db.session.commit()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Failed to update repay verify date: {e}") logger.error(f"Failed to update repay verify date: {e}")
raise e raise e
@classmethod @classmethod
def set_repay_result(cls, repayment_id, result, description): def set_repay_result(cls, repayment_id, result, description):
logger.info("repay result called") logger.info("repay result called")
""" """
Update the repayment result and description of the repayment with the given repayment_id. Update the repayment result and description of the repayment with the given repayment_id.
""" """
# Retrieve loan # Retrieve loan
repayment = cls.query.get(repayment_id) repayment = cls.query.get(repayment_id)
if not repayment: if not repayment:
raise ValueError(f"repayment with ID {repayment_id} does not exist.") raise ValueError(f"repayment with ID {repayment_id} does not exist.")
# Update repayment result and description # Update repayment result and description
repayment.repay_result = result repayment.repay_result = result
repayment.repay_description = description repayment.repay_description = description
# Commit changes to database # Commit changes to database
try: try:
logger.info(f"Updating repayment result for repayment ID {repayment_id} to {result} with description {description}") logger.info(f"Updating repayment result for repayment ID {repayment_id} to {result} with description {description}")
db.session.commit() db.session.commit()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Failed to update repayment result: {e}") logger.error(f"Failed to update repayment result: {e}")
raise raise
@classmethod @classmethod
def set_verify_date_result(cls, repayment_id, result, description): def set_verify_date_result(cls, repayment_id, result, description):
""" """
Update the verify result and description of the repayment with the given repayment_id. Update the verify result and description of the repayment with the given repayment_id.
""" """
# Retrieve repayment # Retrieve repayment
repayment = cls.query.get(repayment_id) repayment = cls.query.get(repayment_id)
if not repayment: if not repayment:
raise ValueError(f"repayment with ID {repayment_id} does not exist.") raise ValueError(f"repayment with ID {repayment_id} does not exist.")
# Update disburse result and description # Update disburse result and description
repayment.verify_result = result repayment.verify_result = result
repayment.verify_description = description repayment.verify_description = description
# Commit changes to database # Commit changes to database
try: try:
logger.info(f"Updating verify result for repayment ID {repayment_id} to {result} with description {description}") logger.info(f"Updating verify result for repayment ID {repayment_id} to {result} with description {description}")
db.session.commit() db.session.commit()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Failed to update verify result: {e}") logger.error(f"Failed to update verify result: {e}")
raise raise
@classmethod @classmethod
def get_latest_repayment_without_repay_date(cls): def get_latest_repayment_without_repay_date(cls):
""" """
Get the latest repayment without a repay date. Get the latest repayment without a repay date.
""" """
return cls.query.filter( return cls.query.filter(
cls.repay_date.is_(None) cls.repay_date.is_(None)
).order_by(cls.created_at.desc()).first() ).order_by(cls.created_at.desc()).first()
@classmethod @classmethod
def get_latest_repayment_with_loanId(cls, loan_id): def get_latest_repayment_with_loanId(cls, loan_id):
""" """
Get the latest repayment with loan Id. Get the latest repayment with loan Id.
""" """
return cls.query.filter( return cls.query.filter(
cls.loan_id == loan_id cls.loan_id == loan_id
).order_by(cls.created_at.desc()).first() ).order_by(cls.created_at.desc()).first()
@classmethod @classmethod
def get_latest_loan_with_repay_date(cls): def get_latest_loan_with_repay_date(cls):
""" """
Get the latest repayment with a repay date and no verification date. Get the latest repayment with a repay date and no verification date.
""" """
return cls.query.filter( return cls.query.filter(
cls.repay_date.isnot(None), cls.repay_date.isnot(None),
cls.verify_date.is_(None) cls.verify_date.is_(None)
).order_by(cls.created_at.desc()).first() ).order_by(cls.created_at.desc()).first()
+74 -74
View File
@@ -1,75 +1,75 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from app.extensions import db from app.extensions import db
from app.utils.logger import logger from app.utils.logger import logger
class RepaymentsData(db.Model): class RepaymentsData(db.Model):
__tablename__ = 'repayments_data' __tablename__ = 'repayments_data'
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
transaction_id = db.Column(db.String(50), nullable=False) transaction_id = db.Column(db.String(50), nullable=False)
added_date = db.Column(db.DateTime(timezone=True), default=datetime.now(timezone.utc), nullable=False) added_date = db.Column(db.DateTime(timezone=True), default=datetime.now(timezone.utc), nullable=False)
response_code = db.Column(db.String(10), nullable=True) response_code = db.Column(db.String(10), nullable=True)
response_descr = db.Column(db.String(255), nullable=True) response_descr = db.Column(db.String(255), nullable=True)
fbn_transaction_id = db.Column(db.String(255),nullable=True) fbn_transaction_id = db.Column(db.String(255),nullable=True)
account_id = db.Column(db.String(50), nullable=True) account_id = db.Column(db.String(50), nullable=True)
customer_id = db.Column(db.String(50), nullable=True) customer_id = db.Column(db.String(50), nullable=True)
repayment_amount = db.Column(db.Float, nullable=True) repayment_amount = db.Column(db.Float, nullable=True)
amount_collected = db.Column(db.Float, nullable=True) amount_collected = db.Column(db.Float, nullable=True)
balance = db.Column(db.Float, nullable=True, default=0.0) balance = db.Column(db.Float, nullable=True, default=0.0)
def to_dict(self): def to_dict(self):
return { return {
"id": self.id, "id": self.id,
"transaction_id": self.transaction_id, "transaction_id": self.transaction_id,
"added_date": self.added_date.isoformat() if self.added_date else None, "added_date": self.added_date.isoformat() if self.added_date else None,
"response_code": self.response_code, "response_code": self.response_code,
"response_descr": self.response_descr, "response_descr": self.response_descr,
"customerId": self.customer_id, "customerId": self.customer_id,
"accountId": self.account_id, "accountId": self.account_id,
"fbnTransactionId": self.fbn_transaction_id, "fbnTransactionId": self.fbn_transaction_id,
"repaymentAmount": self.repayment_amount, "repaymentAmount": self.repayment_amount,
"amountCollected": self.amount_collected, "amountCollected": self.amount_collected,
"balance": self.balance "balance": self.balance
} }
def __repr__(self): def __repr__(self):
return f"<RepaymentsData id={self.id}, transaction_id={self.transaction_id}>" return f"<RepaymentsData id={self.id}, transaction_id={self.transaction_id}>"
@classmethod @classmethod
def add_repayment_data(cls, data): def add_repayment_data(cls, data):
""" """
Add a new repayment data entry. Add a new repayment data entry.
""" """
try: try:
repayment_amount = float(data.get('repaymentAmount', 0.0)) repayment_amount = float(data.get('repaymentAmount', 0.0))
amount_collected = float(data.get('amountCollected', 0.0)) amount_collected = float(data.get('amountCollected', 0.0))
if amount_collected < 0 or repayment_amount < 0: if amount_collected < 0 or repayment_amount < 0:
raise ValueError("Amounts cannot be negative.") raise ValueError("Amounts cannot be negative.")
account_balance = round(repayment_amount - amount_collected, 2) account_balance = round(repayment_amount - amount_collected, 2)
new_data = cls( new_data = cls(
transaction_id=data.get('transactionId'), transaction_id=data.get('transactionId'),
response_code=data.get('responseCode'), response_code=data.get('responseCode'),
response_descr=data.get('responseDescr'), response_descr=data.get('responseDescr'),
fbn_transaction_id=data.get('fbnTransactionId'), fbn_transaction_id=data.get('fbnTransactionId'),
account_id=data.get('accountId'), account_id=data.get('accountId'),
customer_id=data.get('customerId'), customer_id=data.get('customerId'),
amount_collected=amount_collected, amount_collected=amount_collected,
repayment_amount=repayment_amount, repayment_amount=repayment_amount,
balance=account_balance, balance=account_balance,
) )
db.session.add(new_data) db.session.add(new_data)
db.session.commit() db.session.commit()
logger.info("Repayment data committed successfully") logger.info("Repayment data committed successfully")
return new_data return new_data
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error adding repayment data: {e}") logger.error(f"Error adding repayment data: {e}")
raise Exception(f"Error adding repayment data: {str(e)}") raise Exception(f"Error adding repayment data: {str(e)}")
+98 -98
View File
@@ -1,98 +1,98 @@
from app.extensions import db from app.extensions import db
from datetime import datetime, timezone from datetime import datetime, timezone
from app.utils.logger import logger from app.utils.logger import logger
class Salary(db.Model): class Salary(db.Model):
__tablename__ = "salaries" __tablename__ = "salaries"
id = db.Column( id = db.Column(
db.Integer, db.Integer,
primary_key=True, primary_key=True,
autoincrement=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)
amount = db.Column(db.Float, nullable=True, default=0.0) amount = db.Column(db.Float, nullable=True, default=0.0)
status = db.Column(db.String(20), nullable=True) status = db.Column(db.String(20), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.now) created_at = db.Column(db.DateTime, default=datetime.now)
updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
salary_date = db.Column(db.DateTime, nullable=True) salary_date = db.Column(db.DateTime, nullable=True)
def __repr__(self): def __repr__(self):
return f'<Salary {self.id}>' return f'<Salary {self.id}>'
def to_dict(self): def to_dict(self):
""" """
Convert the Salary object to a dictionary format for JSON serialization. Convert the Salary object to a dictionary format for JSON serialization.
""" """
return { return {
'id': self.id, 'id': self.id,
'customerId': self.customer_id, 'customerId': self.customer_id,
'accountId' : self.account_id, 'accountId' : self.account_id,
'salaryAmount': self.amount, 'salaryAmount': self.amount,
'status': self.status, 'status': self.status,
'createdAt': self.created_at.isoformat() if self.created_at else None, 'createdAt': self.created_at.isoformat() if self.created_at else None,
'updatedAt': self.updated_at.isoformat() if self.updated_at else None, 'updatedAt': self.updated_at.isoformat() if self.updated_at else None,
'salaryDate': self.salary_date.isoformat() if self.salary_date else None, 'salaryDate': self.salary_date.isoformat() if self.salary_date else None,
} }
@classmethod @classmethod
def add_salary_data(cls, data): def add_salary_data(cls, data):
""" """
Add a new salary data entry. Add a new salary data entry.
""" """
logger.info(f"Received data:{data}") logger.info(f"Received data:{data}")
try: try:
new_data = cls( new_data = cls(
customer_id=data.get('customerId'), customer_id=data.get('customerId'),
amount=data.get('salaryAmount', 0.0), amount=data.get('salaryAmount', 0.0),
status='START', status='START',
salary_date = datetime.strptime(data.get('salaryDate'), "%Y-%m-%d").date() if data.get('salaryDate') else None, salary_date = datetime.strptime(data.get('salaryDate'), "%Y-%m-%d").date() if data.get('salaryDate') else None,
account_id=data.get('accountId') account_id=data.get('accountId')
) )
db.session.add(new_data) db.session.add(new_data)
db.session.commit() db.session.commit()
logger.info("Salary data has been committed.") logger.info("Salary data has been committed.")
return new_data return new_data
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.info(f"error : {str(e)}") logger.info(f"error : {str(e)}")
raise Exception(f"Error adding salary data: {str(e)}") raise Exception(f"Error adding salary data: {str(e)}")
@classmethod @classmethod
def get_pending_salaries(cls): def get_pending_salaries(cls):
""" """
Retrieve all salary entries with status 'START', ordered by ID ascending. Retrieve all salary entries with status 'START', ordered by ID ascending.
""" """
try: try:
return cls.query.filter_by(status='START').order_by(cls.id.asc()).all() return cls.query.filter_by(status='START').order_by(cls.id.asc()).all()
except Exception as e: except Exception as e:
logger.error(f"Error fetching pending salaries: {str(e)}") logger.error(f"Error fetching pending salaries: {str(e)}")
return [] return []
@classmethod @classmethod
def update_status(cls, salary_id, status): def update_status(cls, salary_id, status):
""" """
Update the status of the salary record with the given salary_id. Update the status of the salary record with the given salary_id.
""" """
try: try:
# Retrieve salary record # Retrieve salary record
salary = cls.query.get(salary_id) salary = cls.query.get(salary_id)
if not salary: if not salary:
raise ValueError(f"Salary with ID {salary_id} does not exist.") raise ValueError(f"Salary with ID {salary_id} does not exist.")
if salary.status == status: if salary.status == status:
return salary.to_dict() # Still return the current state if no change return salary.to_dict() # Still return the current state if no change
# Update status and timestamp # Update status and timestamp
salary.status = status salary.status = status
salary.updated_at = datetime.now(timezone.utc) # Manually update timestamp if not auto-updating salary.updated_at = datetime.now(timezone.utc) # Manually update timestamp if not auto-updating
db.session.commit() db.session.commit()
logger.info("Salary status updated and committed.") logger.info("Salary status updated and committed.")
return salary.to_dict() return salary.to_dict()
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logger.error(f"Error updating salary status: {e}") logger.error(f"Error updating salary status: {e}")
raise Exception(f"Error updating salary status: {str(e)}") raise Exception(f"Error updating salary status: {str(e)}")
+67 -67
View File
@@ -1,68 +1,68 @@
from app.extensions import db from app.extensions import db
from datetime import datetime, timezone from datetime import datetime, timezone
from app.utils.logger import logger from app.utils.logger import logger
from sqlalchemy import and_, or_, not_ from sqlalchemy import and_, or_, not_
class Transaction(db.Model): class Transaction(db.Model):
__tablename__ = "transactions" __tablename__ = "transactions"
id = db.Column( id = db.Column(
db.Integer, db.Integer,
primary_key=True, primary_key=True,
autoincrement=True, autoincrement=True,
) )
transaction_id = db.Column(db.String(50), nullable=False) transaction_id = db.Column(db.String(50), nullable=False)
account_id = db.Column(db.String(50), nullable=True) account_id = db.Column(db.String(50), nullable=True)
customer_id = db.Column(db.String(50), nullable=True) customer_id = db.Column(db.String(50), nullable=True)
type = db.Column(db.String(50), nullable=False) type = db.Column(db.String(50), nullable=False)
channel = db.Column(db.String(50), nullable=False) channel = db.Column(db.String(50), nullable=False)
phone_number = db.Column(db.String(50), nullable=True) phone_number = db.Column(db.String(50), nullable=True)
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 @classmethod
def create_transaction(cls, transaction_id, account_id, customer_id, type, channel): def create_transaction(cls, transaction_id, account_id, customer_id, type, channel):
logger.error(f"**Setting Transaction {transaction_id} for Type {type}") logger.error(f"**Setting Transaction {transaction_id} for Type {type}")
if cls.query.filter( and_( cls.transaction_id ==transaction_id, cls.type==type) ).first(): if cls.query.filter( and_( cls.transaction_id ==transaction_id, cls.type==type) ).first():
logger.error(f"Transaction already exists for {type}") logger.error(f"Transaction already exists for {type}")
return '' # dont raise - do not crash beacause of this return '' # dont raise - do not crash beacause of this
transaction = cls( transaction = cls(
transaction_id = transaction_id, transaction_id = transaction_id,
customer_id = customer_id, customer_id = customer_id,
account_id = account_id, account_id = account_id,
type = type, type = type,
channel = channel, channel = channel,
created_at=datetime.now(timezone.utc), created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc) updated_at=datetime.now(timezone.utc)
) )
try: try:
db.session.add(transaction) db.session.add(transaction)
db.session.commit() db.session.commit()
except IntegrityError as err: except IntegrityError as err:
raise ValueError(f"Database integrity error: {err}") raise ValueError(f"Database integrity error: {err}")
return transaction return transaction
def __repr__(self): def __repr__(self):
return f'<Transaction {self.id}>' return f'<Transaction {self.id}>'
def to_dict(self): def to_dict(self):
""" """
Convert the Transaction object to a dictionary format for JSON serialization. Convert the Transaction object to a dictionary format for JSON serialization.
""" """
return { return {
'id': self.id, 'id': self.id,
'transaction_id': self.transaction_id, 'transaction_id': self.transaction_id,
'account_id': self.account_id, 'account_id': self.account_id,
'customer_id': self.customer_id, 'customer_id': self.customer_id,
'phone_number':self.phone_number, 'phone_number':self.phone_number,
'type': self.type, 'type': self.type,
'channel': self.channel, 'channel': self.channel,
} }
@classmethod @classmethod
def get_transaction_by_transaction_id(cls, transaction_id): def get_transaction_by_transaction_id(cls, transaction_id):
return cls.query.filter_by(transaction_id=transaction_id).first() return cls.query.filter_by(transaction_id=transaction_id).first()
+1 -1
View File
@@ -1,2 +1,2 @@
from .handlers import (method_not_allowed, unsupported_media_type, from .handlers import (method_not_allowed, unsupported_media_type,
not_found, bad_request, success, created, updated) not_found, bad_request, success, created, updated)
+30 -30
View File
@@ -1,30 +1,30 @@
from flask import jsonify from flask import jsonify
from app.helpers.response_helper import ResponseHelper from app.helpers.response_helper import ResponseHelper
def method_not_allowed(error): def method_not_allowed(error):
return ResponseHelper.method_not_allowed(message="Method Not Allowed") return ResponseHelper.method_not_allowed(message="Method Not Allowed")
def not_found(error): def not_found(error):
return ResponseHelper.not_found(message="URL Not Found") return ResponseHelper.not_found(message="URL Not Found")
def bad_request(error): def bad_request(error):
return ResponseHelper.bad_request(message="Bad Request") return ResponseHelper.bad_request(message="Bad Request")
def unsupported_media_type(error): def unsupported_media_type(error):
return ResponseHelper.error(message="Unsupported Media Type", status_code=415) return ResponseHelper.error(message="Unsupported Media Type", status_code=415)
def success(data): def success(data):
return ResponseHelper.success(data=data) return ResponseHelper.success(data=data)
def created(data): def created(data):
return ResponseHelper.created(data=data) return ResponseHelper.created(data=data)
def updated(data): def updated(data):
return ResponseHelper.updated(data=data) return ResponseHelper.updated(data=data)
+2 -2
View File
@@ -1,2 +1,2 @@
from .authentication import auth_bp from .authentication import auth_bp
from .autocall import autocall_bp from .autocall import autocall_bp
+137 -137
View File
@@ -1,137 +1,137 @@
from flask import Blueprint, request, jsonify, current_app from flask import Blueprint, request, jsonify, current_app
import requests import requests
from app.extensions import db from app.extensions import db
from sqlalchemy import text from sqlalchemy import text
from app.utils.auth import get_headers from app.utils.auth import get_headers
from app.config import settings from app.config import settings
from app.utils.logger import logger from app.utils.logger import logger
from app.integrations.bank_service import BankService from app.integrations.bank_service import BankService
auth_bp = Blueprint("auth", __name__) auth_bp = Blueprint("auth", __name__)
BASE_URL = settings.BANK_CALL_BASE_URL BASE_URL = settings.BANK_CALL_BASE_URL
@auth_bp.route("/health", methods=["GET"]) @auth_bp.route("/health", methods=["GET"])
def health(): def health():
logger.info("Health check endpoint called") logger.info("Health check endpoint called")
errors = [] # collect all errors errors = [] # collect all errors
try: try:
# Detect database type # Detect database type
dialect = db.engine.dialect.name.lower() dialect = db.engine.dialect.name.lower()
logger.info(f"Database dialect detected: {dialect}") logger.info(f"Database dialect detected: {dialect}")
# Build correct query based on DB type # Build correct query based on DB type
if "oracle" in dialect: if "oracle" in dialect:
query = text("SELECT 1 FROM dual") query = text("SELECT 1 FROM dual")
else: else:
query = text("SELECT 1") query = text("SELECT 1")
# Test database connection # Test database connection
try: try:
db.session.execute(query) db.session.execute(query)
logger.info("Database connection successful.") logger.info("Database connection successful.")
except Exception as db_err: except Exception as db_err:
logger.error(f"Database connection failed: {str(db_err)}") logger.error(f"Database connection failed: {str(db_err)}")
errors.append(f"Database connection failed: {str(db_err)}") errors.append(f"Database connection failed: {str(db_err)}")
# Check Bank Service health # Check Bank Service health
try: try:
bank_response = BankService.health_check() bank_response = BankService.health_check()
logger.info(f"Bank Service health check response: {bank_response}") logger.info(f"Bank Service health check response: {bank_response}")
except Exception as bank_err: except Exception as bank_err:
logger.error(f"Bank Service health check failed: {str(bank_err)}") logger.error(f"Bank Service health check failed: {str(bank_err)}")
errors.append(f"Bank Service health check failed: {str(bank_err)}") errors.append(f"Bank Service health check failed: {str(bank_err)}")
# Build final response # Build final response
if errors: if errors:
return jsonify({ return jsonify({
"status": "error", "status": "error",
"database": dialect, "database": dialect,
"errors": errors "errors": errors
}), 500 }), 500
return jsonify({ return jsonify({
"status": "success", "status": "success",
"database": dialect, "database": dialect,
"db_status": "connected", "db_status": "connected",
"bank_service_status": "operational", "bank_service_status": "operational",
"message": "All systems operational" "message": "All systems operational"
}), 200 }), 200
except Exception as e: except Exception as e:
logger.exception("Unexpected error during health check") logger.exception("Unexpected error during health check")
return jsonify({ return jsonify({
"status": "error", "status": "error",
"errors": [str(e)] "errors": [str(e)]
}), 500 }), 500
@auth_bp.route("/login", methods=["POST"]) @auth_bp.route("/login", methods=["POST"])
def login(): def login():
data = request.get_json() data = request.get_json()
api_url = f"{BASE_URL}/login" api_url = f"{BASE_URL}/login"
response = requests.post(api_url, json=data) response = requests.post(api_url, json=data)
if response.status_code == 200: if response.status_code == 200:
return jsonify(response.json()), 200 return jsonify(response.json()), 200
return jsonify({"error": "Invalid credentials"}), response.status_code return jsonify({"error": "Invalid credentials"}), response.status_code
@auth_bp.route("/status-call", methods=["POST"]) @auth_bp.route("/status-call", methods=["POST"])
def status_call(): def status_call():
data = request.get_json() data = request.get_json()
api_url = f"{BASE_URL}/StatusCall" api_url = f"{BASE_URL}/StatusCall"
# response = requests.post(api_url, json=data, headers=get_headers()) # response = requests.post(api_url, json=data, headers=get_headers())
# return jsonify(response.json()), response.status_code # return jsonify(response.json()), response.status_code
response = { response = {
"transactionId": "24110114545374721", "transactionId": "24110114545374721",
"data": { "data": {
"transactionId": "241101", "transactionId": "241101",
"providedAmount": 1000, "providedAmount": 1000,
"collectedAmount": 0, "collectedAmount": 0,
"resultCode": "00", "resultCode": "00",
"resultDescription": "Loan Provision is successful", "resultDescription": "Loan Provision is successful",
}, },
"resultCode": "00", "resultCode": "00",
"resultDescription": "SUCCESS", "resultDescription": "SUCCESS",
} }
return jsonify(response), 200 return jsonify(response), 200
@auth_bp.route("/sms", methods=["POST"]) @auth_bp.route("/sms", methods=["POST"])
def sms(): def sms():
data = request.get_json() data = request.get_json()
api_url = f"{BASE_URL}/SMS" api_url = f"{BASE_URL}/SMS"
# response = requests.post(api_url, json=data, headers=get_headers()) # response = requests.post(api_url, json=data, headers=get_headers())
# return jsonify(response.json()), response.status_code # return jsonify(response.json()), response.status_code
response = { response = {
"data": "", "data": "",
"statusCode": 200, "statusCode": 200,
"IsSuccessful": True, "IsSuccessful": True,
"errorMessage": None, "errorMessage": None,
} }
return jsonify(response), 200 return jsonify(response), 200
@auth_bp.route("/bulk-sms", methods=["POST"]) @auth_bp.route("/bulk-sms", methods=["POST"])
def bulk_sms(): def bulk_sms():
data = request.get_json() data = request.get_json()
api_url = f"{BASE_URL}/BulkSMS" api_url = f"{BASE_URL}/BulkSMS"
# response = requests.post(api_url, json=data, headers=get_headers()) # response = requests.post(api_url, json=data, headers=get_headers())
# return jsonify(response.json()), response.status_code # return jsonify(response.json()), response.status_code
response = { response = {
"data": "", "data": "",
"statusCode": 200, "statusCode": 200,
"IsSuccessful": True, "IsSuccessful": True,
"errorMessage": None, "errorMessage": None,
} }
return jsonify(response), 200 return jsonify(response), 200
+684 -784
View File
File diff suppressed because it is too large Load Diff
+147 -162
View File
@@ -1,163 +1,148 @@
from app.models import Loan, LoanCharge from app.models import Loan, LoanCharge
from app.utils.logger import logger from app.utils.logger import logger
from app.enums.loan_status import LoanStatus from app.enums.loan_status import LoanStatus
from decimal import Decimal, ROUND_HALF_UP from decimal import Decimal, ROUND_HALF_UP
from app.services.loan_repayment_schedule import LoanRepaymentScheduleService from app.services.loan_repayment_schedule import LoanRepaymentScheduleService
class LoanService: class LoanService:
@classmethod @classmethod
def get_loan_by_transaction_id(cls, transaction_id): def get_loan_by_transaction_id(cls, transaction_id):
""" """
Get the loan by transaction ID Get the loan by transaction ID
""" """
return Loan.get_loan_by_transaction_id(transaction_id) return Loan.get_loan_by_transaction_id(transaction_id)
@classmethod @classmethod
def get_loan_by_loan_id(cls, loan_id): def get_loan_by_loan_id(cls, loan_id):
""" """
Get the loan by ID Get the loan by ID
""" """
return Loan.get_loan_by_loan_id(loan_id) return Loan.get_loan_by_loan_id(loan_id)
@classmethod @classmethod
def get_loan_by_debt_id(cls, debt_id): def get_loan_by_debt_id(cls, debt_id):
""" """
Get the loan by transaction ID Get the loan by transaction ID
""" """
return Loan.get_loan_by_debt_id(debt_id) return Loan.get_loan_by_debt_id(debt_id)
@classmethod @classmethod
def get_loan_charge_by_debt_id(cls, debt_id): def get_loan_charge_by_debt_id(cls, debt_id):
""" """
Get the loan charge by debt ID Get the loan charge by debt ID
""" """
return LoanCharge.get_loan_charge_by_debt_id(debt_id) return LoanCharge.get_loan_charge_by_debt_id(debt_id)
@classmethod @classmethod
def set_disbursement_date(cls, loan_id, customer_id): def set_disbursement_date(cls, loan_id, customer_id):
""" """
Update the disbursement status of the loan with the given loan_id. Update the disbursement status of the loan with the given loan_id.
""" """
return Loan.set_disbursement_date(loan_id, customer_id) return Loan.set_disbursement_date(loan_id, customer_id)
@classmethod @classmethod
def set_disburse_verify_date(cls, loan_id, customer_id): def set_disburse_verify_date(cls, loan_id, customer_id):
""" """
Update the disburse verify date of the loan with the given loan_id. Update the disburse verify date of the loan with the given loan_id.
""" """
return Loan.set_disburse_verify_date(loan_id, customer_id) return Loan.set_disburse_verify_date(loan_id, customer_id)
@classmethod @classmethod
def set_disbursement_result(cls, loan_id, result, description): def set_disbursement_result(cls, loan_id, result, description):
""" """
Update the disbursement result of the loan with the given loan_id. Update the disbursement result of the loan with the given loan_id.
""" """
return Loan.set_disbursement_result(loan_id, result, description) return Loan.set_disbursement_result(loan_id, result, description)
@classmethod @classmethod
def set_disbursement_loan_description(cls,loan_id,description): def set_disbursement_loan_description(cls,loan_id,description):
return Loan.set_disbursement_message(loan_id, description) return Loan.set_disbursement_message(loan_id, description)
@classmethod
def get_failed_disbursements(cls): @classmethod
""" def set_disburse_verify_result(cls, loan_id, result, description):
Get all loans with failed disbursement. """
""" Update the disburse verify result of the loan with the given loan_id.
return Loan.get_failed_disbursements() """
return Loan.set_disburse_verify_result(loan_id, result, description)
@classmethod
def update_status(cls, loan_id, status): @classmethod
""" def get_latest_loan_without_disburse_date(cls):
Update the status of the loan with the given loan_id. """
""" Get the latest loan without a disbursement date.
# Retrieve loan """
return Loan.update_status(loan_id, status) return Loan.get_latest_loan_without_disburse_date()
@classmethod
@classmethod def get_latest_loan_with_disburse_date(cls):
def set_disburse_verify_result(cls, loan_id, result, description): """
""" Get the latest loan without a disbursement date.
Update the disburse verify result of the loan with the given loan_id. """
""" return Loan.get_latest_loan_with_disburse_date()
return Loan.set_disburse_verify_result(loan_id, result, description)
@classmethod
@classmethod def get_customer_loans(cls, customer_id):
def get_latest_loan_without_disburse_date(cls): """
""" Get customer's active loans by customer_id.
Get the latest loan without a disbursement date. """
"""
return Loan.get_latest_loan_without_disburse_date() return Loan.get_customer_loans(customer_id=customer_id)
@classmethod
@classmethod def get_customer_active_loans(cls, customer_id):
def get_latest_loan_with_disburse_date(cls): """
""" Get customer's active loans by customer_id.
Get the latest loan without a disbursement date. """
"""
return Loan.get_latest_loan_with_disburse_date() return Loan.get_customer_active_loans(customer_id=customer_id)
@classmethod @classmethod
def get_customer_loans(cls, customer_id): def update_status(cls, loan_id, status):
""" """
Get customer's active loans by customer_id. Update the status of the loan with the given loan_id.
""" """
# Retrieve loan
return Loan.get_customer_loans(customer_id=customer_id) return Loan.update_status(loan_id, status)
@classmethod @classmethod
def get_customer_active_loans(cls, customer_id): def update_loan_balance(cls,loan_id,amount_collected):
""" """
Get customer's active loans by customer_id. update the loan balance after successful repayment
""" """
return Loan.update_loan_balance(loan_id,amount_collected)
return Loan.get_customer_active_loans(customer_id=customer_id)
@classmethod
@classmethod def get_overdue_loans(cls):
def update_status(cls, loan_id, status): """
""" Get all overdue loans.
Update the status of the loan with the given loan_id. """
""" return Loan.get_overdue_loans()
# Retrieve loan @classmethod
return Loan.update_status(loan_id, status) def apply_penal_to_loan(cls,loan_id,penal_charge):
@classmethod return Loan.apply_penal_to_loan(loan_id,penal_charge)
def update_loan_balance(cls,loan_id,amount_collected): @staticmethod
""" def _update_loan_after_collection(loan, loan_data, updated_loan, amount_collected, data, response_message):
update the loan balance after successful repayment if loan.balance is None or loan.balance <= 0:
""" logger.warning(f"Loan ID {loan.id} has no balance. Skipping loan update.")
return Loan.update_loan_balance(loan_id,amount_collected) updated_loan = loan.to_dict()
else:
@classmethod updated_loan = LoanService.update_loan_balance(int(loan_data['debtId']), amount_collected)
def get_overdue_loans(cls): updated_balance = Decimal(str(updated_loan['balance'])).quantize(Decimal('0.01'))
"""
Get all overdue loans. if updated_balance <= Decimal('0.00'):
""" updated_loan = LoanService.update_status(updated_loan['debtId'], LoanStatus.REPAID)
return Loan.get_overdue_loans() else:
@classmethod updated_loan = LoanService.update_status(updated_loan['debtId'], LoanStatus.ACTIVE_PARTIAL)
def apply_penal_to_loan(cls,loan_id,penal_charge): logger.info(f"Updated loan status: {updated_loan.get('status')}")
return Loan.apply_penal_to_loan(loan_id,penal_charge) # lets update the loan repayment schedule
@staticmethod LoanRepaymentScheduleService.handle_schedule_updates(
def _update_loan_after_collection(loan, loan_data, updated_loan, amount_collected, data, response_message): updated_loan=updated_loan,
if loan.balance is None or loan.balance <= 0: data=data,
logger.warning(f"Loan ID {loan.id} has no balance. Skipping loan update.") amount_collected=amount_collected,
updated_loan = loan.to_dict() message=response_message,
else: loan_data=loan_data
updated_loan = LoanService.update_loan_balance(int(loan_data['debtId']), amount_collected) )
updated_balance = Decimal(str(updated_loan['balance'])).quantize(Decimal('0.01')) return updated_loan
if updated_balance <= Decimal('0.00'):
updated_loan = LoanService.update_status(updated_loan['debtId'], LoanStatus.REPAID)
else:
updated_loan = LoanService.update_status(updated_loan['debtId'], LoanStatus.ACTIVE_PARTIAL)
logger.info(f"Updated loan status: {updated_loan.get('status')}")
# lets update the loan repayment schedule
LoanRepaymentScheduleService.handle_schedule_updates(
updated_loan=updated_loan,
data=data,
amount_collected=amount_collected,
message=response_message,
loan_data=loan_data
)
return updated_loan
+12 -12
View File
@@ -1,13 +1,13 @@
from app.models.loan_charge import LoanCharge from app.models.loan_charge import LoanCharge
class LoanChargesService: class LoanChargesService:
@classmethod @classmethod
def create_penal_charges_for_loan(cls, loan_id, transaction_id, percent, penal_no, schedule_number, penal_amount=0.0,): def create_penal_charges_for_loan(cls, loan_id, transaction_id, percent, penal_no, schedule_number, penal_amount=0.0,):
return LoanCharge.create_penal_charges_for_loan(loan_id, transaction_id, percent, penal_no,schedule_number, penal_amount) return LoanCharge.create_penal_charges_for_loan(loan_id, transaction_id, percent, penal_no,schedule_number, penal_amount)
@classmethod @classmethod
def get_last_penal_no(cls,loan_id): def get_last_penal_no(cls,loan_id):
return LoanCharge.get_last_penal_no(loan_id) return LoanCharge.get_last_penal_no(loan_id)
@classmethod @classmethod
def get_penal_charges_by_loan_id(cls,loan_id): def get_penal_charges_by_loan_id(cls,loan_id):
return LoanCharge.get_penal_charges_by_loan_id(loan_id) return LoanCharge.get_penal_charges_by_loan_id(loan_id)
+136 -136
View File
@@ -1,136 +1,136 @@
from app.models.loan_repayment_schedule import LoanRepaymentSchedule from app.models.loan_repayment_schedule import LoanRepaymentSchedule
from app.utils.logger import logger from app.utils.logger import logger
from app.enums.loan_status import LoanStatus from app.enums.loan_status import LoanStatus
from decimal import Decimal, ROUND_HALF_UP from decimal import Decimal, ROUND_HALF_UP
class LoanRepaymentScheduleService: class LoanRepaymentScheduleService:
@classmethod @classmethod
def get_repayment_schedule_by_loan_id(cls, loan_id, include_paid=True): def get_repayment_schedule_by_loan_id(cls, loan_id, include_paid=True):
return LoanRepaymentSchedule.get_repayment_schedule_by_loan_id(loan_id, include_paid=include_paid) return LoanRepaymentSchedule.get_repayment_schedule_by_loan_id(loan_id, include_paid=include_paid)
@classmethod @classmethod
def get_overdue_repayment_schedule(cls): def get_overdue_repayment_schedule(cls):
return LoanRepaymentSchedule.get_overdue_repayment_schedule() return LoanRepaymentSchedule.get_overdue_repayment_schedule()
@classmethod @classmethod
def get_active_overdue_repayment_schedule(cls): def get_active_overdue_repayment_schedule(cls):
return LoanRepaymentSchedule.get_active_overdue_repayment_schedule() return LoanRepaymentSchedule.get_active_overdue_repayment_schedule()
@classmethod @classmethod
def get_partially_paid_overdue_repayment_schedule(cls): def get_partially_paid_overdue_repayment_schedule(cls):
return LoanRepaymentSchedule.get_partially_paid_overdue_repayment_schedule() return LoanRepaymentSchedule.get_partially_paid_overdue_repayment_schedule()
@classmethod @classmethod
def get_repayment_schedule_by_id_and_transaction_id(cls, id, transaction_id): def get_repayment_schedule_by_id_and_transaction_id(cls, id, transaction_id):
return LoanRepaymentSchedule.get_repayment_schedule_by_id_and_transaction_id(id, transaction_id) return LoanRepaymentSchedule.get_repayment_schedule_by_id_and_transaction_id(id, transaction_id)
@classmethod @classmethod
def get_repayment_schedule_by_transaction_id(cls, transaction_id): def get_repayment_schedule_by_transaction_id(cls, transaction_id):
return LoanRepaymentSchedule.get_repayment_schedule_by_transaction_id(transaction_id) return LoanRepaymentSchedule.get_repayment_schedule_by_transaction_id(transaction_id)
@classmethod @classmethod
def update_repayment_schedule_status(cls, schedule_id): def update_repayment_schedule_status(cls, schedule_id):
""" """
Update repayment schedule status. Update repayment schedule status.
""" """
return LoanRepaymentSchedule.update_repayment_schedule_status(schedule_id) return LoanRepaymentSchedule.update_repayment_schedule_status(schedule_id)
@classmethod @classmethod
def update_repayment_schedule_status_to_active(cls, schedule_id): def update_repayment_schedule_status_to_active(cls, schedule_id):
""" """
Update repayment schedule status. Update repayment schedule status.
""" """
return LoanRepaymentSchedule.update_repayment_schedule_status_to_active(schedule_id) return LoanRepaymentSchedule.update_repayment_schedule_status_to_active(schedule_id)
@classmethod @classmethod
def update_repayment_schedule_balance(cls, schedule_id, amount_collected): def update_repayment_schedule_balance(cls, schedule_id, amount_collected):
""" """
Update repayment schedule balance. Update repayment schedule balance.
""" """
return LoanRepaymentSchedule.update_repayment_schedule_balance(schedule_id, amount_collected) return LoanRepaymentSchedule.update_repayment_schedule_balance(schedule_id, amount_collected)
@classmethod @classmethod
def calculate_penal_charge(cls, schedule): def calculate_penal_charge(cls, schedule):
""" """
Calculate penal charge for a repayment schedule. Calculate penal charge for a repayment schedule.
""" """
return LoanRepaymentSchedule.calculate_penal_charge(schedule) return LoanRepaymentSchedule.calculate_penal_charge(schedule)
@classmethod @classmethod
def update_repayment_schedule_description(cls, schedule_id, description): def update_repayment_schedule_description(cls, schedule_id, description):
""" """
Update repayment schedule description. Update repayment schedule description.
""" """
return LoanRepaymentSchedule.update_repayment_schedule_description(schedule_id, description) return LoanRepaymentSchedule.update_repayment_schedule_description(schedule_id, description)
@classmethod @classmethod
def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days, limit=None): def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days, limit=None):
return LoanRepaymentSchedule.get_overdue_repayment_schedule_with_grace_period(grace_period_days, limit=limit) return LoanRepaymentSchedule.get_overdue_repayment_schedule_with_grace_period(grace_period_days, limit=limit)
@classmethod @classmethod
def apply_penal_to_schedule(cls, schedule_id, penal_amount): def apply_penal_to_schedule(cls, schedule_id, penal_amount):
""" """
Apply penal charge to a repayment schedule. Apply penal charge to a repayment schedule.
""" """
return LoanRepaymentSchedule.apply_penal_to_schedule(schedule_id, penal_amount) return LoanRepaymentSchedule.apply_penal_to_schedule(schedule_id, penal_amount)
@staticmethod @staticmethod
def handle_schedule_updates(updated_loan, data, amount_collected, message, loan_data): def handle_schedule_updates(updated_loan, data, amount_collected, message, loan_data):
""" """
Handles updating loan repayment schedules depending on loan status Handles updating loan repayment schedules depending on loan status
and overdue schedule data. and overdue schedule data.
""" """
try: try:
# Case 1: Loan fully repaid → mark all schedules paid # Case 1: Loan fully repaid → mark all schedules paid
if updated_loan and updated_loan.get('status') == LoanStatus.REPAID: if updated_loan and updated_loan.get('status') == LoanStatus.REPAID:
repayment_schedule = LoanRepaymentScheduleService.get_repayment_schedule_by_loan_id( repayment_schedule = LoanRepaymentScheduleService.get_repayment_schedule_by_loan_id(
updated_loan['debtId'], include_paid=False updated_loan['debtId'], include_paid=False
) )
logger.info(f'Loan repayment schedule: {repayment_schedule}') logger.info(f'Loan repayment schedule: {repayment_schedule}')
if repayment_schedule: if repayment_schedule:
for installment in repayment_schedule: for installment in repayment_schedule:
try: try:
logger.info(f'Processing installment: {installment}') logger.info(f'Processing installment: {installment}')
LoanRepaymentScheduleService.update_repayment_schedule_status(installment.id) LoanRepaymentScheduleService.update_repayment_schedule_status(installment.id)
LoanRepaymentScheduleService.update_repayment_schedule_description( LoanRepaymentScheduleService.update_repayment_schedule_description(
installment.id, installment.id,
message message
) )
logger.info(f'Updated installment {installment.id} as paid') logger.info(f'Updated installment {installment.id} as paid')
except Exception as e: except Exception as e:
logger.error(f"Failed to update installment {installment.id}: {e}") logger.error(f"Failed to update installment {installment.id}: {e}")
logger.info('All installments processed') logger.info('All installments processed')
# Case 2: Partial repayment made on a full loan without overdueLoanScheduleId # Case 2: Partial repayment made on a full loan without overdueLoanScheduleId
elif updated_loan and updated_loan.get('status') == LoanStatus.ACTIVE_PARTIAL and not data.get('overdueLoanScheduleId'): elif updated_loan and updated_loan.get('status') == LoanStatus.ACTIVE_PARTIAL and not data.get('overdueLoanScheduleId'):
logger.info("Partial repayment detected, but no overdue schedule ID provided.") logger.info("Partial repayment detected, but no overdue schedule ID provided.")
# TODO: implement proportional installment updates # TODO: implement proportional installment updates
# Case 3: when we are processing Overdue schedule repayment → update balance & description # Case 3: when we are processing Overdue schedule repayment → update balance & description
elif data.get('overdueLoanScheduleId') is not None: elif data.get('overdueLoanScheduleId') is not None:
logger.info(f"Overdue loan schedule ID: {data['overdueLoanScheduleId']}") logger.info(f"Overdue loan schedule ID: {data['overdueLoanScheduleId']}")
try: try:
schedule_to_update = LoanRepaymentScheduleService.get_repayment_schedule_by_id_and_transaction_id( schedule_to_update = LoanRepaymentScheduleService.get_repayment_schedule_by_id_and_transaction_id(
data["overdueLoanScheduleId"], data["transactionId"] data["overdueLoanScheduleId"], data["transactionId"]
) )
logger.info(f"Schedule to update: {schedule_to_update}") logger.info(f"Schedule to update: {schedule_to_update}")
if schedule_to_update is None: if schedule_to_update is None:
logger.warning( logger.warning(
f"Repayment schedule not found for ID {data['overdueLoanScheduleId']} " f"Repayment schedule not found for ID {data['overdueLoanScheduleId']} "
f"and transaction ID {loan_data['transactionId']}" f"and transaction ID {loan_data['transactionId']}"
) )
else: else:
if not schedule_to_update.paid: if not schedule_to_update.paid:
update_schedule_balance = LoanRepaymentScheduleService.update_repayment_schedule_balance( update_schedule_balance = LoanRepaymentScheduleService.update_repayment_schedule_balance(
schedule_to_update.id, amount_collected schedule_to_update.id, amount_collected
) )
logger.info(f"Updated loan schedule balance: {update_schedule_balance}") logger.info(f"Updated loan schedule balance: {update_schedule_balance}")
LoanRepaymentScheduleService.update_repayment_schedule_description( LoanRepaymentScheduleService.update_repayment_schedule_description(
schedule_to_update.id, schedule_to_update.id,
message message
) )
except Exception as e: except Exception as e:
logger.error(f"Failed to update repayment schedule installment: {e}") logger.error(f"Failed to update repayment schedule installment: {e}")
except Exception as e: except Exception as e:
logger.error(f"Unexpected error while handling schedule updates: {e}") logger.error(f"Unexpected error while handling schedule updates: {e}")
+77 -77
View File
@@ -1,78 +1,78 @@
from app.models import Repayment from app.models import Repayment
class RepaymentService: class RepaymentService:
@staticmethod @staticmethod
def get_repayment_by_transaction_id(transaction_id): def get_repayment_by_transaction_id(transaction_id):
""" """
Get the repayment by transaction ID Get the repayment by transaction ID
""" """
return Repayment.get_repayment_by_transaction_id(transaction_id) return Repayment.get_repayment_by_transaction_id(transaction_id)
@staticmethod @staticmethod
def get_repayment_by_id(id): def get_repayment_by_id(id):
""" """
Get the repayment by ID Get the repayment by ID
""" """
return Repayment.get_repayment_by_id(id) return Repayment.get_repayment_by_id(id)
@classmethod @classmethod
def set_repay_date(cls, repayment_id, customer_id): def set_repay_date(cls, repayment_id, customer_id):
""" """
Update the repay status of the repayment with the given repayment_id. Update the repay status of the repayment with the given repayment_id.
""" """
return Repayment.set_repay_date(repayment_id, customer_id) return Repayment.set_repay_date(repayment_id, customer_id)
@classmethod @classmethod
def set_repay_verify_date(cls, repayment_id, customer_id): def set_repay_verify_date(cls, repayment_id, customer_id):
""" """
Update the verify date of the repayment with the given repayment_id. Update the verify date of the repayment with the given repayment_id.
""" """
return Repayment.set_repay_verify_date(repayment_id, customer_id) return Repayment.set_repay_verify_date(repayment_id, customer_id)
@classmethod @classmethod
def set_repay_result(cls, repayment_id, result, description): def set_repay_result(cls, repayment_id, result, description):
""" """
Update the repay result of the repayment with the given repayment_id. Update the repay result of the repayment with the given repayment_id.
""" """
return Repayment.set_repay_result(repayment_id, result, description) return Repayment.set_repay_result(repayment_id, result, description)
@classmethod @classmethod
def set_verify_date_result(cls, repayment_id, result, description): def set_verify_date_result(cls, repayment_id, result, description):
""" """
Update the verify result of the repayment with the given repayment_id. Update the verify result of the repayment with the given repayment_id.
""" """
return Repayment.set_verify_date_result(repayment_id, result, description) return Repayment.set_verify_date_result(repayment_id, result, description)
@classmethod @classmethod
def get_latest_repayment_without_repay_date(cls): def get_latest_repayment_without_repay_date(cls):
""" """
Get the latest repayment without a repay date. Get the latest repayment without a repay date.
""" """
return Repayment.get_latest_repayment_without_repay_date() return Repayment.get_latest_repayment_without_repay_date()
@classmethod @classmethod
def get_latest_repayment_with_loanId(cls,loan_id): def get_latest_repayment_with_loanId(cls,loan_id):
""" """
Get the latest repayment with loan id. Get the latest repayment with loan id.
""" """
return Repayment.get_latest_repayment_with_loanId(loan_id) return Repayment.get_latest_repayment_with_loanId(loan_id)
@classmethod @classmethod
def get_latest_loan_with_repay_date(cls): def get_latest_loan_with_repay_date(cls):
""" """
Get the latest repayment with a repay date and no verification date. Get the latest repayment with a repay date and no verification date.
""" """
return Repayment.get_latest_loan_with_repay_date() return Repayment.get_latest_loan_with_repay_date()
@classmethod @classmethod
def add_repayment(cls, data): def add_repayment(cls, data):
""" """
Add a new repayment entry. Add a new repayment entry.
""" """
return Repayment.add_repayment(data) return Repayment.add_repayment(data)
@classmethod @classmethod
def create_repayment(cls, repayment_data): def create_repayment(cls, repayment_data):
""" """
Add a new repayment entry. Add a new repayment entry.
""" """
return Repayment.create_repayment(repayment_data) return Repayment.create_repayment(repayment_data)
+10 -10
View File
@@ -1,11 +1,11 @@
from app.models import RepaymentsData from app.models import RepaymentsData
class RepaymentService: class RepaymentService:
@classmethod @classmethod
def add_repayment_data(cls,data): def add_repayment_data(cls,data):
""" """
Add a new repayment data entry. Add a new repayment data entry.
""" """
return RepaymentsData.add_repayment_data(data) return RepaymentsData.add_repayment_data(data)
+23 -23
View File
@@ -1,24 +1,24 @@
from app.models import Salary from app.models import Salary
class SalaryService: class SalaryService:
@classmethod @classmethod
def add_salary_data(cls,data): def add_salary_data(cls,data):
""" """
Add a new salary data entry. Add a new salary data entry.
""" """
return Salary.add_salary_data(data) return Salary.add_salary_data(data)
@classmethod @classmethod
def get_pending_salaries(cls): def get_pending_salaries(cls):
""" """
Get the pending salary for a given customer. Get the pending salary for a given customer.
""" """
return Salary.get_pending_salaries() return Salary.get_pending_salaries()
@classmethod @classmethod
def update_status(cls, salary_id, status): def update_status(cls, salary_id, status):
""" """
Update the status of the salary with the given salary_id. Update the status of the salary with the given salary_id.
""" """
return Salary.update_status(salary_id, status) return Salary.update_status(salary_id, status)
+17 -17
View File
@@ -1,17 +1,17 @@
from app.models import Transaction from app.models import Transaction
class TransactionService: class TransactionService:
@staticmethod @staticmethod
def get_transaction_by_transaction_id(transaction_id): def get_transaction_by_transaction_id(transaction_id):
""" """
Get the transaction by ID Get the transaction by ID
""" """
return Transaction.get_transaction_by_transaction_id(transaction_id) return Transaction.get_transaction_by_transaction_id(transaction_id)
@staticmethod @staticmethod
def create_transaction(transaction_id, account_id, customer_id, type, channel): def create_transaction(transaction_id, account_id, customer_id, type, channel):
""" """
Create Transaction Entry Create Transaction Entry
""" """
return Transaction.create_transaction(transaction_id, account_id, customer_id, type, channel) return Transaction.create_transaction(transaction_id, account_id, customer_id, type, channel)
+45 -45
View File
@@ -1,45 +1,45 @@
from app.config import settings from app.config import settings
import requests import requests
from app.utils.logger import logger from app.utils.logger import logger
def get_headers(): def get_headers():
BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL
BANK_CALL_AUTH_ENDPOINT = settings.BANK_CALL_AUTH_ENDPOINT BANK_CALL_AUTH_ENDPOINT = settings.BANK_CALL_AUTH_ENDPOINT
BANK_CALL_BASIC_AUTH_USERNAME = settings.BANK_CALL_BASIC_AUTH_USERNAME BANK_CALL_BASIC_AUTH_USERNAME = settings.BANK_CALL_BASIC_AUTH_USERNAME
BANK_CALL_BASIC_AUTH_PASSWORD = settings.BANK_CALL_BASIC_AUTH_PASSWORD BANK_CALL_BASIC_AUTH_PASSWORD = settings.BANK_CALL_BASIC_AUTH_PASSWORD
BANK_GRANT_TYPE = settings.BANK_GRANT_TYPE BANK_GRANT_TYPE = settings.BANK_GRANT_TYPE
#authenticate #authenticate
url = f"{BANK_CALL_BASE_URL}{BANK_CALL_AUTH_ENDPOINT}" url = f"{BANK_CALL_BASE_URL}{BANK_CALL_AUTH_ENDPOINT}"
data = { data = {
"grant_type": BANK_GRANT_TYPE, "grant_type": BANK_GRANT_TYPE,
"username": BANK_CALL_BASIC_AUTH_USERNAME, "username": BANK_CALL_BASIC_AUTH_USERNAME,
"password": "G7$k9@pL2!qR" "password": "G7$k9@pL2!qR"
} }
logger.info(f"Calling Bank Call-Auth Endpoint: {url}") logger.info(f"Calling Bank Call-Auth Endpoint: {url}")
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
try: try:
response = requests.post(url, json=data, headers=headers, timeout=10) response = requests.post(url, json=data, headers=headers, timeout=10)
response.raise_for_status() # Raises HTTPError for 4xx/5xx response.raise_for_status() # Raises HTTPError for 4xx/5xx
result = response.json() result = response.json()
# Check if access_token is present # Check if access_token is present
if 'access_token' not in result: if 'access_token' not in result:
logger.error("No access_token found in Bank Call Auth response") logger.error("No access_token found in Bank Call Auth response")
return {"error": "Authentication failed: no access_token returned"} return {"error": "Authentication failed: no access_token returned"}
return { return {
"Content-Type": "application/json", "Content-Type": "application/json",
"x-api-key": settings.BANK_CALL_API_KEY, "x-api-key": settings.BANK_CALL_API_KEY,
"App-Id": settings.BANK_CALL_APP_ID, "App-Id": settings.BANK_CALL_APP_ID,
"Authorization": f"Bearer {result['access_token']}" "Authorization": f"Bearer {result['access_token']}"
} }
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
logger.error(f"Failed to get auth token: {e}") logger.error(f"Failed to get auth token: {e}")
raise raise
except ValueError as e: except ValueError as e:
logger.error(f"Failed to parse auth response JSON: {e}") logger.error(f"Failed to parse auth response JSON: {e}")
raise raise
+15 -15
View File
@@ -1,16 +1,16 @@
def preprocess_loan_charges_data(data): def preprocess_loan_charges_data(data):
""" """
Preprocesses the data into a dictionary for efficient lookups by 'code'. Preprocesses the data into a dictionary for efficient lookups by 'code'.
Args: Args:
data: A list of dictionaries. data: A list of dictionaries.
Returns: Returns:
A dictionary where keys are 'code' values and values are the corresponding dictionaries from the input data. A dictionary where keys are 'code' values and values are the corresponding dictionaries from the input data.
If multiple items have the same code, the last one encountered will be stored. If multiple items have the same code, the last one encountered will be stored.
""" """
preprocessed = {} preprocessed = {}
for item in data: for item in data:
if 'code' in item: if 'code' in item:
preprocessed[item['code']] = item preprocessed[item['code']] = item
return preprocessed return preprocessed
+13 -13
View File
@@ -1,13 +1,13 @@
import logging import logging
# Configure logging # Configure logging
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s", format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[ handlers=[
# logging.StreamHandler(), # Log to console # logging.StreamHandler(), # Log to console
logging.FileHandler("app.log", mode='a') # Log to file logging.FileHandler("app.log", mode='a') # Log to file
] ]
) )
logger = logging.getLogger("DetectionService") logger = logging.getLogger("DetectionService")
+39 -39
View File
@@ -1,40 +1,40 @@
from flask_mail import Message from flask_mail import Message
from flask import current_app from flask import current_app
from app.extensions import mail from app.extensions import mail
import pandas as pd import pandas as pd
from io import BytesIO from io import BytesIO
def get_report_data(): def get_report_data():
""" """
Fetch and return loan summary data. Fetch and return loan summary data.
""" """
return [ return [
{"Type": "Disbursement", "Count": 45}, {"Type": "Disbursement", "Count": 45},
{"Type": "Repayment", "Count": 32}, {"Type": "Repayment", "Count": 32},
] ]
def send_report_email(report_data: list, recipients: list): def send_report_email(report_data: list, recipients: list):
""" """
Sends an HTML + Excel report to the given email recipients. Sends an HTML + Excel report to the given email recipients.
""" """
df = pd.DataFrame(report_data) df = pd.DataFrame(report_data)
output = BytesIO() output = BytesIO()
df.to_excel(output, index=False) df.to_excel(output, index=False)
output.seek(0) output.seek(0)
html_table = df.to_html(index=False, border=1) html_table = df.to_html(index=False, border=1)
msg = Message( msg = Message(
subject="Loan Report Summary", subject="Loan Report Summary",
recipients=recipients, recipients=recipients,
html=f"<h3>Loan Report Summary</h3>{html_table}", html=f"<h3>Loan Report Summary</h3>{html_table}",
) )
msg.attach( msg.attach(
"loan_report.xlsx", "loan_report.xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
output.read() output.read()
) )
with current_app.app_context(): with current_app.app_context():
mail.send(msg) mail.send(msg)
return "Report email sent" return "Report email sent"
+32 -32
View File
@@ -1,32 +1,32 @@
version: "3.8" version: "3.8"
services: services:
flask: flask:
build: . build: .
env_file: env_file:
- .env - .env
ports: ports:
- "5000:5000" - "5000:5000"
environment: environment:
- FLASK_APP=app.py - FLASK_APP=app.py
- FLASK_RUN_HOST=0.0.0.0 - FLASK_RUN_HOST=0.0.0.0
volumes: volumes:
- .:/app - .:/app
restart: always restart: always
networks: networks:
- digital - digital
swagger: swagger:
image: swaggerapi/swagger-ui:v5.1.0 image: swaggerapi/swagger-ui:v5.1.0
ports: ports:
- "9000:8080" - "9000:8080"
volumes: volumes:
- ./openapi.yml:/usr/local/openapi.yml - ./openapi.yml:/usr/local/openapi.yml
environment: environment:
- SWAGGER_JSON=/usr/local/openapi.yml - SWAGGER_JSON=/usr/local/openapi.yml
restart: always restart: always
networks: networks:
- digital - digital
networks: networks:
digital: digital:
driver: bridge driver: bridge
+258 -264
View File
@@ -1,265 +1,259 @@
openapi: 3.0.3 openapi: 3.0.3
info: info:
title: Event Manager API title: Event Manager API
description: The documentation for Event Manager API description: The documentation for Event Manager API
version: 1.0.0 version: 1.0.0
contact: contact:
name: API Support name: API Support
email: support@example.com email: support@example.com
license: license:
name: MIT name: MIT
url: https://opensource.org/licenses/MIT url: https://opensource.org/licenses/MIT
servers: servers:
- url: http://localhost:5000 - url: http://localhost:5000
description: Local development server description: Local development server
- url: http://www.simbrellang.net:5000 - url: http://www.simbrellang.net:5000
description: Remote Temporary development server description: Remote Temporary development server
- url: https://event-core.simbrellang.net - url: https://event-core.simbrellang.net
description: Remote development server description: Remote development server
- url: http://10.2.249.133:5000 - url: http://10.2.249.133:5000
description: Internal development server description: Internal development server
paths: paths:
/health: /health:
get: get:
summary: Returns a health message summary: Returns a health message
responses: responses:
200: 200:
description: A successful response description: A successful response
/status-call: /status-call:
post: post:
summary: Perform a status call summary: Perform a status call
requestBody: requestBody:
required: true required: true
content: content:
application/json: application/json:
schema: schema:
type: object type: object
properties: properties:
requestId: requestId:
type: string type: string
example: "R02802" example: "R02802"
countryCode: countryCode:
type: string type: string
example: "NGR" example: "NGR"
transactionId: transactionId:
type: string type: string
example: "Tr201712RK9232P115" example: "Tr201712RK9232P115"
debtId: debtId:
type: string type: string
example: "173021" example: "173021"
transactionType: transactionType:
type: string type: string
example: "Disbursement" example: "Disbursement"
customerId: customerId:
type: string type: string
example: "CN621868" example: "CN621868"
responses: responses:
200: 200:
description: A successful response description: A successful response
/sms: /sms:
post: post:
summary: Send a SMS summary: Send a SMS
requestBody: requestBody:
required: true required: true
content: content:
application/json: application/json:
schema: schema:
type: object type: object
properties: properties:
text: text:
type: string type: string
example: "This is a test message for SMS request method." example: "This is a test message for SMS request method."
dest: dest:
type: string type: string
example: "+2348039409144" example: "+2348039409144"
unicode: unicode:
type: boolean type: boolean
example: false example: false
responses: responses:
200: 200:
description: A successful response description: A successful response
/bulk-sms: /bulk-sms:
post: post:
summary: Send a bulk SMS summary: Send a bulk SMS
requestBody: requestBody:
required: true required: true
content: content:
application/json: application/json:
schema: schema:
type: array type: array
items: items:
type: object type: object
properties: properties:
text: text:
type: string type: string
example: "This is a test message for SMS request method." example: "This is a test message for SMS request method."
dest: dest:
type: string type: string
example: "+2348039409144" example: "+2348039409144"
unicode: unicode:
type: boolean type: boolean
example: true example: true
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/refresh-verify-disbursement: /autocall/refresh-verify-disbursement:
get: get:
summary: Refresh the disbursement to verify summary: Refresh the disbursement to verify
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/retry-failed-disbursements: /autocall/refresh-disbursement:
get: get:
summary: Retry failed disbursements summary: Refresh the disbursement
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/refresh-disbursement: /autocall/refresh-verify-collection:
get: get:
summary: Refresh the disbursement summary: Refresh the disbursement to verify
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/refresh-verify-collection: /autocall/refresh-collection:
get: get:
summary: Refresh the disbursement to verify summary: Refresh the disbursement
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/refresh-collection: /autocall/payment-callback:
get: get:
summary: Refresh the disbursement summary: The Payment callback
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/payment-callback: /autocall/penal-charge:
get: post:
summary: The Payment callback summary: Penal Charge Request
responses: requestBody:
200: required: true
description: A successful response content:
/autocall/penal-charge: application/json:
post: schema:
summary: Penal Charge Request type: array
requestBody: items:
required: true type: object
content: properties:
application/json: transactionId:
schema: type: string
type: array example: "T004"
items: fbnTransactionId:
type: object type: string
properties: example: "Tr201712RK9232P115"
transactionId: debtId:
type: string type: string
example: "T004" example: "273194670"
fbnTransactionId: customerId:
type: string type: string
example: "Tr201712RK9232P115" example: "CN621868"
debtId: accountId:
type: string type: string
example: "273194670" example: "2017821799"
customerId: penalCharge:
type: string type: number
example: "CN621868" example: "1.2"
accountId: lienAmount:
type: string type: number
example: "2017821799" example: "101.2"
penalCharge: countryId:
type: number type: string
example: "1.2" example: "01"
lienAmount: comment:
type: number type: string
example: "101.2" example: "Testing PenalCharge"
countryId: responses:
type: string 200:
example: "01" description: A successful response
comment: /autocall/analytic-salary-detect:
type: string post:
example: "Testing PenalCharge" summary: Salary Detect Endpoint
responses: requestBody:
200: required: true
description: A successful response content:
/autocall/analytic-salary-detect: application/json:
post: schema:
summary: Salary Detect Endpoint type: object
requestBody: properties:
required: true customerId:
content: type: string
application/json: example: "CN621868"
schema: accountId:
type: object type: string
properties: example: "OP621868"
customerId: status:
type: string type: string
example: "CN621868" salaryAmount:
accountId: type: number
type: string example: 200000
example: "OP621868" salaryDate:
status: type: string
type: string example: "2025-01-01"
salaryAmount: responses:
type: number 200:
example: 200000 description: A successful response
salaryDate:
type: string /autocall/report:
example: "2025-01-01" get:
responses: summary: Generate and send a report
200: responses:
description: A successful response 200:
description: A successful response
/autocall/report: /autocall/overdue-loans:
get: get:
summary: Generate and send a report summary: Get all overdue loans
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/overdue-loans: /autocall/process-penal-charges:
get: get:
summary: Get all overdue loans summary: Get all overdue loans with grace period
responses: responses:
200: 200:
description: A successful response description: A successful response
/autocall/process-penal-charges: /autocall/direct/loan:
get: post:
summary: Get all overdue loans with grace period summary: Direct call for loan disbursement
responses: requestBody:
200: required: true
description: A successful response content:
/autocall/direct/loan: application/json:
post: schema:
summary: Direct call for loan disbursement type: object
requestBody: properties:
required: true transactionId:
content: type: string
application/json: example: "TXN123456"
schema: responses:
type: object 200:
properties: description: A successful response
transactionId: /autocall/direct/repayment:
type: string post:
example: "TXN123456" summary: Direct call for loan repayment
responses: requestBody:
200: required: true
description: A successful response content:
/autocall/direct/repayment: application/json:
post: schema:
summary: Direct call for loan repayment type: object
requestBody: properties:
required: true transactionId:
content: type: string
application/json: example: "TXN123456"
schema:
type: object responses:
properties: 200:
transactionId: description: A successful response
type: string
example: "TXN123456"
responses:
200:
description: A successful response
+16 -16
View File
@@ -1,16 +1,16 @@
# Flask and Extensions # Flask and Extensions
Flask==2.3.3 Flask==2.3.3
Flask-Marshmallow==0.15.0 Flask-Marshmallow==0.15.0
marshmallow==3.19.0 marshmallow==3.19.0
Flask-Cors==3.0.10 Flask-Cors==3.0.10
gunicorn gunicorn
requests requests
confluent-kafka==1.9.2 confluent-kafka==1.9.2
flask-sqlalchemy flask-sqlalchemy
psycopg2-binary psycopg2-binary
alembic alembic
python-dateutil python-dateutil
oracledb oracledb
Flask-Mail==0.10.0 Flask-Mail==0.10.0
pandas==2.1.3 pandas==2.1.3
openpyxl==3.1.5 openpyxl==3.1.5
+36 -36
View File
@@ -1,37 +1,37 @@
import threading import threading
from app import create_app from app import create_app
from app.integrations import KafkaIntegration from app.integrations import KafkaIntegration
from app.config import settings from app.config import settings
from app.utils.logger import logger from app.utils.logger import logger
app = create_app() app = create_app()
kafka = KafkaIntegration() kafka = KafkaIntegration()
def start_kafka_consumer(app): def start_kafka_consumer(app):
with app.app_context(): with app.app_context():
logger.info("Starting Kafka consumer...") logger.info("Starting Kafka consumer...")
while True: while True:
try: try:
message = kafka.receive_messages( message = kafka.receive_messages(
topics=settings.KAFKA_TOPICS, timeout=settings.KAFKA_TIMEOUT topics=settings.KAFKA_TOPICS, timeout=settings.KAFKA_TIMEOUT
) )
if message: if message:
logger.info(f"Processed message: {message}") logger.info(f"Processed message: {message}")
else: else:
logger.info("No message received within timeout") logger.info("No message received within timeout")
except Exception as e: except Exception as e:
logger.error(f"Error while receiving message: {e}") logger.error(f"Error while receiving message: {e}")
if __name__ != "__main__": if __name__ != "__main__":
# Expose WSGI app instance for Gunicorn # Expose WSGI app instance for Gunicorn
wsgi_app = app wsgi_app = app
# Start kafka in a thread # Start kafka in a thread
threading.Thread(target=start_kafka_consumer, args=(app,), daemon=True).start() threading.Thread(target=start_kafka_consumer, args=(app,), daemon=True).start()