-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored participant request (team, member) service
- Loading branch information
1 parent
890a0c4
commit cbb4e18
Showing
25 changed files
with
2,752 additions
and
2,056 deletions.
There are no files selected for viewing
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
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,20 +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, | ||
) {} | ||
|
||
async signIn(username: string, pass: string): Promise<any> { | ||
const usernameFromEnv = process.env.ADMIN_USERNAME; | ||
const passwordFromEnv = process.env.ADMIN_PASSWORD; | ||
|
||
if (username !== usernameFromEnv || passwordFromEnv !== pass) { | ||
console.log('Invalid creds'); | ||
throw new UnauthorizedException(); | ||
/** | ||
* 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 login(username: string, password: string): Promise<{ accessToken: string }> { | ||
if (!this.isValidAdminCredentials(username, password)) { | ||
this.logger.error('Invalid credentials provided for admin login.'); | ||
throw new UnauthorizedException('Invalid credentials'); | ||
} | ||
console.log('generating token.....'); | ||
this.logger.info('Generating admin access token...'); | ||
const accessToken = await this.jwtService.getSignedToken(); | ||
return { code: 1, accessToken: accessToken }; | ||
return { 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); | ||
} | ||
} |
Oops, something went wrong.