Matching Ticket Routes
Matching Ticket Routes
Welcome back! In this lesson, you’ll extend the support ticket API with two routes that look similar in the URL but serve very different purposes: a static statistics route and a dynamic ticket-detail route.
You’ll learn how Express matches routes in declaration order, how a route parameter becomes a safe PostgreSQL query value, and how to return a focused 404 response when a requested ticket does not exist. You’ll also build one aggregate query that calculates all ticket status counts in a single database round trip.
Previously, you explored the Ticket domain model and followed GET /api/tickets from the Express router through the repository to PostgreSQL. Now you’ll keep that collection endpoint intact while adding GET /api/tickets/stats and GET /api/tickets/:ticketId.
Static and Dynamic Route Shapes
The completed ticket router exposes three GET routes under the /api/tickets mount point:
The first route returns the full collection. The second uses the fixed path segment stats, while the third accepts a variable segment such as tkt_1001.
That difference is important because the dynamic route /:ticketId can match any single path segment. Without careful ordering, Express could interpret the word stats as a ticket ID.
Defining the Statistics Contract
The response shape for the statistics route is defined in src/domain/ticket.ts. Keeping this contract in the domain layer gives the repository and route a shared understanding of the expected result.
-
The interface contains one numeric property for each supported ticket status. It describes the JSON object returned by
GET /api/tickets/stats. -
The API uses the camel-case property
inProgress, following the TypeScript naming style already used by fields such asrequesterId. The repository will translate the database-facingin_progressname into this property. -
Each field is a required number, so callers do not need to handle missing counts. Even when no tickets exist for a status, the response should contain that property with the value
0.
A valid statistics response therefore has this shape:
These values are illustrative: a status with no matching tickets still appears with 0, and the counts do not need to be equal.
