| 1 | import { |
| 2 | Body, |
| 3 | Controller, |
| 4 | Delete, |
| 5 | Get, |
| 6 | Headers, |
| 7 | HttpCode, |
| 8 | HttpStatus, |
| 9 | Param, |
| 10 | Patch, |
| 11 | Post, |
| 12 | Put, |
| 13 | Req, |
| 14 | Session, |
| 15 | } from '@nestjs/common'; |
| 16 | import { |
| 17 | ApiBadRequestResponse, |
| 18 | ApiBody, |
| 19 | ApiConsumes, |
| 20 | ApiForbiddenResponse, |
| 21 | ApiNoContentResponse, |
| 22 | ApiNotFoundResponse, |
| 23 | ApiOkResponse, |
| 24 | ApiOperation, |
| 25 | ApiTags, |
| 26 | ApiUnsupportedMediaTypeResponse, |
| 27 | } from '@nestjs/swagger'; |
| 28 | import { fromNodeHeaders } |
| 29 | import type { Request |
| 30 | import { OptionalAuth, type |
| 31 | |
| 32 | import { ErrorResponseDTO } |
| 33 | import { |
| 34 | InviteByUsernameRequestDTO, |
| 35 | ListOutsideCollaboratorsResponseDTO, |
| 36 | ListReceivedOrganizationInvitationsResponseDTO, |
| 37 | OrganizationSettingsDTO, |
| 38 | SetPinnedRepositoriesDTO, |
| 39 | UpdateOrganizationSettingsDTO, |
| 40 | ListMyOrganizationsResponseDTO, |
| 41 | ListOrganizationsResponseDTO, |
| 42 | OrganizationProfileDTO, |
| 43 | } from './dto/organization.dto.js'; |
| 44 | import { UploadAvatarResponseDTO } |
| 45 | import { OrganizationsService } |
| 46 | |
| 47 | @ApiTags('Organizations') |
| 48 | @Controller('organizations') |
| 49 | export class OrganizationsController |
| 50 | constructor(private readonly |
| 51 | |
| 52 | @Get() |
| 53 | @ApiOperation({ |
| 54 | summary: 'List my organizations', |
| 55 | description: |
| 56 | "Organizations the signed-in user belongs to, with the user's role in each.", |
| 57 | }) |
| 58 | @ApiOkResponse({ type |
| 59 | listMine( |
| 60 | @Session() session |
| 61 | ): Promise<ListMyOrganizationsResponseDTO |
| 62 | return this.organizations |
| 63 | } |
| 64 | |
| 65 | @Put(':slug/logo' |
| 66 | @ApiOperation({ |
| 67 | summary: "Upload the organization's logo", |
| 68 | description: |
| 69 | 'Takes the raw image bytes, PNG or JPEG, and sets them as the logo. Admins only.', |
| 70 | }) |
| 71 | @ApiConsumes('image/png' |
| 72 | @ApiBody({ schema |
| 73 | @ApiOkResponse({ type |
| 74 | @ApiBadRequestResponse({ type |
| 75 | @ApiForbiddenResponse({ type |
| 76 | @ApiNotFoundResponse({ type |
| 77 | @ApiUnsupportedMediaTypeResponse({ type |
| 78 | setLogo( |
| 79 | @Param('slug' |
| 80 | @Req() request |
| 81 | @Headers('content-type' |
| 82 | @Session() session |
| 83 | ): Promise<UploadAvatarResponseDTO |
| 84 | return this.organizations |
| 85 | slug, |
| 86 | requesterId: session.user |
| 87 | contentType: contentType ?? |
| 88 | body: Buffer.isBuffer |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Get(':slug/settings' |
| 93 | @ApiOperation({ summary |
| 94 | @ApiOkResponse({ type |
| 95 | @ApiForbiddenResponse({ type |
| 96 | @ApiNotFoundResponse({ type |
| 97 | getSettings( |
| 98 | @Param('slug' |
| 99 | @Session() session |
| 100 | ): Promise<OrganizationSettingsDTO |
| 101 | return this.organizations |
| 102 | } |
| 103 | |
| 104 | @Patch(':slug/settings' |
| 105 | @ApiOperation({ |
| 106 | summary: 'Change organization settings', |
| 107 | description: |
| 108 | 'Base permission, repository policies and the public profile. Admins only.', |
| 109 | }) |
| 110 | @ApiOkResponse({ type |
| 111 | @ApiBadRequestResponse({ type |
| 112 | @ApiForbiddenResponse({ type |
| 113 | @ApiNotFoundResponse({ type |
| 114 | updateSettings( |
| 115 | @Param('slug' |
| 116 | @Body() body |
| 117 | @Session() session |
| 118 | ): Promise<OrganizationSettingsDTO |
| 119 | return this.organizations |
| 120 | } |
| 121 | |
| 122 | @Put(':slug/public-members' |
| 123 | @HttpCode(HttpStatus. |
| 124 | @ApiOperation({ |
| 125 | summary: 'Make my membership public', |
| 126 | description: |
| 127 | "Lists the requester among the organization's public members, and the organization on their profile.", |
| 128 | }) |
| 129 | @ApiNoContentResponse() |
| 130 | @ApiNotFoundResponse({ type |
| 131 | publicize( |
| 132 | @Param('slug' |
| 133 | @Session() session |
| 134 | ): Promise<void |
| 135 | return this.organizations |
| 136 | } |
| 137 | |
| 138 | @Delete(':slug/public-members' |
| 139 | @HttpCode(HttpStatus. |
| 140 | @ApiOperation({ summary |
| 141 | @ApiNoContentResponse() |
| 142 | @ApiNotFoundResponse({ type |
| 143 | conceal( |
| 144 | @Param('slug' |
| 145 | @Session() session |
| 146 | ): Promise<void |
| 147 | return this.organizations |
| 148 | } |
| 149 | |
| 150 | @Put(':slug/pins' |
| 151 | @HttpCode(HttpStatus. |
| 152 | @ApiOperation({ |
| 153 | summary: 'Pin repositories', |
| 154 | description: |
| 155 | 'Replaces the pinned repositories, up to six, in the order given. Admins only.', |
| 156 | }) |
| 157 | @ApiNoContentResponse() |
| 158 | @ApiBadRequestResponse({ type |
| 159 | @ApiForbiddenResponse({ type |
| 160 | @ApiNotFoundResponse({ type |
| 161 | setPinned( |
| 162 | @Param('slug' |
| 163 | @Body() body |
| 164 | @Session() session |
| 165 | ): Promise<void |
| 166 | return this.organizations |
| 167 | slug, |
| 168 | session.user.id |
| 169 | body.repositories, |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | @Get(':slug/outside-collaborators' |
| 174 | @ApiOperation({ |
| 175 | summary: 'List outside collaborators', |
| 176 | description: |
| 177 | "People with access to the organization's repositories who are not members. Admins only.", |
| 178 | }) |
| 179 | @ApiOkResponse({ type |
| 180 | @ApiForbiddenResponse({ type |
| 181 | @ApiNotFoundResponse({ type |
| 182 | listOutsideCollaborators( |
| 183 | @Param('slug' |
| 184 | @Session() session |
| 185 | ): Promise<ListOutsideCollaboratorsResponseDTO |
| 186 | return this.organizations |
| 187 | } |
| 188 | |
| 189 | @Delete(':slug/outside-collaborators/:username' |
| 190 | @HttpCode(HttpStatus. |
| 191 | @ApiOperation({ |
| 192 | summary: 'Remove an outside collaborator', |
| 193 | description: |
| 194 | 'Removes them, and their pending invitations, from every repository of the organization. Admins only.', |
| 195 | }) |
| 196 | @ApiNoContentResponse() |
| 197 | @ApiForbiddenResponse({ type |
| 198 | @ApiNotFoundResponse({ type |
| 199 | removeOutsideCollaborator( |
| 200 | @Param('slug' |
| 201 | @Param('username' |
| 202 | @Session() session |
| 203 | ): Promise<void |
| 204 | return this.organizations |
| 205 | slug, |
| 206 | session.user.id |
| 207 | username, |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | @Post(':slug/invitations' |
| 212 | @HttpCode(HttpStatus. |
| 213 | @ApiOperation({ |
| 214 | summary: 'Invite a user by username', |
| 215 | description: |
| 216 | "Creates the same invitation an email invite would, addressed to the user's email without revealing it. Admins only.", |
| 217 | }) |
| 218 | @ApiNoContentResponse() |
| 219 | @ApiBadRequestResponse({ type |
| 220 | @ApiForbiddenResponse({ type |
| 221 | @ApiNotFoundResponse({ type |
| 222 | inviteByUsername( |
| 223 | @Param('slug' |
| 224 | @Body() body |
| 225 | @Req() request |
| 226 | @Session() session |
| 227 | ): Promise<void |
| 228 | return this.organizations |
| 229 | slug, |
| 230 | username: body.username |
| 231 | role: body.role |
| 232 | requesterId: session.user |
| 233 | headers: fromNodeHeaders(request |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | @Delete(':slug/logo' |
| 238 | @HttpCode(HttpStatus. |
| 239 | @ApiOperation({ summary |
| 240 | @ApiNoContentResponse() |
| 241 | @ApiForbiddenResponse({ type |
| 242 | @ApiNotFoundResponse({ type |
| 243 | removeLogo( |
| 244 | @Param('slug' |
| 245 | @Session() session |
| 246 | ): Promise<void |
| 247 | return this.organizations |
| 248 | } |
| 249 | |
| 250 | @Get(':slug' |
| 251 | @OptionalAuth() |
| 252 | @ApiOperation({ |
| 253 | summary: 'Organization profile', |
| 254 | description: |
| 255 | 'Public, except that members and the viewer role are only returned to members.', |
| 256 | }) |
| 257 | @ApiOkResponse({ type |
| 258 | @ApiNotFoundResponse({ type |
| 259 | getProfile( |
| 260 | @Param('slug' |
| 261 | @Session() session |
| 262 | ): Promise<OrganizationProfileDTO |
| 263 | return this.organizations |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | @ApiTags('Organizations') |
| 268 | @Controller('users') |
| 269 | export class UserOrganizationsController |
| 270 | constructor(private readonly |
| 271 | |
| 272 | @Get(':username/organizations' |
| 273 | @OptionalAuth() |
| 274 | @ApiOperation({ |
| 275 | summary: 'List the organizations a user belongs to', |
| 276 | description: |
| 277 | 'Membership is private: the user sees all of theirs, anyone else only the ones they share.', |
| 278 | }) |
| 279 | @ApiOkResponse({ type |
| 280 | @ApiNotFoundResponse({ type |
| 281 | listForUser( |
| 282 | @Param('username' |
| 283 | @Session() session |
| 284 | ): Promise<ListOrganizationsResponseDTO |
| 285 | return this.organizations |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | @ApiTags('Organizations') |
| 290 | @Controller('invitations/organizations') |
| 291 | export class OrganizationInvitationsController |
| 292 | constructor(private readonly |
| 293 | |
| 294 | @Get() |
| 295 | @ApiOperation({ |
| 296 | summary: 'List my organization invitations', |
| 297 | description: |
| 298 | "Pending invitations to organizations, addressed to the signed-in account's email. Accept or reject them through Better Auth's organization endpoints.", |
| 299 | }) |
| 300 | @ApiOkResponse({ type |
| 301 | list( |
| 302 | @Session() session |
| 303 | ): Promise<ListReceivedOrganizationInvitationsResponseDTO |
| 304 | return this.organizations |
| 305 | } |
| 306 | } |