-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathorganization.controller.ts
More file actions
165 lines (155 loc) · 5.06 KB
/
organization.controller.ts
File metadata and controls
165 lines (155 loc) · 5.06 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Patch,
Post,
UseGuards,
} from '@nestjs/common';
import {
ApiBody,
ApiHeader,
ApiOperation,
ApiResponse,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import {
AuthContext,
IsApiKeyAuth,
OrganizationId,
} from '../auth/auth-context.decorator';
import { HybridAuthGuard } from '../auth/hybrid-auth.guard';
import type { AuthContext as AuthContextType } from '../auth/types';
import type { UpdateOrganizationDto } from './dto/update-organization.dto';
import type { TransferOwnershipDto } from './dto/transfer-ownership.dto';
import { OrganizationService } from './organization.service';
import { GET_ORGANIZATION_RESPONSES } from './schemas/get-organization.responses';
import { UPDATE_ORGANIZATION_RESPONSES } from './schemas/update-organization.responses';
import { DELETE_ORGANIZATION_RESPONSES } from './schemas/delete-organization.responses';
import { TRANSFER_OWNERSHIP_RESPONSES } from './schemas/transfer-ownership.responses';
import {
UPDATE_ORGANIZATION_BODY,
TRANSFER_OWNERSHIP_BODY,
} from './schemas/organization-api-bodies';
import { ORGANIZATION_OPERATIONS } from './schemas/organization-operations';
@ApiTags('Organization')
@Controller({ path: 'organization', version: '1' })
@UseGuards(HybridAuthGuard)
@ApiSecurity('apikey') // Still document API key for external customers
@ApiHeader({
name: 'X-Organization-Id',
description:
'Organization ID (required for session auth, optional for API key auth)',
required: false,
})
export class OrganizationController {
constructor(private readonly organizationService: OrganizationService) {}
@Get()
@ApiOperation(ORGANIZATION_OPERATIONS.getOrganization)
@ApiResponse(GET_ORGANIZATION_RESPONSES[200])
@ApiResponse(GET_ORGANIZATION_RESPONSES[401])
async getOrganization(
@OrganizationId() organizationId: string,
@AuthContext() authContext: AuthContextType,
@IsApiKeyAuth() isApiKey: boolean,
) {
const org = await this.organizationService.findById(organizationId);
return {
...org,
authType: authContext.authType,
// Include user context for session auth (helpful for debugging)
...(authContext.userId && {
authenticatedUser: {
id: authContext.userId,
email: authContext.userEmail,
},
}),
};
}
@Patch()
@ApiOperation(ORGANIZATION_OPERATIONS.updateOrganization)
@ApiBody(UPDATE_ORGANIZATION_BODY)
@ApiResponse(UPDATE_ORGANIZATION_RESPONSES[200])
@ApiResponse(UPDATE_ORGANIZATION_RESPONSES[400])
@ApiResponse(UPDATE_ORGANIZATION_RESPONSES[401])
@ApiResponse(UPDATE_ORGANIZATION_RESPONSES[404])
async updateOrganization(
@OrganizationId() organizationId: string,
@AuthContext() authContext: AuthContextType,
@Body() updateData: UpdateOrganizationDto,
) {
const updatedOrg = await this.organizationService.updateById(
organizationId,
updateData,
);
return {
...updatedOrg,
authType: authContext.authType,
// Include user context for session auth (helpful for debugging)
...(authContext.userId && {
authenticatedUser: {
id: authContext.userId,
email: authContext.userEmail,
},
}),
};
}
@Post('transfer-ownership')
@ApiOperation(ORGANIZATION_OPERATIONS.transferOwnership)
@ApiBody(TRANSFER_OWNERSHIP_BODY)
@ApiResponse(TRANSFER_OWNERSHIP_RESPONSES[200])
@ApiResponse(TRANSFER_OWNERSHIP_RESPONSES[400])
@ApiResponse(TRANSFER_OWNERSHIP_RESPONSES[401])
@ApiResponse(TRANSFER_OWNERSHIP_RESPONSES[403])
@ApiResponse(TRANSFER_OWNERSHIP_RESPONSES[404])
async transferOwnership(
@OrganizationId() organizationId: string,
@AuthContext() authContext: AuthContextType,
@Body() transferData: TransferOwnershipDto,
) {
if (!authContext.userId) {
throw new BadRequestException(
'User ID is required for this operation. This endpoint requires session authentication.',
);
}
const result = await this.organizationService.transferOwnership(
organizationId,
authContext.userId,
transferData.newOwnerId,
);
return {
...result,
authType: authContext.authType,
// Include user context for session auth (helpful for debugging)
authenticatedUser: {
id: authContext.userId,
email: authContext.userEmail,
},
};
}
@Delete()
@ApiOperation(ORGANIZATION_OPERATIONS.deleteOrganization)
@ApiResponse(DELETE_ORGANIZATION_RESPONSES[200])
@ApiResponse(DELETE_ORGANIZATION_RESPONSES[401])
@ApiResponse(DELETE_ORGANIZATION_RESPONSES[404])
async deleteOrganization(
@OrganizationId() organizationId: string,
@AuthContext() authContext: AuthContextType,
) {
const result = await this.organizationService.deleteById(organizationId);
return {
...result,
authType: authContext.authType,
// Include user context for session auth (helpful for debugging)
...(authContext.userId && {
authenticatedUser: {
id: authContext.userId,
email: authContext.userEmail,
},
}),
};
}
}