Spaces:
Running
Running
add endpoint for retrieving fraud status of existing an transaction
Browse files- src/main.py +47 -6
src/main.py
CHANGED
|
@@ -32,7 +32,7 @@ from src.service.logging_service import setup_logging
|
|
| 32 |
from src.service.notification_service import send_notification
|
| 33 |
from src.repository.common import get_session
|
| 34 |
from src.repository.fraud_details_repo import insert_fraud
|
| 35 |
-
from src.repository.transaction_repo import insert_transaction
|
| 36 |
|
| 37 |
# ------------------------------------------------------------------------------
|
| 38 |
# Configure logging
|
|
@@ -99,7 +99,48 @@ class TransactionProcessingOutput(BaseModel):
|
|
| 99 |
"fraud_score": 0.85
|
| 100 |
}
|
| 101 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
@app.post("/transaction/process",
|
| 104 |
tags=["transaction"],
|
| 105 |
description="Process a transaction",
|
|
@@ -127,11 +168,11 @@ async def process_transaction(
|
|
| 127 |
try:
|
| 128 |
# Insert every single transaction into the database
|
| 129 |
transaction = insert_transaction(db, transaction)
|
| 130 |
-
except UniqueViolation
|
| 131 |
-
logger.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
)
|
| 136 |
except IntegrityError as e:
|
| 137 |
logger.error(e)
|
|
|
|
| 32 |
from src.service.notification_service import send_notification
|
| 33 |
from src.repository.common import get_session
|
| 34 |
from src.repository.fraud_details_repo import insert_fraud
|
| 35 |
+
from src.repository.transaction_repo import insert_transaction, fetch_transaction
|
| 36 |
|
| 37 |
# ------------------------------------------------------------------------------
|
| 38 |
# Configure logging
|
|
|
|
| 99 |
"fraud_score": 0.85
|
| 100 |
}
|
| 101 |
})
|
| 102 |
+
|
| 103 |
+
@app.get("/transaction/get_fraud_status/{transaction_number}",
|
| 104 |
+
tags=["transaction"],
|
| 105 |
+
description="Get the fraud status of a transaction",
|
| 106 |
+
response_model=TransactionProcessingOutput,
|
| 107 |
+
)
|
| 108 |
+
async def get_fraud_status(
|
| 109 |
+
transaction_number: str,
|
| 110 |
+
db: Annotated[Session, Depends(provide_connection)]
|
| 111 |
+
):
|
| 112 |
+
"""
|
| 113 |
+
Get the fraud status of a transaction
|
| 114 |
+
"""
|
| 115 |
+
# Check if the transaction exists in the database
|
| 116 |
+
try:
|
| 117 |
+
transaction = fetch_transaction(
|
| 118 |
+
db=db,
|
| 119 |
+
transaction_number=transaction_number
|
| 120 |
+
)
|
| 121 |
+
except ValueError as e:
|
| 122 |
+
logger.error(e)
|
| 123 |
+
raise HTTPException(
|
| 124 |
+
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
| 125 |
+
detail=f"Transaction {transaction_number} does not exist"
|
| 126 |
+
)
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logger.error(e)
|
| 129 |
+
raise HTTPException(
|
| 130 |
+
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
|
| 131 |
+
detail="An error occurred while fetching the transaction. See logs for details."
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Check if the transaction is fraudulent
|
| 135 |
+
is_fraud = transaction.is_fraud
|
| 136 |
+
fraud_score = transaction.fraud_score
|
| 137 |
|
| 138 |
+
return {
|
| 139 |
+
'is_fraud': is_fraud,
|
| 140 |
+
'fraud_score': fraud_score
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
# ------------------------------------------------------------------------------
|
| 144 |
@app.post("/transaction/process",
|
| 145 |
tags=["transaction"],
|
| 146 |
description="Process a transaction",
|
|
|
|
| 168 |
try:
|
| 169 |
# Insert every single transaction into the database
|
| 170 |
transaction = insert_transaction(db, transaction)
|
| 171 |
+
except UniqueViolation:
|
| 172 |
+
logger.warning("Transaction already exists in the database - retrieving it")
|
| 173 |
+
return get_fraud_status(
|
| 174 |
+
transaction_number=transaction.transaction_number,
|
| 175 |
+
db=db
|
| 176 |
)
|
| 177 |
except IntegrityError as e:
|
| 178 |
logger.error(e)
|