77 HttpStatus ,
88 Inject ,
99 OnModuleInit ,
10+ Param ,
1011 Post ,
1112 Get ,
1213 UnauthorizedException ,
@@ -20,32 +21,49 @@ import {
2021 ApiHeader ,
2122 ApiResponse ,
2223 ApiBody ,
24+ ApiParam ,
2325} from '@nestjs/swagger' ;
24- import { ApiKeyProto , CommonProto , JwtProto , ProjectProto } from 'juno-proto' ;
26+ import {
27+ ApiKeyProto ,
28+ CommonProto ,
29+ JwtProto ,
30+ ProjectProto ,
31+ UserProto ,
32+ } from 'juno-proto' ;
2533import { lastValueFrom } from 'rxjs' ;
2634import { User } from 'src/decorators/user.decorator' ;
2735import {
2836 IssueApiKeyRequest ,
2937 IssueApiKeyResponse ,
3038 IssueJWTResponse ,
3139} from 'src/models/auth.dto' ;
40+ import {
41+ RequestNewAccountModel ,
42+ NewAccountRequestResponse ,
43+ NewAccountRequestsResponse ,
44+ userTypeStringToProto ,
45+ } from 'src/models/registration.dto' ;
3246import { userLinkedToProject } from 'src/user_project_validator' ;
3347
3448const { JWT_SERVICE_NAME } = JwtProto ;
3549const { API_KEY_SERVICE_NAME } = ApiKeyProto ;
3650const { PROJECT_SERVICE_NAME } = ProjectProto ;
51+ const { ACCOUNT_REQUEST_SERVICE_NAME } = UserProto ;
3752
3853@ApiTags ( 'auth' )
3954@Controller ( 'auth' )
4055export class AuthController implements OnModuleInit {
4156 private jwtService : JwtProto . JwtServiceClient ;
4257 private apiKeyService : ApiKeyProto . ApiKeyServiceClient ;
4358 private projectService : ProjectProto . ProjectServiceClient ;
59+ private accountRequestService : UserProto . AccountRequestServiceClient ;
4460
4561 constructor (
4662 @Inject ( JWT_SERVICE_NAME ) private jwtClient : ClientGrpc ,
4763 @Inject ( API_KEY_SERVICE_NAME ) private apiClient : ClientGrpc ,
4864 @Inject ( PROJECT_SERVICE_NAME ) private projectClient : ClientGrpc ,
65+ @Inject ( ACCOUNT_REQUEST_SERVICE_NAME )
66+ private accountRequestClient : ClientGrpc ,
4967 ) { }
5068
5169 onModuleInit ( ) {
@@ -59,6 +77,10 @@ export class AuthController implements OnModuleInit {
5977 this . projectClient . getService < ProjectProto . ProjectServiceClient > (
6078 PROJECT_SERVICE_NAME ,
6179 ) ;
80+ this . accountRequestService =
81+ this . accountRequestClient . getService < UserProto . AccountRequestServiceClient > (
82+ ACCOUNT_REQUEST_SERVICE_NAME ,
83+ ) ;
6284 }
6385
6486 @Post ( '/api_key/jwt' )
@@ -255,4 +277,129 @@ export class AuthController implements OnModuleInit {
255277 } ,
256278 } ;
257279 }
280+
281+ @Post ( '/account-request' )
282+ @ApiOperation ( {
283+ summary : 'Submit a new account request' ,
284+ description :
285+ 'Allows a prospective user to submit a request for a new account. The request is stored and can be reviewed by an admin.' ,
286+ } )
287+ @ApiCreatedResponse ( {
288+ description : 'Account request successfully created' ,
289+ type : NewAccountRequestResponse ,
290+ } )
291+ @ApiResponse ( {
292+ status : HttpStatus . BAD_REQUEST ,
293+ description : 'Bad request' ,
294+ } )
295+ @ApiBody ( { type : RequestNewAccountModel } )
296+ async createAccountRequest (
297+ @Body ( ) body : RequestNewAccountModel ,
298+ ) : Promise < NewAccountRequestResponse > {
299+ const created = await lastValueFrom (
300+ this . accountRequestService . createAccountRequest ( {
301+ email : body . email ,
302+ name : body . name ,
303+ password : body . password ,
304+ userType : userTypeStringToProto ( body . userType ) ,
305+ projectName : body . projectName ,
306+ } ) ,
307+ ) ;
308+ return new NewAccountRequestResponse ( created ) ;
309+ }
310+
311+ @Get ( '/account-request' )
312+ @ApiOperation ( {
313+ summary : 'Retrieve all account requests' ,
314+ description :
315+ 'Returns all pending account requests. Requires admin or superadmin credentials.' ,
316+ } )
317+ @ApiResponse ( {
318+ status : HttpStatus . OK ,
319+ description : 'All account requests returned' ,
320+ type : NewAccountRequestsResponse ,
321+ } )
322+ @ApiResponse ( {
323+ status : HttpStatus . UNAUTHORIZED ,
324+ description : 'Unauthorized' ,
325+ } )
326+ @ApiHeader ( {
327+ name : 'X-User-Email' ,
328+ description : 'Email of an admin or superadmin user' ,
329+ required : true ,
330+ schema : { type : 'string' } ,
331+ } )
332+ @ApiHeader ( {
333+ name : 'X-User-Password' ,
334+ description : 'Password of the admin or superadmin user' ,
335+ required : true ,
336+ schema : { type : 'string' } ,
337+ } )
338+ @ApiBearerAuth ( 'API_Key' )
339+ async getAllAccountRequests (
340+ @User ( ) user : CommonProto . User ,
341+ ) : Promise < NewAccountRequestsResponse > {
342+ if ( ! user || user . type === CommonProto . UserType . USER ) {
343+ throw new HttpException ( 'Unauthorized' , HttpStatus . UNAUTHORIZED ) ;
344+ }
345+ const result = await lastValueFrom (
346+ this . accountRequestService . getAllAccountRequests ( { } ) ,
347+ ) ;
348+ return new NewAccountRequestsResponse ( result ) ;
349+ }
350+
351+ @Delete ( '/account-request/:id' )
352+ @ApiOperation ( {
353+ summary : 'Delete an account request by ID' ,
354+ description :
355+ 'Deletes an account request by its ID. Requires admin or superadmin credentials.' ,
356+ } )
357+ @ApiParam ( {
358+ name : 'id' ,
359+ required : true ,
360+ description : 'ID of the account request to delete' ,
361+ type : String ,
362+ } )
363+ @ApiResponse ( {
364+ status : HttpStatus . OK ,
365+ description : 'Account request successfully deleted' ,
366+ type : NewAccountRequestResponse ,
367+ } )
368+ @ApiResponse ( {
369+ status : HttpStatus . NOT_FOUND ,
370+ description : 'Account request not found' ,
371+ } )
372+ @ApiResponse ( {
373+ status : HttpStatus . UNAUTHORIZED ,
374+ description : 'Unauthorized' ,
375+ } )
376+ @ApiHeader ( {
377+ name : 'X-User-Email' ,
378+ description : 'Email of an admin or superadmin user' ,
379+ required : true ,
380+ schema : { type : 'string' } ,
381+ } )
382+ @ApiHeader ( {
383+ name : 'X-User-Password' ,
384+ description : 'Password of the admin or superadmin user' ,
385+ required : true ,
386+ schema : { type : 'string' } ,
387+ } )
388+ @ApiBearerAuth ( 'API_Key' )
389+ async deleteAccountRequest (
390+ @User ( ) user : CommonProto . User ,
391+ @Param ( 'id' ) idStr : string ,
392+ ) : Promise < NewAccountRequestResponse > {
393+ if ( ! user || user . type === CommonProto . UserType . USER ) {
394+ throw new HttpException ( 'Unauthorized' , HttpStatus . UNAUTHORIZED ) ;
395+ }
396+ const id = parseInt ( idStr ) ;
397+ if ( Number . isNaN ( id ) ) {
398+ throw new HttpException ( 'id must be a number' , HttpStatus . BAD_REQUEST ) ;
399+ }
400+ const removed = await lastValueFrom (
401+ this . accountRequestService . deleteAccountRequest ( { id } ) ,
402+ ) ;
403+ return new NewAccountRequestResponse ( removed ) ;
404+ }
258405}
0 commit comments