2021-07-07 10:46:50 +02:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import Controller from '../controller';
|
|
|
|
import { IUnleashServices } from '../../types/services';
|
|
|
|
import { IUnleashConfig } from '../../types/option';
|
2021-09-13 15:57:38 +02:00
|
|
|
import { ISortOrder } from '../../types/model';
|
2021-07-07 10:46:50 +02:00
|
|
|
import EnvironmentService from '../../services/environment-service';
|
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import { ADMIN } from '../../types/permissions';
|
|
|
|
|
|
|
|
interface EnvironmentParam {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EnvironmentsController extends Controller {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private service: EnvironmentService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{ environmentService }: Pick<IUnleashServices, 'environmentService'>,
|
|
|
|
) {
|
|
|
|
super(config);
|
|
|
|
this.logger = config.getLogger('admin-api/environments-controller.ts');
|
|
|
|
this.service = environmentService;
|
|
|
|
this.get('/', this.getAll);
|
2021-09-13 15:57:38 +02:00
|
|
|
this.put('/sort-order', this.updateSortOrder, ADMIN);
|
2021-07-07 10:46:50 +02:00
|
|
|
this.get('/:name', this.getEnv);
|
2021-09-13 15:57:38 +02:00
|
|
|
this.post('/:name/on', this.toggleEnvironmentOn, ADMIN);
|
|
|
|
this.post('/:name/off', this.toggleEnvironmentOff, ADMIN);
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(req: Request, res: Response): Promise<void> {
|
2021-09-13 10:23:57 +02:00
|
|
|
const environments = await this.service.getAll();
|
|
|
|
res.status(200).json({ version: 1, environments });
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:57:38 +02:00
|
|
|
async updateSortOrder(
|
|
|
|
req: Request<any, any, ISortOrder, any>,
|
2021-07-07 10:46:50 +02:00
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
2021-09-13 15:57:38 +02:00
|
|
|
await this.service.updateSortOrder(req.body);
|
|
|
|
res.status(200).end();
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:57:38 +02:00
|
|
|
async toggleEnvironmentOn(
|
2021-07-07 10:46:50 +02:00
|
|
|
req: Request<EnvironmentParam, any, any, any>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { name } = req.params;
|
2021-09-13 15:57:38 +02:00
|
|
|
await this.service.toggleEnvironment(name, true);
|
|
|
|
res.status(204).end();
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:57:38 +02:00
|
|
|
async toggleEnvironmentOff(
|
|
|
|
req: Request<EnvironmentParam, any, any, any>,
|
2021-07-07 10:46:50 +02:00
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { name } = req.params;
|
2021-09-13 15:57:38 +02:00
|
|
|
await this.service.toggleEnvironment(name, false);
|
|
|
|
res.status(204).end();
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:57:38 +02:00
|
|
|
async getEnv(
|
2021-07-07 10:46:50 +02:00
|
|
|
req: Request<EnvironmentParam, any, any, any>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { name } = req.params;
|
2021-09-13 15:57:38 +02:00
|
|
|
|
|
|
|
const env = await this.service.get(name);
|
|
|
|
res.status(200).json(env);
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
}
|