|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Put, |
| 5 | + Post, |
| 6 | + Delete, |
| 7 | + Body, |
| 8 | + UseGuards, |
| 9 | + UsePipes, |
| 10 | + ValidationPipe, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import { |
| 13 | + ApiHeader, |
| 14 | + ApiOperation, |
| 15 | + ApiResponse, |
| 16 | + ApiSecurity, |
| 17 | + ApiTags, |
| 18 | +} from '@nestjs/swagger'; |
| 19 | +import { OrganizationId } from '../auth/auth-context.decorator'; |
| 20 | +import { HybridAuthGuard } from '../auth/hybrid-auth.guard'; |
| 21 | +import { OrgChartService } from './org-chart.service'; |
| 22 | +import { UpsertOrgChartDto } from './dto/upsert-org-chart.dto'; |
| 23 | +import { UploadOrgChartDto } from './dto/upload-org-chart.dto'; |
| 24 | + |
| 25 | +@ApiTags('Org Chart') |
| 26 | +@Controller({ path: 'org-chart', version: '1' }) |
| 27 | +@UseGuards(HybridAuthGuard) |
| 28 | +@ApiSecurity('apikey') |
| 29 | +@ApiHeader({ |
| 30 | + name: 'X-Organization-Id', |
| 31 | + description: |
| 32 | + 'Organization ID (required for session auth, optional for API key auth)', |
| 33 | + required: false, |
| 34 | +}) |
| 35 | +export class OrgChartController { |
| 36 | + constructor(private readonly orgChartService: OrgChartService) {} |
| 37 | + |
| 38 | + @Get() |
| 39 | + @ApiOperation({ summary: 'Get the organization chart' }) |
| 40 | + @ApiResponse({ status: 200, description: 'The organization chart' }) |
| 41 | + async getOrgChart(@OrganizationId() organizationId: string) { |
| 42 | + return await this.orgChartService.findByOrganization(organizationId); |
| 43 | + } |
| 44 | + |
| 45 | + @Put() |
| 46 | + @ApiOperation({ summary: 'Create or update an interactive organization chart' }) |
| 47 | + @ApiResponse({ status: 200, description: 'The saved organization chart' }) |
| 48 | + @UsePipes(new ValidationPipe({ whitelist: false, transform: false })) |
| 49 | + async upsertOrgChart( |
| 50 | + @OrganizationId() organizationId: string, |
| 51 | + @Body() body: Record<string, unknown>, |
| 52 | + ) { |
| 53 | + const dto: UpsertOrgChartDto = { |
| 54 | + name: typeof body?.name === 'string' ? body.name : undefined, |
| 55 | + nodes: Array.isArray(body?.nodes) ? body.nodes : [], |
| 56 | + edges: Array.isArray(body?.edges) ? body.edges : [], |
| 57 | + }; |
| 58 | + return await this.orgChartService.upsertInteractive(organizationId, dto); |
| 59 | + } |
| 60 | + |
| 61 | + @Post('upload') |
| 62 | + @ApiOperation({ summary: 'Upload an image as the organization chart' }) |
| 63 | + @ApiResponse({ status: 201, description: 'The uploaded organization chart' }) |
| 64 | + @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true })) |
| 65 | + async uploadOrgChart( |
| 66 | + @OrganizationId() organizationId: string, |
| 67 | + @Body() dto: UploadOrgChartDto, |
| 68 | + ) { |
| 69 | + return await this.orgChartService.uploadImage(organizationId, dto); |
| 70 | + } |
| 71 | + |
| 72 | + @Delete() |
| 73 | + @ApiOperation({ summary: 'Delete the organization chart' }) |
| 74 | + @ApiResponse({ status: 200, description: 'Deletion confirmation' }) |
| 75 | + async deleteOrgChart(@OrganizationId() organizationId: string) { |
| 76 | + return await this.orgChartService.delete(organizationId); |
| 77 | + } |
| 78 | +} |
0 commit comments