[add]: Loan repayment event
This commit is contained in:
@@ -41,9 +41,7 @@ class ProvideLoanService(BaseService):
|
|||||||
"message": "Failed to log transaction."
|
"message": "Failed to log transaction."
|
||||||
}), 400
|
}), 400
|
||||||
|
|
||||||
# Save the loan details
|
# Save the loan details
|
||||||
loan_id = f"loan_{transaction_id}"
|
|
||||||
|
|
||||||
loan = Loan.create_loan(
|
loan = Loan.create_loan(
|
||||||
customer_id=customer_id,
|
customer_id=customer_id,
|
||||||
account_id=account_id,
|
account_id=account_id,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
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
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
class Account(db.Model):
|
class Account(db.Model):
|
||||||
__tablename__ = 'accounts'
|
__tablename__ = 'accounts'
|
||||||
@@ -27,8 +28,13 @@ class Account(db.Model):
|
|||||||
customer_id=customer_id,
|
customer_id=customer_id,
|
||||||
account_type=account_type
|
account_type=account_type
|
||||||
)
|
)
|
||||||
db.session.add(account)
|
|
||||||
db.session.commit()
|
try:
|
||||||
|
db.session.add(account)
|
||||||
|
db.session.commit()
|
||||||
|
except IntegrityError as err:
|
||||||
|
db.session.rollback()
|
||||||
|
raise ValueError(f"Database integrity error: {err}")
|
||||||
return account
|
return account
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
+15
-10
@@ -2,6 +2,7 @@ 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
|
||||||
from app.models.account import Account
|
from app.models.account import Account
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
class Customer(db.Model):
|
class Customer(db.Model):
|
||||||
__tablename__ = 'customers'
|
__tablename__ = 'customers'
|
||||||
@@ -33,16 +34,20 @@ class Customer(db.Model):
|
|||||||
|
|
||||||
# Create the customer
|
# Create the customer
|
||||||
customer = cls(id=id, msisdn=msisdn, country_code=country_code)
|
customer = cls(id=id, msisdn=msisdn, country_code=country_code)
|
||||||
db.session.add(customer)
|
try:
|
||||||
|
db.session.add(customer)
|
||||||
# Create an associated account
|
|
||||||
account = Account.create_account(
|
# Create an associated account
|
||||||
id=account_id,
|
account = Account.create_account(
|
||||||
customer_id=id,
|
id=account_id,
|
||||||
account_type=account_type
|
customer_id=id,
|
||||||
)
|
account_type=account_type
|
||||||
|
)
|
||||||
db.session.commit()
|
|
||||||
|
db.session.commit()
|
||||||
|
except IntegrityError as err:
|
||||||
|
db.session.rollback()
|
||||||
|
raise ValueError(f"Database integrity error: {err}")
|
||||||
return customer
|
return customer
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
+7
-2
@@ -2,6 +2,7 @@ from datetime import datetime, timezone
|
|||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.models.customer import Customer
|
from app.models.customer import Customer
|
||||||
from app.models.account import Account
|
from app.models.account import Account
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
|
|
||||||
class Loan(db.Model):
|
class Loan(db.Model):
|
||||||
@@ -44,8 +45,12 @@ class Loan(db.Model):
|
|||||||
status=status
|
status=status
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.add(loan)
|
try:
|
||||||
db.session.commit()
|
db.session.add(loan)
|
||||||
|
db.session.commit()
|
||||||
|
except IntegrityError as err:
|
||||||
|
db.session.rollback()
|
||||||
|
raise ValueError(f"Database integrity error: {err}")
|
||||||
return loan
|
return loan
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from datetime import datetime, timezone
|
|||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.models.customer import Customer
|
from app.models.customer import Customer
|
||||||
from app.models.loan import Loan
|
from app.models.loan import Loan
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
|
|
||||||
class Repayment(db.Model):
|
class Repayment(db.Model):
|
||||||
@@ -37,8 +38,13 @@ class Repayment(db.Model):
|
|||||||
product_id=product_id,
|
product_id=product_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.add(repayment)
|
try:
|
||||||
db.session.commit()
|
db.session.add(repayment)
|
||||||
|
db.session.commit()
|
||||||
|
except IntegrityError as err:
|
||||||
|
db.session.rollback()
|
||||||
|
raise ValueError(f"Database integrity error: {err}")
|
||||||
|
|
||||||
return repayment
|
return repayment
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user