API handlers should be lightweight and focused on request/response handling, not business logic. Extract specific parameters from requests and delegate complex operations to dedicated services rather than handling everything in the controller.
API handlers should be lightweight and focused on request/response handling, not business logic. Extract specific parameters from requests and delegate complex operations to dedicated services rather than handling everything in the controller.
Key principles:
Example of what to avoid:
async reassignBooking(bookingUid: string, requestUser: UserWithProfile, request: Request) {
// Handler doing too much work with entire request object
}
Better approach:
async reassignBooking(bookingUid: string, requestUser: UserWithProfile, teamMemberEmail?: string) {
// Extract specific parameter in handler, pass only what's needed
const teamMemberEmail = req.query.teamMemberEmail;
return this.bookingService.reassignBooking(bookingUid, requestUser, teamMemberEmail);
}
This pattern improves testability, reduces coupling, and makes the API surface clearer by explicitly showing what data each operation requires.
Enter the URL of a public GitHub repository