Source: Router/listEntries/controller.js

// @ts-check
/**
 * @import {ExpressRequestAuthorized, ExpressResponse} from '../../types.js'
 */

/**
 * List Entries Controllers - HTTP Request/Response Handling
 *
 * Handles HTTP request/response cycle for all list entry endpoints,
 * extracting parameters from req and delegating to the service layer.
 */

import * as listEntriesService from './service.js';
import paginateList from '../../utils/paginateList.js';
import { errorLoggerRead, errorLoggerUpdate } from '../../utils/requestLogger.js';
import { UUID2hex } from '@commtool/sql-query';


/**
 * Handle PUT /list/person/:UIDlist — add persons to a static list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const putEntryController = async (req, res) => {
    try {
        const result = await listEntriesService.putEntry(req);
        res.json(result);
    } catch (e) {
        errorLoggerUpdate(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle POST /list/params/:UIDlist/:UIDperson — update entry params for a static list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const postListEntryController = async (req, res) => {
    try {
        const result = await listEntriesService.postEntry(req, 'list');
        res.json(result);
    } catch (e) {
        errorLoggerUpdate(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle POST /dlist/params/:UIDlist/:UIDperson — update entry params for a dynamic list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const postDlistEntryController = async (req, res) => {
    try {
        const result = await listEntriesService.postEntry(req, 'dlist');
        res.json(result);
    } catch (e) {
        errorLoggerUpdate(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle DELETE /list/person/:UIDlist — remove persons from a static list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const deleteEntryController = async (req, res) => {
    try {
        const result = await listEntriesService.deleteEntry(req);
        res.json(result);
    } catch (e) {
        errorLoggerUpdate(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle GET /list/persons/:UID (paginated) — pass through to next if no __page param
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 * @param {Function} next
 */
export const getListPersonsPaginatedController = async (req, res, next) => {
    if (!req.query.__page) {
        next(req.res.next);
        return;
    }
    try {
        res.json(await paginateList(req, listEntriesService.getListing));
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle GET /list/persons/:UID — retrieve all persons in a static list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getListPersonsController = async (req, res) => {
    try {
        const result = await listEntriesService.getListing(req, false);
        res.json({ success: true, result: result });
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle GET /dlist/persons/:UID (paginated) — pass through to next if no __page param
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 * @param {Function} next
 */
export const getDlistPersonsPaginatedController = async (req, res, next) => {
    if (!req.query.__page) {
        next(req.res.next);
        return;
    }
    try {
        res.json(await paginateList(req, listEntriesService.getListing));
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle GET /dlist/persons/:UID — retrieve all persons in a dynamic list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getDlistPersonsController = async (req, res) => {
    try {
        const result = await listEntriesService.getListing(req);
        res.json({ success: true, result: result });
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle GET /list/lists/:UIDperson — get all lists containing a specific person
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getPersonListsController = async (req, res) => {
    try {
        let type = ['list', 'dlist'];
        if (req.query['type'] && ['list', 'dlist'].includes(req.query['type'])) {
            type = [req.query['type']];
        }
        const UIDperson = UUID2hex(req.params.UIDperson);
        const userUID = UUID2hex(req.session.user);
        const result = await listEntriesService.getPersonLists(UIDperson, type, userUID);
        res.json(result);
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle POST /list/revert/:UID/:timestamp — revert list entries to a previous state
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const revertListController = async (req, res) => {
    try {
        const result = await listEntriesService.revertList(req.params.UID, req.params.timestamp);
        res.json(result);
    } catch (e) {
        errorLoggerUpdate(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle GET /list/entry/:UIDlist/:UIDperson — get entry data for a specific person in a list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getListEntryController = async (req, res) => {
    try {
        const UIDlist = UUID2hex(req.params.UIDlist);
        const UIDperson = UUID2hex(req.params.UIDperson);
        const result = await listEntriesService.getListEntry(UIDlist, UIDperson);
        res.json(result);
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};


/**
 * Handle POST /list/persons/:UID — retrieve specific persons in a list by UID list
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getListPersonsByUIDsController = async (req, res) => {
    try {
        const result = await listEntriesService.getListingByUIDs(req, false);
        res.json({ success: true, result: result });
    } catch (e) {
        errorLoggerRead(e);
        res.status(500).json({ success: false, message: e.message });
    }
};