-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refactored participant request (team, member) service #1936
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,16 @@ | ||
/* eslint-disable prettier/prettier */ | ||
import { CacheModule, Module } from '@nestjs/common'; | ||
|
||
import { ParticipantsRequestService } from '../participants-request/participants-request.service'; | ||
import { LocationTransferService } from '../utils/location-transfer/location-transfer.service'; | ||
import { AwsService } from '../utils/aws/aws.service'; | ||
import { RedisService } from '../utils/redis/redis.service'; | ||
import { SlackService } from '../utils/slack/slack.service'; | ||
import { AdminService } from './admin.service'; | ||
import { JwtService } from '../utils/jwt/jwt.service'; | ||
import { AdminController } from './admin.controller'; | ||
import { ForestAdminService } from '../utils/forest-admin/forest-admin.service'; | ||
|
||
import { ParticipantsRequestModule } from '../participants-request/participants-request.module'; | ||
import { SharedModule } from '../shared/shared.module'; | ||
import { AdminParticipantsRequestController } from './participants-request.controller'; | ||
import { AdminAuthController } from './auth.controller'; | ||
@Module({ | ||
imports: [CacheModule.register()], | ||
controllers: [AdminController], | ||
imports: [CacheModule.register(), ParticipantsRequestModule, SharedModule], | ||
controllers: [AdminParticipantsRequestController, AdminAuthController], | ||
providers: [ | ||
ParticipantsRequestService, | ||
LocationTransferService, | ||
AwsService, | ||
RedisService, | ||
SlackService, | ||
AdminService, | ||
JwtService, | ||
ForestAdminService, | ||
JwtService | ||
], | ||
}) | ||
export class AdminModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,41 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { JwtService } from '../utils/jwt/jwt.service'; | ||
import { LogService } from '../shared/log.service'; | ||
|
||
@Injectable() | ||
export class AdminService { | ||
constructor(private readonly jwtService: JwtService) {} | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
private logger: LogService, | ||
) {} | ||
|
||
/** | ||
* Validates given username and password for the admin against the env config and | ||
* creates a signed jwt token if credentials are valid else, throws {@link UnauthorizedException} | ||
* @param username admin username | ||
* @param password admin password | ||
* @returns a signed jwt token in json format {code: 1, accessToken: <token>} if crdentials valid | ||
* @throws UnauthorizedException if credentials not valid | ||
* Logs in the admin using the provided username and password. | ||
* Validates credentials from environment variables. | ||
* @param username - Admin username | ||
* @param password - Admin password | ||
* @returns Object containing access token on successful login | ||
* @throws UnauthorizedException if credentials are invalid | ||
*/ | ||
async signIn(username: string, password: string): Promise<any> { | ||
const usernameFromEnv = process.env.ADMIN_USERNAME; | ||
const passwordFromEnv = process.env.ADMIN_PASSWORD; | ||
|
||
if (username !== usernameFromEnv || passwordFromEnv !== password) { | ||
throw new UnauthorizedException(); | ||
async login(username: string, password: string): Promise<{ code:Number, accessToken: string }> { | ||
if (!this.isValidAdminCredentials(username, password)) { | ||
this.logger.error('Invalid credentials provided for admin login.'); | ||
throw new UnauthorizedException('Invalid credentials'); | ||
} | ||
this.logger.info('Generating admin access token...'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can add some context like username |
||
const accessToken = await this.jwtService.getSignedToken(['DIRECTORYADMIN']); | ||
return { code: 1, accessToken: accessToken }; | ||
} | ||
|
||
/** | ||
* Validates the provided credentials against stored environment variables. | ||
* @param username - Input username | ||
* @param password - Input password | ||
* @returns Boolean indicating if credentials are valid | ||
*/ | ||
private isValidAdminCredentials(username: string, password: string): boolean { | ||
const validUsername = process.env.ADMIN_USERNAME; | ||
const validPassword = process.env.ADMIN_PASSWORD; | ||
return username === validUsername && password === validPassword; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Body, Controller, Post, UsePipes } from '@nestjs/common'; | ||
import { LoginRequestDto } from 'libs/contracts/src/schema'; | ||
import { ZodValidationPipe } from 'nestjs-zod'; | ||
import { AdminService } from './admin.service'; | ||
|
||
@Controller('v1/admin/auth') | ||
export class AdminAuthController { | ||
constructor(private readonly adminService: AdminService) {} | ||
|
||
/** | ||
* Handles admin login requests. | ||
* Validates the request body against the LoginRequestDto schema. | ||
* @param loginRequestDto - The login request data transfer object | ||
* @returns Access token if login is successful | ||
*/ | ||
@Post('login') | ||
@UsePipes(ZodValidationPipe) | ||
async login(@Body() loginRequestDto: LoginRequestDto): Promise<{ accessToken: string }> { | ||
const { username, password } = loginRequestDto; | ||
return await this.adminService.login(username, password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
Param, | ||
Patch, | ||
Put, | ||
Query, | ||
UseGuards, | ||
UsePipes, | ||
BadRequestException, | ||
NotFoundException | ||
} from '@nestjs/common'; | ||
import { NoCache } from '../decorators/no-cache.decorator'; | ||
import { ParticipantsRequestService } from '../participants-request/participants-request.service'; | ||
import { AdminAuthGuard } from '../guards/admin-auth.guard'; | ||
import { ParticipantsReqValidationPipe } from '../pipes/participant-request-validation.pipe'; | ||
import { ProcessParticipantReqDto } from 'libs/contracts/src/schema'; | ||
import { ApprovalStatus, ParticipantsRequest, ParticipantType } from '@prisma/client'; | ||
|
||
@Controller('v1/admin/participants-request') | ||
@UseGuards(AdminAuthGuard) | ||
export class AdminParticipantsRequestController { | ||
constructor( | ||
private readonly participantsRequestService: ParticipantsRequestService | ||
) {} | ||
|
||
/** | ||
* Retrieve all participants requests based on query parameters. | ||
* @param query - Filter parameters for participants requests | ||
* @returns A list of participants requests | ||
*/ | ||
@Get("/") | ||
@NoCache() | ||
async findAll(@Query() query) { | ||
return this.participantsRequestService.getAll(query); | ||
} | ||
|
||
/** | ||
* Retrieve a single participants request by its UID. | ||
* @param uid - The unique identifier of the participants request | ||
* @returns The participants request entry matching the UID | ||
*/ | ||
@Get("/:uid") | ||
@NoCache() | ||
async findOne(@Param('uid') uid: string) { | ||
return await this.participantsRequestService.findOneByUid(uid); | ||
} | ||
|
||
/** | ||
* Update an existing participants request by its UID. | ||
* @param body - The updated data for the participants request | ||
* @param uid - The unique identifier of the participants request | ||
* @returns The updated participants request entry | ||
*/ | ||
@Put('/:uid') | ||
@UsePipes(new ParticipantsReqValidationPipe()) | ||
async updateRequest( | ||
@Body() body: any, | ||
@Param('uid') uid: string | ||
) { | ||
return await this.participantsRequestService.updateByUid(uid, body); | ||
} | ||
|
||
/** | ||
* Process (approve/reject) a pending participants request. | ||
* @param body - The request body containing the status for processing (e.g., approve/reject) | ||
* @param uid - The unique identifier of the participants request | ||
* @returns The result of processing the participants request | ||
*/ | ||
@Patch('/:uid') | ||
async processRequest( | ||
@Param('uid') uid: string, | ||
@Body() body: ProcessParticipantReqDto | ||
): Promise<any> { | ||
const participantRequest: ParticipantsRequest | null = await this.participantsRequestService.findOneByUid(uid); | ||
if (!participantRequest) { | ||
throw new NotFoundException('Request not found'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change as Participant Request no found |
||
} | ||
if (participantRequest.status !== ApprovalStatus.PENDING) { | ||
throw new BadRequestException( | ||
`Request cannot be processed. It has already been ${participantRequest.status.toLowerCase()}.` | ||
); | ||
} | ||
if (participantRequest.participantType === ParticipantType.TEAM && !participantRequest.requesterEmailId) { | ||
throw new BadRequestException( | ||
'Requester email is required for team participation requests. Please provide a valid email address.' | ||
); | ||
} | ||
return await this.participantsRequestService.processRequestByUid(uid, participantRequest, body.status); | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name can be simply ParticipantsRequestController