|
| 1 | +import { |
| 2 | + ApiBearerAuth, |
| 3 | + ApiForbiddenResponse, |
| 4 | + ApiOperation, |
| 5 | + ApiResponse, |
| 6 | + ApiTags, |
| 7 | + ApiUnauthorizedResponse |
| 8 | +} from '@nestjs/swagger'; |
| 9 | +import { Body, Controller, Get, HttpStatus, Param, Post, Res, UseFilters, UseGuards } from '@nestjs/common'; |
| 10 | +import { Response } from 'express'; |
| 11 | +import { AuthGuard } from '@nestjs/passport'; |
| 12 | +import { CustomExceptionFilter } from '@credebl/common/exception-handler'; |
| 13 | +import { EcosystemService } from './ecosystem.service'; |
| 14 | +import { ForbiddenErrorDto } from '../dtos/forbidden-error.dto'; |
| 15 | +import { OrgRoles } from 'libs/org-roles/enums'; |
| 16 | +import { ResponseMessages } from '@credebl/common/response-messages'; |
| 17 | +import { Roles } from '../authz/decorators/roles.decorator'; |
| 18 | +import { UnauthorizedErrorDto } from '../dtos/unauthorized-error.dto'; |
| 19 | +import { CreateEcosystemInvitationDto } from './dtos/send-ecosystem-invitation'; |
| 20 | +import { OrgRolesGuard } from '../authz/guards/org-roles.guard'; |
| 21 | +import { user } from '@prisma/client'; |
| 22 | +import { User } from '../authz/decorators/user.decorator'; |
| 23 | +import { ApiResponseDto } from '../dtos/apiResponse.dto'; |
| 24 | +import { CreateEcosystemDto } from 'apps/ecosystem/dtos/create-ecosystem-dto'; |
| 25 | +import { IResponse } from '@credebl/common/interfaces/response.interface'; |
| 26 | + |
| 27 | +@UseFilters(CustomExceptionFilter) |
| 28 | +@Controller('ecosystem') |
| 29 | +@ApiTags('ecosystem') |
| 30 | +@ApiUnauthorizedResponse({ |
| 31 | + description: 'Unauthorized', |
| 32 | + type: UnauthorizedErrorDto |
| 33 | +}) |
| 34 | +@ApiForbiddenResponse({ |
| 35 | + description: 'Forbidden', |
| 36 | + type: ForbiddenErrorDto |
| 37 | +}) |
| 38 | +export class EcosystemController { |
| 39 | + constructor(private readonly ecosystemService: EcosystemService) {} |
| 40 | + |
| 41 | + /** |
| 42 | + * Invitation to create ecosystem (platform admin) |
| 43 | + * @param createEcosystemInvitationDto |
| 44 | + * @returns Success message |
| 45 | + */ |
| 46 | + @Post('/invitations') |
| 47 | + @Roles(OrgRoles.PLATFORM_ADMIN) |
| 48 | + @ApiOperation({ |
| 49 | + summary: 'Create ecosystem invitation (platform admin)', |
| 50 | + description: 'Invite a user to create an ecosystem' |
| 51 | + }) |
| 52 | + @ApiResponse({ |
| 53 | + status: HttpStatus.CREATED, |
| 54 | + description: 'Success', |
| 55 | + type: ApiResponseDto |
| 56 | + }) |
| 57 | + @UseGuards(AuthGuard('jwt'), OrgRolesGuard) |
| 58 | + @ApiBearerAuth() |
| 59 | + async createInvitation( |
| 60 | + @Body() createEcosystemInvitationDto: CreateEcosystemInvitationDto, |
| 61 | + @User() reqUser: user, |
| 62 | + @Res() res: Response |
| 63 | + ): Promise<Response> { |
| 64 | + await this.ecosystemService.inviteUserToCreateEcosystem(createEcosystemInvitationDto.email, reqUser.id); |
| 65 | + |
| 66 | + return res.status(HttpStatus.CREATED).json({ |
| 67 | + statusCode: HttpStatus.CREATED, |
| 68 | + message: ResponseMessages.ecosystem.success.createInvitation |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get invitations sent by platform admin |
| 74 | + * @returns Invitation details |
| 75 | + */ |
| 76 | + |
| 77 | + @Get('/invitations') |
| 78 | + @ApiOperation({ |
| 79 | + summary: 'Get ecosystem invitations by user (platform admin)', |
| 80 | + description: 'Fetch all ecosystem invitations created by the logged-in user' |
| 81 | + }) |
| 82 | + @ApiResponse({ |
| 83 | + status: HttpStatus.OK, |
| 84 | + description: 'Invitations fetched successfully' |
| 85 | + }) |
| 86 | + @Roles(OrgRoles.PLATFORM_ADMIN) |
| 87 | + @UseGuards(AuthGuard('jwt'), OrgRolesGuard) |
| 88 | + @ApiBearerAuth() |
| 89 | + async getInvitations(@User() reqUser: user, @Res() res: Response): Promise<Response> { |
| 90 | + const invitations = await this.ecosystemService.getInvitationsByUserId(reqUser.id); |
| 91 | + |
| 92 | + return res.status(HttpStatus.OK).json({ |
| 93 | + statusCode: HttpStatus.OK, |
| 94 | + message: ResponseMessages.ecosystem.success.fetch, |
| 95 | + data: invitations |
| 96 | + }); |
| 97 | + } |
| 98 | + /** |
| 99 | + * Create new ecosystem |
| 100 | + * @param createEcosystemDto |
| 101 | + * @param orgId The ID of the organization |
| 102 | + * @returns Created ecosystem details |
| 103 | + */ |
| 104 | + @Post('/:orgId') |
| 105 | + @ApiOperation({ |
| 106 | + summary: 'Create a new ecosystem', |
| 107 | + description: 'Create a new ecosystem' |
| 108 | + }) |
| 109 | + @ApiResponse({ |
| 110 | + status: HttpStatus.CREATED, |
| 111 | + description: 'Created', |
| 112 | + type: ApiResponseDto |
| 113 | + }) |
| 114 | + @UseGuards(AuthGuard('jwt'), OrgRolesGuard) |
| 115 | + @ApiBearerAuth() |
| 116 | + @Roles(OrgRoles.OWNER) |
| 117 | + async createNewEcosystem( |
| 118 | + @Body() createEcosystemDto: CreateEcosystemDto, |
| 119 | + @Param('orgId') orgId: string, |
| 120 | + @User() user: user, |
| 121 | + @Res() res: Response |
| 122 | + ): Promise<Response> { |
| 123 | + createEcosystemDto.orgId = orgId; |
| 124 | + createEcosystemDto.userId = user?.id; |
| 125 | + |
| 126 | + const ecosystem = await this.ecosystemService.createEcosystem(createEcosystemDto); |
| 127 | + |
| 128 | + const finalResponse: IResponse = { |
| 129 | + statusCode: HttpStatus.CREATED, |
| 130 | + message: ResponseMessages.ecosystem.success.create, |
| 131 | + data: ecosystem |
| 132 | + }; |
| 133 | + |
| 134 | + return res.status(HttpStatus.CREATED).json(finalResponse); |
| 135 | + } |
| 136 | + /** |
| 137 | + * Get all ecosystems (platform admin) |
| 138 | + * @returns All ecosystems from platform |
| 139 | + */ |
| 140 | + @Get() |
| 141 | + @ApiOperation({ |
| 142 | + summary: 'Get all ecosystems (platform admin)', |
| 143 | + description: 'Fetch all ecosystems available on the platform' |
| 144 | + }) |
| 145 | + @ApiResponse({ |
| 146 | + status: HttpStatus.OK, |
| 147 | + description: 'Ecosystems fetched successfully' |
| 148 | + }) |
| 149 | + @Roles(OrgRoles.PLATFORM_ADMIN) |
| 150 | + @UseGuards(AuthGuard('jwt'), OrgRolesGuard) |
| 151 | + @ApiBearerAuth() |
| 152 | + async getAllEcosystems(@User() reqUser: user, @Res() res: Response): Promise<Response> { |
| 153 | + const ecosystems = await this.ecosystemService.getAllEcosystems(); |
| 154 | + |
| 155 | + return res.status(HttpStatus.OK).json({ |
| 156 | + statusCode: HttpStatus.OK, |
| 157 | + message: ResponseMessages.ecosystem.success.fetch, |
| 158 | + data: ecosystems |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Get specific ecosystem dashboard details |
| 164 | + * @param createEcosystemInvitationDto The details of the invitation |
| 165 | + * @param ecosystemId the ecosystem |
| 166 | + * @param orgId ID of the organization |
| 167 | + * @returns Details of specific ecosystem |
| 168 | + */ |
| 169 | + @Get('/:ecosystemId/:orgId/dashboard') |
| 170 | + @ApiOperation({ |
| 171 | + summary: 'Get ecosystem dashboard details (platform admin and organization owner)', |
| 172 | + description: 'Fetch ecosystem dashboard data for a specific organization' |
| 173 | + }) |
| 174 | + @ApiResponse({ |
| 175 | + status: HttpStatus.OK, |
| 176 | + description: 'Ecosystem dashboard data fetched successfully' |
| 177 | + }) |
| 178 | + @Roles(OrgRoles.PLATFORM_ADMIN, OrgRoles.OWNER, OrgRoles.ADMIN) |
| 179 | + @UseGuards(AuthGuard('jwt'), OrgRolesGuard) |
| 180 | + @ApiBearerAuth() |
| 181 | + async getEcosystemDashboard( |
| 182 | + @Param('ecosystemId') ecosystemId: string, |
| 183 | + @Param('orgId') orgId: string, |
| 184 | + @Res() res: Response |
| 185 | + ): Promise<Response> { |
| 186 | + const dashboardData = await this.ecosystemService.getEcosystemDashboard(ecosystemId, orgId); |
| 187 | + |
| 188 | + return res.status(HttpStatus.OK).json({ |
| 189 | + statusCode: HttpStatus.OK, |
| 190 | + message: ResponseMessages.ecosystem.success.fetch, |
| 191 | + data: dashboardData |
| 192 | + }); |
| 193 | + } |
| 194 | +} |
0 commit comments