2019-04-24 21:01:54 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
const httpStatus = require('http-status');
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// PRIVATE FUNCTIONS
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
function buildErrorLog(err) {
|
|
|
|
|
let errorLog;
|
|
|
|
|
if (_.isUndefined(err)) {
|
|
|
|
|
errorLog = 'Error not defined';
|
|
|
|
|
} else if (!_.isUndefined(err.message)) {
|
|
|
|
|
errorLog = err.message;
|
|
|
|
|
} else if (!_.isUndefined(err.stack)) {
|
|
|
|
|
errorLog = err.stack;
|
|
|
|
|
} else {
|
|
|
|
|
errorLog = JSON.stringify(err);
|
|
|
|
|
}
|
|
|
|
|
return errorLog;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildErrorResponse(nameController, nameMethod, error) {
|
|
|
|
|
|
|
|
|
|
const errorDescription = buildErrorLog(error);
|
|
|
|
|
|
|
|
|
|
const jsonResultFailed = {
|
|
|
|
|
code: httpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
message: 'Internal Server Error',
|
|
|
|
|
description: `Internal Application Error in ${nameController}:${nameMethod}. ${errorDescription}`
|
|
|
|
|
}
|
|
|
|
|
return jsonResultFailed;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 16:23:54 +00:00
|
|
|
function getTotalCount(result) {
|
|
|
|
|
|
|
|
|
|
const toType = function (obj) {
|
|
|
|
|
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (toType(result)) {
|
|
|
|
|
case 'boolean':
|
|
|
|
|
return 1;
|
|
|
|
|
case 'object':
|
|
|
|
|
return 1;
|
|
|
|
|
case 'array':
|
|
|
|
|
return result.length;
|
|
|
|
|
case 'null':
|
|
|
|
|
return 0;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setPaginationInfo(totalCount, res) {
|
|
|
|
|
res.set({
|
|
|
|
|
'X-Total-Count': totalCount,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const params = extractParamsFromRequest(null, res);
|
|
|
|
|
console.log(params);
|
|
|
|
|
if (params.paginate) {
|
|
|
|
|
const
|
|
|
|
|
page = (params.paginate && params.paginate.page) ? params.paginate.page : null,
|
|
|
|
|
limit = (params.paginate && params.paginate.limit) ? params.paginate.limit : null,
|
|
|
|
|
count = (limit) ? Math.ceil(totalCount / limit) : null;
|
|
|
|
|
|
|
|
|
|
if (params.paginate.hasNextPages(count)) {
|
|
|
|
|
const nextPage = params.paginate.href();
|
|
|
|
|
res.set('Link', nextPage + '; rel=next');
|
|
|
|
|
res.set('Pagination-Next-Page', true);
|
|
|
|
|
} else {
|
|
|
|
|
res.set('Pagination-Next-Page', false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.set({
|
|
|
|
|
'Pagination-Count': count,
|
|
|
|
|
'Pagination-Page': page,
|
|
|
|
|
'Pagination-Limit': limit,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 21:01:54 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// PUBLIC FUNCTIONS
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-05-09 16:23:54 +00:00
|
|
|
function extractParamsFromRequest(req, res) {
|
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
|
|
result.params = (req && req.params) ? req.params : null;
|
|
|
|
|
result.query = (req && req.query) ? req.params : null;
|
|
|
|
|
|
|
|
|
|
if (res && res.locals) {
|
|
|
|
|
Object.keys(res.locals).forEach(key => {
|
|
|
|
|
result[key] = res.locals[key]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-24 21:01:54 +00:00
|
|
|
function handleErrorResponse(controllerName, methodName, error, res) {
|
|
|
|
|
|
|
|
|
|
const jsonResultFailed = buildErrorResponse(controllerName, methodName, error);
|
|
|
|
|
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(jsonResultFailed);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 16:23:54 +00:00
|
|
|
function handleResultResponse(result, totalCount = null, params, res, statusCode = httpStatus.OK) {
|
|
|
|
|
setPaginationInfo((totalCount) ? totalCount : getTotalCount(result), res);
|
|
|
|
|
res.status(statusCode).send(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-24 21:01:54 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// MODULE EXPORTS
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2019-05-09 16:23:54 +00:00
|
|
|
extractParamsFromRequest,
|
2019-04-24 21:01:54 +00:00
|
|
|
handleErrorResponse,
|
2019-05-09 16:23:54 +00:00
|
|
|
handleResultResponse,
|
2019-04-24 21:01:54 +00:00
|
|
|
// for testing
|
|
|
|
|
buildErrorLog,
|
|
|
|
|
buildErrorResponse
|
|
|
|
|
};
|