// @ts-check
/**
* @import {ExpressRequestAuthorized, ExpressResponse} from '../types.js'
*/
/**
* Action Template Router
*
* This router defines all endpoints for managing action templates:
* - Create/update action templates
* - Restart action templates
* - Delete action templates
* - Get all templates for an organization
* - Get all templates with data for an organization
* - Get a specific action template
* - Register action templates for multiple organizations (multi-orga bot)
* - Get all action templates for a specific bot
*
* All endpoints require admin rights, except for the register endpoint
* which requires employee rights.
*
* @swagger
* components:
* securitySchemes:
* bearerAuth:
* type: http
* scheme: bearer
* bearerFormat: JWT
* schemas:
* ActionTemplate:
* type: object
* properties:
* UID:
* type: string
* format: uuid
* description: Unique identifier for the action template
* Name:
* type: object
* description: Multi-language template names
* properties:
* en:
* type: string
* description: English name
* de:
* type: string
* description: German name
* Pattern:
* type: string
* description: Regular expression pattern for matching user input
* Data:
* type: object
* description: Template configuration and execution logic
* UISchema:
* type: object
* description: JSON Schema for ActionBot UI form generation
* CreatedAt:
* type: string
* format: date-time
* UpdatedAt:
* type: string
* format: date-time
* UIDBelongsTo:
* type: string
* format: uuid
* description: Organization UUID that owns this template
* Error:
* type: object
* properties:
* success:
* type: boolean
* example: false
* error:
* type: string
* description: Error message
* code:
* type: integer
* description: Error code
*
* tags:
* - name: Action Templates
* description: Action template management endpoints for multi-tenant bot system
*/
import express from 'express';
import { checkAdmin, checkEmployee } from '../utils/authChecks.js';
import { requestUpdateLogger, errorLoggerRead } from '../utils/requestLogger.js';
import * as actionTemplateController from './actionTemplate/controller.js';
import { checkRoot } from '../utils/organizationUtils.js';
/** @type {express.Express} */
const api = express();
/**
* @swagger
* /actionTemplate:
* put:
* summary: Create or update an action template
* description: Creates a new action template or updates an existing one. Requires admin privileges.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - Name
* - Pattern
* properties:
* UID:
* type: string
* format: uuid
* description: Template UUID (for updates, omit for new templates)
* Name:
* type: object
* description: Multi-language template names
* properties:
* en:
* type: string
* description: English name
* de:
* type: string
* description: German name
* Pattern:
* type: string
* description: Regular expression pattern for matching user input
* Data:
* type: object
* description: Template configuration and logic
* UISchema:
* type: object
* description: UI schema for ActionBot form generation
* example:
* Name:
* en: "Weather Information"
* de: "Wetterinformationen"
* Pattern: "weather|temperature|forecast"
* Data:
* type: "weather"
* apiEndpoint: "/api/weather"
* UISchema:
* type: "object"
* properties:
* location:
* type: "string"
* title: "Location"
* responses:
* 200:
* description: Template created or updated successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* uid:
* type: string
* format: uuid
* description: Template UUID
* 400:
* description: Invalid request data
* 401:
* description: Unauthorized - Admin rights required
* 500:
* description: Internal server error
*/
// Create or update action template
// @ts-ignore
api.put('/',checkRoot, requestUpdateLogger, checkAdmin, actionTemplateController.createOrUpdateActionTemplate);
/**
* @swagger
* /actionTemplate/restart/{UID}:
* post:
* summary: Restart an action template
* description: Sends a restart signal to all bots using the specified action template. Forces bots to reload template configuration.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: UID
* required: true
* schema:
* type: string
* format: uuid
* description: Action template UUID to restart
* responses:
* 200:
* description: Restart signal sent successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* 401:
* description: Unauthorized - Admin rights required
* 404:
* description: Action template not found
* 500:
* description: Internal server error
*/
// Restart action template
// @ts-ignore
api.post('/restart/:UID',checkRoot, requestUpdateLogger, checkAdmin, actionTemplateController.restartActionTemplate);
/**
* @swagger
* /actionTemplate:
* get:
* summary: Get all action templates for organization
* description: Retrieves all action templates belonging to the current organization with basic information.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* parameters:
* - in: query
* name: includeData
* required: false
* schema:
* type: string
* enum: ['true']
* description: When set to 'true', includes complete template Data field in response. Otherwise only metadata is returned.
* example: 'true'
* responses:
* 200:
* description: List of action templates retrieved successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* templates:
* type: array
* items:
* type: object
* properties:
* UID:
* type: string
* format: uuid
* description: Template UUID
* Name:
* type: object
* description: Multi-language template names
* Pattern:
* type: string
* description: Regex pattern for matching
* Data:
* type: object
* description: Complete template configuration data (only included when includeData=true)
* CreatedAt:
* type: string
* format: date-time
* UpdatedAt:
* type: string
* format: date-time
* linkedBots:
* type: array
* items:
* type: object
* properties:
* botUID:
* type: string
* format: uuid
* botName:
* type: string
* 401:
* description: Unauthorized - Authentication required
* 500:
* description: Internal server error
*/
// Get all templates for the actual organization
// @ts-ignore
api.get('/',checkRoot, actionTemplateController.getActionTemplates);
/**
* @swagger
* /actionTemplate/{UID}:
* get:
* summary: Get a specific action template
* description: Retrieves detailed information about a specific action template. Requires admin privileges.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: UID
* required: true
* schema:
* type: string
* format: uuid
* description: Action template UUID
* responses:
* 200:
* description: Action template retrieved successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* template:
* type: object
* properties:
* UID:
* type: string
* format: uuid
* Name:
* type: object
* description: Multi-language template names
* Pattern:
* type: string
* description: Regular expression pattern
* Data:
* type: object
* description: Template configuration data
* UISchema:
* type: object
* description: UI schema for form generation
* CreatedAt:
* type: string
* format: date-time
* UpdatedAt:
* type: string
* format: date-time
* 401:
* description: Unauthorized - Admin rights required
* 404:
* description: Action template not found
* 500:
* description: Internal server error
*/
// Get a specific template for the actual organization
// @ts-ignore
api.get('/:UID', checkRoot, checkAdmin, actionTemplateController.getActionTemplate);
/**
* @swagger
* /actionTemplate/register:
* post:
* summary: Register action templates for multiple organizations
* description: Registers action templates across multiple organizations. Used by multi-organization bots to deploy templates. Requires employee privileges.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - templates
* - organizations
* properties:
* templates:
* type: array
* description: Array of action templates to register
* items:
* type: object
* properties:
* Name:
* type: object
* description: Multi-language template names
* Pattern:
* type: string
* description: Regular expression pattern
* Data:
* type: object
* description: Template configuration
* UISchema:
* type: object
* description: UI schema for ActionBots
* organizations:
* type: array
* description: Array of organization UIDs to register templates for
* items:
* type: string
* format: uuid
* botUID:
* type: string
* format: uuid
* description: Bot UUID that will be linked to templates
* example:
* templates:
* - Name:
* en: "System Status"
* de: "Systemstatus"
* Pattern: "status|health|uptime"
* Data:
* type: "system"
* endpoint: "/api/status"
* organizations:
* - "123e4567-e89b-12d3-a456-426614174000"
* - "987fcdeb-51a2-43d7-8f9e-123456789abc"
* botUID: "456e7890-e12b-34d5-a678-426614174111"
* responses:
* 200:
* description: Templates registered successfully across organizations
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* registeredCount:
* type: integer
* description: Number of templates successfully registered
* organizations:
* type: array
* description: Organizations where templates were registered
* 400:
* description: Invalid request data
* 401:
* description: Unauthorized - Employee rights required
* 500:
* description: Internal server error
*/
// Register endpoint for multi-orga bot to create action templates
// @ts-ignore
api.post('/register', requestUpdateLogger, checkEmployee, actionTemplateController.registerBotActionTemplates);
/**
* @swagger
* /actionTemplate/cleanup:
* delete:
* summary: Delete action templates for bot when organizations lose access
* description: Deletes all action templates for a specific bot that belong to organizations not in the provided list. Used for cleanup when organizations are removed from the bot's access list or lose entitlement to use the bot. Requires admin privileges.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - botUID
* - OrgUIDs
* properties:
* botUID:
* type: string
* format: uuid
* description: Bot UUID to clean up templates for
* OrgUIDs:
* type: array
* description: Array of organization UIDs that still have access to the bot (templates for organizations not in this list will be deleted)
* items:
* type: string
* format: uuid
* example:
* botUID: "456e7890-e12b-34d5-a678-426614174111"
* OrgUIDs:
* - "123e4567-e89b-12d3-a456-426614174000"
* - "987fcdeb-51a2-43d7-8f9e-123456789abc"
* responses:
* 200:
* description: Templates cleaned up successfully (removed from organizations that lost access)
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* deletedCount:
* type: integer
* description: Number of templates deleted from organizations that lost access
* 400:
* description: Invalid request data
* 401:
* description: Unauthorized - Admin rights required
* 404:
* description: Bot not found
* 500:
* description: Internal server error
*/
// Delete action templates for bot not in specified organizations
// @ts-ignore
api.delete('/cleanup', requestUpdateLogger, checkEmployee, actionTemplateController.deleteActionTemplatesForBotNotInOrgs);
/**
* @swagger
* /actionTemplate/{UID}:
* delete:
* summary: Delete an action template
* description: Permanently deletes an action template and all associated data including bot links and active actions. Requires admin privileges.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: UID
* required: true
* schema:
* type: string
* format: uuid
* description: Action template UUID to delete
* responses:
* 200:
* description: Action template deleted successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* 401:
* description: Unauthorized - Admin rights required
* 403:
* description: Forbidden - Template doesn't belong to organization
* 404:
* description: Action template not found
* 500:
* description: Internal server error
*/
// Delete action template
// @ts-ignore
api.delete('/:UID',checkRoot, requestUpdateLogger, checkAdmin, actionTemplateController.deleteActionTemplate);
/**
* @swagger
* /actionTemplate/bot/{botUID}:
* get:
* summary: Get all action templates for a specific bot
* description: Retrieves all action templates that are linked to a specific bot. Requires admin privileges.
* tags:
* - Action Templates
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: botUID
* required: true
* schema:
* type: string
* format: uuid
* description: Bot UUID to get templates for
* - in: query
* name: includeData
* required: false
* schema:
* type: string
* enum: ['true']
* description: When set to 'true', includes complete template Data field in response. Otherwise only metadata is returned.
* example: 'true'
* responses:
* 200:
* description: Bot action templates retrieved successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* templates:
* type: array
* items:
* type: object
* properties:
* UID:
* type: string
* format: uuid
* description: Template UUID
* Name:
* type: object
* description: Multi-language template names
* Pattern:
* type: string
* description: Regular expression pattern
* Data:
* type: object
* description: Template configuration data (only included when includeData=true)
* UISchema:
* type: object
* description: UI schema for ActionBots
* linkedAt:
* type: string
* format: date-time
* description: When bot was linked to template
* botInfo:
* type: object
* properties:
* UID:
* type: string
* format: uuid
* Name:
* type: string
* description: Bot name
* Type:
* type: string
* description: Bot type (ActionBot/SystemBot)
* 401:
* description: Unauthorized - Admin rights required
* 404:
* description: Bot not found
* 500:
* description: Internal server error
*/
// Get all action templates for a specific bot
// @ts-ignore
api.get('/bot/:botUID', checkEmployee, actionTemplateController.getActionTemplatesForBot);
export default api;