⚙️ Category handlers
Category handlers are route-pipeline functions, not Express middleware. They read RouteHandlerPayload values from params and context, merge successful values into context.data, or terminate a successful Result with an HTTP response descriptor.
Inventory
| Handler | Pipeline role | Inputs / context | Result / behavior / errors | Used by routes |
|---|---|---|---|---|
createCategory | Handler | Non-empty requestBody, context.db.categories | Writes categoryId; creates base entity; 400 or 500 on failure. | createCategoryRoute |
getCategoryById | Handler | context.data.categoryId or path categoryId, context.db.categories | Writes category; 400, 404, or 500 on failure. | createCategoryRoute, getCategoryRoute, updateCategoryRoute |
findCategories | Handler | requestQuery, context.db.categories | Writes matching categories; 500 on query failure. | findCategoriesRoute |
updateCategory | Handler | Category ID, non-empty requestBody, context.db.categories | Updates base-entity fields and writes update result; 400, 404, or 500. | updateCategoryRoute |
deleteCategory | Handler | Category ID, context.db.categories | Deletes matching category and writes delete result; 400, 404, or 500. | deleteCategoryRoute |
enableCategory | Handler | Category ID, context.db.categories | Sets status to 'active'; 400, 404, or 500. | enableCategoryRoute |
disableCategory | Handler | Category ID, context.db.categories | Sets status to 'inactive'; 400, 404, or 500. | disableCategoryRoute |
normalizeCategoryTerminator | Terminator | context.data.category | Removes _id and returns one category; missing data is 500. | createCategoryRoute, getCategoryRoute, updateCategoryRoute |
normalizeCategoriesListTerminator | Terminator | context.data.categories | Removes _id and preserves pagination metadata; invalid shape is 500. | findCategoriesRoute |
deleteCategoryTerminator | Terminator | context.data.deleteCategory | Returns { statusCode: 204 }; missing data is 500. | deleteCategoryRoute |
enableCategoryTerminator | Terminator | context.data.enableCategory | Returns { statusCode: 204 }; missing data is 500. | enableCategoryRoute |
disableCategoryTerminator | Terminator | context.data.disableCategory | Returns { statusCode: 204 }; missing data is 500. | disableCategoryRoute |
Details
createCategory
Implementation
Rejects a missing or empty body with 400, inserts createBaseEntity(requestBody), and writes its generated id as categoryId. Database failures and missing insertedId return 500 and 400, respectively. Used by createCategoryRoute.
getCategoryById
Implementation
Uses context.data.categoryId before the path parameter. A missing ID is 400, a database failure is 500, and no matching category is 404. Used by createCategoryRoute, getCategoryRoute, and updateCategoryRoute.
findCategories
Implementation
Passes requestQuery (or {}) directly to categories.find, converts the cursor to an array, and writes categories; a database failure is 500. In the exported route, withPagination removes page and limit before this handler and changes toArray() into a paginated { data, metadata } result. Used by findCategoriesRoute. Pagination is applied by that route's withPagination wrapper.
updateCategory
Implementation
Requires an ID and non-empty body, then applies updateBaseEntity(requestBody). Missing ID/body is 400; no match is 404; no modification is 400. Used by updateCategoryRoute.
deleteCategory
Implementation
Deletes by ID and writes deleteCategory. Missing ID is 400, no deletion is 404, and database failures are 500. Used by deleteCategoryRoute.
enableCategory
Implementation
Updates the category with updateBaseEntity({ status: 'active' }). Missing ID is 400, no match is 404, and an unchanged result is 500. Used by enableCategoryRoute.
disableCategory
Implementation
Updates the category with updateBaseEntity({ status: 'inactive' }). Missing ID is 400, no match is 404, and an unchanged result is 500. Used by disableCategoryRoute.
normalizeCategoryTerminator
Implementation
Throws the pipeline error, or a 500 when context.data.category is absent; otherwise returns the category without MongoDB _id. Used by createCategoryRoute, getCategoryRoute, and updateCategoryRoute.
normalizeCategoriesListTerminator
Implementation
Throws the pipeline error, or a 500 unless categories is an array or a paginated { data, metadata } value. It removes _id from every returned category. For the exported route, the paginated input is always normalized to { data, metadata: { pagination } }. Used by findCategoriesRoute.
deleteCategoryTerminator
Implementation
Throws the pipeline error or a 500 when deleteCategory is absent; otherwise returns { statusCode: 204 }. Used by deleteCategoryRoute.
enableCategoryTerminator
Implementation
Throws the pipeline error or a 500 when enableCategory is absent; otherwise returns { statusCode: 204 }. Used by enableCategoryRoute.
disableCategoryTerminator
Implementation
Throws the pipeline error or a 500 when disableCategory is absent; otherwise returns { statusCode: 204 }. Used by disableCategoryRoute.