mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 15:12:08 +01:00
feat(workers): update workers
This commit is contained in:
@@ -2,7 +2,7 @@ from datetime import datetime, timedelta
|
||||
from typing import List, Optional
|
||||
import random
|
||||
|
||||
from fastapi import APIRouter, Depends, Response, status
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel, Field, conint, confloat, validator
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -55,8 +55,8 @@ class GeneratedTransaction(BaseModel):
|
||||
|
||||
@router.post("/generate", response_model=List[GeneratedTransaction])
|
||||
async def generate_mock_transactions(
|
||||
options: GenerateOptions,
|
||||
user: User = Depends(current_active_user),
|
||||
options: GenerateOptions,
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
# Seed randomness per user to make results less erratic across multiple calls in quick succession
|
||||
seed = int(datetime.utcnow().timestamp()) ^ int(user.id)
|
||||
@@ -98,46 +98,19 @@ async def generate_mock_transactions(
|
||||
return results
|
||||
|
||||
|
||||
@router.get("/scrape")
|
||||
async def scrape_mock_bank():
|
||||
# 80% of the time: nothing to scrape
|
||||
if random.random() < 0.8:
|
||||
return []
|
||||
|
||||
@router.get("/scrape", response_model=Optional[TransactionRead])
|
||||
async def scrape_mock_bank(
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
# 95% of the time: nothing to scrape
|
||||
if random.random() < 0.95:
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
transactions = []
|
||||
count = random.randint(1, 10)
|
||||
for _ in range(count):
|
||||
transactions.append({
|
||||
"amount": round(random.uniform(-200.0, 200.0), 2),
|
||||
"date": (datetime.utcnow().date() - timedelta(days=random.randint(0, 30))).isoformat(),
|
||||
"description": "Mock transaction",
|
||||
})
|
||||
|
||||
# 5% chance: create a new transaction and return it
|
||||
amount = round(random.uniform(-200.0, 200.0), 2)
|
||||
tx_date = datetime.utcnow().date()
|
||||
|
||||
# Optionally attach a random category owned by this user (if any)
|
||||
res = await session.execute(select(Category).where(Category.user_id == user.id))
|
||||
user_categories = list(res.scalars())
|
||||
chosen_categories = []
|
||||
if user_categories:
|
||||
chosen_categories = [random.choice(user_categories)]
|
||||
|
||||
# Build and persist transaction
|
||||
tx = Transaction(
|
||||
amount=amount,
|
||||
description="Mock bank scrape",
|
||||
user_id=user.id,
|
||||
date=tx_date,
|
||||
)
|
||||
if chosen_categories:
|
||||
tx.categories = chosen_categories
|
||||
|
||||
session.add(tx)
|
||||
await session.commit()
|
||||
await session.refresh(tx)
|
||||
await session.refresh(tx, attribute_names=["categories"]) # ensure categories are loaded
|
||||
|
||||
return TransactionRead(
|
||||
id=tx.id,
|
||||
amount=float(tx.amount),
|
||||
description=tx.description,
|
||||
date=tx.date,
|
||||
category_ids=[c.id for c in (tx.categories or [])],
|
||||
)
|
||||
return transactions
|
||||
|
||||
Reference in New Issue
Block a user