1
0

[add]: transaction fix

This commit is contained in:
VivianDee
2025-04-11 13:15:56 +01:00
parent 8a018545ec
commit e69335cb71
6 changed files with 44 additions and 21 deletions
+1 -2
View File
@@ -50,8 +50,7 @@ class BaseService:
"""
return Transaction.create_transaction(
transaction_id = validated_data.get("transactionId"),
ref_id = validated_data.get("refId") or validated_data.get("accountId"),
ref_model = validated_data.get("refModel", "account"),
account_id = validated_data.get("accountId", None),
type = cls.TRANSACTION_TYPE,
channel = validated_data.get("channel"),
)
-5
View File
@@ -35,11 +35,6 @@ class LoanStatusService(BaseService):
# Get loans
loans = [loan.to_dict() for loan in customer.loans]
validated_data['refId'] = customer.id
validated_data['refModel'] = "customer"
transaction = LoanStatusService.log_transaction(validated_data = validated_data)
if not transaction:
-4
View File
@@ -51,10 +51,6 @@ class ProvideLoanService(BaseService):
"message": "Failed to save loan details."
}), 400
db.session.flush()
validated_data['refId'] = loan.id
validated_data['refModel'] = "loan"
# Log Transaction
transaction = ProvideLoanService.log_transaction(validated_data = validated_data)
+1 -5
View File
@@ -47,11 +47,7 @@ class RepaymentService(BaseService):
return jsonify({
"message": "Failed to save repayment details."
}), 400
db.session.flush()
validated_data['refId'] = repayment.id
validated_data['refModel'] = "repayment"
#Update Loan status
Loan.update_status(loan_id = loan_id, status = LoanStatus.REPAID)
+4 -5
View File
@@ -1,5 +1,6 @@
from datetime import datetime, timezone
from app.extensions import db
from app.models import account
from sqlalchemy.exc import IntegrityError
from sqlalchemy import and_, or_, not_
@@ -11,8 +12,7 @@ class Transaction(db.Model):
autoincrement=True,
)
transaction_id = db.Column(db.String(50), nullable=False)
ref_id = db.Column(db.String(50), nullable=False)
ref_model = db.Column(db.String(50), nullable=True, default='account')
account_id = db.Column(db.String(50), nullable=True)
type = db.Column(db.String(50), nullable=False)
channel = db.Column(db.String(50), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
@@ -22,7 +22,7 @@ class Transaction(db.Model):
return f'<Transaction {self.id}>'
@classmethod
def create_transaction(cls, transaction_id, ref_id, ref_model, type, channel):
def create_transaction(cls, transaction_id, account_id, type, channel):
# if cls.query.filter_by(transaction_id=transaction_id).first():
# raise ValueError("Duplicate Transaction")
@@ -34,8 +34,7 @@ class Transaction(db.Model):
transaction = cls(
transaction_id = transaction_id,
ref_id = ref_id,
ref_model = ref_model,
account_id = account_id,
type = type,
channel = channel
)
@@ -0,0 +1,38 @@
"""Migration on Fri Apr 11 12:02:45 UTC 2025
Revision ID: fd447d78b161
Revises: 1340e7e578b9
Create Date: 2025-04-11 12:03:28.346671
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'fd447d78b161'
down_revision = '1340e7e578b9'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('transactions', schema=None) as batch_op:
batch_op.alter_column('account_id',
existing_type=sa.VARCHAR(length=50),
nullable=True)
batch_op.drop_column('ref_model')
# ### 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.add_column(sa.Column('ref_model', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
batch_op.alter_column('account_id',
existing_type=sa.VARCHAR(length=50),
nullable=False)
# ### end Alembic commands ###