Source: Router/apiConfigFile.js

// @ts-check
/**
 * @import {ExpressRequestAuthorized, ExpressResponse} from '../types.js'
 */
/**
 * Configuration File API Routes
 * 
 * Defines REST API endpoints for configuration file management.
 * Routes are organized by HTTP method and include proper middleware
 * for authentication and validation.
 * 
 * @module ConfigRoutes
 */

import express from 'express';
import { checkAdmin } from '../utils/authChecks.js';
import {
    uploadConfig,
    downloadConfig,
    listConfigs,
    deleteConfig
} from './configRoute/controller.js';

const configApi = express.Router({ caseSensitive: true });

// Configuration constants
const SUPPORTED_APPS = process.env.apps || '';

/**
 * @swagger
 * components:
 *   schemas:
 *     ConfigFile:
 *       type: object
 *       properties:
 *         UID:
 *           type: string
 *           description: Configuration file unique identifier
 *         name:
 *           type: string
 *           description: Configuration file name
 *         app:
 *           type: string
 *           description: Application the config belongs to
 *         data:
 *           type: object
 *           description: Configuration data
 *     
 *     ConfigUploadResponse:
 *       type: object
 *       properties:
 *         success:
 *           type: boolean
 *           description: Whether the upload was successful
 *         config:
 *           $ref: '#/components/schemas/ConfigFile'
 *         message:
 *           type: string
 *           description: Success or error message
 *     
 *     ConfigListResponse:
 *       type: object
 *       properties:
 *         success:
 *           type: boolean
 *           description: Whether the request was successful
 *         files:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/ConfigFile'
 *         message:
 *           type: string
 *           description: Success or error message
 */

/**
 * @swagger
 * /api/config/{app}:
 *   post:
 *     summary: Upload and validate a new configuration file
 *     tags: [Configuration]
 *     security:
 *       - AdminAuth: []
 *     parameters:
 *       - in: path
 *         name: app
 *         required: true
 *         schema:
 *           type: string
 *         description: Application name (must match supported apps)
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: string
 *                 format: binary
 *                 description: YAML configuration file
 *     responses:
 *       200:
 *         description: Configuration uploaded and validated successfully
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ConfigUploadResponse'
 *       400:
 *         description: Invalid file or validation error
 *       401:
 *         description: Unauthorized access
 *       500:
 *         description: Server error
 */
// @ts-ignore
configApi.post(`/:app(${SUPPORTED_APPS})`,  uploadConfig);

/**
 * @swagger
 * /api/config/{app}/{UID}:
 *   get:
 *     summary: Download a specific configuration file
 *     tags: [Configuration]
 *     security:
 *       - AdminAuth: []
 *     parameters:
 *       - in: path
 *         name: app
 *         required: true
 *         schema:
 *           type: string
 *         description: Application name
 *       - in: path
 *         name: UID
 *         required: true
 *         schema:
 *           type: string
 *         description: Configuration file UID
 *     responses:
 *       200:
 *         description: Configuration file download
 *         content:
 *           application/octet-stream:
 *             schema:
 *               type: string
 *               format: binary
 *       404:
 *         description: Configuration file not found
 *       401:
 *         description: Unauthorized access
 *       500:
 *         description: Server error
 */
// @ts-ignore
configApi.get(`/:app(${SUPPORTED_APPS})/:UID`,  downloadConfig);

/**
 * @swagger
 * /api/config/{app}:
 *   get:
 *     summary: List all configuration files for an application
 *     tags: [Configuration]
 *     security:
 *       - AdminAuth: []
 *     parameters:
 *       - in: path
 *         name: app
 *         required: true
 *         schema:
 *           type: string
 *         description: Application name
 *     responses:
 *       200:
 *         description: List of configuration files
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ConfigListResponse'
 *       404:
 *         description: No configurations found
 *       401:
 *         description: Unauthorized access
 *       500:
 *         description: Server error
 */
// @ts-ignore
configApi.get(`/:app(${SUPPORTED_APPS})`,  listConfigs);

/**
 * @swagger
 * /api/config/{app}/{UID}:
 *   delete:
 *     summary: Delete a configuration file
 *     tags: [Configuration]
 *     security:
 *       - AdminAuth: []
 *     parameters:
 *       - in: path
 *         name: app
 *         required: true
 *         schema:
 *           type: string
 *         description: Application name
 *       - in: path
 *         name: UID
 *         required: true
 *         schema:
 *           type: string
 *         description: Configuration file UID to delete
 *     responses:
 *       200:
 *         description: Configuration file deleted successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *       401:
 *         description: Unauthorized access
 *       500:
 *         description: Server error
 */
// @ts-ignore
configApi.delete(`/:app(${SUPPORTED_APPS})/:UID`,  deleteConfig);

export default configApi;