import { Controller, Delete, Get, HttpException, Param, Post, Put, Query, UseGuards, } from '@nestjs/common'; import { ApiExcludeController } from '@nestjs/swagger'; import { ManagementGuard } from 'src/guards/management.guard'; import { DatabaseService } from '../objects/database/database.service'; import { ZoneEntity } from '../objects/database/zone.entity'; @ApiExcludeController() @UseGuards(ManagementGuard) @Controller({ path: 'api/v1/management', }) export class ManagementController { constructor(private service: DatabaseService) {} @Get('zones') async getZoneList(@Query('uuid') uuid?: string) { let list: ZoneEntity[] = []; if (uuid) { list = await this.service.getZonesByIcynetUUID(uuid); } else { list = await this.service.getAllZones(); } return list.map(({ zone }) => zone); } @Get('access') async getAccessList() { return this.service.getZonesWithIcynet(); } @Put('zone/:domain/:uuid') async addZoneAccess( @Param('uuid') uuid: string, @Param('domain') domain: string, ) { const success = await this.service.authorizeIcynetUser(uuid, domain); return { success }; } @Delete('zone/:domain/:uuid') async removeZoneAccess( @Param('uuid') uuid: string, @Param('domain') domain: string, ) { const success = await this.service.revokeIcynetUser(uuid, domain); return { success }; } @Post('zone/:domain/:uuid') async getAccess( @Param('uuid') uuid: string, @Param('domain') domain: string, ) { const added = await this.service.createIcynetAccessKey(uuid, domain); if (!added) throw new HttpException('Invalid request', 400); return { token: added.key }; } @Put('zone/:domain') async addZone(@Param('domain') domain: string) { const added = await this.service.addZone(domain); return { success: !!added }; } @Delete('zone/:domain') async removeZone(@Param('domain') domain: string) { const success = await this.service.removeZone(domain); return { success }; } }