Skip to content

Commit 6d78026

Browse files
Copilottikazyq
andcommitted
Address code review feedback: add projectId validation and improve PATCH endpoint clarity
Co-authored-by: tikazyq <[email protected]>
1 parent 891b977 commit 6d78026

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

apps/web/app/api/events/route.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,26 @@ export async function POST(request: NextRequest) {
105105
);
106106
}
107107

108+
// Validate and parse projectId
109+
const projectId =
110+
typeof body.projectId === 'number' ? body.projectId : parseInt(body.projectId, 10);
111+
if (isNaN(projectId) || projectId <= 0) {
112+
return NextResponse.json(
113+
{
114+
success: false,
115+
error: 'Invalid projectId: must be a positive integer',
116+
},
117+
{ status: 400 },
118+
);
119+
}
120+
108121
// Create event input
109122
const eventInput: CreateAgentEventInput = {
110123
type: body.type,
111124
agentId: body.agentId,
112125
agentVersion: body.agentVersion,
113126
sessionId: body.sessionId,
114-
projectId: parseInt(body.projectId),
127+
projectId: projectId,
115128
context: body.context,
116129
data: body.data,
117130
metrics: body.metrics,

apps/web/app/api/sessions/[id]/route.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,20 @@ export async function PATCH(request: NextRequest, { params }: { params: { id: st
7676
);
7777
}
7878

79-
// Handle special case: ending a session with just outcome
80-
if (body.outcome && Object.keys(body).length === 1) {
79+
// Special case: If only 'outcome' is provided, use endSession which also sets endTime and duration
80+
// This is a convenience for the common case of just ending a session
81+
const bodyKeys = Object.keys(body);
82+
const isJustOutcome = bodyKeys.length === 1 && bodyKeys[0] === 'outcome';
83+
84+
if (isJustOutcome) {
8185
const updatedSession = await sessionService.endSession(id, body.outcome as SessionOutcome);
8286
return NextResponse.json({
8387
success: true,
8488
data: updatedSession,
8589
});
8690
}
8791

88-
// Handle general update
92+
// General update: Apply all provided fields
8993
const updateInput: UpdateAgentSessionInput = {};
9094

9195
if (body.endTime) updateInput.endTime = new Date(body.endTime);

apps/web/app/api/sessions/route.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,24 @@ export async function POST(request: NextRequest) {
9797
);
9898
}
9999

100+
// Validate and parse projectId
101+
const projectId =
102+
typeof body.projectId === 'number' ? body.projectId : parseInt(body.projectId, 10);
103+
if (isNaN(projectId) || projectId <= 0) {
104+
return NextResponse.json(
105+
{
106+
success: false,
107+
error: 'Invalid projectId: must be a positive integer',
108+
},
109+
{ status: 400 },
110+
);
111+
}
112+
100113
// Create session input
101114
const sessionInput: CreateAgentSessionInput = {
102115
agentId: body.agentId,
103116
agentVersion: body.agentVersion,
104-
projectId: parseInt(body.projectId),
117+
projectId: projectId,
105118
context: body.context,
106119
};
107120

0 commit comments

Comments
 (0)