|
3 | 3 | Policies represent tax-benefit parameter reforms that can be compared against |
4 | 4 | baseline (current law). Create a policy, then use its ID with the household |
5 | 5 | calculation or economic impact endpoints to see the reform's effects. |
| 6 | +
|
| 7 | +WORKFLOW: To analyze a policy reform (e.g. lowering UK basic income tax rate to 16%): |
| 8 | +
|
| 9 | +1. Search for the parameter: GET /parameters?search=basic_rate |
| 10 | +2. Note the parameter_id from the results |
| 11 | +3. Create a policy with parameter values: |
| 12 | + POST /policies |
| 13 | + { |
| 14 | + "name": "Lower basic rate to 16p", |
| 15 | + "description": "Reduce UK basic income tax rate from 20p to 16p", |
| 16 | + "parameter_values": [ |
| 17 | + { |
| 18 | + "parameter_id": "<uuid-from-step-1>", |
| 19 | + "value_json": 0.16, |
| 20 | + "start_date": "2026-01-01T00:00:00Z", |
| 21 | + "end_date": null |
| 22 | + } |
| 23 | + ] |
| 24 | + } |
| 25 | +4. Test on a household: POST /household/impact with the policy_id |
| 26 | +5. Run population analysis: POST /analysis/economic-impact with policy_id and dataset_id |
| 27 | +6. Poll GET /analysis/economic-impact/{report_id} until status="completed" |
6 | 28 | """ |
7 | 29 |
|
| 30 | +from datetime import datetime |
8 | 31 | from typing import List |
9 | 32 | from uuid import UUID |
10 | 33 |
|
11 | 34 | from fastapi import APIRouter, Depends, HTTPException |
12 | 35 | from sqlmodel import Session, select |
13 | 36 |
|
14 | | -from policyengine_api.models import Policy, PolicyCreate, PolicyRead |
| 37 | +from policyengine_api.models import ( |
| 38 | + Parameter, |
| 39 | + ParameterValue, |
| 40 | + Policy, |
| 41 | + PolicyCreate, |
| 42 | + PolicyRead, |
| 43 | +) |
15 | 44 | from policyengine_api.services.database import get_session |
16 | 45 |
|
17 | 46 | router = APIRouter(prefix="/policies", tags=["policies"]) |
18 | 47 |
|
19 | 48 |
|
20 | 49 | @router.post("/", response_model=PolicyRead) |
21 | 50 | def create_policy(policy: PolicyCreate, session: Session = Depends(get_session)): |
22 | | - """Create a new policy reform. |
| 51 | + """Create a new policy reform with parameter values. |
23 | 52 |
|
24 | 53 | Policies define changes to tax-benefit parameters. After creating a policy, |
25 | 54 | use its ID with /household/calculate or /analysis/economic-impact to see effects. |
| 55 | +
|
| 56 | + Include parameter_values in the request to specify which parameters to change: |
| 57 | + { |
| 58 | + "name": "Lower basic rate to 16p", |
| 59 | + "description": "Reduce UK basic income tax rate from 20p to 16p", |
| 60 | + "parameter_values": [ |
| 61 | + { |
| 62 | + "parameter_id": "uuid-from-parameters-search", |
| 63 | + "value_json": 0.16, |
| 64 | + "start_date": "2026-01-01T00:00:00Z", |
| 65 | + "end_date": null |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
26 | 69 | """ |
27 | | - db_policy = Policy.model_validate(policy) |
| 70 | + # Create the policy |
| 71 | + db_policy = Policy(name=policy.name, description=policy.description) |
28 | 72 | session.add(db_policy) |
| 73 | + session.flush() # Get the policy ID before adding parameter values |
| 74 | + |
| 75 | + # Create associated parameter values |
| 76 | + for pv_data in policy.parameter_values: |
| 77 | + # Validate parameter exists |
| 78 | + param = session.get(Parameter, pv_data["parameter_id"]) |
| 79 | + if not param: |
| 80 | + raise HTTPException( |
| 81 | + status_code=404, |
| 82 | + detail=f"Parameter {pv_data['parameter_id']} not found", |
| 83 | + ) |
| 84 | + |
| 85 | + # Parse dates |
| 86 | + start_date = ( |
| 87 | + datetime.fromisoformat(pv_data["start_date"].replace("Z", "+00:00")) |
| 88 | + if isinstance(pv_data["start_date"], str) |
| 89 | + else pv_data["start_date"] |
| 90 | + ) |
| 91 | + end_date = None |
| 92 | + if pv_data.get("end_date"): |
| 93 | + end_date = ( |
| 94 | + datetime.fromisoformat(pv_data["end_date"].replace("Z", "+00:00")) |
| 95 | + if isinstance(pv_data["end_date"], str) |
| 96 | + else pv_data["end_date"] |
| 97 | + ) |
| 98 | + |
| 99 | + # Create parameter value |
| 100 | + db_pv = ParameterValue( |
| 101 | + parameter_id=pv_data["parameter_id"], |
| 102 | + value_json=pv_data["value_json"], |
| 103 | + start_date=start_date, |
| 104 | + end_date=end_date, |
| 105 | + policy_id=db_policy.id, |
| 106 | + ) |
| 107 | + session.add(db_pv) |
| 108 | + |
29 | 109 | session.commit() |
30 | 110 | session.refresh(db_policy) |
31 | 111 | return db_policy |
|
0 commit comments