|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Post, |
| 5 | + Patch, |
| 6 | + Delete, |
| 7 | + Body, |
| 8 | + Param, |
| 9 | + UseGuards |
| 10 | +} from '@nestjs/common'; |
| 11 | +import { |
| 12 | + ApiBody, |
| 13 | + ApiHeader, |
| 14 | + ApiOperation, |
| 15 | + ApiParam, |
| 16 | + ApiResponse, |
| 17 | + ApiSecurity, |
| 18 | + ApiTags, |
| 19 | +} from '@nestjs/swagger'; |
| 20 | +import { |
| 21 | + AuthContext, |
| 22 | + OrganizationId, |
| 23 | +} from '../auth/auth-context.decorator'; |
| 24 | +import { HybridAuthGuard } from '../auth/hybrid-auth.guard'; |
| 25 | +import type { AuthContext as AuthContextType } from '../auth/types'; |
| 26 | +import { CreateContextDto } from './dto/create-context.dto'; |
| 27 | +import { UpdateContextDto } from './dto/update-context.dto'; |
| 28 | +import { ContextService } from './context.service'; |
| 29 | +import { CONTEXT_OPERATIONS } from './schemas/context-operations'; |
| 30 | +import { CONTEXT_PARAMS } from './schemas/context-params'; |
| 31 | +import { CONTEXT_BODIES } from './schemas/context-bodies'; |
| 32 | +import { GET_ALL_CONTEXT_RESPONSES } from './schemas/get-all-context.responses'; |
| 33 | +import { GET_CONTEXT_BY_ID_RESPONSES } from './schemas/get-context-by-id.responses'; |
| 34 | +import { CREATE_CONTEXT_RESPONSES } from './schemas/create-context.responses'; |
| 35 | +import { UPDATE_CONTEXT_RESPONSES } from './schemas/update-context.responses'; |
| 36 | +import { DELETE_CONTEXT_RESPONSES } from './schemas/delete-context.responses'; |
| 37 | + |
| 38 | +@ApiTags('Context') |
| 39 | +@Controller({ path: 'context', version: '1' }) |
| 40 | +@UseGuards(HybridAuthGuard) |
| 41 | +@ApiSecurity('apikey') |
| 42 | +@ApiHeader({ |
| 43 | + name: 'X-Organization-Id', |
| 44 | + description: |
| 45 | + 'Organization ID (required for session auth, optional for API key auth)', |
| 46 | + required: false, |
| 47 | +}) |
| 48 | +export class ContextController { |
| 49 | + constructor(private readonly contextService: ContextService) {} |
| 50 | + |
| 51 | + @Get() |
| 52 | + @ApiOperation(CONTEXT_OPERATIONS.getAllContext) |
| 53 | + @ApiResponse(GET_ALL_CONTEXT_RESPONSES[200]) |
| 54 | + @ApiResponse(GET_ALL_CONTEXT_RESPONSES[401]) |
| 55 | + @ApiResponse(GET_ALL_CONTEXT_RESPONSES[404]) |
| 56 | + @ApiResponse(GET_ALL_CONTEXT_RESPONSES[500]) |
| 57 | + async getAllContext( |
| 58 | + @OrganizationId() organizationId: string, |
| 59 | + @AuthContext() authContext: AuthContextType, |
| 60 | + ) { |
| 61 | + const contextEntries = await this.contextService.findAllByOrganization(organizationId); |
| 62 | + |
| 63 | + return { |
| 64 | + data: contextEntries, |
| 65 | + count: contextEntries.length, |
| 66 | + authType: authContext.authType, |
| 67 | + ...(authContext.userId && authContext.userEmail && { |
| 68 | + authenticatedUser: { |
| 69 | + id: authContext.userId, |
| 70 | + email: authContext.userEmail, |
| 71 | + }, |
| 72 | + }), |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + @Get(':id') |
| 77 | + @ApiOperation(CONTEXT_OPERATIONS.getContextById) |
| 78 | + @ApiParam(CONTEXT_PARAMS.contextId) |
| 79 | + @ApiResponse(GET_CONTEXT_BY_ID_RESPONSES[200]) |
| 80 | + @ApiResponse(GET_CONTEXT_BY_ID_RESPONSES[401]) |
| 81 | + @ApiResponse(GET_CONTEXT_BY_ID_RESPONSES[404]) |
| 82 | + @ApiResponse(GET_CONTEXT_BY_ID_RESPONSES[500]) |
| 83 | + async getContextById( |
| 84 | + @Param('id') contextId: string, |
| 85 | + @OrganizationId() organizationId: string, |
| 86 | + @AuthContext() authContext: AuthContextType, |
| 87 | + ) { |
| 88 | + const contextEntry = await this.contextService.findById(contextId, organizationId); |
| 89 | + |
| 90 | + return { |
| 91 | + ...contextEntry, |
| 92 | + authType: authContext.authType, |
| 93 | + ...(authContext.userId && authContext.userEmail && { |
| 94 | + authenticatedUser: { |
| 95 | + id: authContext.userId, |
| 96 | + email: authContext.userEmail, |
| 97 | + }, |
| 98 | + }), |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + @Post() |
| 103 | + @ApiOperation(CONTEXT_OPERATIONS.createContext) |
| 104 | + @ApiBody(CONTEXT_BODIES.createContext) |
| 105 | + @ApiResponse(CREATE_CONTEXT_RESPONSES[201]) |
| 106 | + @ApiResponse(CREATE_CONTEXT_RESPONSES[400]) |
| 107 | + @ApiResponse(CREATE_CONTEXT_RESPONSES[401]) |
| 108 | + @ApiResponse(CREATE_CONTEXT_RESPONSES[404]) |
| 109 | + @ApiResponse(CREATE_CONTEXT_RESPONSES[500]) |
| 110 | + async createContext( |
| 111 | + @Body() createContextDto: CreateContextDto, |
| 112 | + @OrganizationId() organizationId: string, |
| 113 | + @AuthContext() authContext: AuthContextType, |
| 114 | + ) { |
| 115 | + const contextEntry = await this.contextService.create(organizationId, createContextDto); |
| 116 | + |
| 117 | + return { |
| 118 | + ...contextEntry, |
| 119 | + authType: authContext.authType, |
| 120 | + ...(authContext.userId && authContext.userEmail && { |
| 121 | + authenticatedUser: { |
| 122 | + id: authContext.userId, |
| 123 | + email: authContext.userEmail, |
| 124 | + }, |
| 125 | + }), |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + @Patch(':id') |
| 130 | + @ApiOperation(CONTEXT_OPERATIONS.updateContext) |
| 131 | + @ApiParam(CONTEXT_PARAMS.contextId) |
| 132 | + @ApiBody(CONTEXT_BODIES.updateContext) |
| 133 | + @ApiResponse(UPDATE_CONTEXT_RESPONSES[200]) |
| 134 | + @ApiResponse(UPDATE_CONTEXT_RESPONSES[400]) |
| 135 | + @ApiResponse(UPDATE_CONTEXT_RESPONSES[401]) |
| 136 | + @ApiResponse(UPDATE_CONTEXT_RESPONSES[404]) |
| 137 | + @ApiResponse(UPDATE_CONTEXT_RESPONSES[500]) |
| 138 | + async updateContext( |
| 139 | + @Param('id') contextId: string, |
| 140 | + @Body() updateContextDto: UpdateContextDto, |
| 141 | + @OrganizationId() organizationId: string, |
| 142 | + @AuthContext() authContext: AuthContextType, |
| 143 | + ) { |
| 144 | + const updatedContextEntry = await this.contextService.updateById( |
| 145 | + contextId, |
| 146 | + organizationId, |
| 147 | + updateContextDto, |
| 148 | + ); |
| 149 | + |
| 150 | + return { |
| 151 | + ...updatedContextEntry, |
| 152 | + authType: authContext.authType, |
| 153 | + ...(authContext.userId && authContext.userEmail && { |
| 154 | + authenticatedUser: { |
| 155 | + id: authContext.userId, |
| 156 | + email: authContext.userEmail, |
| 157 | + }, |
| 158 | + }), |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + @Delete(':id') |
| 163 | + @ApiOperation(CONTEXT_OPERATIONS.deleteContext) |
| 164 | + @ApiParam(CONTEXT_PARAMS.contextId) |
| 165 | + @ApiResponse(DELETE_CONTEXT_RESPONSES[200]) |
| 166 | + @ApiResponse(DELETE_CONTEXT_RESPONSES[401]) |
| 167 | + @ApiResponse(DELETE_CONTEXT_RESPONSES[404]) |
| 168 | + @ApiResponse(DELETE_CONTEXT_RESPONSES[500]) |
| 169 | + async deleteContext( |
| 170 | + @Param('id') contextId: string, |
| 171 | + @OrganizationId() organizationId: string, |
| 172 | + @AuthContext() authContext: AuthContextType, |
| 173 | + ) { |
| 174 | + const result = await this.contextService.deleteById(contextId, organizationId); |
| 175 | + |
| 176 | + return { |
| 177 | + ...result, |
| 178 | + authType: authContext.authType, |
| 179 | + ...(authContext.userId && authContext.userEmail && { |
| 180 | + authenticatedUser: { |
| 181 | + id: authContext.userId, |
| 182 | + email: authContext.userEmail, |
| 183 | + }, |
| 184 | + }), |
| 185 | + }; |
| 186 | + } |
| 187 | +} |
0 commit comments