[add]: DB migrations fix and Save loan repayment details

This commit is contained in:
VivianDee
2025-04-10 17:50:25 +01:00
parent f252e33be2
commit 1081467f6f
6 changed files with 117 additions and 11 deletions
+10 -8
View File
@@ -33,14 +33,6 @@ class ProvideLoanService(BaseService):
if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)): if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
transaction = ProvideLoanService.log_transaction(validated_data = validated_data)
if not transaction:
logger.error(f"Failed to log transaction")
return jsonify({
"message": "Failed to log transaction."
}), 400
# Save the loan details # Save the loan details
loan = Loan.create_loan( loan = Loan.create_loan(
customer_id=customer_id, customer_id=customer_id,
@@ -56,6 +48,16 @@ class ProvideLoanService(BaseService):
"message": "Failed to save loan details." "message": "Failed to save loan details."
}), 400 }), 400
# Log Transaction
transaction = ProvideLoanService.log_transaction(validated_data = validated_data)
if not transaction:
logger.error(f"Failed to log transaction")
return jsonify({
"message": "Failed to log transaction."
}), 400
else: else:
return jsonify({ return jsonify({
+16
View File
@@ -1,5 +1,6 @@
from flask import request, jsonify from flask import request, jsonify
from marshmallow import ValidationError from marshmallow import ValidationError
from app.models import Repayment
from app.utils.logger import logger from app.utils.logger import logger
from app.api.schemas.repayment import RepaymentSchema from app.api.schemas.repayment import RepaymentSchema
from app.api.services.base_service import BaseService from app.api.services.base_service import BaseService
@@ -30,6 +31,21 @@ class RepaymentService(BaseService):
if (RepaymentService.validate_account_ownership(account_id = account.id, customer_id = customer_id)): if (RepaymentService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
# Save the repayment details
repayment = Repayment.create_repayment(
customer_id = customer_id,
loan_id = validated_data.get('debtId'),
product_id = validated_data.get('productId')
)
if not repayment:
logger.error(f"Failed to save repayment details")
return jsonify({
"message": "Failed to save repayment details."
}), 400
transaction = RepaymentService.log_transaction(validated_data = validated_data) transaction = RepaymentService.log_transaction(validated_data = validated_data)
if not transaction: if not transaction:
+2 -1
View File
@@ -2,5 +2,6 @@ from .customer import Customer
from .account import Account from .account import Account
from .loan import Loan from .loan import Loan
from .transaction import Transaction from .transaction import Transaction
from .repayment import Repayment
__all__ = ['Customer', 'Account', 'Loan', 'Transaction'] __all__ = ['Customer', 'Account', 'Loan', 'Transaction', 'Repayment']
+2 -2
View File
@@ -7,7 +7,7 @@
}, },
"debtId": { "debtId": {
"type": "string", "type": "string",
"example": "273194670" "example": "10"
}, },
"productId": { "productId": {
"type": "string", "type": "string",
@@ -19,7 +19,7 @@
}, },
"customerId": { "customerId": {
"type": "string", "type": "string",
"example": "CN621868" "example": "CID0000025585"
}, },
"channel": { "channel": {
"type": "string", "type": "string",
@@ -0,0 +1,86 @@
"""Migration on Thu Apr 10 16:21:45 UTC 2025
Revision ID: b8f6fd76ead8
Revises:
Create Date: 2025-04-10 16:22:15.946157
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = 'b8f6fd76ead8'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('repayments',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('loan_id', sa.String(length=50), nullable=False),
sa.Column('customer_id', sa.String(length=50), nullable=False),
sa.Column('product_id', sa.String(length=20), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('loans', schema=None) as batch_op:
batch_op.alter_column('id',
existing_type=sa.VARCHAR(length=50),
type_=sa.Integer(),
existing_nullable=False,
autoincrement=True,
existing_server_default=sa.text("nextval('loan_id_seq'::regclass)"))
with op.batch_alter_table('transactions', schema=None) as batch_op:
batch_op.alter_column('channel',
existing_type=sa.VARCHAR(length=8),
type_=sa.String(length=50),
existing_nullable=False)
batch_op.alter_column('created_at',
existing_type=postgresql.TIMESTAMP(timezone=True),
type_=sa.DateTime(),
existing_nullable=True,
existing_server_default=sa.text('now()'))
batch_op.alter_column('updated_at',
existing_type=postgresql.TIMESTAMP(timezone=True),
type_=sa.DateTime(),
existing_nullable=True,
existing_server_default=sa.text('now()'))
batch_op.drop_constraint('transactions_id_key', type_='unique')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('transactions', schema=None) as batch_op:
batch_op.create_unique_constraint('transactions_id_key', ['id'])
batch_op.alter_column('updated_at',
existing_type=sa.DateTime(),
type_=postgresql.TIMESTAMP(timezone=True),
existing_nullable=True,
existing_server_default=sa.text('now()'))
batch_op.alter_column('created_at',
existing_type=sa.DateTime(),
type_=postgresql.TIMESTAMP(timezone=True),
existing_nullable=True,
existing_server_default=sa.text('now()'))
batch_op.alter_column('channel',
existing_type=sa.String(length=50),
type_=sa.VARCHAR(length=8),
existing_nullable=False)
with op.batch_alter_table('loans', schema=None) as batch_op:
batch_op.alter_column('id',
existing_type=sa.Integer(),
type_=sa.VARCHAR(length=50),
existing_nullable=False,
autoincrement=True,
existing_server_default=sa.text("nextval('loan_id_seq'::regclass)"))
op.drop_table('repayments')
# ### end Alembic commands ###
+1
View File
@@ -1,6 +1,7 @@
#!/bin/sh #!/bin/sh
echo "Running DB migrations..." echo "Running DB migrations..."
flask db migrate -m "Migration on $(date)"
flask db upgrade flask db upgrade
echo "Starting Gunicorn server..." echo "Starting Gunicorn server..."