Source: Router/collections/controller.js

// @ts-check
/**
what are the issues in the service sql * @typedef {import('../../types.js').ExpressRequestAuthorized} ExpressRequestAuthorized
 * @typedef {import('../../types.js').ExpressResponse} ExpressResponse
 */

import paginateList from '../../utils/paginateList.js';
import { errorLoggerRead } from '../../utils/requestLogger.js';
import * as collectionsService from './service.js';

/**
 * @param {ExpressRequestAuthorized} req
 */
const toListingInput = (req) => ({
    session: req.session,
    query: req.query,
    body: req.body,
    params: req.params,
});

/**
 * Pagination pre-handler for GET /:UID.
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 * @param {(error?: any) => void} next
 */
export const paginateCollectionsUIDController = async (req, res, next) => {
    try {
        if (!req.query.__page) {
            next();
            return;
        }

        const getListingForPagination = async (/** @type {unknown} */ paginationReq) => {
            const listingReq = /** @type {ExpressRequestAuthorized} */ (paginationReq);
            return collectionsService.getCollectionsListing(toListingInput(listingReq));
        };

        res.json(await paginateList(req, getListingForPagination));
    } catch (e) {
        errorLoggerRead(e);
    }
};

/**
 * Controller for GET /:UID
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getCollectionsUIDController = async (req, res) => {
    try {
        const result = await collectionsService.getCollectionsListing(toListingInput(req));
        res.json({ success: true, result });
    } catch (e) {
        errorLoggerRead(e);
    }
};

/**
 * Controller for POST /
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const postCollectionsController = async (req, res) => {
    try {
        const result = await collectionsService.getCollectionsListing(toListingInput(req));
        res.json({ success: true, result });
    } catch (e) {
        errorLoggerRead(e);
    }
};

/**
 * Controller for GET /
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getCollectionsController = async (req, res) => {
    try {
        const result = await collectionsService.getCollectionsListing(toListingInput(req));
        res.json({ success: true, result });
    } catch (e) {
        errorLoggerRead(e);
    }
};

/**
 * Controller for POST /counts
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const postCountsController = async (req, res) => {
    try {
        const counts = await collectionsService.getCollectionCounts({ query: req.query, body: req.body });
        res.json(counts);
    } catch (e) {
        errorLoggerRead(e);
    }
};