import { Request, Response } from 'express'; import Controller from '../controller'; import { IUnleashServices } from '../../types/services'; import { IUnleashConfig } from '../../types/option'; import EnvironmentService from '../../services/environment-service'; import { Logger } from '../../logger'; import { ADMIN, NONE } from '../../types/permissions'; import { OpenApiService } from '../../services/openapi-service'; import { createRequestSchema, createResponseSchema } from '../../openapi'; import { environmentsSchema, EnvironmentsSchema, } from '../../openapi/spec/environments-schema'; import { environmentSchema, EnvironmentSchema, } from '../../openapi/spec/environment-schema'; import { SortOrderSchema } from '../../openapi/spec/sort-order-schema'; import { emptyResponse } from '../../openapi/spec/empty-response'; interface EnvironmentParam { name: string; } export class EnvironmentsController extends Controller { private logger: Logger; private openApiService: OpenApiService; private service: EnvironmentService; constructor( config: IUnleashConfig, { environmentService, openApiService, }: Pick, ) { super(config); this.logger = config.getLogger('admin-api/environments-controller.ts'); this.openApiService = openApiService; this.service = environmentService; this.route({ method: 'get', path: '', handler: this.getAllEnvironments, permission: NONE, middleware: [ openApiService.validPath({ tags: ['admin'], operationId: 'getAllEnvironments', responses: { 200: emptyResponse }, }), ], }); this.route({ method: 'get', path: '/:name', handler: this.getEnvironment, permission: NONE, middleware: [ openApiService.validPath({ tags: ['admin'], operationId: 'getEnvironment', responses: { 200: createResponseSchema('environmentSchema'), }, }), ], }); this.route({ method: 'put', path: '/sort-order', handler: this.updateSortOrder, permission: ADMIN, middleware: [ openApiService.validPath({ tags: ['admin'], operationId: 'updateSortOrder', requestBody: createRequestSchema('sortOrderSchema'), responses: { 200: emptyResponse }, }), ], }); this.route({ method: 'post', path: '/:name/on', acceptAnyContentType: true, handler: this.toggleEnvironmentOn, permission: ADMIN, middleware: [ openApiService.validPath({ tags: ['admin'], operationId: 'toggleEnvironmentOn', responses: { 200: emptyResponse }, }), ], }); this.route({ method: 'post', path: '/:name/off', acceptAnyContentType: true, handler: this.toggleEnvironmentOff, permission: ADMIN, middleware: [ openApiService.validPath({ tags: ['admin'], operationId: 'toggleEnvironmentOff', responses: { 200: emptyResponse }, }), ], }); } async getAllEnvironments( req: Request, res: Response, ): Promise { this.openApiService.respondWithValidation( 200, res, environmentsSchema.$id, { version: 1, environments: await this.service.getAll() }, ); } async updateSortOrder( req: Request, res: Response, ): Promise { await this.service.updateSortOrder(req.body); res.status(200).end(); } async toggleEnvironmentOn( req: Request, res: Response, ): Promise { const { name } = req.params; await this.service.toggleEnvironment(name, true); res.status(204).end(); } async toggleEnvironmentOff( req: Request, res: Response, ): Promise { const { name } = req.params; await this.service.toggleEnvironment(name, false); res.status(204).end(); } async getEnvironment( req: Request, res: Response, ): Promise { this.openApiService.respondWithValidation( 200, res, environmentSchema.$id, await this.service.get(req.params.name), ); } }