⚙️ Chat handlers
Chat 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 |
|---|---|---|---|---|
createChatChannel | Handler | requestBody, db.chatChannels | Creates a base entity and writes channelId and channel; 400 for an empty body or failed insert, 500 on database failure. | createChatChannelRoute |
getChatChannelById | Handler | context.data.channelId or path channelId, db.chatChannels | Writes channel; 400 without an ID, 404 when absent, 500 on database failure. | createChatChannelRoute, getChatChannelRoute, updateChatChannelRoute |
findChatChannels | Handler | requestQuery, db.chatChannels | Writes channels from query filter (defaults to {}); 500 on database failure. | findChatChannelsRoute |
updateChatChannel | Handler | Path/context channel ID, requestBody, optional fileStorageDriver | Updates the channel, removes a replaced icon from storage, and writes channelId; 400, 404, or 500 on invalid or failed work. | updateChatChannelRoute |
deleteChatChannel | Handler | Path/context channel ID, db.chatChannels, optional fileStorageDriver | Deletes the channel and existing icon, then writes deleteChatChannel: true; 400, 404, or 500 on failure. | deleteChatChannelRoute |
createChatChannelTerminator | Terminator | context.data.channel, optional fileStorageDriver | Normalizes the created channel and returns { data, statusCode: 201 }; missing data or normalization failure becomes an error. | createChatChannelRoute |
normalizeChatChannelTerminator | Terminator | context.data.channel, optional fileStorageDriver | Returns the normalized channel; missing channel data produces 500. | getChatChannelRoute, updateChatChannelRoute |
normalizeChatChannelsListTerminator | Terminator | Raw or paginated context.data.channels, optional fileStorageDriver | Normalizes every channel while retaining pagination metadata; invalid pipeline data produces 500. | findChatChannelsRoute |
deleteChatChannelTerminator | Terminator | context.data.deleteChatChannel | Returns { statusCode: 204 }; missing success state produces 500. | deleteChatChannelRoute |
getChatMessageById | Handler | context.data.messageId or path messageId, db.chatMessages | Writes chatMessage; 400 without an ID, 404 when absent, 500 on database failure. | getChatMessageRoute, updateChatMessageRoute |
findChatMessages | Handler | requestQuery, db.chatMessages | Sorts by createdAt descending and writes chatMessages; 500 on database failure. | Custom composition only |
updateChatMessage | Handler | Path/context message ID, requestBody, db.chatMessages | Updates the entity and writes messageId; 400, 404, or 500 on failure. | updateChatMessageRoute |
deleteChatMessage | Handler | Path/context message ID, db.chatMessages | Deletes the entity and writes deleteChatMessage: true; 400, 404, or 500 on failure. | deleteChatMessageRoute |
createChatMessageTerminator | Terminator | context.data.chatMessage | Removes _id and returns { data, statusCode: 201 }; missing data produces 500. | Custom composition only |
normalizeChatMessageTerminator | Terminator | context.data.chatMessage | Removes _id and returns the message; missing data produces 500. | Custom composition only |
normalizeChatMessagesListTerminator | Terminator | Raw or paginated context.data.chatMessages | Removes _id from each message and retains pagination metadata; invalid data produces 500. | Custom composition only |
deleteChatMessageTerminator | Terminator | context.data.deleteChatMessage | Returns { statusCode: 204 }; missing success state produces 500. | deleteChatMessageRoute |
createChatSubscription | Handler | requestBody, db.subscriptions | Creates a base entity and writes subscriptionId and subscription; 400 or 500 on failure. | createChatSubscriptionRoute |
getChatSubscriptionById | Handler | context.data.subscriptionId or path subscriptionId, db.subscriptions | Writes subscription; 400 without an ID, 404 when absent, 500 on database failure. | createChatSubscriptionRoute, getChatSubscriptionRoute |
findChatSubscriptions | Handler | requestQuery, db.subscriptions | Writes subscriptions from query filter (defaults to {}); 500 on database failure. | findChatSubscriptionsRoute |
deleteChatSubscription | Handler | Path/context subscription ID, db.subscriptions | Deletes the entity and writes deleteChatSubscription: true; 400, 404, or 500 on failure. | deleteChatSubscriptionRoute |
createChatSubscriptionTerminator | Terminator | context.data.subscription | Removes _id and returns { data, statusCode: 201 }; missing data produces 500. | createChatSubscriptionRoute |
normalizeChatSubscriptionTerminator | Terminator | context.data.subscription | Removes _id and returns the subscription; missing data produces 500. | getChatSubscriptionRoute |
normalizeChatSubscriptionsListTerminator | Terminator | Raw or paginated context.data.subscriptions | Removes _id and retains pagination metadata; invalid data produces 500. | findChatSubscriptionsRoute |
deleteChatSubscriptionTerminator | Terminator | context.data.deleteChatSubscription | Returns { statusCode: 204 }; missing success state produces 500. | deleteChatSubscriptionRoute |
Details
createChatChannel
Implementation
Reads params.requestBody, rejects an absent or empty body with 400, creates a base entity in context.db.chatChannels, and merges both channelId and the created channel into pipeline data. A missing insertedId is 400; thrown database errors become 500. Used by createChatChannelRoute.
getChatChannelById
Implementation
Reads the channel ID from context.data.channelId first and then params.requestParams.channelId, fetches from context.db.chatChannels, and merges the document as channel. Missing IDs return 400, absent channels return 404, and database failures return 500. Used by createChatChannelRoute, getChatChannelRoute, and updateChatChannelRoute.
findChatChannels
Implementation
Uses params.requestQuery || {} directly as the MongoDB filter and merges the resulting array as channels; query failures return 500. findChatChannelsRoute wraps it with pagination before normalization.
updateChatChannel
Implementation
Reads the channel ID from prior data or the path and requires a non-empty body. It loads the previous channel, applies updateBaseEntity, removes a replaced icon through fileStorageDriver when present, and merges channelId for the following lookup. It returns 400 for missing inputs, 404 when the channel is absent, and 500 for database or file deletion failures. Used by updateChatChannelRoute.
deleteChatChannel
Implementation
Reads the channel ID from prior data or the path, loads and deletes the channel, deletes its icon through fileStorageDriver when applicable, and writes deleteChatChannel: true. Missing IDs return 400, a missing channel returns 404, and database or storage failures return 500. Used by deleteChatChannelRoute.
createChatChannelTerminator
Implementation
Requires context.data.channel, normalizes its optional icon with fileStorageDriver, and returns { data: normalizedChannel, statusCode: 201 }. It throws prior errors unchanged and reports 500 if pipeline data is missing or normalization fails. Final step of createChatChannelRoute.
normalizeChatChannelTerminator
Implementation
Requires context.data.channel, normalizes its optional icon with fileStorageDriver, and returns the normalized entity as the ordinary response body. It throws prior errors and emits 500 for missing pipeline data. Used by getChatChannelRoute and updateChatChannelRoute.
normalizeChatChannelsListTerminator
Implementation
Accepts either a raw channel array or the paginated { data, ...metadata } shape, normalizes each icon, and returns the same outer shape with normalized data. Invalid pipeline data produces 500; normalization errors are propagated. Used by findChatChannelsRoute.
deleteChatChannelTerminator
Implementation
Requires context.data.deleteChatChannel and returns { statusCode: 204 }, producing an empty successful response. Missing success state produces 500. Final step of deleteChatChannelRoute.
getChatMessageById
Implementation
Reads the message ID from context.data.messageId first and then the path, fetches from context.db.chatMessages, and merges the document as chatMessage. Missing IDs return 400, absent messages return 404, and database failures return 500. Used by getChatMessageRoute and updateChatMessageRoute.
findChatMessages
Implementation
Uses params.requestQuery as a MongoDB filter, sorts results by createdAt descending, and merges them as chatMessages; database failures return 500. No exported Chat route uses this handler—the current list and read-state routes call the similarly named block export.
updateChatMessage
Implementation
Reads the message ID from prior data or the path, requires a non-empty request body, updates with updateBaseEntity, and merges messageId. It returns 400 for missing inputs, 404 when no message matches, and 500 for database failures. Used by updateChatMessageRoute.
deleteChatMessage
Implementation
Reads the message ID from prior data or the path, deletes it from context.db.chatMessages, and writes deleteChatMessage: true. It returns 400 without an ID, 404 when nothing is deleted, and 500 on database failure. Used by deleteChatMessageRoute.
createChatMessageTerminator
Implementation
Requires context.data.chatMessage, removes MongoDB _id, and returns { data: chatMessage, statusCode: 201 }; missing data produces 500. This legacy terminator is public but is not used by an exported Chat route.
normalizeChatMessageTerminator
Implementation
Requires context.data.chatMessage, removes MongoDB _id, and returns the remaining entity; missing data produces 500. This public legacy terminator is available only for custom composition.
normalizeChatMessagesListTerminator
Implementation
Accepts a raw or paginated context.data.chatMessages collection, removes _id from every message, and preserves pagination metadata. Invalid pipeline data produces 500. No exported Chat route currently uses it.
deleteChatMessageTerminator
Implementation
Requires context.data.deleteChatMessage and returns { statusCode: 204 }; missing success state produces 500. Final step of deleteChatMessageRoute.
createChatSubscription
Implementation
Requires a non-empty body, creates a base entity in context.db.subscriptions, and merges subscriptionId and subscription. A missing body or failed insertion returns 400; database failures return 500. Used by createChatSubscriptionRoute.
getChatSubscriptionById
Implementation
Reads the subscription ID from prior data or the path, fetches from context.db.subscriptions, and merges subscription. Missing IDs return 400, absent subscriptions return 404, and database failures return 500. Used by createChatSubscriptionRoute and getChatSubscriptionRoute.
findChatSubscriptions
Implementation
Uses params.requestQuery || {} as the MongoDB filter and merges the resulting array as subscriptions; database failures return 500. findChatSubscriptionsRoute wraps it with pagination.
deleteChatSubscription
Implementation
Reads the subscription ID from prior data or the path, deletes it, and writes deleteChatSubscription: true. Missing IDs return 400, absent subscriptions return 404, and database failures return 500. Used by deleteChatSubscriptionRoute.
createChatSubscriptionTerminator
Implementation
Requires context.data.subscription, removes MongoDB _id, and returns { data: subscription, statusCode: 201 }; missing data produces 500. Final step of createChatSubscriptionRoute.
normalizeChatSubscriptionTerminator
Implementation
Requires context.data.subscription, removes MongoDB _id, and returns the entity; missing data produces 500. Final step of getChatSubscriptionRoute.
normalizeChatSubscriptionsListTerminator
Implementation
Accepts a raw or paginated subscription collection, removes _id from each item, and preserves pagination metadata. Invalid pipeline data produces 500. Final step of findChatSubscriptionsRoute.
deleteChatSubscriptionTerminator
Implementation
Requires context.data.deleteChatSubscription and returns { statusCode: 204 }; missing success state produces 500. Final step of deleteChatSubscriptionRoute.