-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathadmin-feature-flags.controller.ts
More file actions
69 lines (64 loc) · 2.07 KB
/
admin-feature-flags.controller.ts
File metadata and controls
69 lines (64 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
Body,
Controller,
Get,
NotFoundException,
Param,
Patch,
UseGuards,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiExcludeController, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Throttle } from '@nestjs/throttler';
import { db } from '@db';
import { PlatformAdminGuard } from '../auth/platform-admin.guard';
import { AdminAuditLogInterceptor } from '../admin-organizations/admin-audit-log.interceptor';
import { AdminFeatureFlagsService } from './admin-feature-flags.service';
import { UpdateFeatureFlagDto } from './dto/update-feature-flag.dto';
@ApiExcludeController()
@ApiTags('Admin - Feature Flags')
@Controller({ path: 'admin/organizations', version: '1' })
@UseGuards(PlatformAdminGuard)
@UseInterceptors(AdminAuditLogInterceptor)
@Throttle({ default: { ttl: 60000, limit: 60 } })
export class AdminFeatureFlagsController {
constructor(private readonly service: AdminFeatureFlagsService) {}
@Get(':orgId/feature-flags')
@ApiOperation({
summary:
'List all admin-managed feature flags with their current state for an organization',
})
async list(@Param('orgId') orgId: string) {
const org = await db.organization.findUnique({ where: { id: orgId } });
if (!org) throw new NotFoundException('Organization not found');
const flags = await this.service.listForOrganization(orgId);
return { data: flags };
}
@Patch(':orgId/feature-flags')
@ApiOperation({
summary: 'Enable or disable a feature flag for an organization',
})
@UsePipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
)
async update(
@Param('orgId') orgId: string,
@Body() dto: UpdateFeatureFlagDto,
) {
const org = await db.organization.findUnique({ where: { id: orgId } });
if (!org) throw new NotFoundException('Organization not found');
const result = await this.service.setFlagForOrganization({
orgId,
orgName: org.name,
flagKey: dto.flagKey,
enabled: dto.enabled,
});
return { data: result };
}
}