Feature/6 pilots delete (#42)

* adding pilot delete

* adding pilot delete

* adding pilot delete
This commit was merged in pull request #42.
This commit is contained in:
2025-02-19 00:16:05 +00:00
committed by GitHub
parent b9629ee4e9
commit c19b8aa855
24 changed files with 220 additions and 450 deletions

View File

@@ -1,82 +0,0 @@
import {
Controller,
Get,
Headers,
Query,
StreamableFile,
UseGuards
} from '@nestjs/common';
import { Client as MsGraphClient } from '@microsoft/microsoft-graph-client';
import { MsGraphService } from './msGraph/ms-graph.service'
import { Person } from '@microsoft/microsoft-graph-types';
import { AppService } from './app.service';
import { AuthGuard } from '@nestjs/passport';
@Controller()
@UseGuards(AuthGuard('azure-ad'))
export class AppController {
constructor(
private readonly appService: AppService,
private readonly msGraphService: MsGraphService
) {}
@Get('userPhoto')
async getProfilePhoto(@Headers() headers: any): Promise<StreamableFile> {
try {
const graphToken: string = await this.msGraphService.getMsGraphAuth(
headers.authorization.replace('Bearer ', ''),
['user.read']
);
const client: MsGraphClient =
await this.msGraphService.getMsGraphClientDelegated(graphToken);
const blob: Blob = await client.api(`me/photos('48x48')/$value`).get();
const arrayBuffer: ArrayBuffer = await blob.arrayBuffer();
const buffer: Buffer = Buffer.from(arrayBuffer);
return new StreamableFile(buffer, {
type: 'application/json',
disposition: `attachment; filename="user_photo.png"`
});
} catch (error) {
return error;
}
}
@Get('userProfile')
async getUserProfile(@Headers() headers: any): Promise<any> {
try {
const graphToken: string = await this.msGraphService.getMsGraphAuth(
headers.authorization.replace('Bearer ', ''),
['user.read']
);
const client: MsGraphClient =
await this.msGraphService.getMsGraphClientDelegated(graphToken);
const userProfile = await client.api(`me`).get();
return userProfile;
} catch (error) {
return error;
}
}
@Get('personSearch')
async searchUsers(
@Headers() headers: any,
@Query('search') search: any
): Promise<any> {
try {
const accessToken: string = headers.authorization.replace('Bearer ', '');
const personSearchResults: any[] =
await this.appService.getPersonSearchResults(accessToken, search);
return personSearchResults;
} catch (error) {
return error;
}
}
@Get('hello')
async getHello(): Promise<string> {
return this.appService.getHello();
}
}

View File

@@ -1,19 +1,17 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { MsGraphModule } from './msGraph/ms-graph.module';
import { FeatureFlagModule } from './featureFlag/feature-flag.module'
import { LogModule } from './log/log.module';
import { PilotModule } from './pilot/pilot.module';
import { APP_FILTER } from '@nestjs/core';
import { APP_FILTER, APP_GUARD, Reflector } from '@nestjs/core';
import { HttpExceptionFilter } from './filters/http-exception.filter';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AuthGuard, AuthModule, UserModule } from '@noahspan/noahspan-modules';
import configuration from './config/configuration';
@Module({
imports: [
AuthModule.registerAsync({
inject: [ConfigService],
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
return {
@@ -22,33 +20,35 @@ import configuration from './config/configuration';
tenantId: configService.get<string>('tenantId')
};
},
inject: [ConfigService]
}),
ConfigModule.forRoot({
load: [configuration]
}),
FeatureFlagModule,
LogModule,
MsGraphModule.registerAsync({
PilotModule,
UserModule.registerAsync({
inject: [ConfigService],
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
return {
clientId: configService.get<string>('clientId'),
clientSecret: configService.get<string>('clientSecret'),
tenantId: configService.get<string>('tenantId')
};
},
inject: [ConfigService]
}),
PilotModule
}
}
})
],
controllers: [AppController],
providers: [
{
provide: APP_FILTER,
useClass: HttpExceptionFilter
},
AppService
{
provide: APP_GUARD,
useClass: AuthGuard
},
Reflector
]
})
export class AppModule {}

View File

@@ -1,49 +0,0 @@
import { Injectable } from '@nestjs/common';
// import { MsGraphService, MsGraphClient } from '@noahspan/noahspan-modules';
import { Client as MsGraphClient } from '@microsoft/microsoft-graph-client';
import { MsGraphService } from './msGraph/ms-graph.service';
@Injectable()
export class AppService {
constructor(private readonly msGraphService: MsGraphService) {}
getHello(): string {
return JSON.stringify(process.env);
}
async getPersonSearchResults(
accessToken: string,
search: string
): Promise<any[]> {
try {
const graphToken: string = await this.msGraphService.getMsGraphAuth(
accessToken,
['user.read']
);
const client: MsGraphClient =
await this.msGraphService.getMsGraphClientDelegated(graphToken);
const results: any = await client
.api('users')
.header('ConsistencyLevel', 'eventual')
.search(`"displayName:${search}"`)
.orderby('displayName')
.select(['displayName', 'userPrincipalName'])
.get();
let personResults: any[];
if (results.value) {
personResults = results.value.filter((result: any) => {
if (result.userPrincipalName !== null) {
return result;
}
});
} else {
personResults = [];
}
return personResults;
} catch (error) {
throw new Error(error);
}
}
}

View File

@@ -13,11 +13,10 @@ import { LogDto } from './log.dto';
import { Log } from './log.entity';
import { LogService } from './log.service';
import { CustomError } from '../error/customError';
import { AuthGuard } from '@nestjs/passport';
import { Public } from '@noahspan/noahspan-modules';
@Controller('logs')
@UseGuards(AuthGuard('azure-ad'))
export class LogController {
constructor(private readonly logService: LogService) {}
@@ -35,7 +34,7 @@ export class LogController {
}
}
// @Public()
@Public()
@Get()
async findAll(): Promise<Log[]> {
try {

View File

@@ -1,5 +0,0 @@
export interface MsGraphModuleOptions {
tenantId: string;
clientId: string;
clientSecret: string;
}

View File

@@ -1,4 +0,0 @@
import { ConfigurableModuleBuilder } from '@nestjs/common';
import { MsGraphModuleOptions } from './ms-graph.interface';
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder<MsGraphModuleOptions>().build()

View File

@@ -1,9 +0,0 @@
import { Module } from '@nestjs/common';
import { MsGraphService } from './ms-graph.service';
import { ConfigurableModuleClass } from './ms-graph.module-definition';
@Module({
providers: [MsGraphService],
exports: [MsGraphService]
})
export class MsGraphModule extends ConfigurableModuleClass {}

View File

@@ -1,41 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { MsGraphModuleOptions } from './ms-graph.interface';
import { Client } from '@microsoft/microsoft-graph-client';
import { AuthenticationResult, ConfidentialClientApplication, OnBehalfOfRequest } from '@azure/msal-node';
import { MODULE_OPTIONS_TOKEN } from './ms-graph.module-definition';
@Injectable()
export class MsGraphService {
constructor(@Inject(MODULE_OPTIONS_TOKEN) private msGraphModuleOptions: MsGraphModuleOptions) {}
async getMsGraphAuth(accessToken: string, scopes: string[]): Promise<string> {
try {
const oboRequest: OnBehalfOfRequest = {
oboAssertion: accessToken,
scopes: scopes
}
const cca = new ConfidentialClientApplication({
auth: {
clientId: this.msGraphModuleOptions.clientId,
clientSecret: this.msGraphModuleOptions.clientSecret,
authority: `https://login.microsoftonline.com/${this.msGraphModuleOptions.tenantId}`
}
});
const authenticationResult: AuthenticationResult = await cca.acquireTokenOnBehalfOf(oboRequest);
return authenticationResult.accessToken
} catch (error) {
return error
}
}
async getMsGraphClientDelegated(accessToken): Promise<Client> {
const client = await Client.init({
authProvider: (done) => {
done(null, accessToken);
}
});
return client;
}
}

View File

@@ -1,33 +1,33 @@
import { Injectable } from '@nestjs/common';
import { Repository, InjectRepository } from '@noahspan/azure-database';
import { Medical } from './medical.entity';
// import { Injectable } from '@nestjs/common';
// import { Repository, InjectRepository } from '@noahspan/azure-database';
// import { Medical } from './medical.entity';
@Injectable()
export class MedicalService {
private readonly partitionKey: string = 'medical';
// @Injectable()
// export class MedicalService {
// private readonly partitionKey: string = 'medical';
constructor(
@InjectRepository(Medical)
private readonly profileRepository: Repository<Medical>
) {}
// constructor(
// @InjectRepository(Medical)
// private readonly profileRepository: Repository<Medical>
// ) {}
async find(rowKey: string): Promise<Medical> {
return this.profileRepository.find(this.partitionKey, rowKey);
}
// async find(rowKey: string): Promise<Medical> {
// return this.profileRepository.find(this.partitionKey, rowKey);
// }
async findAll(): Promise<Medical[]> {
return this.profileRepository.findAll();
}
// async findAll(): Promise<Medical[]> {
// return this.profileRepository.findAll();
// }
async create(profile: Medical): Promise<Medical> {
return this.profileRepository.create(profile);
}
// async create(profile: Medical): Promise<Medical> {
// return this.profileRepository.create(profile);
// }
async update(rowKey: string, profile: Medical): Promise<Medical> {
return this.profileRepository.update(this.partitionKey, rowKey, profile);
}
// async update(rowKey: string, profile: Medical): Promise<Medical> {
// return this.profileRepository.update(this.partitionKey, rowKey, profile);
// }
async delete(rowKey: string) {
return this.profileRepository.delete(this.partitionKey, rowKey);
}
}
// async delete(rowKey: string) {
// return this.profileRepository.delete(this.partitionKey, rowKey);
// }
// }

View File

@@ -16,7 +16,6 @@ import { CustomError } from '../error/customError';
import { AuthGuard } from '@nestjs/passport'
@Controller('pilots')
@UseGuards(AuthGuard('azure-ad'))
export class PilotController {
constructor(private readonly pilotService: PilotService) {}