UNCLASSIFIED - NO CUI

Skip to content
Snippets Groups Projects
controller.js 1.67 KiB
const { getDbVersion, healthCheck } = require('../data/dataLayer');
const { jwtDecode } = require('jwt-decode');
const apiVersion = "0.0.1";

/** 
 * Perform a health check by ensuring data layer connectivity is healthy.
 * */ 
exports.getHealth = async (req, res) => {
    const dbHealth = await healthCheck();

    if (!dbHealth) {
        console.log("Unable to get DB health, returning 500 response");
        res.status(500).json({
            status: 500,
            message: "Internal Server Error"
        });
        return;
    }

    let statusCode = 500;
    if (dbHealth.healthy) {
        statusCode = 200;
    }
    res.status(statusCode).json({
        status: statusCode,
        message: dbHealth.message
    });
};

/**
 * Return the user's current JWT. Useful to ensure that the app is integrated properly in the Party Bus infrastructure.
 */
exports.getUser = async (req, res) => {
    try {
        const header = req.headersDistinct?.Authorization?.[0] || req.headersDistinct?.authorization?.[0];
        const responseData = header ? jwtDecode(header) : { result: "no authorization header found" };
        res.status(200).json(responseData);
    } catch (error) {
        const errorResponse = {
            error: 'Unable to read authorization header as jwt! Error: ' + error.message
        }
        res.status(400).json(errorResponse);
    }
};

/**
 * Return basic version information. Useful to check that the app can query from its database.
 */
exports.getVersion = async (req, res) => {
    const dbVersion = await getDbVersion();
    const versionResponse = {
        apiVersion: apiVersion,
        dbVersion: dbVersion
    };
    res.status(200).json(versionResponse);
};