refactor(campaigns): remove dead future-start_date scheduling code
Now that only the manager sets start_date, and only at the moment of approval (which immediately activates the campaign), a draft campaign can never have a start_date. This made three things permanently unreachable: the hourly _run_scheduled_campaign_activation cron job, the activate_campaign 409/force future-date guard, and a query filter hiding tests from scheduled-but-inactive draft campaigns. Removes all three rather than leaving dead code behind. Also adds the missing manager-cannot-directly-activate router test.
This commit is contained in:
@@ -785,36 +785,18 @@ def remove_test_from_campaign(
|
||||
def activate_campaign(
|
||||
# Entry: campaign_id
|
||||
campaign_id: str,
|
||||
force: bool = Query(False, description="Activate even if start_date is in the future"),
|
||||
db: Session = Depends(get_db),
|
||||
# admin passes automatically via require_any_role's built-in bypass — do not add "admin" here
|
||||
current_user: User = Depends(require_any_role("admin")),
|
||||
):
|
||||
"""Activate a campaign, moving it from draft to active.
|
||||
"""Admin-only emergency override: activate a draft campaign directly, bypassing the approval queue.
|
||||
|
||||
If the campaign has a start_date in the future and force=False, returns a 409
|
||||
with a warning so the frontend can show a confirmation modal. If force=True,
|
||||
activates immediately regardless of start_date.
|
||||
Every other path to 'active' goes through /submit -> /approve, where the
|
||||
manager sets start_date. A draft campaign never has a start_date set
|
||||
(only /approve sets it, in the same call that moves the campaign to
|
||||
'active'), so there is no "scheduled for the future" case to guard
|
||||
against here anymore.
|
||||
"""
|
||||
from fastapi import HTTPException
|
||||
campaign_obj = db.query(Campaign).filter(Campaign.id == uuid.UUID(campaign_id)).first()
|
||||
if campaign_obj and campaign_obj.start_date and not force:
|
||||
now = datetime.utcnow()
|
||||
if campaign_obj.start_date > now:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "start_date_in_future",
|
||||
"start_date": campaign_obj.start_date.strftime("%Y-%m-%d"),
|
||||
"message": (
|
||||
f"This campaign is scheduled to start on "
|
||||
f"{campaign_obj.start_date.strftime('%d %b %Y')}. "
|
||||
f"It will activate automatically on that date. "
|
||||
f"Do you want to activate it now anyway?"
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
with UnitOfWork(db) as uow:
|
||||
# Assign campaign = crud_activate(db, campaign_id)
|
||||
campaign = crud_activate(db, campaign_id)
|
||||
|
||||
Reference in New Issue
Block a user