Feature/21 pilots view (#23)
* adding pilot details view * adding pilot details view * pilot view * pilot view
This commit was merged in pull request #23.
This commit is contained in:
@@ -74,7 +74,7 @@ export class AppController {
|
||||
}
|
||||
|
||||
@Get('userProfile')
|
||||
async getUserProfile(@Headers() headers: any) {
|
||||
async getUserProfile(@Headers() headers: any): Promise<any> {
|
||||
try {
|
||||
const graphToken: string = await this.msGraphService.getMsGraphAuth(
|
||||
headers.authorization.replace('Bearer ', ''),
|
||||
@@ -94,12 +94,12 @@ export class AppController {
|
||||
async searchUsers(
|
||||
@Headers() headers: any,
|
||||
@Query('search') search: any
|
||||
): Promise<Person[]> {
|
||||
): Promise<any> {
|
||||
try {
|
||||
const accessToken: string = headers.authorization.replace('Bearer ', '');
|
||||
const personSearchResults: Person[] =
|
||||
const personSearchResults: any[] =
|
||||
await this.appService.getPersonSearchResults(accessToken, search);
|
||||
console.log(personSearchResults);
|
||||
|
||||
return personSearchResults;
|
||||
} catch (error) {
|
||||
return error;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { MsGraphService, MsGraphClient } from '@noahspan/noahspan-modules';
|
||||
import { Person } from '@microsoft/microsoft-graph-types';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
@@ -13,7 +12,7 @@ export class AppService {
|
||||
async getPersonSearchResults(
|
||||
accessToken: string,
|
||||
search: string
|
||||
): Promise<Person[]> {
|
||||
): Promise<any[]> {
|
||||
try {
|
||||
const graphToken: string = await this.msGraphService.getMsGraphAuth(
|
||||
accessToken,
|
||||
@@ -22,12 +21,16 @@ export class AppService {
|
||||
const client: MsGraphClient =
|
||||
await this.msGraphService.getMsGraphClientDelegated(graphToken);
|
||||
const results: any = await client
|
||||
.api(`me/people/?$search=${search}`)
|
||||
.api('users')
|
||||
.header('ConsistencyLevel', 'eventual')
|
||||
.search(`"displayName:${search}"`)
|
||||
.orderby('displayName')
|
||||
.select(['displayName', 'userPrincipalName'])
|
||||
.get();
|
||||
let personResults: Person[];
|
||||
let personResults: any[];
|
||||
|
||||
if (results.value) {
|
||||
personResults = results.value.filter((result: Person) => {
|
||||
personResults = results.value.filter((result: any) => {
|
||||
if (result.userPrincipalName !== null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -11,9 +11,43 @@ export class PilotInfoService {
|
||||
|
||||
constructor(private readonly tableService: TableService) {}
|
||||
|
||||
// async find(rowKey: string): Promise<PilotInfo> {
|
||||
// return await this.pilotInfoRepository.find(this.partitionKey, rowKey);
|
||||
// }
|
||||
async find(pilotId: string): Promise<PilotInfoEntity> {
|
||||
try {
|
||||
const client: TableClient =
|
||||
await this.tableService.getTableClient('Pilots');
|
||||
const entities = await client.listEntities({
|
||||
queryOptions: {
|
||||
filter: odata`PartitionKey eq 'pilot' and RowKey eq '${pilotId}'`
|
||||
}
|
||||
});
|
||||
let pilot: PilotInfoEntity;
|
||||
console.log(entities);
|
||||
for await (const entity of entities) {
|
||||
pilot = {
|
||||
partitionKey: entity.partitionKey,
|
||||
rowKey: entity.rowKey,
|
||||
id: entity.id.toString(),
|
||||
name: entity.name.toString(),
|
||||
address: entity.address.toString(),
|
||||
city: entity.city.toString(),
|
||||
state: entity.state.toString(),
|
||||
postalCode: entity.postalCode.toString(),
|
||||
email: entity.email.toString(),
|
||||
phone: entity.phone.toString()
|
||||
};
|
||||
}
|
||||
|
||||
return pilot;
|
||||
} catch (error) {
|
||||
const restError: RestError = error as RestError;
|
||||
|
||||
throw new CustomError(
|
||||
restError.details['odataError']['message']['value'],
|
||||
restError.details['odataError']['code'],
|
||||
restError.statusCode
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(): Promise<PilotInfoEntity[]> {
|
||||
try {
|
||||
|
||||
38
api/src/pilot/interceptors/pilot.interceptor.ts
Normal file
38
api/src/pilot/interceptors/pilot.interceptor.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
||||
import { Observable, map } from 'rxjs';
|
||||
|
||||
export class PilotInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, handler: CallHandler): Observable<any> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const authHeader = req.headers.authorization;
|
||||
const token = authHeader && authHeader.split(' ')[1];
|
||||
|
||||
if (!token) {
|
||||
return handler.handle().pipe(
|
||||
map((data) => {
|
||||
if (data.length) {
|
||||
const pilots = data.map((pilot) => {
|
||||
return {
|
||||
partitionKey: pilot.partitionKey,
|
||||
rowKey: pilot.rowKey,
|
||||
id: pilot.id,
|
||||
name: pilot.name
|
||||
};
|
||||
});
|
||||
|
||||
return pilots;
|
||||
} else {
|
||||
return {
|
||||
partitionKey: data.partitionKey,
|
||||
rowKey: data.rowKey,
|
||||
id: data.id,
|
||||
name: data.name
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return handler.handle().pipe(map((data) => data));
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,46 @@
|
||||
import { Body, Controller, Get, HttpException, Post } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpException,
|
||||
Param,
|
||||
Post,
|
||||
UseInterceptors
|
||||
} from '@nestjs/common';
|
||||
import { PilotInfoService } from './info/pilot-info.service';
|
||||
import { PilotInfoDto } from './info/pilot-info.dto';
|
||||
import { TableInsertEntityHeaders } from '@azure/data-tables';
|
||||
import { CustomError } from '../customError/CustomError';
|
||||
import { PilotInfoEntity } from './info/pilot-info.entity';
|
||||
import { PilotInterceptor } from 'src/pilot/interceptors/pilot.interceptor';
|
||||
import { Public } from '@noahspan/noahspan-modules';
|
||||
|
||||
@Controller('pilots')
|
||||
export class PilotController {
|
||||
constructor(private readonly pilotInfoService: PilotInfoService) {}
|
||||
|
||||
@Get(':pilotId')
|
||||
@Public()
|
||||
@UseInterceptors(PilotInterceptor)
|
||||
async find(@Param() params: any): Promise<PilotInfoEntity> {
|
||||
try {
|
||||
const pilot: PilotInfoEntity = await this.pilotInfoService.find(
|
||||
params.pilotId
|
||||
);
|
||||
|
||||
return pilot;
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode, {
|
||||
cause: customError.name
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Public()
|
||||
@UseInterceptors(PilotInterceptor)
|
||||
async findAll(): Promise<PilotInfoEntity[]> {
|
||||
try {
|
||||
const pilots: PilotInfoEntity[] = await this.pilotInfoService.findAll();
|
||||
|
||||
Reference in New Issue
Block a user