{"success":true,"name":"keep-business-logic-out-of-route-handlers-routes-translate-s","description":"API route files are growing fat with rules, queries, and calculations","category":"ai-brimind","path":"ai-brimind/keep-business-logic-out-of-route-handlers-routes-translate-s/SKILL.md","file":"SKILL.md","content":"---\nname: keep-business-logic-out-of-route-handlers-routes-translate-s\ndescription: \"API route files are growing fat with rules, queries, and calculations\"\n---\n\n# Keep Business Logic Out of Route Handlers: Routes Translate, Services Decide\n\n## When to use\nEndpoint handlers mix HTTP plumbing with business rules (pricing, eligibility, status changes), so the same logic is copy-pasted between routes or unreachable from jobs and scripts.\n\n## Example request\nThe 'register member' rules live inside the POST route → the import script and the admin panel each re-implement them slightly differently; logic trapped in a route can only be reached by HTTP, so it gets cloned. A 200-line handler validates, computes prices, writes three collections, and sends email → nothing in it can be tested or reused alone; routes should read like a table of contents, not the book. The same discount calculation exists in two endpoints and already disagrees → one service function called by both is the cure and the prevention. A trivial one-query GET → a service layer adds ceremony for nothing; extract when logic appears, not by dogma.\n\n## Instructions\nAct as a backend developer who gives every layer one job and holds it to that job: the route handler is a translator at the border — it speaks HTTP (parse and validate input, call the right internal function, shape the response per your contracts) — while the business rules live in plain service functions that know nothing about req and res, because rules trapped inside HTTP handlers can only ever be invoked by HTTP, and the day a cron job, a CLI script, an import, or a second endpoint needs the same rule, it gets copy-pasted and the copies start disagreeing. Do this: (1) When a handler grows past plumbing — the moment it contains a decision (may this user do this? what does this cost? what状态 follows?) or a multi-step write — extract that into a named service function (registerMember(data, tenantId), renewMembership(id)) in a services/ or lib/ module; the route becomes: validate input → call service → map result/errors to HTTP. Reading a route should tell you WHAT happens, the service tells you HOW. (2) Keep the service layer HTTP-free on purpose: it takes plain typed arguments and returns plain results or throws domain errors ('MembershipExpired'), and the route translates those into your API error contract's status codes — that separation is exactly what lets a scheduled job or seed script call the same function tomorrow with zero duplication, which is your reuse discipline applied to your own code. (3) Give each service the disciplines you already hold routes to: it owns the transaction boundary for its multi-step writes, it enforces the state-machine transitions for its entity, it does the money math with the shared utilities — so those guarantees exist ONCE, behind the function name, instead of per-endpoint. (4) Resist both extremes and say which side you are on: a one-query GET can stay inline (extraction has a cost too), but the SECOND place that needs a rule is the deadline — extract on the second use at the latest, before the copies drift. (5) Verify with the reuse test: pick your most important business action and ask 'could a script call this tonight without an HTTP request?' Only call the backend structured once the answer is yes for every rule that matters — an app whose rules live only in its routes is not a system with an API, it is an API pretending to be a system.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #235\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}