66import uuid
77
88from ...models .vf .commitment import Commitment , CommitmentStatus
9+ from ...models .requests .vf_objects import CommitmentCreate , CommitmentUpdate
910from ...database import get_database
1011from ...repositories .vf .commitment_repo import CommitmentRepository
1112from ...services .vf_bundle_publisher import VFBundlePublisher
@@ -64,14 +65,26 @@ async def get_commitment(commitment_id: str):
6465
6566
6667@router .post ("/" , response_model = dict )
67- async def create_commitment (commitment_data : dict ):
68- """Create a new commitment"""
68+ async def create_commitment (commitment_data : CommitmentCreate ):
69+ """
70+ Create a new commitment.
71+
72+ GAP-43: Now uses Pydantic validation model.
73+
74+ Validates:
75+ - Required fields present
76+ - Field types correct
77+ - Numeric ranges valid
78+ - String lengths reasonable
79+ """
6980 try :
70- if "id" not in commitment_data :
71- commitment_data ["id" ] = f"commitment:{ uuid .uuid4 ()} "
72- commitment_data ["created_at" ] = datetime .now ().isoformat ()
81+ # Convert validated Pydantic model to dict
82+ data = commitment_data .model_dump ()
7383
74- commitment = Commitment .from_dict (commitment_data )
84+ data ["id" ] = f"commitment:{ uuid .uuid4 ()} "
85+ data ["created_at" ] = datetime .now ().isoformat ()
86+
87+ commitment = Commitment .from_dict (data )
7588
7689 db = get_database ()
7790 db .connect ()
@@ -93,8 +106,12 @@ async def create_commitment(commitment_data: dict):
93106
94107
95108@router .patch ("/{commitment_id}" , response_model = dict )
96- async def update_commitment (commitment_id : str , updates : dict ):
97- """Update a commitment's status"""
109+ async def update_commitment (commitment_id : str , updates : CommitmentUpdate ):
110+ """
111+ Update a commitment's status.
112+
113+ GAP-43: Now uses Pydantic validation model.
114+ """
98115 try :
99116 db = get_database ()
100117 db .connect ()
@@ -104,13 +121,13 @@ async def update_commitment(commitment_id: str, updates: dict):
104121 if not commitment :
105122 raise HTTPException (status_code = 404 , detail = "Commitment not found" )
106123
107- # Update status if provided
108- if "status" in updates :
109- commitment .status = CommitmentStatus (updates ["status" ])
124+ # Convert validated Pydantic model to dict
125+ update_dict = updates .model_dump (exclude_unset = True )
110126
111- # Update fulfilled_by_event_id if provided
112- if "fulfilled_by_event_id" in updates :
113- commitment .fulfilled_by_event_id = updates ["fulfilled_by_event_id" ]
127+ # Update fields from validated model
128+ for key , value in update_dict .items ():
129+ if hasattr (commitment , key ):
130+ setattr (commitment , key , value )
114131
115132 updated_commitment = commitment_repo .update (commitment )
116133
0 commit comments