diff --git a/backend/routes/live.py b/backend/routes/live.py index fe38c83..73d4b8a 100644 --- a/backend/routes/live.py +++ b/backend/routes/live.py @@ -383,7 +383,12 @@ async def live_analytics(current_user: User = Depends(require_trainer)): if cids: repeat_offenders = await db.live_recipients.aggregate( [ - {"$match": {"campaign_id": {"$in": cids}, "state": {"$in": ["clicked", "submitted"]}}}, + { + "$match": { + "campaign_id": {"$in": cids}, + "state": {"$in": ["clicked", "submitted"]}, + } + }, {"$group": {"_id": "$email", "campaigns": {"$addToSet": "$campaign_id"}}}, {"$project": {"_id": 0, "email": "$_id", "count": {"$size": "$campaigns"}}}, {"$match": {"count": {"$gte": 2}}}, diff --git a/backend/tests/test_live.py b/backend/tests/test_live.py index b7aef44..ea4742c 100644 --- a/backend/tests/test_live.py +++ b/backend/tests/test_live.py @@ -374,15 +374,43 @@ async def test_live_analytics(api): _, h = await make_user("admin") for cid, launched in [("a1", "2026-01-01T00:00:00+00:00"), ("a2", "2026-02-01T00:00:00+00:00")]: await db.live_campaigns.insert_one( - {"id": cid, "organization_id": None, "status": "closed", "name": cid, - "launched_at": launched, "objective": "click_link"} + { + "id": cid, + "organization_id": None, + "status": "closed", + "name": cid, + "launched_at": launched, + "objective": "click_link", + } ) - await db.live_recipients.insert_many([ - {"campaign_id": "a1", "email": "x@acme.test", "state": "email_opened", "reported": True}, - {"campaign_id": "a1", "email": "repeat@acme.test", "state": "clicked", "reported": False}, - {"campaign_id": "a2", "email": "repeat@acme.test", "state": "clicked", "reported": False}, - {"campaign_id": "a2", "email": "y@acme.test", "state": "email_opened", "reported": True}, - ]) + await db.live_recipients.insert_many( + [ + { + "campaign_id": "a1", + "email": "x@acme.test", + "state": "email_opened", + "reported": True, + }, + { + "campaign_id": "a1", + "email": "repeat@acme.test", + "state": "clicked", + "reported": False, + }, + { + "campaign_id": "a2", + "email": "repeat@acme.test", + "state": "clicked", + "reported": False, + }, + { + "campaign_id": "a2", + "email": "y@acme.test", + "state": "email_opened", + "reported": True, + }, + ] + ) d = (await api.get("/api/live/analytics", headers=h)).json() assert d["overall"]["campaigns"] == 2 assert d["overall"]["recipients"] == 4 and d["overall"]["reported"] == 2