diff --git a/db/models.py b/db/models.py index 475fe21..63dc59c 100644 --- a/db/models.py +++ b/db/models.py @@ -24,7 +24,7 @@ class UPIInwardRecord: "TXN_TYPE": self.txn_type, # converted to CR before insert "BENEFICIARY_ACCOUNT": self.credit_account, "STATUS": self.status, - "NOTE": self.note, + "NARRATION": self.note, } diff --git a/db/repository.py b/db/repository.py index cb0a48a..577a58a 100644 --- a/db/repository.py +++ b/db/repository.py @@ -88,24 +88,24 @@ class Repository: logger.info(batch_data) insert_sql = """ - INSERT INTO pacs_db.upi_inward_api_log ( + INSERT INTO pacs_db.inward_upi_api_log ( BANKCODE, RRN, TXN_DATE, TXN_AMT, TXN_TYPE, - CREDIT_ACCOUNT, + BENEFICIARY_ACCOUNT, STATUS, - NOTE + NARRATION ) VALUES ( :BANKCODE, :RRN, :TXN_DATE, :TXN_AMT, :TXN_TYPE, - :CREDIT_ACCOUNT, + :BENEFICIARY_ACCOUNT, :STATUS, - :NOTE + :NARRATION ) """ @@ -144,7 +144,7 @@ class Repository: cursor.execute( """ SELECT COUNT(*) - FROM neft_processed_files + FROM upi_processed_files WHERE filename = :filename AND bankcode = :bankcode """, @@ -173,7 +173,7 @@ class Repository: file_data = processed_file.to_dict() insert_sql = """ - INSERT INTO neft_processed_files ( + INSERT INTO upi_processed_files ( filename, bankcode, file_path, transaction_count, status, error_message, processed_at ) VALUES ( @@ -198,3 +198,105 @@ class Repository: if cursor: cursor.close() conn.close() + + def get_processed_files(self, bankcode: Optional[str] = None) -> List[str]: + conn = self.connector.get_connection() + cursor = None + try: + cursor = conn.cursor() + + if bankcode: + cursor.execute( + """ + SELECT filename + FROM upi_processed_files + WHERE bankcode = :bankcode + ORDER BY processed_at DESC + """, + {'bankcode': bankcode} + ) + else: + cursor.execute( + """ + SELECT filename + FROM upi_processed_files + ORDER BY processed_at DESC + """ + ) + + filenames = [row[0] for row in cursor.fetchall()] + return filenames + + except Exception as e: + logger.error(f"Error retrieving processed files: {e}", exc_info=True) + return [] + finally: + if cursor: + cursor.close() + conn.close() + + # def call_neft_api_txn_post(self) -> bool: + # conn = self.connector.get_connection() + # cursor = None + # try: + # cursor = conn.cursor() + # logger.info("Calling neft_api_txn_post procedure to process all inserted transactions...") + + # try: + # cursor.callproc('neft_api_txn_post') + # except Exception: + # cursor.execute("BEGIN neft_api_txn_post; END;") + + # conn.commit() + # logger.info("neft_api_txn_post procedure executed successfully") + # return True + # except Exception as e: + # logger.error(f"Error calling neft_api_txn_post procedure: {e}", exc_info=True) + # return False + # finally: + # if cursor: + # cursor.close() + # conn.close() + + def verify_tables_exist(self): + conn = self.connector.get_connection() + cursor = None + try: + cursor = conn.cursor() + + try: + cursor.execute("SELECT COUNT(*) FROM inward_upi_api_log WHERE ROWNUM = 1") + logger.info("✓ inward_upi_api_log table exists") + except Exception as e: + logger.error(f"✗ inward_upi_api_log table not found: {e}") + raise SystemExit( + "FATAL: inward_upi_api_log table must be created manually before running this application" + ) + + try: + cursor.execute("SELECT COUNT(*) FROM upi_processed_files WHERE ROWNUM = 1") + logger.info("✓ upi_processed_files table exists") + except Exception as e: + logger.error(f"✗ upi_processed_files table not found: {e}") + raise SystemExit( + "FATAL: upi_processed_files table must be created manually before running this application" + ) + + logger.info("Database tables verified successfully") + + except SystemExit: + raise + except Exception as e: + logger.error(f"Error verifying tables: {e}", exc_info=True) + raise SystemExit(f"FATAL: Error verifying database tables: {e}") + finally: + if cursor: + cursor.close() + conn.close() + + + + + + + diff --git a/sftp/file_monitor.py b/sftp/file_monitor.py index 163eeda..423bd3b 100644 --- a/sftp/file_monitor.py +++ b/sftp/file_monitor.py @@ -38,7 +38,7 @@ class FileMonitor: for bank_code in self.config.bank_codes: remote_path = f"{self.config.sftp_base_path}/{bank_code}/UPI" - # ✅ Pattern for UPI files + # Pattern for UPI files files = self.sftp_client.list_files( remote_path, pattern='*_UPI.txt.gz' diff --git a/upi_parser.py b/upi_parser.py index baeb3a8..d7cf800 100644 --- a/upi_parser.py +++ b/upi_parser.py @@ -176,7 +176,7 @@ class UPIParser: by_type[txn_type]["count"] += 1 by_type[txn_type]["amount"] += amt - # Convert Decimal → string + # Convert Decimal ΓåÆ string by_status_final = { k: {"count": v["count"], "amount": f"{v['amount']:.2f}"} for k, v in by_status.items() @@ -257,9 +257,9 @@ def print_summary(summary: Dict): # ------------------------- if __name__ == "__main__": - file_path = r"C:\Users\2780475\Desktop\Test\TUMLUK_31052026_8C_UPI.txt.gz" + file_path = r"C:\Users\2780475\Desktop\upi_file_based\TUMLUK_05062026_8C_UPI.txt" - parser = UPISwitchParser(file_path) + parser = UPIParser(file_path) transactions, metadata, summary = parser.parse()