⚙️ Attribute handlers
Attribute 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 |
|---|---|---|---|---|
createAttributeGroup | Handler | Non-empty requestBody, attributes collection | Writes attributeId; creates base entity; 400 or 500 on failure. | createAttributeRoute |
updateAttributeGroup | Handler | Attribute ID, non-empty requestBody, attributes collection | Updates base fields and writes attributeId; 400, 404, or 500 on failure. | updateAttributeRoute |
getAttributeGroupById | Handler | Context or path attributeId, attributes collection | Writes attributeGroup; 400, 404, or 500 on failure. | createAttributeRoute, getAttributeRoute, updateAttributeRoute |
findAttributeGroups | Handler | requestQuery, attributes collection | Writes matching attributeGroups; 500 on failure. | findAttributesRoute |
deleteAttributeGroup | Handler | Context or path attributeId, attributes collection | Writes attributeGroupDeleted; 400, 404, or 500 on failure. | deleteAttributeRoute |
createAttributeGroupTerminator | Terminator | context.data.attributeGroup | Returns normalized data with statusCode: 201; absent data is 404. | createAttributeRoute |
normalizeAttributeGroupTerminator | Terminator | context.data.attributeGroup | Removes _id and returns the group; absent data is 404. | getAttributeRoute, updateAttributeRoute |
normalizeAttributesListTerminator | Terminator | context.data.attributeGroups | Removes _id and preserves pagination metadata; unexpected list data is 500. | findAttributesRoute |
deleteAttributeTerminator | Terminator | context.data.attributeGroupDeleted | Returns { statusCode: 204 }; absent deletion flag is 500. | deleteAttributeRoute |
Details
createAttributeGroup
Implementation
Reads params.requestBody and context.db.attributes. It requires a non-empty body, inserts createBaseEntity(requestBody), then writes the generated attributeId to context.data for the subsequent get handler. A missing body or missing insertedId is 400; a thrown entity/insert operation is 500. Used by createAttributeRoute.
updateAttributeGroup
Implementation
Reads context.data.attributeId before params.requestParams.attributeId, plus params.requestBody and context.db.attributes. A missing ID or empty body is 400, even though the schema permits {}. It applies updateBaseEntity, updates by string ID, and writes attributeId for the reloading handler. No match is 404, no modification is 400, and a thrown update is 500. Used by updateAttributeRoute.
getAttributeGroupById
Implementation
Reads context.data.attributeId before params.requestParams.attributeId and queries context.db.attributes by string ID. A missing ID is 400, no record is 404, and a thrown query is 500. Success writes the stored record to context.data.attributeGroup. Used by createAttributeRoute, getAttributeRoute, and updateAttributeRoute.
findAttributeGroups
Implementation
Reads params.requestQuery and passes it, or {}, directly to context.db.attributes.find, converts the cursor to an array, and writes context.data.attributeGroups. A thrown query/cursor operation is 500; withPagination wraps this handler in the list route. Used by findAttributesRoute.
deleteAttributeGroup
Implementation
Reads context.data.attributeId before params.requestParams.attributeId and deletes from context.db.attributes by string ID. A missing ID is 400, zero deletions are 404, and a false result or thrown deletion is 500. Success writes context.data.attributeGroupDeleted: true. Used by deleteAttributeRoute.
createAttributeGroupTerminator
Implementation
Rethrows a failed pipeline result, reads context.data.attributeGroup, and throws 404 when it is absent. It removes MongoDB _id and returns the exact response descriptor {data: attributeGroup, statusCode: 201}. Used by createAttributeRoute.
normalizeAttributeGroupTerminator
Implementation
Rethrows a failed pipeline result, reads context.data.attributeGroup, and throws 404 when it is absent. It removes MongoDB _id and returns the remaining group as the ordinary 200 response body. Used by getAttributeRoute and updateAttributeRoute.
normalizeAttributesListTerminator
Implementation
Rethrows a failed pipeline result and reads context.data.attributeGroups. It accepts either a raw array or a paginated object whose data is an array; any other shape throws 500. It removes every MongoDB _id, returning an array or {data, metadata: {pagination}}. Used by findAttributesRoute.
deleteAttributeTerminator
Implementation
Rethrows a failed pipeline result, reads context.data.attributeGroupDeleted, and throws 500 when the flag is absent or false. Success returns the exact descriptor {statusCode: 204}. Used by deleteAttributeRoute.