import { LoggedInUser , Role } from '@energyweb/origin-backend-core' ; import { NullOrUndefinedResultInterceptor , Roles , RolesGuard , UserDecorator } from '@energyweb/origin-backend-utils' ; import { Body , ClassSerializerInterceptor , Controller , Get , HttpStatus , Post , UseGuards , UseInterceptors , UsePipes , ValidationPipe } from '@nestjs/common' ; import { AuthGuard } from '@nestjs/passport' ; import { ApiTags , ApiResponse , ApiBearerAuth , ApiBody } from '@nestjs/swagger' ; import { NewRegistrationDTO } from './new-registration.dto' ; import { RegisterResponseDTO } from './registration-response.dto' ; import { RegistrationDTO } from './registration.dto' ; import { RegistrationService } from './registration.service' ; @ ApiTags ( 'irec-registration' ) @ ApiBearerAuth ( 'access-token' ) @ UseInterceptors ( ClassSerializerInterceptor , NullOrUndefinedResultInterceptor ) @ UsePipes ( ValidationPipe ) @ Controller ( 'irec/registration' ) export class RegistrationController { constructor ( private readonly registrationService ) { } @ Get ( ) @ UseGuards ( AuthGuard ( ) ) @ ApiResponse ( { status : HttpStatus . OK , type : [ RegistrationDTO ] , description : 'Get all registrations' } ) public getRegistrations ( @ UserDecorator ( ) loggedInUser ) { const isAdmin = loggedInUser . hasRole ( Role . Admin , Role . SupportAgent ) ; return this . registrationService . find ( isAdmin ? null : loggedInUser . organizationId . toString ( ) ) ; } @ Post ( ) @ UseGuards ( AuthGuard ( ) , RolesGuard ) @ Roles ( Role . OrganizationAdmin , Role . Admin ) @ ApiBody ( { type : NewRegistrationDTO } ) @ ApiResponse ( { status : HttpStatus . CREATED , type : RegisterResponseDTO , description : 'Register an I-REC organization' } ) public async register ( @ UserDecorator ( ) loggedInUser , @ Body ( ) registration ) { const id = await this . registrationService . register ( loggedInUser , registration ) ; return { id } ; } }