Skip to content

Commit

Permalink
feat: refactored participant request (team, member) service
Browse files Browse the repository at this point in the history
  • Loading branch information
navneethkrish committed Oct 26, 2024
1 parent 890a0c4 commit cbb4e18
Show file tree
Hide file tree
Showing 25 changed files with 2,752 additions and 2,056 deletions.
16 changes: 16 additions & 0 deletions .forestadmin-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4057,6 +4057,22 @@
"type": "String",
"validations": []
},
{
"defaultValue": null,
"enums": null,
"field": "priority",
"integration": null,
"inverseOf": null,
"isFilterable": true,
"isPrimaryKey": false,
"isReadOnly": false,
"isRequired": false,
"isSortable": true,
"isVirtual": false,
"reference": null,
"type": "Number",
"validations": []
},
{
"defaultValue": null,
"enums": null,
Expand Down
173 changes: 0 additions & 173 deletions apps/web-api/src/admin/admin.controller.ts

This file was deleted.

26 changes: 7 additions & 19 deletions apps/web-api/src/admin/admin.module.ts
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 {}
41 changes: 31 additions & 10 deletions apps/web-api/src/admin/admin.service.ts
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;
}
}
22 changes: 22 additions & 0 deletions apps/web-api/src/admin/auth.controller.ts
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);
}
}
Loading

0 comments on commit cbb4e18

Please sign in to comment.