from datetime import datetime, timezone from app.extensions import db from sqlalchemy.orm import relationship from sqlalchemy.exc import IntegrityError from uuid import uuid4 from sqlalchemy.types import JSON class RACCheck(db.Model): __tablename__ = 'rac_checks' id = db.Column(db.Integer, primary_key=True, autoincrement=True) transaction_id = db.Column(db.String(50), nullable=False) customer_id = db.Column(db.String, nullable=False) account_id = db.Column(db.String, nullable=False) rac_response = db.Column(db.JSON, nullable=False) 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)) @classmethod def add_rac_check(cls, customer_id, account_id, transaction_id, data = None): # Save the response rac_check = cls( customer_id = customer_id, account_id = account_id, transaction_id = transaction_id, rac_response = data ) try: db.session.add(rac_check) except IntegrityError as err: raise ValueError(f"Database integrity error: {err}") return rac_check @classmethod def get_all_rac_checks(cls): """ Return all RAC checks in dictionary format. """ rac_checks = cls.query.all() if not rac_checks: return None return rac_checks @classmethod def get_rac_check(cls, customer_id, account_id): """ Return a RAC check by its ID. """ rac_check = cls.query.filter_by( customer_id = customer_id, account_id = account_id,).first() if not rac_check: raise ValueError(f"RAC Check for customer not found") return rac_check def to_dict(self): return { "id": str(self.id), "transactionId": str(self.transaction_id), "customerId": self.customer_id, "accountId": self.account_id, "racResponse": self.rac_response, "createdAt": self.created_at.isoformat(), "updatedAt": self.updated_at.isoformat() if self.updated_at else None } def __repr__(self): return f''