adding pilot

This commit is contained in:
2024-09-19 21:39:23 -05:00
parent ae8e407bce
commit 4600c5e698
15 changed files with 405 additions and 248 deletions

View File

@@ -1,4 +1,11 @@
import { Controller, Get, Headers, Query } from '@nestjs/common';
import {
Controller,
Get,
Headers,
Query,
Res,
StreamableFile
} from '@nestjs/common';
import {
AppConfigService,
MsGraphService,
@@ -8,6 +15,10 @@ import { FeatureFlagValue } from '@azure/app-configuration';
import { Public } from '@noahspan/noahspan-modules';
import { Person } from '@microsoft/microsoft-graph-types';
import { AppService } from './app.service';
import { createReadStream } from 'fs';
import { join } from 'path';
import { arrayBuffer } from 'stream/consumers';
import type { Response } from 'express';
@Controller()
export class AppController {
@@ -23,7 +34,6 @@ export class AppController {
@Query() query: any
): Promise<{ key: string; enabled: boolean }[]> {
try {
console.log(query);
const featureFlagKeys: string[] =
query.keys && query.keys.toString().includes(';')
? query.keys.split(';')
@@ -41,9 +51,27 @@ export class AppController {
}
}
@Public()
@Get('profilePhoto')
async getProfilePhoto(@Query() query: any): Promise<any> {}
@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) {

View File

@@ -1,7 +1,6 @@
export class PilotInfoDto {
id: string;
firstName: string;
lastName: string;
name: string;
address: string;
city: string;
state: string;

View File

@@ -2,12 +2,11 @@ export class PilotInfoEntity {
partitionKey: string;
rowKey: string;
id: string;
firstName: string;
lastName: string;
address: string;
city: string;
state: string;
postalCode: string;
name: string;
address?: string;
city?: string;
state?: string;
postalCode?: string;
email?: string;
phone?: string;
}

View File

@@ -1,28 +1,51 @@
import { HttpException, Injectable } from '@nestjs/common';
// import { Repository, InjectRepository } from '@nestjs/azure-database';
import { Injectable } from '@nestjs/common';
import { PilotInfoDto } from './pilot-info.dto';
import { PilotInfoEntity } from './pilot-info.entity';
import { TableClient, TableService } from '@noahspan/noahspan-modules';
import { RestError, TableInsertEntityHeaders } from '@azure/data-tables';
import { odata, RestError, TableInsertEntityHeaders } from '@azure/data-tables';
import { CustomError } from '../../customError/CustomError';
@Injectable()
export class PilotInfoService {
private readonly partitionKey: string = 'info';
constructor(
// @InjectRepository(PilotInfo)
// private readonly pilotInfoRepository: Repository<PilotInfo>
private readonly tableService: TableService
) {}
constructor(private readonly tableService: TableService) {}
// async find(rowKey: string): Promise<PilotInfo> {
// return await this.pilotInfoRepository.find(this.partitionKey, rowKey);
// }
// async findAll(): Promise<PilotInfo[]> {
// return await this.pilotInfoRepository.findAll();
// }
async findAll(): Promise<PilotInfoEntity[]> {
try {
const client: TableClient =
await this.tableService.getTableClient('Pilots');
const entities = await client.listEntities({
queryOptions: { filter: odata`PartitionKey eq 'pilot'` }
});
const pilots: PilotInfoEntity[] = [];
for await (const entity of entities) {
const pilot: PilotInfoEntity = {
partitionKey: entity.partitionKey,
rowKey: entity.rowKey,
id: entity.id.toString(),
name: entity.name.toString()
};
pilots.push(pilot);
}
return pilots;
} catch (error) {
const restError: RestError = error as RestError;
throw new CustomError(
restError.details['odataError']['message']['value'],
restError.details['odataError']['code'],
restError.statusCode
);
}
}
async create(pilotInfoData: PilotInfoDto): Promise<TableInsertEntityHeaders> {
const client: TableClient =

View File

@@ -1,33 +1,40 @@
import { Body, Controller, HttpException, Post } from '@nestjs/common';
import { Body, Controller, Get, HttpException, Post } 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';
@Controller('pilots')
export class PilotController {
constructor(private readonly pilotInfoService: PilotInfoService) {}
@Post()
async createPilot(@Body() pilotInfoData: PilotInfoDto): Promise<void> {
@Get()
async findAll(): Promise<PilotInfoEntity[]> {
try {
const response: TableInsertEntityHeaders =
await this.pilotInfoService.create(pilotInfoData);
const pilots: PilotInfoEntity[] = await this.pilotInfoService.findAll();
console.log(`Not Broken: ${response}`);
return pilots;
} catch (error) {
const customError = error as CustomError;
console.log(customError.name);
throw new HttpException(customError.message, customError.statusCode, {
cause: customError.name
});
}
}
// throw new HttpException({
// status: customError.statusCode,
// error: customError.message
// }, customError.statusCode, {
// cause: customError.name
// });
@Post()
async create(@Body() pilotInfoData: PilotInfoDto): Promise<void> {
try {
const response: TableInsertEntityHeaders =
await this.pilotInfoService.create(pilotInfoData);
} catch (error) {
const customError = error as CustomError;
throw new HttpException(customError.message, customError.statusCode, {
cause: customError.name
});
}
}
}