added salary table
This commit was merged in pull request #25.
This commit is contained in:
@@ -10,6 +10,7 @@ from app.services.transactions import TransactionService
|
|||||||
from app.services.repayment import RepaymentService
|
from app.services.repayment import RepaymentService
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.services.repayments_data import RepaymentsData
|
from app.services.repayments_data import RepaymentsData
|
||||||
|
from app.services.salary import SalaryService
|
||||||
|
|
||||||
|
|
||||||
class SimbrellaClient:
|
class SimbrellaClient:
|
||||||
@@ -186,7 +187,18 @@ class SimbrellaClient:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def collect_loan_user_salary_detect(data):
|
def collect_loan_user_salary_detect(data):
|
||||||
#InitiatedBy = SALARY_DETECT
|
#InitiatedBy = SALARY_DETECT
|
||||||
return SimbrellaClient._collect_loan(data)
|
logger.info(f"salary data: {data}")
|
||||||
|
try:
|
||||||
|
salary = SalaryService.add_salary_data(data)
|
||||||
|
if salary:
|
||||||
|
return ResponseHelper.success(salary.to_dict(), "Successful")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.info(f"Failed to save salary: {e}")
|
||||||
|
return ResponseHelper.error(message="Failed to call salary endpoint",
|
||||||
|
status_code=400,
|
||||||
|
error=str(e) )
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def collect_loan_user_due_payment(data):
|
def collect_loan_user_due_payment(data):
|
||||||
@@ -218,6 +230,7 @@ class SimbrellaClient:
|
|||||||
return ResponseHelper.error("Loan not found")
|
return ResponseHelper.error("Loan not found")
|
||||||
|
|
||||||
loan_data = loan.to_dict()
|
loan_data = loan.to_dict()
|
||||||
|
logger.info(f"loan dict : {loan_data}")
|
||||||
|
|
||||||
if repayment_data['repayDate'] is not None:
|
if repayment_data['repayDate'] is not None:
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -242,7 +255,7 @@ class SimbrellaClient:
|
|||||||
"channel": "USSD",
|
"channel": "USSD",
|
||||||
"collectionMethod": "1",
|
"collectionMethod": "1",
|
||||||
"lienAmount": 0,
|
"lienAmount": 0,
|
||||||
"countryId": "01",
|
"countryId": "NG",
|
||||||
"comment": "COLLECT LOAN"
|
"comment": "COLLECT LOAN"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,6 +276,7 @@ class SimbrellaClient:
|
|||||||
"repaymentAmount": collect_loan_data.get('collectAmount'),
|
"repaymentAmount": collect_loan_data.get('collectAmount'),
|
||||||
"responseCode": result.get('responseCode'),
|
"responseCode": result.get('responseCode'),
|
||||||
"responseDescr": result.get('responseMessage'),
|
"responseDescr": result.get('responseMessage'),
|
||||||
|
"balance":result.get('lienAmount')
|
||||||
}
|
}
|
||||||
|
|
||||||
new_repayment_data = RepaymentsData.add_repayment_data(data_to_add)
|
new_repayment_data = RepaymentsData.add_repayment_data(data_to_add)
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ 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
|
||||||
|
|
||||||
__all__ = ['Transaction', 'Repayment', 'Loan', 'LoanCharge', 'Customer', 'Account', 'RepaymentsData']
|
__all__ = ['Transaction', 'Repayment', 'Loan', 'LoanCharge', 'Customer', 'Account', 'RepaymentsData','Salary']
|
||||||
@@ -161,6 +161,14 @@ class Repayment(db.Model):
|
|||||||
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
|
||||||
|
def get_latest_repayment_with_loanId(cls, loan_id):
|
||||||
|
"""
|
||||||
|
Get the latest repayment with loan Id.
|
||||||
|
"""
|
||||||
|
return cls.query.filter(
|
||||||
|
cls.loan_id == loan_id
|
||||||
|
).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):
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class RepaymentsData(db.Model):
|
|||||||
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)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
@@ -27,7 +28,8 @@ class RepaymentsData(db.Model):
|
|||||||
"accountId": self.customer_id,
|
"accountId": self.customer_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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
from app.extensions import db
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from app.utils.logger import logger
|
||||||
|
|
||||||
|
class Salary(db.Model):
|
||||||
|
__tablename__ = "salaries"
|
||||||
|
|
||||||
|
id = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
primary_key=True,
|
||||||
|
autoincrement=True,
|
||||||
|
)
|
||||||
|
customer_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)
|
||||||
|
status = db.Column(db.String(20), nullable=True)
|
||||||
|
created_at = db.Column(db.DateTime, default=datetime.now)
|
||||||
|
updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
|
||||||
|
salary_date = db.Column(db.DateTime, nullable=True)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<Salary {self.id}>'
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the Salary object to a dictionary format for JSON serialization.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'customerId': self.customer_id,
|
||||||
|
'accountId' : self.account_id,
|
||||||
|
'amount': self.amount,
|
||||||
|
'status': self.status,
|
||||||
|
'createdAt': self.created_at.isoformat() if self.created_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,
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def add_salary_data(cls, data):
|
||||||
|
"""
|
||||||
|
Add a new salary data entry.
|
||||||
|
"""
|
||||||
|
logger.info(f"receieved data:{data}")
|
||||||
|
try:
|
||||||
|
new_data = cls(
|
||||||
|
customer_id=data.get('customerId'),
|
||||||
|
|
||||||
|
amount=data.get('amount', 0.0),
|
||||||
|
status=data.get('status'),
|
||||||
|
salary_date = datetime.strptime(data.get('salaryDate'), "%Y-%m-%d").date() if data.get('salaryDate') else None,
|
||||||
|
account_id=data.get('accountId')
|
||||||
|
)
|
||||||
|
db.session.add(new_data)
|
||||||
|
db.session.commit()
|
||||||
|
logger.info("Salary data has been committed.")
|
||||||
|
return new_data
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
logger.info(f"error : {str(e)}")
|
||||||
|
raise Exception(f"Error adding salary data: {str(e)}")
|
||||||
@@ -72,6 +72,7 @@ def refresh_collection():
|
|||||||
logger.info(f"Calling Collection ")
|
logger.info(f"Calling Collection ")
|
||||||
#grab the last repayments with repay date is none
|
#grab the last repayments with repay date is none
|
||||||
repayment = RepaymentService.get_latest_repayment_without_repay_date()
|
repayment = RepaymentService.get_latest_repayment_without_repay_date()
|
||||||
|
#repayment = RepaymentService.get_latest_repayment_with_loanId(13735)
|
||||||
if not repayment:
|
if not repayment:
|
||||||
logger.info(f"No repayment found without disbursement date")
|
logger.info(f"No repayment found without disbursement date")
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -43,6 +43,12 @@ class RepaymentService:
|
|||||||
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
|
||||||
|
def get_latest_repayment_with_loanId(cls,loan_id):
|
||||||
|
"""
|
||||||
|
Get the latest repayment with 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):
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
from app.models import Salary
|
||||||
|
|
||||||
|
class SalaryService:
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def add_salary_data(cls,data):
|
||||||
|
"""
|
||||||
|
Add a new salary data entry.
|
||||||
|
"""
|
||||||
|
return Salary.add_salary_data(data)
|
||||||
+16
-17
@@ -178,23 +178,22 @@ paths:
|
|||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
type: array
|
type: object
|
||||||
items:
|
properties:
|
||||||
type: object
|
customerId:
|
||||||
properties:
|
type: string
|
||||||
salaryDate:
|
example: "CN621868"
|
||||||
type: string
|
accountId:
|
||||||
format: date
|
type: string
|
||||||
example: "2022-01-01"
|
example: "OP621868"
|
||||||
customerId:
|
status:
|
||||||
type: string
|
type: string
|
||||||
example: "CN621868"
|
amount:
|
||||||
accountId:
|
type: number
|
||||||
type: string
|
example: 200000
|
||||||
example: "2017821799"
|
salaryDate:
|
||||||
salaryAmount:
|
type: string
|
||||||
type: number
|
example: "2025-01-01"
|
||||||
example: 200000
|
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: A successful response
|
description: A successful response
|
||||||
Reference in New Issue
Block a user