Helpers
Utility functions live in apps/backend/src/helpers/. These are pure functions with no side effects, shared across controllers and business classes.
response.helper.js
The single entry point for all HTTP responses. Do not call res.status().json() directly in controllers — use these helpers instead.
Functions
import {
successResponse,
createdResponse,
errorResponse,
internalServerError,
notFoundResponse,
unauthorizedResponse,
forbiddenResponse,
} from '../helpers/response.helper.js'| Function | HTTP Status | Body Shape |
|---|---|---|
successResponse | 200 | { success: true, message, ...data } |
createdResponse | 201 | { success: true, message, ...data } |
errorResponse | 400 | { success: false, message } |
notFoundResponse | 404 | { success: false, message } |
unauthorizedResponse | 401 | { success: false, message } |
forbiddenResponse | 403 | { success: false, message } |
internalServerError | 500 | { success: false, message } |
Signature
All helpers accept a single options object:
successResponse({ res, msg?: string, data?: object, statusCode?: number })Examples
// 200 with data
successResponse({ res, msg: 'Fetched successfully', data: { units } })
// 201 on creation
createdResponse({ res, data: { unit } })
// 400 validation error
errorResponse({ res, msg: 'Unit name is required' })
// 404 not found
notFoundResponse({ res, msg: 'Device not found' })
// 500 catch-all
internalServerError({ res })query.helper.js
Parses standard pagination and sorting query parameters into MongoDB-ready values.
import { parseQueryParams } from '../helpers/query.helper.js'
const { skip, limit, sort, projection } = parseQueryParams(req.query)Parsed Parameters
| Query Param | Default | Description |
|---|---|---|
page | 1 | Current page number |
limit | 10 | Items per page |
order_by | — | Field name to sort by |
order_type | 'asc' | 'asc' or 'desc' |
get_all | false | If true: sets skip = null, limit = null |
drop_down | false | If true: sets minimal projection for dropdowns |
Returns
{
skip: number | null, // (page - 1) * limit
limit: number | null, // items per page (or null if get_all)
sort: { [field]: 1|-1 }, // MongoDB sort object
projection: {}, // field projection (for drop_down)
query: {} // empty base query object
}paginaton.helper.js
Builds a consistent paginated response envelope from query results.
import { getPaginationResponse } from '../helpers/paginaton.helper.js'
const response = getPaginationResponse({
page,
count, // total matching documents
limit,
skip,
data, // array of results
data_field, // key to use in response (e.g. 'units', 'devices')
message, // optional message string
})Response Shape
{
"success": true,
"message": "...",
"total": 42,
"page": 1,
"limit": 10,
"total_pages": 5,
"has_more": true,
"units": [ ... ]
}The data_field parameter controls what key the array is nested under (e.g., passing data_field: 'units' puts the results under response.units).
date.helper.js
import { getCurDate } from '../helpers/date.helper.js'
const now = getCurDate() // → new Date()Trivial helper that returns the current date as a Date object. Used for consistency — all "created at" timestamps in business logic come from this function, making it easy to mock in tests.
