Skip to content

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

js
import {
  successResponse,
  createdResponse,
  errorResponse,
  internalServerError,
  notFoundResponse,
  unauthorizedResponse,
  forbiddenResponse,
} from '../helpers/response.helper.js'
FunctionHTTP StatusBody Shape
successResponse200{ success: true, message, ...data }
createdResponse201{ success: true, message, ...data }
errorResponse400{ success: false, message }
notFoundResponse404{ success: false, message }
unauthorizedResponse401{ success: false, message }
forbiddenResponse403{ success: false, message }
internalServerError500{ success: false, message }

Signature

All helpers accept a single options object:

js
successResponse({ res, msg?: string, data?: object, statusCode?: number })

Examples

js
// 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.

js
import { parseQueryParams } from '../helpers/query.helper.js'

const { skip, limit, sort, projection } = parseQueryParams(req.query)

Parsed Parameters

Query ParamDefaultDescription
page1Current page number
limit10Items per page
order_byField name to sort by
order_type'asc''asc' or 'desc'
get_allfalseIf true: sets skip = null, limit = null
drop_downfalseIf true: sets minimal projection for dropdowns

Returns

js
{
  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.

js
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

json
{
  "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

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.

Intecog Logistech IoT Monitoring Platform