diff --git a/api/package.json b/api/package.json index 91511e0..116c59c 100644 --- a/api/package.json +++ b/api/package.json @@ -30,7 +30,7 @@ "@nestjs/core": "^10.0.0", "@nestjs/passport": "^10.0.3", "@nestjs/platform-express": "^10.0.0", - "@noahspan/noahspan-modules": "^0.3.9", + "@noahspan/noahspan-modules": "^0.4.0", "@schematics/angular": "^17.3.7", "dotenv": "^16.4.5", "reflect-metadata": "0.1.13", diff --git a/api/src/app.controller.ts b/api/src/app.controller.ts index aee90ca..0f30106 100644 --- a/api/src/app.controller.ts +++ b/api/src/app.controller.ts @@ -74,7 +74,7 @@ export class AppController { } @Get('userProfile') - async getUserProfile(@Headers() headers: any) { + async getUserProfile(@Headers() headers: any): Promise { 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 { + ): Promise { 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; diff --git a/api/src/app.service.ts b/api/src/app.service.ts index 75b8c72..b0ae395 100644 --- a/api/src/app.service.ts +++ b/api/src/app.service.ts @@ -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 { + ): Promise { 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; } diff --git a/api/src/pilot/info/pilot-info.service.ts b/api/src/pilot/info/pilot-info.service.ts index e560678..5c385fa 100644 --- a/api/src/pilot/info/pilot-info.service.ts +++ b/api/src/pilot/info/pilot-info.service.ts @@ -11,9 +11,43 @@ export class PilotInfoService { constructor(private readonly tableService: TableService) {} - // async find(rowKey: string): Promise { - // return await this.pilotInfoRepository.find(this.partitionKey, rowKey); - // } + async find(pilotId: string): Promise { + 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 { try { diff --git a/api/src/pilot/interceptors/pilot.interceptor.ts b/api/src/pilot/interceptors/pilot.interceptor.ts new file mode 100644 index 0000000..8a8362b --- /dev/null +++ b/api/src/pilot/interceptors/pilot.interceptor.ts @@ -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 { + 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)); + } +} diff --git a/api/src/pilot/pilot.controller.ts b/api/src/pilot/pilot.controller.ts index e2b8b9f..d70ed0e 100644 --- a/api/src/pilot/pilot.controller.ts +++ b/api/src/pilot/pilot.controller.ts @@ -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 { + 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 { try { const pilots: PilotInfoEntity[] = await this.pilotInfoService.findAll(); diff --git a/app/package.json b/app/package.json index 8de5828..7dd2a1f 100644 --- a/app/package.json +++ b/app/package.json @@ -16,7 +16,7 @@ "@fortawesome/free-regular-svg-icons": "^6.5.2", "@fortawesome/free-solid-svg-icons": "^6.5.2", "@fortawesome/react-fontawesome": "^0.2.2", - "@noahspan/noahspan-components": "^0.6.8", + "@noahspan/noahspan-components": "^0.7.0", "axios": "^1.7.2", "framer-motion": "^11.1.7", "react": "^18.2.0", diff --git a/app/src/components/pilotForm/IPilotFormProps.ts b/app/src/components/pilotForm/IPilotFormProps.ts index 7fae56c..af4d3b2 100644 --- a/app/src/components/pilotForm/IPilotFormProps.ts +++ b/app/src/components/pilotForm/IPilotFormProps.ts @@ -1,5 +1,8 @@ +import { PilotFormMode } from './PilotForm'; + export interface IPilotFormProps { - pilotId?: string; isDrawerOpen: boolean; - onOpenCloseDrawer: () => void; + mode: PilotFormMode; + onOpenClose: (mode: PilotFormMode) => void; + pilotId?: string; } diff --git a/app/src/components/pilotForm/PilotForm.tsx b/app/src/components/pilotForm/PilotForm.tsx index 264bd8a..1941b26 100644 --- a/app/src/components/pilotForm/PilotForm.tsx +++ b/app/src/components/pilotForm/PilotForm.tsx @@ -1,49 +1,60 @@ import { useEffect, useState } from 'react'; -import { - useForm, - Controller, - FormProvider, - FieldValues -} from 'react-hook-form'; +import { useForm, Controller, FormProvider } from 'react-hook-form'; import { Button, - DatePicker, Drawer, DrawerBody, DrawerHeader, DrawerFooter, Input, - Option, PeoplePicker, SaveIcon, - Select, StateSelect, Typography, XmarkIcon } from '@noahspan/noahspan-components'; import { IPilotFormProps } from './IPilotFormProps'; -import PilotFormCertificates from '../pilotFormCertificates/PilotFormCertificates'; -import PilotFormEndorsements from '../pilotFormEndorsements/PilotFormEndorsements'; -import { Person } from '@microsoft/microsoft-graph-types'; -import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios'; +import axios, { AxiosInstance, AxiosResponse } from 'axios'; import { useHttpClient } from '../../hooks/httpClient/UseHttpClient'; import { useAccessToken } from '../../hooks/accessToken/UseAcessToken'; -import { IPilotFormCertificates } from '../pilotFormCertificates/IPilotFormCertificates'; -import { IPilotFormEndorsements } from '../pilotFormEndorsements/IPilotFormEndorsements'; -import { EventMessage, EventPayload, EventType } from '@azure/msal-browser'; +import { useIsAuthenticated } from '@azure/msal-react'; + +export enum PilotFormMode { + ADD = 'ADD', + EDIT = 'EDIT', + VIEW = 'VIEW', + CANCEL = 'CANCEL' +} const PilotForm: React.FC = ({ pilotId, isDrawerOpen, - onOpenCloseDrawer + mode, + onOpenClose }: IPilotFormProps) => { const httpClient: AxiosInstance = useHttpClient(); - const [peoplePickerResults, setPeoplePickerResults] = useState([]); + const [peoplePickerResults, setPeoplePickerResults] = useState([]); const [isPeoplePickerLoading, setIsPeoplePickerLoading] = useState(false); const [isLoading, setIsLoading] = useState(false); const { getAccessToken } = useAccessToken(); - const methods = useForm(); + const isAuthenticated = useIsAuthenticated(); + const defaultValues = { + partitionKey: '', + rowKey: '', + id: '', + name: '', + address: '', + city: '', + state: '', + postalCode: '', + email: '', + phone: '' + }; + const methods = useForm({ + defaultValues: defaultValues + }); + const [isDisabled, setIsDisabled] = useState(false); const handlePeoplePickerOnClick = ( event: React.MouseEvent @@ -51,7 +62,7 @@ const PilotForm: React.FC = ({ const divElement: HTMLDivElement = event.target as HTMLDivElement; methods.setValue('id', divElement.id); - methods.setValue('name', divElement.textContent); + methods.setValue('name', divElement.textContent!); setPeoplePickerResults([]); }; @@ -82,6 +93,11 @@ const PilotForm: React.FC = ({ } }; + const onCancel = () => { + methods.reset(defaultValues); + onOpenClose(PilotFormMode.CANCEL); + }; + const onSubmit = async (data: unknown) => { try { setIsLoading(true); @@ -111,8 +127,38 @@ const PilotForm: React.FC = ({ }; useEffect(() => { - console.log(methods.formState.errors); - }, [methods.formState.errors]); + if (mode === PilotFormMode.VIEW) { + setIsDisabled(true); + } + }, [mode]); + + useEffect(() => { + const getPilot = async () => { + try { + setIsLoading(true); + + const config = isAuthenticated + ? { headers: { Authorization: await getAccessToken() } } + : {}; + const response: AxiosResponse = await httpClient.get( + `api/pilots/${pilotId}`, + config + ); + const pilot = response.data; + console.log(pilot); + // methods.setValue('blah', pilot.value) + methods.reset(pilot); + console.log(methods.getValues()); + } catch (error) { + } finally { + setIsLoading(false); + } + }; + + if (pilotId) { + getPilot(); + } + }, [pilotId]); return ( = ({ data-testid="pilot-drawer" > - +
@@ -134,11 +180,11 @@ const PilotForm: React.FC = ({ name="name" control={methods.control} rules={{ required: 'A name must be selected' }} - render={({ field: { disabled, value } }) => ( + render={({ field: { value } }) => ( = ({ )} />
-
- Address * -
-
- ( - +
+ Address * +
+
+ ( + + )} + /> +
+ + )} + {isAuthenticated && ( + <> +
+ City * +
+
+ ( + + )} + /> +
+ + )} + {isAuthenticated && ( + <> +
+ State * +
+
+ ( + + )} + /> +
+ + )} + {isAuthenticated && ( + <> +
+ Postal Code * +
+
+ ( + + )} + /> +
+ + )} + {isAuthenticated && ( + <> +
+ Email +
+
+ ( + + )} /> - )} - /> -
-
- City * -
-
- ( - + + )} + {isAuthenticated && ( + <> +
+ Phone Number +
+
+ ( + + )} /> - )} - /> -
-
- State * -
-
- ( - - )} - /> -
-
- Postal Code * -
-
- ( - - )} - /> -
-
- Email -
-
- ( - - )} - /> -
-
- Phone Number -
-
- ( - - )} - /> -
- {pilotId && ( +
+ + )} + {/* {pilotId && ( <>
Last Review @@ -354,8 +431,8 @@ const PilotForm: React.FC = ({ />
- )} - {pilotId && ( + )} */} + {/* {pilotId && ( <>
Medical @@ -434,35 +511,41 @@ const PilotForm: React.FC = ({
- )} + )} */}
-
-
- -
-
- -
-
+ <> + {mode !== PilotFormMode.VIEW && ( +
+
+ +
+
+ +
+
+ )} +
diff --git a/app/src/components/pilots/Pilots.tsx b/app/src/components/pilots/Pilots.tsx index 115cb03..cafad67 100644 --- a/app/src/components/pilots/Pilots.tsx +++ b/app/src/components/pilots/Pilots.tsx @@ -19,14 +19,34 @@ import { import { useHttpClient } from '../../hooks/httpClient/UseHttpClient'; import { AxiosInstance, AxiosResponse } from 'axios'; import { useAccessToken } from '../../hooks/accessToken/UseAcessToken'; +import { useIsAuthenticated } from '@azure/msal-react'; +import { PilotFormMode } from '../pilotForm/PilotForm'; const Pilots: React.FC = () => { const httpClient: AxiosInstance = useHttpClient(); + const isAuthenticated = useIsAuthenticated(); const { getAccessToken } = useAccessToken(); const [isDrawerOpen, setIsDrawerOpen] = useState(false); + const [pilotFormMode, setPilotFormMode] = useState( + PilotFormMode.CANCEL + ); + const [selectedPilotId, setSelectedPilotId] = useState(); const [pilots, setPilots] = useState([]); - const onOpenCloseDrawer = () => { - setIsDrawerOpen(!isDrawerOpen); + const onOpenClosePilotForm = (mode: PilotFormMode, pilotId?: string) => { + switch (mode) { + case PilotFormMode.ADD: + case PilotFormMode.EDIT: + case PilotFormMode.VIEW: + setPilotFormMode(mode); + setSelectedPilotId(pilotId); + setIsDrawerOpen(true); + break; + case PilotFormMode.CANCEL: + setPilotFormMode(mode); + setSelectedPilotId(undefined); + setIsDrawerOpen(false); + break; + } }; type Pilot = { @@ -40,54 +60,66 @@ const Pilots: React.FC = () => { { accessorKey: 'name', header: 'Name' + }, + { + id: 'actions', + header: 'Actions', + cellProps: { + className: 'text-end' + }, + cell: (info: any) => { + const pilotId = info.row.original.rowKey; + return ( + + +
+ + + +
+
+ + + onOpenClosePilotForm(PilotFormMode.EDIT, pilotId) + } + > + + Edit + + + onOpenClosePilotForm(PilotFormMode.VIEW, pilotId) + } + > + + View + +
+ + + Delete + +
+
+ ); + }, + enableSorting: false } - // { - // id: 'actions', - // header: 'Actions', - // cellProps: { - // className: 'text-center' - // }, - // cell: () => { - // return ( - // - // - //
- // - // - // - //
- //
- // - // - // - // Edit - // - // - // - // View - // - //
- // - // - // Delete - // - //
- //
- // ); - // }, - // enableSorting: false - // } ]; useEffect(() => { const getPilots = async () => { try { - const accessToken: string = await getAccessToken(); - const response: AxiosResponse = await httpClient.get(`api/pilots`, { - headers: { - Authorization: accessToken - } - }); + const config = isAuthenticated + ? { headers: { Authorization: await getAccessToken() } } + : {}; + const response: AxiosResponse = await httpClient.get( + `api/pilots`, + config + ); console.log(response.data); setPilots(response.data); } catch (error) { @@ -108,7 +140,7 @@ const Pilots: React.FC = () => { - */} - +
+ {userPhoto && ( + + )} + {!userPhoto && ( +
+ NS +
+ )} +
diff --git a/infrastructure/dns.tf b/infrastructure/dns.tf new file mode 100644 index 0000000..7a3be84 --- /dev/null +++ b/infrastructure/dns.tf @@ -0,0 +1,4 @@ +data "azurerm_dns_zone" "dns_zone" { + name = var.DOMAIN_NAME + resource_group_name = data.azurerm_resource_group.resource_group.name +} \ No newline at end of file diff --git a/infrastructure/main.tf b/infrastructure/main.tf index 1e4b366..a5537ee 100644 --- a/infrastructure/main.tf +++ b/infrastructure/main.tf @@ -1,9 +1,31 @@ -module "static_web_app" { - source = "github.com/noahspannbauer/noahspan-root/infrastructure/modules/static_web_app" - region = var.REGION - resource_group_name = data.azurerm_resource_group.resource_group.name - static_web_app_name = var.STATIC_WEB_APP_NAME - custom_domain_name_count = var.CUSTOM_DOMAIN_NAME_COUNT - domain_name = var.DOMAIN_NAME - subdomain_name = var.SUBDOMAIN_NAME +# module "static_web_app" { +# source = "github.com/noahspannbauer/noahspan-root/infrastructure/modules/static_web_app" +# region = var.REGION +# resource_group_name = data.azurerm_resource_group.resource_group.name +# static_web_app_name = var.STATIC_WEB_APP_NAME +# custom_domain_name_count = var.CUSTOM_DOMAIN_NAME_COUNT +# domain_name = var.DOMAIN_NAME +# subdomain_name = var.SUBDOMAIN_NAME +# } + +resource "azurerm_static_web_app" "static_web_app" { + name = var.STATIC_WEB_APP_NAME + resource_group_name = data.azurerm_resource_group.resource_group.name + location = var.REGION +} + +resource "azurerm_dns_cname_record" "dns_cname_record" { + name = var.SUBDOMAIN_NAME + zone_name = data.azurerm_dns_zone.dns_zone.name + resource_group_name = data.azurerm_resource_group.resource_group.name + ttl = 14400 + record = azurerm_static_web_app.static_web_app.default_host_name +} + +resource "azurerm_static_web_app_custom_domain" "static_web_app_custom_domain" { + static_web_app_id = azurerm_static_web_app.static_web_app.id + domain_name = "${var.SUBDOMAIN_NAME}.${var.DOMAIN_NAME}" + validation_type = "cname-delegation" + + depends_on = [ azurerm_dns_cname_record.dns_cname_record ] } \ No newline at end of file diff --git a/infrastructure/outputs.tf b/infrastructure/outputs.tf index c433ec7..5ec0493 100644 --- a/infrastructure/outputs.tf +++ b/infrastructure/outputs.tf @@ -1,8 +1,8 @@ output "api_key" { - value = module.static_web_app.api_key + value = azurerm_static_web_app.static_web_app.api_key sensitive = true } output "default_host_name" { - value = module.static_web_app.default_host_name + value = azurerm_static_web_app.static_web_app.default_host_name } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5ff1e2b..6a6ee38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,7 @@ "@nestjs/core": "^10.0.0", "@nestjs/passport": "^10.0.3", "@nestjs/platform-express": "^10.0.0", - "@noahspan/noahspan-modules": "^0.3.9", + "@noahspan/noahspan-modules": "^0.4.0", "@schematics/angular": "^17.3.7", "dotenv": "^16.4.5", "reflect-metadata": "0.1.13", @@ -76,7 +76,7 @@ "@fortawesome/free-regular-svg-icons": "^6.5.2", "@fortawesome/free-solid-svg-icons": "^6.5.2", "@fortawesome/react-fontawesome": "^0.2.2", - "@noahspan/noahspan-components": "^0.6.8", + "@noahspan/noahspan-components": "^0.7.0", "axios": "^1.7.2", "framer-motion": "^11.1.7", "react": "^18.2.0", @@ -3397,9 +3397,9 @@ "link": true }, "node_modules/@noahspan/noahspan-components": { - "version": "0.6.8", - "resolved": "https://registry.npmjs.org/@noahspan/noahspan-components/-/noahspan-components-0.6.8.tgz", - "integrity": "sha512-X4ftKtH9/ICyOCRKUnPMAh/u27Bi391edSKvIwWOxxawyIEZj58whjB0RfgcHV0O/Ap84lGt7YYctZTTzJHBEQ==", + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/@noahspan/noahspan-components/-/noahspan-components-0.7.0.tgz", + "integrity": "sha512-UJ0UBEwjt3IJTUhdNh/sUMFuuruJl6zn8pzy2JOZoMBfRmrX/6Dzz+u+3F0Q7RAitgV/XhJskSYrJBripenGzw==", "dependencies": { "@fortawesome/fontawesome-svg-core": "^6.5.2", "@fortawesome/free-brands-svg-icons": "^6.5.2", @@ -3431,9 +3431,9 @@ } }, "node_modules/@noahspan/noahspan-modules": { - "version": "0.3.9", - "resolved": "https://registry.npmjs.org/@noahspan/noahspan-modules/-/noahspan-modules-0.3.9.tgz", - "integrity": "sha512-gRW+DiXQP+/0vHvEBi5yijQwwUnP+z5YxlSU7Q/bzm7YdUv5ecbZrz4is+BzH1S6Ctds9DeoQKtvb9xbAKwg8w==", + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@noahspan/noahspan-modules/-/noahspan-modules-0.4.0.tgz", + "integrity": "sha512-oFOFL6AiEVlNmURAxDdnKg6xNZVZhQOPNeDtwWwbgYf2CQAHOapiBpKwHDAWT7DVpoBEurvmDEXeCHGl8mhGUw==", "dependencies": { "@azure/app-configuration": "^1.6.0", "@azure/data-tables": "^13.2.2",