Source: RouterAddons/addons.js

/**
 * Addon CRUD API — Bot-registrierte UI-Addons im Runtime-Config-Cache
 *
 * Reine Routendefinition. Delegiert an addons/controller.js → addons/service.js.
 *
 * @module AddonRoutes
 * @swagger
 * tags:
 *   - name: Addons
 *     description: |
 *       Bot-registrierte UI-Addons für beliebige Apps (db, portal, admin, events, …).
 *
 *       Bots registrieren ihre Addons zur Laufzeit via `PUT /api/addons/{app}`.
 *       Die Addons werden ausschliesslich im Runtime-Config-Cache gespeichert —
 *       nicht in der statischen YAML-Datei in MinIO. Bei jedem Config-Reload
 *       (Backend-Restart, Admin-Upload) verlieren sie sich; der Bot muss sie
 *       via `onStartup` oder `extraPatterns` neu registrieren.
 *
 * components:
 *   schemas:
 *     AddonEntry:
 *       type: object
 *       description: Ein einzelnes UI-Addon im Runtime-Config-Cache.
 *       required:
 *         - url
 *         - name
 *       properties:
 *         url:
 *           type: string
 *           format: uri
 *           description: URL unter der das Addon geladen wird.
 *           example: /addons/my-bot/index.html
 *         name:
 *           type: string
 *           description: Eindeutiger Name des Addons (Key für Upsert).
 *           example: my-bot-addon
 *         moduleName:
 *           type: string
 *           description: Name des Bot-Moduls (für RemoteAddon-Script-Load).
 *           example: beitraege-bot
 *         modulePath:
 *           type: string
 *           description: Subpath für HMR-Import (z.B. "balance").
 *           example: balance
 *         remoteAddon:
 *           type: boolean
 *           description: |
 *             true = RemoteAddon (Script-Load per IIFE-Bundle),
 *             false/undefined = Iframe-Addon.
 *           example: true
 *         menus:
 *           type: array
 *           items:
 *             type: string
 *           description: Menü-Einträge, in denen das Addon erscheint.
 *         icon:
 *           type: string
 *           description: Icon-Klasse oder -URL.
 *         mailLink:
 *           type: string
 *           description: Mail-Link-Konfiguration.
 *         listContext:
 *           type: array
 *           items:
 *             type: string
 *           description: List-Kontexte, in denen das Addon verfügbar ist.
 *         objectTypes:
 *           type: array
 *           items:
 *             type: string
 *           description: Objekt-Typen, für die das Addon verfügbar ist.
 *     AddonRequest:
 *       type: object
 *       description: Request-Body für PUT /api/addons/{app}. Die Organisation wird
 *       über den `x-organization`-Header gescoped.
 *       required:
 *         - url
 *         - name
 *       properties:
 *         url:
 *           type: string
 *           format: uri
 *         name:
 *           type: string
 *         moduleName:
 *           type: string
 *           description: Name des Bot-Moduls (für RemoteAddon-Script-Load).
 *           example: beitraege-bot
 *         modulePath:
 *           type: string
 *           description: Subpath für HMR-Import (z.B. "balance").
 *           example: balance
 *         remoteAddon:
 *           type: boolean
 *           description: |
 *             true = RemoteAddon (Script-Load per IIFE-Bundle),
 *             false/undefined = Iframe-Addon.
 *           example: true
 *         menus:
 *           type: array
 *           items:
 *             type: string
 *         icon:
 *           type: string
 *         mailLink:
 *           type: string
 *         listContext:
 *           type: array
 *           items:
 *             type: string
 *         objectTypes:
 *           type: array
 *           items:
 *             type: string
 *         overwrite:
 *           type: boolean
 *           default: false
 *           description: |
 *             `true` = Eintrag komplett ersetzen,
 *             `false`/fehlt = merge (Array-Felder werden vereinigt).
 */

import { Router } from 'express'
import { makeAuthCheck } from '@commtool/shared-auth'
import * as addonController from './addons/controller.js'

const router = Router()

/**
 * @swagger
 * /api/addons/{app}:
 *   put:
 *     summary: Addon registrieren oder überschreiben (Upsert per name)
 *     description: |
 *       Registriert ein UI-Addon im Runtime-Config-Cache für die angegebene App.
 *       Existiert bereits ein Addon mit demselben `name` (gleiche App + Orga),
 *       wird es gemerget (oder bei `overwrite: true` komplett ersetzt).
 *     tags:
 *       - Addons
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: app
 *         required: true
 *         schema:
 *           type: string
 *         description: App-Key (z. B. "db", "portal", "admin", "events").
 *       - in: header
 *         name: x-organization
 *         required: true
 *         schema:
 *           type: string
 *           format: uuid
 *         description: Organisation-UUID (Bot-Auth).
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/AddonRequest'
 *     responses:
 *       200:
 *         description: Addon erfolgreich registriert.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 addon:
 *                   $ref: '#/components/schemas/AddonEntry'
 *       400:
 *         description: Fehlende Pflichtfelder (url, name) oder fehlender x-organization Header.
 */
router.put('/:app', makeAuthCheck(['bot']), addonController.registerAddon)

/**
 * @swagger
 * /api/addons/{app}/{name}:
 *   delete:
 *     summary: Addon entfernen
 *     description: |
 *       Entfernt ein Addon aus dem Runtime-Config-Cache anhand von App und Name.
 *     tags:
 *       - Addons
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: app
 *         required: true
 *         schema:
 *           type: string
 *         description: App-Key (z. B. "db", "portal", "admin", "events").
 *       - in: path
 *         name: name
 *         required: true
 *         schema:
 *           type: string
 *         description: Name des zu entfernenden Addons.
 *       - in: header
 *         name: x-organization
 *         required: true
 *         schema:
 *           type: string
 *           format: uuid
 *         description: Organisation-UUID (Bot-Auth).
 *     responses:
 *       200:
 *         description: Addon erfolgreich entfernt.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Addon 'my-bot-addon' entfernt"
 *       404:
 *         description: Addon oder Orga nicht gefunden.
 */
router.delete('/:app/:name', makeAuthCheck(['bot']), addonController.deleteAddon)

export default router