Compare commits
2 Commits
5c04870d37
...
feature/86
| Author | SHA1 | Date | |
|---|---|---|---|
| 6f2ca6ccfb | |||
| 9b633672ae |
@@ -1,44 +0,0 @@
|
|||||||
name: Deploy
|
|
||||||
on:
|
|
||||||
workflow_call:
|
|
||||||
inputs:
|
|
||||||
app_name:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
environment_name:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
version_number:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
id-token: write
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment: ${{ inputs.environment_name }}
|
|
||||||
steps:
|
|
||||||
- name: Get Secrets from Infisical
|
|
||||||
uses: Infisical/secrets-action@v1.0.16
|
|
||||||
with:
|
|
||||||
domain: ${{ secrets.INFISICAL_DOMAIN }}
|
|
||||||
client-id: ${{ secrets.INFISICAL_CLIENT_ID }}
|
|
||||||
client-secret: ${{ secrets.INFISICAL_CLIENT_SECRET }}
|
|
||||||
project-slug: ${{ secrets.INFISICAL_PROJECT_SLUG }}
|
|
||||||
env-slug: ${{ inputs.environment_name }}
|
|
||||||
secret-path: /flying
|
|
||||||
|
|
||||||
- name: Install Azure CLI
|
|
||||||
run: |
|
|
||||||
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
|
|
||||||
|
|
||||||
- name: Log in to Azure
|
|
||||||
run: |
|
|
||||||
az login --service-principal --username ${{ env.AZURE_CLIENT_ID }} --password ${{ env.AZURE_CLIENT_SECRET }} --tenant ${{ env.AZURE_TENANT_ID }}
|
|
||||||
|
|
||||||
- name: Update Container App
|
|
||||||
run: |
|
|
||||||
az containerapp update --name ${{ inputs.app_name }}-${{ inputs.environment_name }} --container-name ${{ inputs.app_name }} --resource-group ${{ env.RESOURCE_GROUP }} --image docker.io/noahspan/${{ inputs.app_name }}:${{ inputs.version_number }}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
name: 'Infrastructure'
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
inputs:
|
|
||||||
environment_name:
|
|
||||||
type: string
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
infrastructure:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment:
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
sparse-checkout: |
|
|
||||||
infrastructure
|
|
||||||
|
|
||||||
- name: Get Secrets from Infisical
|
|
||||||
uses: Infisical/secrets-action@v1.0.16
|
|
||||||
with:
|
|
||||||
domain: ${{ secrets.INFISICAL_DOMAIN }}
|
|
||||||
client-id: ${{ secrets.INFISICAL_CLIENT_ID }}
|
|
||||||
client-secret: ${{ secrets.INFISICAL_CLIENT_SECRET }}
|
|
||||||
project-slug: ${{ secrets.INFISICAL_PROJECT_SLUG }}
|
|
||||||
env-slug: ${{ inputs.environment_name }}
|
|
||||||
secret-path: /flying/terraform
|
|
||||||
|
|
||||||
- name: Setup Terraform
|
|
||||||
uses: hashicorp/setup-terraform@v4
|
|
||||||
|
|
||||||
- name: Terraform init
|
|
||||||
run: |
|
|
||||||
terraform init -input=false
|
|
||||||
|
|
||||||
- name: Terraform plan
|
|
||||||
run: |
|
|
||||||
terraform -chdir=/infrastructure plan -no-color -input=false
|
|
||||||
|
|
||||||
# - name: Terraform apply
|
|
||||||
# run: |
|
|
||||||
# terraform apply -no-color -
|
|
||||||
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
name: Main
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
changes:
|
|
||||||
uses: ./.gitea/workflows/changes.yaml
|
|
||||||
|
|
||||||
build-and-test:
|
|
||||||
if: ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.client == 'true' }}
|
|
||||||
name: build-and-test
|
|
||||||
needs:
|
|
||||||
- changes
|
|
||||||
uses: ./.gitea/workflows/build_and_test.yaml
|
|
||||||
with:
|
|
||||||
environment_name: test
|
|
||||||
version_number: ${{ github.run_id }}
|
|
||||||
secrets: inherit
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
name: deploy
|
|
||||||
needs:
|
|
||||||
- changes
|
|
||||||
- build
|
|
||||||
uses: ./.gitea/workflows/deploy.yaml
|
|
||||||
with:
|
|
||||||
app_name: flying
|
|
||||||
environment_name: test
|
|
||||||
version_number: ${{ github.run_id }}
|
|
||||||
secrets: inherit
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
name: Pull Request
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
changes:
|
|
||||||
uses: ./.gitea/workflows/changes.yaml
|
|
||||||
|
|
||||||
build:
|
|
||||||
if: ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.client == 'true'}}
|
|
||||||
name: build-and-test
|
|
||||||
needs:
|
|
||||||
- changes
|
|
||||||
uses: ./.gitea/workflows/build_and_test.yaml
|
|
||||||
with:
|
|
||||||
environment_name: test
|
|
||||||
version_number: ${{ github.run_id }}
|
|
||||||
secrets: inherit
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
name: Tag
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags:
|
|
||||||
- '**'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: build
|
|
||||||
uses: ./.gitea/workflows/build_and_test.yaml
|
|
||||||
with:
|
|
||||||
environment_name: prod
|
|
||||||
version_number: ${{ github.ref_name }}
|
|
||||||
secrets: inherit
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
name: deploy
|
|
||||||
needs:
|
|
||||||
- build
|
|
||||||
uses: ./.gitea/workflows/deploy.yaml
|
|
||||||
with:
|
|
||||||
app_name: flying
|
|
||||||
environment_name: prod
|
|
||||||
version_number: ${{ github.ref_name }}
|
|
||||||
secrets: inherit
|
|
||||||
@@ -10,7 +10,7 @@ on:
|
|||||||
type: string
|
type: string
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-test:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
environment: ${{ inputs.environment_name }}
|
environment: ${{ inputs.environment_name }}
|
||||||
steps:
|
steps:
|
||||||
@@ -19,17 +19,12 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
sparse-checkout: |
|
sparse-checkout: |
|
||||||
api
|
api
|
||||||
client
|
|
||||||
|
|
||||||
- name: Get Secrets from Infisical
|
- uses: pnpm/action-setup@v4
|
||||||
uses: Infisical/secrets-action@v1.0.16
|
name: Install pnpm
|
||||||
with:
|
with:
|
||||||
domain: ${{ secrets.INFISICAL_DOMAIN }}
|
version: 10
|
||||||
client-id: ${{ secrets.INFISICAL_CLIENT_ID }}
|
run_install: false
|
||||||
client-secret: ${{ secrets.INFISICAL_CLIENT_SECRET }}
|
|
||||||
project-slug: ${{ secrets.INFISICAL_PROJECT_SLUG }}
|
|
||||||
env-slug: ${{ inputs.environment_name }}
|
|
||||||
secret-path: /flying
|
|
||||||
|
|
||||||
- name: Install Nest CLI
|
- name: Install Nest CLI
|
||||||
run: |
|
run: |
|
||||||
@@ -38,36 +33,27 @@ jobs:
|
|||||||
- name: 'Setup Node'
|
- name: 'Setup Node'
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: ${{ env.NODE_VERSION }}
|
node-version: ${{ vars.NODE_VERSION }}
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
npm ci
|
pnpm install
|
||||||
|
|
||||||
- name: Build API
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
npm run build -w api
|
pnpm --filter api build
|
||||||
|
|
||||||
- name: Test API
|
- name: Deploy
|
||||||
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
run: |
|
run: |
|
||||||
npm run test -w api
|
pnpm --filter api --prod deploy ./.prod/api
|
||||||
|
|
||||||
- name: Build Client
|
|
||||||
env:
|
|
||||||
VITE_API_URL: ${{ env.VITE_API_URL }}
|
|
||||||
VITE_BASE_URL: ${{ env.VITE_BASE_URL }}
|
|
||||||
VITE_CLIENT_ID: ${{ env.VITE_CLIENT_ID }}
|
|
||||||
VITE_ISSUER_URI: ${{ env.VITE_ISSUER_URI }}
|
|
||||||
VITE_TENANT_ID: ${{ env.VITE_TENANT_ID }}
|
|
||||||
run: |
|
|
||||||
npm run build -w client
|
|
||||||
|
|
||||||
- name: Log into Docker Hub
|
- name: Log into Docker Hub
|
||||||
if: ${{ github.event_name != 'pull_request' }}
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
username: ${{ env.DOCKERHUB_USERNAME }}
|
username: ${{ vars.DOCKERHUB_USERNAME }}
|
||||||
password: ${{ env.DOCKERHUB_TOKEN }}
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Setup Docker Buildx
|
- name: Setup Docker Buildx
|
||||||
if: ${{ github.event_name != 'pull_request' }}
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
@@ -78,5 +64,6 @@ jobs:
|
|||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
push: true
|
push: true
|
||||||
tags: noahspan/flying:${{ inputs.version_number }}
|
tags: noahspan/flying-api:${{ inputs.version_number }}
|
||||||
context: .
|
context: .
|
||||||
|
target: api
|
||||||
70
.github/workflows/app_build.yaml
vendored
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
name: 'App Build'
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
environment_name:
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
version_number:
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
|
||||||
|
env:
|
||||||
|
VITE_API_URL: ${{ vars.VITE_API_URL }}
|
||||||
|
VITE_CLIENT_ID: ${{ vars.VITE_CLIENT_ID }}
|
||||||
|
VITE_TENANT_ID: ${{ vars.VITE_TENANT_ID }}
|
||||||
|
VITE_REDIRECT_URL: ${{ vars.VITE_REDIRECT_URL }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
environment: ${{ inputs.environment_name }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
sparse-checkout: |
|
||||||
|
app
|
||||||
|
|
||||||
|
- uses: pnpm/action-setup@v4
|
||||||
|
name: Install pnpm
|
||||||
|
with:
|
||||||
|
version: 10
|
||||||
|
run_install: false
|
||||||
|
|
||||||
|
- name: 'Setup Node'
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: ${{ vars.NODE_VERSION }}
|
||||||
|
|
||||||
|
- name: Install
|
||||||
|
run: |
|
||||||
|
pnpm install
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: |
|
||||||
|
pnpm --filter app build
|
||||||
|
|
||||||
|
- name: Deploy
|
||||||
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
|
run: |
|
||||||
|
pnpm --filter app --prod deploy ./.prod/app
|
||||||
|
|
||||||
|
- name: Setup Docker Buildx
|
||||||
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Log into Docker Hub
|
||||||
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
username: ${{ vars.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push to registry
|
||||||
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
push: true
|
||||||
|
tags: noahspan/flying-app:${{ inputs.version_number }}
|
||||||
|
context: .
|
||||||
|
target: app
|
||||||
@@ -4,11 +4,8 @@ on:
|
|||||||
outputs:
|
outputs:
|
||||||
api:
|
api:
|
||||||
value: ${{ jobs.changes.outputs.api }}
|
value: ${{ jobs.changes.outputs.api }}
|
||||||
client:
|
app:
|
||||||
value: ${{ jobs.changes.outputs.client }}
|
value: ${{ jobs.changes.outputs.app }}
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
changes:
|
changes:
|
||||||
@@ -16,21 +13,16 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
outputs:
|
outputs:
|
||||||
api: ${{ steps.filter.outputs.api }}
|
api: ${{ steps.filter.outputs.api }}
|
||||||
client: ${{ steps.filter.outputs.client }}
|
app: ${{ steps.filter.outputs.app }}
|
||||||
steps:
|
steps:
|
||||||
- name: 'Setup Node'
|
- uses: actions/checkout@v3
|
||||||
uses: actions/setup-node@v6
|
|
||||||
with:
|
|
||||||
node-version: '22.x'
|
|
||||||
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: dorny/paths-filter@v3
|
- uses: dorny/paths-filter@v3
|
||||||
id: filter
|
id: filter
|
||||||
with:
|
with:
|
||||||
|
initial-fetch-depth: 2
|
||||||
filters: |
|
filters: |
|
||||||
api:
|
api:
|
||||||
- 'api/**'
|
- 'api/**'
|
||||||
client:
|
app:
|
||||||
- 'client/**'
|
- 'app/**'
|
||||||
35
.github/workflows/deploy.yaml
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
name: Deploy
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
app_name:
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
environment_name:
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
version_number:
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
id-token: write
|
||||||
|
contents: read
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
environment: ${{ inputs.environment_name }}
|
||||||
|
steps:
|
||||||
|
- name: Log in to Azure
|
||||||
|
uses: azure/login@v2
|
||||||
|
with:
|
||||||
|
client-id: ${{ vars.VITE_CLIENT_ID }}
|
||||||
|
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
|
||||||
|
tenant-id: ${{ vars.VITE_TENANT_ID }}
|
||||||
|
|
||||||
|
- name: Azure CLI script
|
||||||
|
uses: azure/cli@v2
|
||||||
|
with:
|
||||||
|
azcliversion: latest
|
||||||
|
inlineScript: |
|
||||||
|
az containerapp update --name ${{ inputs.app_name }}-${{ inputs.environment_name }} --resource-group noahspan-flying --image docker.io/noahspan/${{ inputs.app_name }}:${{ inputs.version_number }}
|
||||||
55
.github/workflows/main.yaml
vendored
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
name: Main
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
changes:
|
||||||
|
uses: ./.github/workflows/changes.yaml
|
||||||
|
|
||||||
|
build-api:
|
||||||
|
if: ${{ needs.changes.outputs.api == 'true' }}
|
||||||
|
name: build-api
|
||||||
|
needs:
|
||||||
|
- changes
|
||||||
|
uses: ./.github/workflows/api_build.yaml
|
||||||
|
with:
|
||||||
|
environment_name: test
|
||||||
|
version_number: ${{ github.run_id }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
deploy-api:
|
||||||
|
name: deploy-api
|
||||||
|
needs:
|
||||||
|
- changes
|
||||||
|
- build-api
|
||||||
|
uses: ./.github/workflows/deploy.yaml
|
||||||
|
with:
|
||||||
|
app_name: flying-api
|
||||||
|
environment_name: test
|
||||||
|
version_number: ${{ github.run_id }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
build-app:
|
||||||
|
if: ${{ needs.changes.outputs.app == 'true' }}
|
||||||
|
name: build-app
|
||||||
|
needs:
|
||||||
|
- changes
|
||||||
|
uses: ./.github/workflows/app_build.yaml
|
||||||
|
with:
|
||||||
|
environment_name: test
|
||||||
|
version_number: ${{ github.run_id }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
deploy-app:
|
||||||
|
name: deploy-app
|
||||||
|
needs:
|
||||||
|
- changes
|
||||||
|
- build-app
|
||||||
|
uses: ./.github/workflows/deploy.yaml
|
||||||
|
with:
|
||||||
|
app_name: flying-app
|
||||||
|
environment_name: test
|
||||||
|
version_number: ${{ github.run_id }}
|
||||||
|
secrets: inherit
|
||||||
30
.github/workflows/pull_request.yaml
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
name: Pull Request
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
changes:
|
||||||
|
uses: ./.github/workflows/changes.yaml
|
||||||
|
|
||||||
|
build-api:
|
||||||
|
if: ${{ needs.changes.outputs.api == 'true' }}
|
||||||
|
name: build-api
|
||||||
|
needs:
|
||||||
|
- changes
|
||||||
|
uses: ./.github/workflows/api_build.yaml
|
||||||
|
with:
|
||||||
|
environment_name: pull_request
|
||||||
|
version_number: ${{ github.run_id }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
build-app:
|
||||||
|
if: ${{ needs.changes.outputs.app == 'true' }}
|
||||||
|
name: build-app
|
||||||
|
needs:
|
||||||
|
- changes
|
||||||
|
uses: ./.github/workflows/app_build.yaml
|
||||||
|
with:
|
||||||
|
environment_name: pull_request
|
||||||
|
version_number: ${{ github.run_id }}
|
||||||
|
secrets: inherit
|
||||||
44
.github/workflows/tag.yaml
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
name: Tag
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v[0-9]+.[0-9]+.[0-9]+'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-api:
|
||||||
|
name: build-api
|
||||||
|
uses: ./.github/workflows/api_build.yaml
|
||||||
|
with:
|
||||||
|
environment_name: prod
|
||||||
|
version_number: ${{ github.ref_name }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
deploy-api:
|
||||||
|
name: deploy-api
|
||||||
|
needs:
|
||||||
|
- build-api
|
||||||
|
uses: ./.github/workflows/deploy.yaml
|
||||||
|
with:
|
||||||
|
app_name: flying-api
|
||||||
|
environment_name: prod
|
||||||
|
version_number: ${{ github.ref_name }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
build-app:
|
||||||
|
name: build-app
|
||||||
|
uses: ./.github/workflows/app_build.yaml
|
||||||
|
with:
|
||||||
|
environment_name: prod
|
||||||
|
version_number: ${{ github.ref_name }}
|
||||||
|
secrets: inherit
|
||||||
|
|
||||||
|
deploy-app:
|
||||||
|
name: deploy-app
|
||||||
|
needs:
|
||||||
|
- build-app
|
||||||
|
uses: ./.github/workflows/deploy.yaml
|
||||||
|
with:
|
||||||
|
app_name: flying-app
|
||||||
|
environment_name: prod
|
||||||
|
version_number: ${{ github.ref_name }}
|
||||||
|
secrets: inherit
|
||||||
45
Dockerfile
@@ -1,16 +1,39 @@
|
|||||||
FROM node:22
|
# FROM --platform=linux/amd64 node:18-alpine AS base
|
||||||
|
# ENV PNPM_HOME="/pnpm"
|
||||||
|
# ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
# RUN corepack enable
|
||||||
|
|
||||||
WORKDIR app
|
# FROM base AS api
|
||||||
COPY ./api/dist ./api/dist
|
# COPY /api/dist /app/dist
|
||||||
COPY ./api/package.json package-lock.json ./api
|
# COPY /api/node_modules /app/node_modules
|
||||||
COPY ./client/dist ./client/dist
|
# WORKDIR /app
|
||||||
|
# EXPOSE 3000
|
||||||
|
# # CMD ["node", "dist/main.js"]
|
||||||
|
# ENTRYPOINT ["tail", "-f", "/dev/null"]
|
||||||
|
|
||||||
|
# FROM base AS app
|
||||||
|
# COPY /app/dist /app/dist
|
||||||
|
# WORKDIR /app
|
||||||
|
# RUN npm i -g serve
|
||||||
|
# EXPOSE 8080
|
||||||
|
# CMD [ "serve", "-s", "/dist", "-p", "8080" ]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FROM node:18-slim AS base
|
||||||
|
|
||||||
|
FROM base AS api
|
||||||
WORKDIR api
|
WORKDIR api
|
||||||
RUN npm ci
|
COPY ./.prod/api .
|
||||||
|
|
||||||
WORKDIR /
|
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
CMD ["node", "dist/main.js"]
|
||||||
|
# ENTRYPOINT ["tail", "-f", "/dev/null"]
|
||||||
|
|
||||||
CMD ["node", "./app/api/dist/main.js"]
|
FROM base AS app
|
||||||
# ENTRYPOINT ["tail", "-f", "/dev/null"]
|
WORKDIR /app
|
||||||
|
COPY ./.prod/app .
|
||||||
|
RUN npm i -g serve
|
||||||
|
EXPOSE 8080
|
||||||
|
CMD [ "serve", "-s", "dist", "-p", "8080" ]
|
||||||
24
README.md
@@ -1,23 +1,3 @@
|
|||||||
# Flying
|
# Flying (React and NestJS)
|
||||||
|
|
||||||
A web app for tracking pilot flight hours and sharing flight tracks.
|
A pilot logbook app
|
||||||
|
|
||||||
## Built With
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
|
|
||||||
### Installation
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
## Roadmap
|
|
||||||
|
|
||||||
- [ ] Expense tracking for plane rental, instructor fees, and fuel rates
|
|
||||||
- [ ] Export logbook to CSV
|
|
||||||
- [ ]
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
## Contact
|
|
||||||
|
|||||||
3
api/.gitignore
vendored
@@ -57,6 +57,3 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|||||||
|
|
||||||
|
|
||||||
local.settings.json
|
local.settings.json
|
||||||
|
|
||||||
|
|
||||||
/src/database/*.db
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
# Flying API
|
# Flying API
|
||||||
|
|
||||||
An API for the Flying app.
|
An API for the Flying app.
|
||||||
...
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "api",
|
"name": "api",
|
||||||
"version": "2.1.5",
|
"version": "1.3.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "",
|
"author": "",
|
||||||
"private": true,
|
"private": true,
|
||||||
@@ -16,40 +16,24 @@
|
|||||||
"test:watch": "jest --watch",
|
"test:watch": "jest --watch",
|
||||||
"test:cov": "jest --coverage",
|
"test:cov": "jest --coverage",
|
||||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||||
"test:e2e": "jest --config ./test/jest-e2e.json",
|
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||||
"typeorm": "npm run build && npx typeorm -d dist/database/data-source.js",
|
|
||||||
"migration:generate": "npm run typeorm -- migration:generate",
|
|
||||||
"migration:run": "npm run typeorm -- migration:run",
|
|
||||||
"migration:revert": "npm run typeorm -- migration:revert"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@azure/storage-blob": "^12.27.0",
|
"@azure/storage-blob": "^12.27.0",
|
||||||
"@microsoft/microsoft-graph-client": "^3.0.7",
|
"@microsoft/microsoft-graph-client": "^3.0.7",
|
||||||
"@nestjs/axios": "^4.0.1",
|
"@nestjs/axios": "^3.0.3",
|
||||||
"@nestjs/common": "^11.1.6",
|
"@nestjs/common": "^10.0.0",
|
||||||
"@nestjs/config": "^4.0.2",
|
"@nestjs/config": "^3.2.2",
|
||||||
"@nestjs/core": "^11.1.6",
|
"@nestjs/core": "^10.0.0",
|
||||||
"@nestjs/jwt": "^11.0.0",
|
"@nestjs/passport": "^10.0.3",
|
||||||
"@nestjs/passport": "^11.0.5",
|
"@nestjs/platform-express": "^10.0.0",
|
||||||
"@nestjs/platform-express": "^11.1.6",
|
|
||||||
"@nestjs/serve-static": "^5.0.3",
|
|
||||||
"@nestjs/typeorm": "^11.0.0",
|
|
||||||
"@noahspan/azure-database": "^3.1.2",
|
"@noahspan/azure-database": "^3.1.2",
|
||||||
"@noahspan/noahspan-modules": "^1.2.11",
|
"@noahspan/noahspan-modules": "^1.1.5",
|
||||||
"@schematics/angular": "^17.3.7",
|
"@schematics/angular": "^17.3.7",
|
||||||
"@types/multer": "^1.4.12",
|
"@types/multer": "^1.4.12",
|
||||||
"better-sqlite3": "^12.2.0",
|
"dotenv": "^16.4.7",
|
||||||
"dotenv": "^16.6.1",
|
|
||||||
"express-session": "^1.18.2",
|
|
||||||
"jwks-rsa": "^3.2.0",
|
|
||||||
"jwt-decode": "^4.0.0",
|
|
||||||
"node-gyp": "^11.4.1",
|
|
||||||
"passport": "^0.7.0",
|
|
||||||
"passport-jwt": "^4.0.1",
|
|
||||||
"passport-openidconnect": "^0.1.2",
|
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1",
|
"rxjs": "^7.8.1",
|
||||||
"typeorm": "^0.3.25",
|
|
||||||
"uuid": "^10.0.0",
|
"uuid": "^10.0.0",
|
||||||
"uuidv4": "^6.2.13"
|
"uuidv4": "^6.2.13"
|
||||||
},
|
},
|
||||||
@@ -57,13 +41,11 @@
|
|||||||
"@microsoft/microsoft-graph-types": "^2.40.0",
|
"@microsoft/microsoft-graph-types": "^2.40.0",
|
||||||
"@nestjs/cli": "^10.0.0",
|
"@nestjs/cli": "^10.0.0",
|
||||||
"@nestjs/schematics": "^10.0.0",
|
"@nestjs/schematics": "^10.0.0",
|
||||||
"@nestjs/testing": "^11.1.6",
|
"@nestjs/testing": "^10.0.0",
|
||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/express-session": "^1.18.2",
|
|
||||||
"@types/jest": "^29.5.2",
|
"@types/jest": "^29.5.2",
|
||||||
"@types/node": "^20.3.1",
|
"@types/node": "^20.3.1",
|
||||||
"@types/passport-azure-ad": "^4.3.6",
|
"@types/passport-azure-ad": "^4.3.6",
|
||||||
"@types/passport-openidconnect": "^0.1.3",
|
|
||||||
"@types/supertest": "^6.0.0",
|
"@types/supertest": "^6.0.0",
|
||||||
"jest": "^29.5.0",
|
"jest": "^29.5.0",
|
||||||
"source-map-support": "^0.5.21",
|
"source-map-support": "^0.5.21",
|
||||||
|
|||||||
22
api/src/app.controller.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { AppController } from './app.controller';
|
||||||
|
import { AppService } from './app.service';
|
||||||
|
|
||||||
|
describe('AppController', () => {
|
||||||
|
let appController: AppController;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const app: TestingModule = await Test.createTestingModule({
|
||||||
|
controllers: [AppController],
|
||||||
|
providers: [AppService]
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
appController = app.get<AppController>(AppController);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('root', () => {
|
||||||
|
it('should return "Hello World!"', () => {
|
||||||
|
expect(appController.getHello()).toBe('Hello World!');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,17 +1,12 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { HealthModule } from './health/health.module';
|
import { FeatureFlagModule } from './featureFlag/feature-flag.module'
|
||||||
import { LogModule } from './log/log.module';
|
import { LogModule } from './log/log.module';
|
||||||
import { PilotModule } from './pilot/pilot.module';
|
import { PilotModule } from './pilot/pilot.module';
|
||||||
import { APP_FILTER, APP_GUARD, Reflector } from '@nestjs/core';
|
import { APP_FILTER, APP_GUARD, Reflector } from '@nestjs/core';
|
||||||
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { AuthModule, MsGraphModule } from '@noahspan/noahspan-modules';
|
import { AuthGuard, AuthModule, UserModule } from '@noahspan/noahspan-modules';
|
||||||
import configuration from './config/configuration';
|
import configuration from './config/configuration';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
import { dataSourceOptions } from './database/data-source';
|
|
||||||
import { TrackModule } from './track/track.module';
|
|
||||||
import { ServeStaticModule } from '@nestjs/serve-static';
|
|
||||||
import { join } from 'path';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -20,42 +15,35 @@ import { join } from 'path';
|
|||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
useFactory: async (configService: ConfigService) => {
|
useFactory: async (configService: ConfigService) => {
|
||||||
return {
|
return {
|
||||||
audience: configService.get<string>('audience'),
|
clientId: configService.get<string>('clientId'),
|
||||||
issuerUrl: configService.get<string>('issuer'),
|
clientSecret: configService.get<string>('clientSecret'),
|
||||||
jwksUri: configService.get<string>('jwksUri')
|
tenantId: configService.get<string>('tenantId')
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
isGlobal: true,
|
|
||||||
load: [configuration]
|
load: [configuration]
|
||||||
}),
|
}),
|
||||||
HealthModule,
|
FeatureFlagModule,
|
||||||
LogModule,
|
LogModule,
|
||||||
MsGraphModule.registerAsync({
|
PilotModule,
|
||||||
|
UserModule.registerAsync({
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
useFactory: async (configService: ConfigService) => {
|
useFactory: async (configService: ConfigService) => {
|
||||||
return {
|
return {
|
||||||
authority: configService.get<string>('authority'),
|
|
||||||
clientId: configService.get<string>('clientId'),
|
clientId: configService.get<string>('clientId'),
|
||||||
clientSecret: configService.get<string>('clientSecret'),
|
clientSecret: configService.get<string>('clientSecret'),
|
||||||
tenantId: configService.get<string>('tenantId')
|
tenantId: configService.get<string>('tenantId')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
})
|
||||||
PilotModule,
|
|
||||||
ServeStaticModule.forRoot({
|
|
||||||
rootPath: join(__dirname, '../..', 'client', 'dist')
|
|
||||||
}),
|
|
||||||
TrackModule,
|
|
||||||
TypeOrmModule.forRoot(dataSourceOptions)
|
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: APP_FILTER,
|
provide: APP_FILTER,
|
||||||
useClass: HttpExceptionFilter
|
useClass: HttpExceptionFilter
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|||||||
5
api/src/auth/auth.interface.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export interface AuthModuleOptions {
|
||||||
|
tenantId: string;
|
||||||
|
clientId: string;
|
||||||
|
clientSecret: string;
|
||||||
|
}
|
||||||
4
api/src/auth/auth.module-definition.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import { ConfigurableModuleBuilder } from '@nestjs/common';
|
||||||
|
import { AuthModuleOptions } from './auth.interface';
|
||||||
|
|
||||||
|
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder<AuthModuleOptions>().build()
|
||||||
14
api/src/auth/auth.module.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { PassportModule } from '@nestjs/passport';
|
||||||
|
import { AzureAdStrategy } from './auth.strategy';
|
||||||
|
import { ConfigurableModuleClass } from './auth.module-definition';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
PassportModule.register({
|
||||||
|
defaultStrategy: 'azure-ad'
|
||||||
|
})
|
||||||
|
],
|
||||||
|
providers: [AzureAdStrategy]
|
||||||
|
})
|
||||||
|
export class AuthModule extends ConfigurableModuleClass {}
|
||||||
25
api/src/auth/auth.strategy.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { Inject, Injectable } from "@nestjs/common";
|
||||||
|
import { PassportStrategy } from "@nestjs/passport";
|
||||||
|
import { AuthModuleOptions } from './auth.interface'
|
||||||
|
import { MODULE_OPTIONS_TOKEN } from "./auth.module-definition";
|
||||||
|
import { BearerStrategy } from 'passport-azure-ad'
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class AzureAdStrategy extends PassportStrategy(
|
||||||
|
BearerStrategy,
|
||||||
|
'azure-ad'
|
||||||
|
) {
|
||||||
|
constructor(@Inject(MODULE_OPTIONS_TOKEN) authModuleOptions: AuthModuleOptions) {
|
||||||
|
super({
|
||||||
|
identityMetadata: `https://login.microsoftonline.com/${authModuleOptions.tenantId}/.well-known/openid-configuration`,
|
||||||
|
clientID: authModuleOptions.clientId,
|
||||||
|
audience: `api://${authModuleOptions.clientId}`,
|
||||||
|
loggingLevel: 'info',
|
||||||
|
loggingNoPII: false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async validate(data: any): Promise<any> {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
|
||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
|
||||||
|
|
||||||
@Entity({ name: 'certificates'})
|
|
||||||
export class CertificateEntity {
|
|
||||||
@PrimaryGeneratedColumn('uuid')
|
|
||||||
id: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
type: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
number: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
issueDate: Date
|
|
||||||
|
|
||||||
@ManyToOne(() => PilotEntity, (pilot: PilotEntity) => pilot.certificates, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'pilotId' })
|
|
||||||
pilot: PilotEntity;
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,6 @@
|
|||||||
export default () => ({
|
export default () => ({
|
||||||
azureStorageConnectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
|
azureStorageConnectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
|
||||||
audience: process.env.AUDIENCE,
|
|
||||||
authority: process.env.AUTHORITY,
|
|
||||||
clientId: process.env.CLIENT_ID,
|
clientId: process.env.CLIENT_ID,
|
||||||
clientSecret: process.env.CLIENT_SECRET,
|
clientSecret: process.env.CLIENT_SECRET,
|
||||||
issuer: process.env.ISSUER_URL,
|
|
||||||
jwksUri: process.env.JWKS_URI,
|
|
||||||
tenantId: process.env.TENANT_ID
|
tenantId: process.env.TENANT_ID
|
||||||
})
|
})
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
import { DataSource, DataSourceOptions } from 'typeorm';
|
|
||||||
import { config } from 'dotenv';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
|
|
||||||
config();
|
|
||||||
|
|
||||||
const configService = new ConfigService();
|
|
||||||
|
|
||||||
export const dataSourceOptions: DataSourceOptions = {
|
|
||||||
type: 'better-sqlite3',
|
|
||||||
database: configService.get<string>('DB_PATH'),
|
|
||||||
entities: ['../**/*.entity.js'],
|
|
||||||
migrations: ['./migrations/*.js'],
|
|
||||||
synchronize: configService.get<boolean>('DB_SYNC'),
|
|
||||||
migrationsRun: true
|
|
||||||
}
|
|
||||||
|
|
||||||
const dataSource = new DataSource(dataSourceOptions);
|
|
||||||
|
|
||||||
export default dataSource;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
dbs:
|
|
||||||
- path: /var/lib/data/flying.db
|
|
||||||
replicas:
|
|
||||||
- path: /mnt/data/backup
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
|
||||||
|
|
||||||
export class InitialMigration1758802917932 implements MigrationInterface {
|
|
||||||
name = 'InitialMigration1758802917932'
|
|
||||||
|
|
||||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
||||||
await queryRunner.query(`CREATE TABLE "certificates" ("id" varchar PRIMARY KEY NOT NULL, "type" varchar NOT NULL, "number" varchar NOT NULL, "issueDate" datetime NOT NULL, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "endorsements" ("id" varchar PRIMARY KEY NOT NULL, "type" varchar NOT NULL, "issueDate" datetime NOT NULL, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "medical" ("id" varchar PRIMARY KEY NOT NULL, "class" varchar NOT NULL, "expirationDate" datetime NOT NULL, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "pilots" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "address" varchar NOT NULL, "city" varchar NOT NULL, "state" varchar NOT NULL, "postalCode" varchar NOT NULL, "email" varchar NOT NULL, "phone" varchar NOT NULL, "userId" varchar NOT NULL)`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "logs" ("id" varchar PRIMARY KEY NOT NULL, "date" datetime NOT NULL, "aircraftMakeModel" varchar NOT NULL, "aircraftIdentity" varchar NOT NULL, "routeFrom" varchar NOT NULL, "routeTo" varchar NOT NULL, "durationOfFlight" integer NOT NULL, "singleEngineLand" integer, "simulatorAtd" integer, "landingsDay" integer, "landingsNight" integer, "groundTrainingReceived" integer, "flightTrainingReceived" integer, "crossCountry" integer, "night" integer, "solo" integer, "pilotInCommand" integer, "instrumentActual" integer, "instrumentSimulated" integer, "instrumentApproaches" integer, "instrumentHolds" integer, "instrumentNavTrack" integer, "notes" varchar, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "tracks" ("id" varchar PRIMARY KEY NOT NULL, "url" varchar NOT NULL, "order" integer NOT NULL, "logId" varchar)`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "temporary_certificates" ("id" varchar PRIMARY KEY NOT NULL, "type" varchar NOT NULL, "number" varchar NOT NULL, "issueDate" datetime NOT NULL, "pilotId" varchar, CONSTRAINT "FK_05a68997dc2d27dfcc642a4cf51" FOREIGN KEY ("pilotId") REFERENCES "pilots" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "temporary_certificates"("id", "type", "number", "issueDate", "pilotId") SELECT "id", "type", "number", "issueDate", "pilotId" FROM "certificates"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "certificates"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "temporary_certificates" RENAME TO "certificates"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "temporary_endorsements" ("id" varchar PRIMARY KEY NOT NULL, "type" varchar NOT NULL, "issueDate" datetime NOT NULL, "pilotId" varchar, CONSTRAINT "FK_96a69a0bfbdeccce6bffe34002d" FOREIGN KEY ("pilotId") REFERENCES "pilots" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "temporary_endorsements"("id", "type", "issueDate", "pilotId") SELECT "id", "type", "issueDate", "pilotId" FROM "endorsements"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "endorsements"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "temporary_endorsements" RENAME TO "endorsements"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "temporary_medical" ("id" varchar PRIMARY KEY NOT NULL, "class" varchar NOT NULL, "expirationDate" datetime NOT NULL, "pilotId" varchar, CONSTRAINT "FK_cb1f5d88fa2b105cc77513e9082" FOREIGN KEY ("pilotId") REFERENCES "pilots" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "temporary_medical"("id", "class", "expirationDate", "pilotId") SELECT "id", "class", "expirationDate", "pilotId" FROM "medical"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "medical"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "temporary_medical" RENAME TO "medical"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "temporary_logs" ("id" varchar PRIMARY KEY NOT NULL, "date" datetime NOT NULL, "aircraftMakeModel" varchar NOT NULL, "aircraftIdentity" varchar NOT NULL, "routeFrom" varchar NOT NULL, "routeTo" varchar NOT NULL, "durationOfFlight" integer NOT NULL, "singleEngineLand" integer, "simulatorAtd" integer, "landingsDay" integer, "landingsNight" integer, "groundTrainingReceived" integer, "flightTrainingReceived" integer, "crossCountry" integer, "night" integer, "solo" integer, "pilotInCommand" integer, "instrumentActual" integer, "instrumentSimulated" integer, "instrumentApproaches" integer, "instrumentHolds" integer, "instrumentNavTrack" integer, "notes" varchar, "pilotId" varchar, CONSTRAINT "FK_19598551658f7625a82c7f029c8" FOREIGN KEY ("pilotId") REFERENCES "pilots" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "temporary_logs"("id", "date", "aircraftMakeModel", "aircraftIdentity", "routeFrom", "routeTo", "durationOfFlight", "singleEngineLand", "simulatorAtd", "landingsDay", "landingsNight", "groundTrainingReceived", "flightTrainingReceived", "crossCountry", "night", "solo", "pilotInCommand", "instrumentActual", "instrumentSimulated", "instrumentApproaches", "instrumentHolds", "instrumentNavTrack", "notes", "pilotId") SELECT "id", "date", "aircraftMakeModel", "aircraftIdentity", "routeFrom", "routeTo", "durationOfFlight", "singleEngineLand", "simulatorAtd", "landingsDay", "landingsNight", "groundTrainingReceived", "flightTrainingReceived", "crossCountry", "night", "solo", "pilotInCommand", "instrumentActual", "instrumentSimulated", "instrumentApproaches", "instrumentHolds", "instrumentNavTrack", "notes", "pilotId" FROM "logs"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "logs"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "temporary_logs" RENAME TO "logs"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "temporary_tracks" ("id" varchar PRIMARY KEY NOT NULL, "url" varchar NOT NULL, "order" integer NOT NULL, "logId" varchar, CONSTRAINT "FK_71881df31cfff2362e39accc4b2" FOREIGN KEY ("logId") REFERENCES "logs" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "temporary_tracks"("id", "url", "order", "logId") SELECT "id", "url", "order", "logId" FROM "tracks"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "tracks"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "temporary_tracks" RENAME TO "tracks"`);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
||||||
await queryRunner.query(`ALTER TABLE "tracks" RENAME TO "temporary_tracks"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "tracks" ("id" varchar PRIMARY KEY NOT NULL, "url" varchar NOT NULL, "order" integer NOT NULL, "logId" varchar)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "tracks"("id", "url", "order", "logId") SELECT "id", "url", "order", "logId" FROM "temporary_tracks"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "temporary_tracks"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "logs" RENAME TO "temporary_logs"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "logs" ("id" varchar PRIMARY KEY NOT NULL, "date" datetime NOT NULL, "aircraftMakeModel" varchar NOT NULL, "aircraftIdentity" varchar NOT NULL, "routeFrom" varchar NOT NULL, "routeTo" varchar NOT NULL, "durationOfFlight" integer NOT NULL, "singleEngineLand" integer, "simulatorAtd" integer, "landingsDay" integer, "landingsNight" integer, "groundTrainingReceived" integer, "flightTrainingReceived" integer, "crossCountry" integer, "night" integer, "solo" integer, "pilotInCommand" integer, "instrumentActual" integer, "instrumentSimulated" integer, "instrumentApproaches" integer, "instrumentHolds" integer, "instrumentNavTrack" integer, "notes" varchar, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "logs"("id", "date", "aircraftMakeModel", "aircraftIdentity", "routeFrom", "routeTo", "durationOfFlight", "singleEngineLand", "simulatorAtd", "landingsDay", "landingsNight", "groundTrainingReceived", "flightTrainingReceived", "crossCountry", "night", "solo", "pilotInCommand", "instrumentActual", "instrumentSimulated", "instrumentApproaches", "instrumentHolds", "instrumentNavTrack", "notes", "pilotId") SELECT "id", "date", "aircraftMakeModel", "aircraftIdentity", "routeFrom", "routeTo", "durationOfFlight", "singleEngineLand", "simulatorAtd", "landingsDay", "landingsNight", "groundTrainingReceived", "flightTrainingReceived", "crossCountry", "night", "solo", "pilotInCommand", "instrumentActual", "instrumentSimulated", "instrumentApproaches", "instrumentHolds", "instrumentNavTrack", "notes", "pilotId" FROM "temporary_logs"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "temporary_logs"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "medical" RENAME TO "temporary_medical"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "medical" ("id" varchar PRIMARY KEY NOT NULL, "class" varchar NOT NULL, "expirationDate" datetime NOT NULL, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "medical"("id", "class", "expirationDate", "pilotId") SELECT "id", "class", "expirationDate", "pilotId" FROM "temporary_medical"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "temporary_medical"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "endorsements" RENAME TO "temporary_endorsements"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "endorsements" ("id" varchar PRIMARY KEY NOT NULL, "type" varchar NOT NULL, "issueDate" datetime NOT NULL, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "endorsements"("id", "type", "issueDate", "pilotId") SELECT "id", "type", "issueDate", "pilotId" FROM "temporary_endorsements"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "temporary_endorsements"`);
|
|
||||||
await queryRunner.query(`ALTER TABLE "certificates" RENAME TO "temporary_certificates"`);
|
|
||||||
await queryRunner.query(`CREATE TABLE "certificates" ("id" varchar PRIMARY KEY NOT NULL, "type" varchar NOT NULL, "number" varchar NOT NULL, "issueDate" datetime NOT NULL, "pilotId" varchar)`);
|
|
||||||
await queryRunner.query(`INSERT INTO "certificates"("id", "type", "number", "issueDate", "pilotId") SELECT "id", "type", "number", "issueDate", "pilotId" FROM "temporary_certificates"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "temporary_certificates"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "tracks"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "logs"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "pilots"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "medical"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "endorsements"`);
|
|
||||||
await queryRunner.query(`DROP TABLE "certificates"`);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
import { Body, Controller, Delete, Get, HttpException, Param, Post, Put } from "@nestjs/common";
|
|
||||||
import { EndorsementService } from "./endorsement.service";
|
|
||||||
import { CustomError } from "@noahspan/noahspan-modules";
|
|
||||||
import { EndorsementDto } from "./endorsement.dto";
|
|
||||||
|
|
||||||
|
|
||||||
@Controller('endorsements')
|
|
||||||
export class EndorsementController {
|
|
||||||
constructor(private readonly endorsementService: EndorsementService) {}
|
|
||||||
|
|
||||||
@Get(':id')
|
|
||||||
async find(@Param('id') id: string) {
|
|
||||||
try {
|
|
||||||
return await this.endorsementService.find(id);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get()
|
|
||||||
async findAll() {
|
|
||||||
try {
|
|
||||||
return await this.endorsementService.findAll();
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post()
|
|
||||||
async create(@Body() endorsementDto: EndorsementDto) {
|
|
||||||
try {
|
|
||||||
return await this.endorsementService.create(endorsementDto);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Put(':id')
|
|
||||||
async update(@Param('id') id: string, @Body() endorsementDto: EndorsementDto) {
|
|
||||||
try {
|
|
||||||
return await this.endorsementService.update(id, endorsementDto);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Delete(':id')
|
|
||||||
async delete(@Param('id') id: string) {
|
|
||||||
try {
|
|
||||||
return await this.endorsementService.delete(id);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export class EndorsementDto {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
|
||||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
|
||||||
|
|
||||||
@Entity({ name: 'endorsements' })
|
|
||||||
export class EndorsementEntity {
|
|
||||||
@PrimaryGeneratedColumn('uuid')
|
|
||||||
id: string
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
type: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
issueDate: Date;
|
|
||||||
|
|
||||||
@ManyToOne(() => PilotEntity, (pilot: PilotEntity) => pilot.endorsements, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'pilotId' })
|
|
||||||
pilot: PilotEntity
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import { InjectRepository } from "@nestjs/typeorm";
|
|
||||||
import { EndorsementEntity } from "./endorsement.entity";
|
|
||||||
import { DeleteResult, InsertResult, Repository, UpdateResult } from 'typeorm';
|
|
||||||
import { Injectable } from "@nestjs/common";
|
|
||||||
import { EndorsementDto } from "./endorsement.dto";
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class EndorsementService {
|
|
||||||
constructor(
|
|
||||||
@InjectRepository(EndorsementEntity) private readonly endorsementRepository: Repository<EndorsementEntity>
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async find(id: string): Promise<EndorsementEntity> {
|
|
||||||
return await this.endorsementRepository.findOneBy({ id });
|
|
||||||
}
|
|
||||||
|
|
||||||
async findAll(): Promise<EndorsementEntity[]> {
|
|
||||||
return await this.endorsementRepository.find();
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(endorsement: EndorsementDto): Promise<InsertResult> {
|
|
||||||
return await this.endorsementRepository.insert(endorsement);
|
|
||||||
}
|
|
||||||
|
|
||||||
async update(id: string, endorsement: EndorsementDto): Promise<UpdateResult> {
|
|
||||||
return await this.endorsementRepository.update(id, endorsement);
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(id: string): Promise<DeleteResult> {
|
|
||||||
return await this.endorsementRepository.delete({ id });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
41
api/src/featureFlag/feature-flag.controller.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import {
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
HttpException,
|
||||||
|
Param,
|
||||||
|
UseGuards,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { FeatureFlagService } from './feature-flag.service';
|
||||||
|
import { CustomError } from '../error/customError';
|
||||||
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
|
|
||||||
|
@Controller('featureFlags')
|
||||||
|
@UseGuards(AuthGuard('azure-ad'))
|
||||||
|
export class FeatureFlagController {
|
||||||
|
constructor(private readonly featureFlagService: FeatureFlagService) {}
|
||||||
|
|
||||||
|
@Get(':partitionKey/:rowKey')
|
||||||
|
async find(
|
||||||
|
@Param('partitionKey') partitionKey: string,
|
||||||
|
@Param('rowKey') rowKey: string
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
return await this.featureFlagService.find(partitionKey, rowKey);
|
||||||
|
} catch (error) {
|
||||||
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
throw new HttpException(customError.message, customError.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
async findAll() {
|
||||||
|
try {
|
||||||
|
return await this.featureFlagService.findAll();
|
||||||
|
} catch (error) {
|
||||||
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
throw new HttpException(customError.message, customError.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
api/src/featureFlag/feature-flag.dto.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export class FeatureFlagDto {
|
||||||
|
partitionKey: string;
|
||||||
|
rowKey: string;
|
||||||
|
active: string;
|
||||||
|
}
|
||||||
7
api/src/featureFlag/feature-flag.entity.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { EntityString } from '@noahspan/azure-database';
|
||||||
|
|
||||||
|
export class FeatureFlag {
|
||||||
|
@EntityString() partitionKey: string;
|
||||||
|
@EntityString() rowKey: string;
|
||||||
|
@EntityString() active: string;
|
||||||
|
}
|
||||||
27
api/src/featureFlag/feature-flag.module.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { FeatureFlagController } from './feature-flag.controller';
|
||||||
|
import { FeatureFlagService } from './feature-flag.service';
|
||||||
|
import { AzureTableStorageModule } from '@noahspan/azure-database';
|
||||||
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
import { FeatureFlag } from './feature-flag.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
AzureTableStorageModule.forRootAsync({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
useFactory: async (configService: ConfigService) => {
|
||||||
|
return {
|
||||||
|
connectionString: configService.get<string>('azureStorageConnectionString')
|
||||||
|
};
|
||||||
|
},
|
||||||
|
inject: [ConfigService]
|
||||||
|
}),
|
||||||
|
AzureTableStorageModule.forFeature(FeatureFlag, {
|
||||||
|
createTableIfNotExists: false,
|
||||||
|
table: 'featureFlags'
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
controllers: [FeatureFlagController],
|
||||||
|
providers: [FeatureFlagService]
|
||||||
|
})
|
||||||
|
export class FeatureFlagModule {}
|
||||||
18
api/src/featureFlag/feature-flag.service.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { InjectRepository, Repository } from '@noahspan/azure-database';
|
||||||
|
import { FeatureFlag } from './feature-flag.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class FeatureFlagService {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(FeatureFlag) private readonly featureFlagRepository: Repository<FeatureFlag>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async find(partitionKey: string, rowKey: string): Promise<FeatureFlag> {
|
||||||
|
return await this.featureFlagRepository.find(partitionKey, rowKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
async findAll(): Promise<FeatureFlag[]> {
|
||||||
|
return await this.featureFlagRepository.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,10 +37,10 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
return blockBlobClient;
|
return blockBlobClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
async uploadFile(file: Express.Multer.File, containerName: string, logId: string): Promise<string> {
|
async uploadFile(file: Express.Multer.File, containerName: string, rowKey: string): Promise<string> {
|
||||||
this.containerName = containerName;
|
this.containerName = containerName;
|
||||||
|
|
||||||
const blockBlobClient = await this.getBlobClient(`${logId}/${file.originalname}`);
|
const blockBlobClient = await this.getBlobClient(`${rowKey}/${file.originalname}`);
|
||||||
const fileUrl = blockBlobClient.url;
|
const fileUrl = blockBlobClient.url;
|
||||||
|
|
||||||
await blockBlobClient.uploadData(file.buffer);
|
await blockBlobClient.uploadData(file.buffer);
|
||||||
@@ -48,42 +48,21 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
return fileUrl;
|
return fileUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadFile(containerName: string, logId: string, fileName: string): Promise<string> {
|
async downloadFile(containerName: string, rowKey: string, fileName: string): Promise<string> {
|
||||||
this.containerName = containerName;
|
this.containerName = containerName;
|
||||||
|
|
||||||
const blockBlobClient = await this.getBlobClient(`${logId}/${fileName}`);
|
const blockBlobClient = await this.getBlobClient(`${rowKey}/${fileName}`);
|
||||||
const downloadBlockBlobResponse = await blockBlobClient.download();
|
const downloadBlockBlobResponse = await blockBlobClient.download();
|
||||||
const downloaded: string = (await this.streamToBuffer(downloadBlockBlobResponse.readableStreamBody)).toString()
|
const downloaded: string = (await this.streamToBuffer(downloadBlockBlobResponse.readableStreamBody)).toString()
|
||||||
|
|
||||||
return downloaded
|
return downloaded
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteFile(containerName: string, logId: string, fileName: string): Promise<void> {
|
async deleteFile(containerName: string, rowKey:string, fileName: string): Promise<void> {
|
||||||
this.containerName = containerName;
|
this.containerName = containerName;
|
||||||
|
|
||||||
const blockBlobClient = await this.getBlobClient(`${logId}/${fileName}`);
|
const blockBlobClient = await this.getBlobClient(`${rowKey}/${fileName}`);
|
||||||
|
|
||||||
await blockBlobClient.deleteIfExists();
|
await blockBlobClient.deleteIfExists();
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteFolder(containerName: string, logId: string): Promise<void> {
|
|
||||||
const blobService = await this.getBlobServiceInstance();
|
|
||||||
|
|
||||||
this.containerName = containerName;
|
|
||||||
|
|
||||||
const containerClient = blobService.getContainerClient(containerName);
|
|
||||||
const blobsToDelete = []
|
|
||||||
|
|
||||||
for await (const blob of containerClient.listBlobsFlat({ prefix: logId })) {
|
|
||||||
blobsToDelete.push(blob.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const blobName of blobsToDelete) {
|
|
||||||
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
|
||||||
|
|
||||||
await blockBlobClient.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import { Test, TestingModule } from "@nestjs/testing";
|
|
||||||
import { HealthController } from "./health.controller"
|
|
||||||
import { HttpStatus } from "@nestjs/common";
|
|
||||||
|
|
||||||
describe('HealthController', () => {
|
|
||||||
let controller: HealthController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [HealthController]
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<HealthController>(HealthController);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('isHealth => should return status ok', async () => {
|
|
||||||
const result = await controller.isHealthy();
|
|
||||||
|
|
||||||
expect(result).toEqual(HttpStatus.OK);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import {
|
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
HttpStatus,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
|
|
||||||
@Controller('health')
|
|
||||||
export class HealthController {
|
|
||||||
@Get()
|
|
||||||
async isHealthy(): Promise<HttpStatus> {
|
|
||||||
return HttpStatus.OK
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { HealthController } from './health.controller';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
controllers: [HealthController],
|
|
||||||
})
|
|
||||||
export class HealthModule {}
|
|
||||||
41
api/src/log/interceptors/log.interceptor.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
||||||
|
import { Observable, map } from 'rxjs';
|
||||||
|
|
||||||
|
export class LogInterceptor 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];
|
||||||
|
console.log(token)
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return handler.handle().pipe(
|
||||||
|
map((data) => {
|
||||||
|
if (data.length) {
|
||||||
|
const logs = data.map((log) => {
|
||||||
|
return {
|
||||||
|
partitionKey: log.partitionKey,
|
||||||
|
rowKey: log.rowKey,
|
||||||
|
pilotId: log.pilotId,
|
||||||
|
pilotName: log.pilotName,
|
||||||
|
date: log.date,
|
||||||
|
aircraftMakeModel: log.aircraftMakeModel,
|
||||||
|
routeFrom: log.routeFrom,
|
||||||
|
routeTo: log.routeTo,
|
||||||
|
durationOfFlight: log.durationOfFlight,
|
||||||
|
tracks: log.tracks,
|
||||||
|
notes: log.notes
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return logs;
|
||||||
|
} else {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler.handle().pipe(map((data) => data));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,449 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { LogController } from './log.controller';
|
|
||||||
import { LogService } from './log.service';
|
|
||||||
import { LogDto } from './log.dto';
|
|
||||||
import { LogEntity } from './log.entity';
|
|
||||||
import { FileService } from '../file/file.service';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
|
|
||||||
describe('LogController', () => {
|
|
||||||
let controller: LogController;
|
|
||||||
|
|
||||||
const mockLogService = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findLogsWithCount: jest.fn(),
|
|
||||||
findLogsWithTracks: jest.fn(),
|
|
||||||
update: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [LogController],
|
|
||||||
providers: [
|
|
||||||
ConfigService,
|
|
||||||
FileService,
|
|
||||||
{
|
|
||||||
provide: LogService,
|
|
||||||
useValue: mockLogService
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}).compile()
|
|
||||||
|
|
||||||
controller = module.get<LogController>(LogController);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('find => should find a log by id', async () => {
|
|
||||||
const log = {
|
|
||||||
id: '95834f84-0a02-44d3-884e-a20237adeca0',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'find').mockReturnValue(log);
|
|
||||||
|
|
||||||
const result = await controller.find(log.id);
|
|
||||||
|
|
||||||
expect(result).toEqual(log);
|
|
||||||
expect(mockLogService.find).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.find).toHaveBeenCalledWith(log.id);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('find => should fail to find a log by id', async () => {
|
|
||||||
const id: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'find').mockRejectedValue(new Error('Log not found'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.find(id);
|
|
||||||
|
|
||||||
fail('find did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockLogService.find).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.find).toHaveBeenCalledWith(id);
|
|
||||||
expect(mockLogService.find).rejects.toThrow('Log not found')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findLogsWithCount => should find logs with count', async () => {
|
|
||||||
const log = {
|
|
||||||
id: '95834f84-0a02-44d3-884e-a20237adeca0',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: [
|
|
||||||
{
|
|
||||||
"id": "4645ce0c-6fd7-432c-8089-777a7139cf7e",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KANE_KONA_20250628.kml",
|
|
||||||
"order": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "5ce26898-a18b-46f4-b989-3fb7acdace08",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KONA_KANE_20250628.kml",
|
|
||||||
"order": 2
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as LogEntity;
|
|
||||||
const logs = [log]
|
|
||||||
const count = 1
|
|
||||||
const morePages = false
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'findLogsWithCount').mockReturnValue({
|
|
||||||
entities: logs,
|
|
||||||
total: count,
|
|
||||||
hasNextPage: morePages
|
|
||||||
});
|
|
||||||
|
|
||||||
const {entities, total, hasNextPage} = await controller.findLogsWithCount();
|
|
||||||
|
|
||||||
expect(entities).toEqual(logs);
|
|
||||||
expect(total).toEqual(count);
|
|
||||||
expect(hasNextPage).toEqual(morePages)
|
|
||||||
expect(mockLogService.findLogsWithCount).toHaveBeenCalled();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findLogsWithCount => should fail to find logs with count', async () => {
|
|
||||||
jest.spyOn(mockLogService, 'findLogsWithCount').mockRejectedValue(new Error('Logs not found'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.findLogsWithCount();
|
|
||||||
|
|
||||||
fail('findLogsWithCount did not throw error');
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockLogService.findLogsWithCount).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.findLogsWithCount).rejects.toThrow('Logs not found')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findLogsWithTracks => should find logs with tracks', async () => {
|
|
||||||
const log = {
|
|
||||||
id: '95834f84-0a02-44d3-884e-a20237adeca0',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: [
|
|
||||||
{
|
|
||||||
"id": "4645ce0c-6fd7-432c-8089-777a7139cf7e",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KANE_KONA_20250628.kml",
|
|
||||||
"order": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "5ce26898-a18b-46f4-b989-3fb7acdace08",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KONA_KANE_20250628.kml",
|
|
||||||
"order": 2
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as LogEntity;
|
|
||||||
const logs = [log]
|
|
||||||
const count = 1
|
|
||||||
const morePages = false
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'findLogsWithTracks').mockReturnValue({
|
|
||||||
entities: logs,
|
|
||||||
total: count,
|
|
||||||
hasNextPage: morePages
|
|
||||||
});
|
|
||||||
|
|
||||||
const {entities, total, hasNextPage} = await controller.findLogsWithTracks();
|
|
||||||
|
|
||||||
expect(entities).toEqual(logs);
|
|
||||||
expect(total).toEqual(count);
|
|
||||||
expect(hasNextPage).toEqual(morePages)
|
|
||||||
expect(mockLogService.findLogsWithTracks).toHaveBeenCalled();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findLogsWithCount => should fail to find logs with tracks', async () => {
|
|
||||||
jest.spyOn(mockLogService, 'findLogsWithTracks').mockRejectedValue(new Error('Logs not found'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.findLogsWithTracks();
|
|
||||||
|
|
||||||
fail('findLogsWithTracks did not throw error');
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockLogService.findLogsWithTracks).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.findLogsWithTracks).rejects.toThrow('Logs not found')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should create a new log', async () => {
|
|
||||||
const logDto = {
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test'
|
|
||||||
} as LogDto;
|
|
||||||
|
|
||||||
const log = {
|
|
||||||
id: '95834f84-0a02-44d3-884e-a20237adeca0',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity;
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'create').mockReturnValue(log);
|
|
||||||
|
|
||||||
const result = await controller.create(logDto);
|
|
||||||
|
|
||||||
expect(mockLogService.create).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.create).toHaveBeenCalledWith(logDto);
|
|
||||||
expect(result).toEqual(log);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should fail to create a new log', async () => {
|
|
||||||
const logDto = {
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test'
|
|
||||||
} as LogDto;
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'create').mockRejectedValue(new Error('Log failed to create'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.create(logDto);
|
|
||||||
|
|
||||||
fail('create did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockLogService.create).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.create).toHaveBeenCalledWith(logDto);
|
|
||||||
expect(mockLogService.create).rejects.toThrow('Log failed to create')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should update an existing log', async () => {
|
|
||||||
const id: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const logDto = {
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test'
|
|
||||||
} as LogDto
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'update').mockReturnValue(logDto);
|
|
||||||
|
|
||||||
const result = await controller.update(id, logDto);
|
|
||||||
|
|
||||||
expect(result).toEqual(logDto);
|
|
||||||
expect(mockLogService.update).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.update).toHaveBeenCalledWith(id, logDto);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should fail to update an exising log', async () => {
|
|
||||||
const id: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const logDto = {
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test'
|
|
||||||
} as LogDto;
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'update').mockRejectedValue(new Error('Log failed to update'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.update(id, logDto);
|
|
||||||
|
|
||||||
fail('update did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockLogService.update).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.update).toHaveBeenCalledWith(id, logDto);
|
|
||||||
expect(mockLogService.update).rejects.toThrow('Log failed to update')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should delete and existing log', async () => {
|
|
||||||
const id: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'delete');
|
|
||||||
|
|
||||||
const result = await controller.delete(id);
|
|
||||||
|
|
||||||
expect(mockLogService.delete).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.delete).toHaveBeenCalledWith(id);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should fail to delete and exising log', async () => {
|
|
||||||
const id: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'delete').mockRejectedValue(new Error('Log failed to delete'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.delete(id);
|
|
||||||
|
|
||||||
fail('delete did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockLogService.delete).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.delete).toHaveBeenCalledWith(id);
|
|
||||||
expect(mockLogService.delete).rejects.toThrow('Log failed to delete')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -8,73 +8,64 @@ import {
|
|||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
Query,
|
Query,
|
||||||
|
StreamableFile,
|
||||||
|
UploadedFile,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
UseInterceptors
|
UseInterceptors
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { LogDto } from './log.dto';
|
import { LogDto } from './log.dto';
|
||||||
import { LogEntity } from './log.entity';
|
import { Log } from './log.entity';
|
||||||
import { LogService } from './log.service';
|
import { LogService } from './log.service';
|
||||||
import { CustomError } from '../error/customError';
|
import { CustomError } from '../error/customError';
|
||||||
import { AuthGuard, Public } from '@noahspan/noahspan-modules';
|
import { AuthGuard } from '@noahspan/noahspan-modules';
|
||||||
import { LogInterceptor } from './log.interceptor';
|
import { LogInterceptor } from './interceptors/log.interceptor';
|
||||||
import { FileService } from '../file/file.service';
|
import { FileService } from '../file/file.service';
|
||||||
import { DeleteResult, InsertResult, UpdateResult } from 'typeorm';
|
import { FileInterceptor } from '@nestjs/platform-express';
|
||||||
import { Reflector } from '@nestjs/core';
|
|
||||||
import { Logs } from './logs.interface';
|
|
||||||
|
|
||||||
const reflector = new Reflector();
|
|
||||||
|
|
||||||
@Controller('logs')
|
@Controller('logs')
|
||||||
@UseInterceptors(new LogInterceptor(reflector))
|
|
||||||
export class LogController {
|
export class LogController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly fileService: FileService,
|
private readonly fileService: FileService,
|
||||||
private readonly logService: LogService
|
private readonly logService: LogService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Get()
|
|
||||||
@Public()
|
@Get(':partitionKey/:rowKey')
|
||||||
async findLogsWithCount(@Query('skip') skip?, @Query('take') take?: number,): Promise<Logs> {
|
@UseInterceptors(new LogInterceptor())
|
||||||
try {
|
|
||||||
return await this.logService.findLogsWithCount(skip, take)
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get('flights')
|
|
||||||
@Public()
|
|
||||||
async findLogsWithTracks(@Query('skip') skip?, @Query('take') take?: number,): Promise<Logs> {
|
|
||||||
try {
|
|
||||||
return await this.logService.findLogsWithTracks(skip, take)
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get(':id')
|
|
||||||
@Public()
|
|
||||||
async find(
|
async find(
|
||||||
@Param('id') id: string,
|
@Param('partitionKey') partitionKey: string,
|
||||||
): Promise<LogEntity> {
|
@Param('rowKey') rowKey: string
|
||||||
|
): Promise<Log> {
|
||||||
try {
|
try {
|
||||||
return await this.logService.find(id);
|
return await this.logService.find(partitionKey, rowKey);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
throw new HttpException(customError.message, customError.statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
@UseInterceptors(new LogInterceptor())
|
||||||
|
async findAll(): Promise<Log[]> {
|
||||||
|
try {
|
||||||
|
return await this.logService.findAll();
|
||||||
|
} catch (error) {
|
||||||
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
throw new HttpException(customError.message, customError.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
@UseGuards(AuthGuard)
|
async create(@Body() logDto: LogDto): Promise<Log> {
|
||||||
async create(@Body() logDto: LogDto) {
|
|
||||||
try {
|
try {
|
||||||
return await this.logService.create(logDto);
|
const log = new Log();
|
||||||
|
|
||||||
|
Object.assign(log, logDto);
|
||||||
|
|
||||||
|
return await this.logService.create(log);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
@@ -82,14 +73,19 @@ export class LogController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put(':id')
|
@UseGuards(AuthGuard)
|
||||||
@UseGuards(AuthGuard)
|
@Put(':partitionKey/:rowKey')
|
||||||
async update(
|
async update(
|
||||||
@Param('id') id: string,
|
@Param('partitionKey') partitionKey: string,
|
||||||
|
@Param('rowKey') rowKey: string,
|
||||||
@Body() logDto: LogDto
|
@Body() logDto: LogDto
|
||||||
) {
|
): Promise<Log> {
|
||||||
try {
|
try {
|
||||||
return await this.logService.update(id, logDto);
|
const log = new Log();
|
||||||
|
|
||||||
|
Object.assign(log, logDto);
|
||||||
|
|
||||||
|
return await this.logService.update(partitionKey, rowKey, log);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
@@ -97,13 +93,52 @@ export class LogController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
|
@Delete(':partitionKey/:rowKey')
|
||||||
async delete(
|
async delete(
|
||||||
@Param('id') id: string,
|
@Param('partitionKey') partitionKey: string,
|
||||||
): Promise<DeleteResult> {
|
@Param('rowKey') rowKey: string
|
||||||
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
return await this.logService.delete(id);
|
return await this.logService.delete(partitionKey, rowKey);
|
||||||
|
} catch (error) {
|
||||||
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
throw new HttpException(customError.message, customError.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@Post(':partitionKey/:rowKey/track')
|
||||||
|
@UseInterceptors(FileInterceptor('file'))
|
||||||
|
async createTrack(@Param('rowKey') rowKey: string, @UploadedFile() file: Express.Multer.File) {
|
||||||
|
try {
|
||||||
|
const containerName = 'tracks';
|
||||||
|
const url = await this.fileService.uploadFile(file, containerName, rowKey);
|
||||||
|
|
||||||
|
return { url }
|
||||||
|
} catch (error) {
|
||||||
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
throw new HttpException(customError.message, customError.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get(':partitionKey/:rowKey/track')
|
||||||
|
async downloadTrack(@Param('rowKey') rowKey: string, @Query('fileName') fileName: string): Promise<string> {
|
||||||
|
const containerName = 'tracks';
|
||||||
|
const downloadedFile: string = await this.fileService.downloadFile(containerName, rowKey, fileName)
|
||||||
|
|
||||||
|
return downloadedFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@Delete(':partitionKey/:rowKey/track')
|
||||||
|
async deleteTrack(@Param('rowKey') rowKey: string, @Query('fileName') fileName: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const containerName = 'tracks';
|
||||||
|
|
||||||
|
return await this.fileService.deleteFile(containerName, rowKey, fileName)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,27 @@
|
|||||||
export class LogDto {
|
export class LogDto {
|
||||||
pilotId: string;
|
pilotId: string;
|
||||||
date: Date;
|
pilotName: string;
|
||||||
|
date: string;
|
||||||
aircraftMakeModel: string;
|
aircraftMakeModel: string;
|
||||||
aircraftIdentity: string;
|
aircraftIdentity: string;
|
||||||
routeFrom: string;
|
routeFrom: string;
|
||||||
routeTo: string;
|
routeTo: string;
|
||||||
durationOfFlight?: number;
|
durationOfFlight: number;
|
||||||
singleEngineLand?: number;
|
singleEngineLand: string;
|
||||||
simulatorAtd?: number;
|
simulatorAtd: number;
|
||||||
landingsDay?: number;
|
landingsDay: number;
|
||||||
landingsNight?: number;
|
landingsNight: number;
|
||||||
instrumentActual?: number;
|
instrumentActual: number;
|
||||||
instrumentSimulated?: number;
|
instrumentSimulated: number;
|
||||||
instrumentApproaches?: number;
|
instrumentApproaches: number;
|
||||||
instrumentHolds?: number;
|
instrumentHolds: number;
|
||||||
instrumentNavTrack?: number;
|
instrumentNavTrack: number;
|
||||||
groundTrainingReceived?: number;
|
groundTrainingReceived: number;
|
||||||
flightTrainingReceived?: number;
|
flightTrainingReceived: number;
|
||||||
crossCountry?: number;
|
crossCountry: number;
|
||||||
night?: number;
|
night: number;
|
||||||
solo?: number;
|
solo: number;
|
||||||
pilotInCommand?: number;
|
pilotInCommand: number;
|
||||||
notes?: string;
|
tracks: string[];
|
||||||
tracks?: []
|
notes: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,165 +1,29 @@
|
|||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
export class Log {
|
||||||
import { TrackEntity } from '../track/track.entity';
|
partitionKey: string;
|
||||||
import { ColumnNumericTransformer } from '../transformers/columnNumeric.transformer';
|
rowKey: string;
|
||||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToMany } from 'typeorm';
|
|
||||||
|
|
||||||
@Entity({ name: 'logs' })
|
|
||||||
export class LogEntity {
|
|
||||||
@PrimaryGeneratedColumn('uuid')
|
|
||||||
id: string
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
pilotId: string;
|
pilotId: string;
|
||||||
|
pilotName: string;
|
||||||
@Column()
|
date: string;
|
||||||
date: Date;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
aircraftMakeModel: string;
|
aircraftMakeModel: string;
|
||||||
|
|
||||||
@Column()
|
|
||||||
aircraftIdentity: string;
|
aircraftIdentity: string;
|
||||||
|
|
||||||
@Column()
|
|
||||||
routeFrom: string;
|
routeFrom: string;
|
||||||
|
|
||||||
@Column()
|
|
||||||
routeTo: string;
|
routeTo: string;
|
||||||
|
durationOfFlight: number | null;
|
||||||
@Column('numeric', {
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer()
|
|
||||||
})
|
|
||||||
durationOfFlight: number;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
singleEngineLand: number | null;
|
singleEngineLand: number | null;
|
||||||
|
simulatorAtd?: number | null;
|
||||||
@Column('numeric', {
|
landingsDay?: number | null;
|
||||||
nullable: true,
|
landingsNight?: number | null;
|
||||||
precision: 10,
|
groundTrainingReceived?: number;
|
||||||
scale: 1,
|
flightTrainingReceived?: number;
|
||||||
transformer: new ColumnNumericTransformer(),
|
crossCountry?: number | null;
|
||||||
})
|
night?: number | null;
|
||||||
simulatorAtd: number | null;
|
solo?: number | null;
|
||||||
|
pilotInCommand?: number | null;
|
||||||
@Column('numeric', {
|
instrumentActual?: number | null;
|
||||||
nullable: true,
|
instrumentSimulated?: number | null;
|
||||||
precision: 10,
|
instrumentApproaches?: number | null;
|
||||||
scale: 1,
|
instrumentHolds?: number | null;
|
||||||
transformer: new ColumnNumericTransformer(),
|
instrumentNavTrack?: number | null;
|
||||||
})
|
tracks?: string[];
|
||||||
landingsDay: number | null;
|
notes?: string;
|
||||||
|
}
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
landingsNight: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
groundTrainingReceived: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
flightTrainingReceived: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
crossCountry: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
night: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
solo: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
pilotInCommand: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
instrumentActual: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
instrumentSimulated: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
instrumentApproaches: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
instrumentHolds: number | null;
|
|
||||||
|
|
||||||
@Column('numeric', {
|
|
||||||
nullable: true,
|
|
||||||
precision: 10,
|
|
||||||
scale: 1,
|
|
||||||
transformer: new ColumnNumericTransformer(),
|
|
||||||
})
|
|
||||||
instrumentNavTrack: number | null;
|
|
||||||
|
|
||||||
@Column({ nullable: true })
|
|
||||||
notes: string | null;
|
|
||||||
|
|
||||||
@OneToMany(() => TrackEntity, (track: TrackEntity) => track.log)
|
|
||||||
tracks: TrackEntity[]
|
|
||||||
|
|
||||||
@ManyToOne(() => PilotEntity, (pilot: PilotEntity) => pilot.logs, {onDelete: 'CASCADE', onUpdate: 'CASCADE'})
|
|
||||||
@JoinColumn({ name: 'pilotId' })
|
|
||||||
pilot: PilotEntity;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
import { CallHandler, ExecutionContext, NestInterceptor, UnauthorizedException } from '@nestjs/common';
|
|
||||||
import { Observable, map } from 'rxjs';
|
|
||||||
import { LogEntity } from './log.entity';
|
|
||||||
import { jwtDecode } from 'jwt-decode';
|
|
||||||
import { IS_PUBLIC_KEY } from '@noahspan/noahspan-modules';
|
|
||||||
import { Reflector } from '@nestjs/core';
|
|
||||||
|
|
||||||
export class LogInterceptor implements NestInterceptor {
|
|
||||||
constructor(private reflector: Reflector) {}
|
|
||||||
|
|
||||||
intercept(context: ExecutionContext, handler: CallHandler): Observable<any> {
|
|
||||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
||||||
context.getHandler(),
|
|
||||||
context.getClass(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return handler.handle().pipe(
|
|
||||||
map((data: any) => {
|
|
||||||
const req = context.switchToHttp().getRequest();
|
|
||||||
const limitData = (log: LogEntity) => {
|
|
||||||
return {
|
|
||||||
id: log.id,
|
|
||||||
pilot: {
|
|
||||||
name: log.pilot.name
|
|
||||||
},
|
|
||||||
date: log.date,
|
|
||||||
aircraftMakeModel: log.aircraftMakeModel,
|
|
||||||
routeFrom: log.routeFrom,
|
|
||||||
routeTo: log.routeTo,
|
|
||||||
durationOfFlight: log.durationOfFlight,
|
|
||||||
tracks: log.tracks,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.headers.authorization) {
|
|
||||||
const authHeader = req.headers.authorization;
|
|
||||||
const token = authHeader && authHeader.split(' ')[1];
|
|
||||||
const jwtPayload = jwtDecode(token);
|
|
||||||
const rolesKeyName = Object.keys(jwtPayload).find((key) => key.includes('roles'));
|
|
||||||
|
|
||||||
if (jwtPayload[rolesKeyName].includes('Flying.Read')) {
|
|
||||||
if (data.entities) {
|
|
||||||
const logs = data.entities.map((entity) => limitData(entity));
|
|
||||||
|
|
||||||
return {
|
|
||||||
entities: logs,
|
|
||||||
total: data.total,
|
|
||||||
hasNextPage: data.hasNextPage
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
const log = limitData(data);
|
|
||||||
|
|
||||||
return log;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
} else if (!req.headers.authorization && isPublic) {
|
|
||||||
if (data.entities) {
|
|
||||||
const publicData = data.entities.map((entity) => limitData(entity))
|
|
||||||
const logs = publicData.slice(0, 5)
|
|
||||||
|
|
||||||
return {
|
|
||||||
entities: logs,
|
|
||||||
total: logs.length,
|
|
||||||
hasNextPage: false
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
const publicData = limitData(data);
|
|
||||||
|
|
||||||
return publicData
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +1,28 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { LogController } from './log.controller';
|
import { LogController } from './log.controller';
|
||||||
import { LogService } from './log.service';
|
import { LogService } from './log.service';
|
||||||
import { LogEntity } from './log.entity';
|
import { AzureTableStorageModule } from '@noahspan/azure-database';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { Log } from './log.entity';
|
||||||
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { FileService } from '../file/file.service';
|
import { FileService } from '../file/file.service';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
import { PilotModule } from '../pilot/pilot.module';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
PilotModule,
|
AzureTableStorageModule.forRootAsync({
|
||||||
TypeOrmModule.forFeature([LogEntity])
|
imports: [ConfigModule],
|
||||||
|
useFactory: async (configService: ConfigService) => {
|
||||||
|
return {
|
||||||
|
connectionString: configService.get<string>('azureStorageConnectionString')
|
||||||
|
};
|
||||||
|
},
|
||||||
|
inject: [ConfigService]
|
||||||
|
}),
|
||||||
|
AzureTableStorageModule.forFeature(Log, {
|
||||||
|
createTableIfNotExists: false,
|
||||||
|
table: 'logs'
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
controllers: [LogController],
|
controllers: [LogController],
|
||||||
exports: [LogService],
|
|
||||||
providers: [
|
providers: [
|
||||||
ConfigService,
|
ConfigService,
|
||||||
FileService,
|
FileService,
|
||||||
|
|||||||
@@ -1,366 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { LogService } from './log.service';
|
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
||||||
import { LogEntity } from './log.entity';
|
|
||||||
import { LogDto } from './log.dto';
|
|
||||||
import { FileService } from '../file/file.service';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { PilotService } from '../pilot/pilot.service';
|
|
||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
|
||||||
|
|
||||||
describe('LogService', () => {
|
|
||||||
let service: LogService;
|
|
||||||
|
|
||||||
const mockFileService = {
|
|
||||||
deleteFolder: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockQueryBuilder = {
|
|
||||||
createQueryBuilder: jest.fn().mockReturnThis(),
|
|
||||||
innerJoinAndSelect: jest.fn().mockReturnThis(),
|
|
||||||
orderBy: jest.fn().mockReturnThis(),
|
|
||||||
skip: jest.fn().mockReturnThis(),
|
|
||||||
take: jest.fn().mockReturnThis(),
|
|
||||||
getManyAndCount: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockLogRepository = {
|
|
||||||
createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder),
|
|
||||||
delete: jest.fn(),
|
|
||||||
findAndCount: jest.fn(),
|
|
||||||
findOne: jest.fn(),
|
|
||||||
findOneBy: jest.fn(),
|
|
||||||
save: jest.fn(),
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockPilotRepository = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findAll: jest.fn(),
|
|
||||||
update: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [
|
|
||||||
ConfigService,
|
|
||||||
LogService,
|
|
||||||
PilotService,
|
|
||||||
{
|
|
||||||
provide: FileService,
|
|
||||||
useValue: mockFileService
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(LogEntity),
|
|
||||||
useValue: mockLogRepository
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(PilotEntity),
|
|
||||||
useValue: mockPilotRepository
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<LogService>(LogService);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should create a log entry', async () => {
|
|
||||||
const logDto = {
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test'
|
|
||||||
} as LogDto
|
|
||||||
|
|
||||||
jest.spyOn(mockLogRepository, 'save').mockReturnValue(logDto);
|
|
||||||
|
|
||||||
const result = await service.create(logDto);
|
|
||||||
|
|
||||||
expect(mockLogRepository.save).toHaveBeenCalled();
|
|
||||||
expect(mockLogRepository.save).toHaveBeenCalledWith(logDto);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should delete a log entry', async () => {
|
|
||||||
const id: string = '';
|
|
||||||
const log = {
|
|
||||||
id: 'd685f1ca-28e0-40b9-8713-74467db12965',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity;
|
|
||||||
|
|
||||||
jest.spyOn(mockFileService, 'deleteFolder').mockReturnValue(undefined);
|
|
||||||
jest.spyOn(mockLogRepository, 'delete').mockReturnValue(log);
|
|
||||||
|
|
||||||
const result = await service.delete(id);
|
|
||||||
|
|
||||||
expect(result).toEqual(log);
|
|
||||||
expect(mockLogRepository.delete).toHaveBeenCalled();
|
|
||||||
expect(mockLogRepository.delete).toHaveBeenCalledWith({ id: id })
|
|
||||||
})
|
|
||||||
|
|
||||||
it('find => should find a log entry by id', async () => {
|
|
||||||
const id: string = 'd685f1ca-28e0-40b9-8713-74467db12965';
|
|
||||||
const log = {
|
|
||||||
id: 'd685f1ca-28e0-40b9-8713-74467db12965',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity;
|
|
||||||
|
|
||||||
jest.spyOn(mockLogRepository, 'findOne').mockReturnValue(log);
|
|
||||||
|
|
||||||
const result = await service.find(id);
|
|
||||||
|
|
||||||
expect(result).toEqual(log);
|
|
||||||
expect(mockLogRepository.findOne).toHaveBeenCalled();
|
|
||||||
expect(mockLogRepository.findOne).toHaveBeenCalledWith({
|
|
||||||
relations: [
|
|
||||||
'pilot',
|
|
||||||
'tracks'
|
|
||||||
],
|
|
||||||
where: {
|
|
||||||
id: id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findLogsWithCount => should find log entries with count', async () => {
|
|
||||||
const log = {
|
|
||||||
id: 'd685f1ca-28e0-40b9-8713-74467db12965',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: [
|
|
||||||
{
|
|
||||||
"id": "4645ce0c-6fd7-432c-8089-777a7139cf7e",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KANE_KONA_20250628.kml",
|
|
||||||
"order": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "5ce26898-a18b-46f4-b989-3fb7acdace08",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KONA_KANE_20250628.kml",
|
|
||||||
"order": 2
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as LogEntity;
|
|
||||||
const logs = [log];
|
|
||||||
const count = 1
|
|
||||||
const morePages = false
|
|
||||||
|
|
||||||
jest.spyOn(mockQueryBuilder, 'getManyAndCount').mockReturnValue([logs, count, morePages]);
|
|
||||||
|
|
||||||
const {entities, total, hasNextPage} = await service.findLogsWithCount();
|
|
||||||
|
|
||||||
expect(entities).toEqual(logs);
|
|
||||||
expect(count).toEqual(total);
|
|
||||||
expect(hasNextPage).toEqual(morePages);
|
|
||||||
expect(mockQueryBuilder.getManyAndCount).toHaveBeenCalled();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findLogsWithTracks => should find log entries with tracks', async () => {
|
|
||||||
const log = {
|
|
||||||
id: 'd685f1ca-28e0-40b9-8713-74467db12965',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: [
|
|
||||||
{
|
|
||||||
"id": "4645ce0c-6fd7-432c-8089-777a7139cf7e",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KANE_KONA_20250628.kml",
|
|
||||||
"order": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "5ce26898-a18b-46f4-b989-3fb7acdace08",
|
|
||||||
"url": "http://127.0.0.1:10000/devstoreaccount1/tracks/0e7a641a-0264-47f0-88bf-7c296c842d8b/FlightAware_N70392_KONA_KANE_20250628.kml",
|
|
||||||
"order": 2
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as LogEntity;
|
|
||||||
const logs = [log];
|
|
||||||
const count = 1
|
|
||||||
const morePages = false
|
|
||||||
|
|
||||||
jest.spyOn(mockQueryBuilder, 'getManyAndCount').mockResolvedValue([logs, count, morePages])
|
|
||||||
|
|
||||||
const {entities, total, hasNextPage} = await service.findLogsWithTracks();
|
|
||||||
|
|
||||||
expect(entities).toEqual(logs);
|
|
||||||
expect(count).toEqual(total);
|
|
||||||
expect(hasNextPage).toEqual(morePages);
|
|
||||||
expect(mockQueryBuilder.getManyAndCount).toHaveBeenCalled();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should update a log entry', async () => {
|
|
||||||
const id: string = 'd685f1ca-28e0-40b9-8713-74467db12965';
|
|
||||||
const logDto = {
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test'
|
|
||||||
} as LogDto
|
|
||||||
const log = {
|
|
||||||
id: 'd685f1ca-28e0-40b9-8713-74467db12965',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity;
|
|
||||||
|
|
||||||
jest.spyOn(mockLogRepository, 'findOneBy').mockReturnValue(log);
|
|
||||||
jest.spyOn(mockLogRepository, 'save').mockReturnValue(logDto);
|
|
||||||
|
|
||||||
const result = await service.update(id, logDto);
|
|
||||||
|
|
||||||
expect(result).toEqual(logDto);
|
|
||||||
expect(mockLogRepository.findOneBy).toHaveBeenCalled();
|
|
||||||
expect(mockLogRepository.findOneBy).toHaveBeenCalledWith({ id: id });
|
|
||||||
expect(mockLogRepository.save).toHaveBeenCalled();
|
|
||||||
expect(mockLogRepository.save).toHaveBeenCalledWith(logDto);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,87 +1,34 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository, Repository } from '@noahspan/azure-database';
|
||||||
import { LogEntity } from './log.entity';
|
import { Log } from './log.entity';
|
||||||
import { DeleteResult, InsertResult, Repository, UpdateResult } from 'typeorm';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { LogDto } from './log.dto';
|
|
||||||
import { PilotService } from '../pilot/pilot.service';
|
|
||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
|
||||||
import { CustomError } from '../error/customError';
|
|
||||||
import { FileService } from '../file/file.service';
|
|
||||||
import { Logs } from './logs.interface';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LogService {
|
export class LogService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(LogEntity) private readonly logRepository: Repository<LogEntity>,
|
@InjectRepository(Log) private readonly logRepository: Repository<Log>
|
||||||
private readonly fileService: FileService,
|
|
||||||
private readonly pilotService: PilotService,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async find(id: string): Promise<LogEntity> {
|
async find(partitionKey: string, rowKey: string): Promise<Log> {
|
||||||
return await this.logRepository.findOne({
|
return await this.logRepository.find(partitionKey, rowKey);
|
||||||
where: { id: id },
|
|
||||||
relations: ['pilot', 'tracks']
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findLogsWithCount(skip?: number, take?: number): Promise<Logs> {
|
async findAll(): Promise<Log[]> {
|
||||||
// const [entities, total] = await this.logRepository.findAndCount({
|
return await this.logRepository.findAll();
|
||||||
// take,
|
|
||||||
// skip,
|
|
||||||
// order: {
|
|
||||||
// date: 'DESC'
|
|
||||||
// },
|
|
||||||
// relations: ['pilot', 'tracks']
|
|
||||||
// })
|
|
||||||
|
|
||||||
const [entities, total] = await this.logRepository
|
|
||||||
.createQueryBuilder('logs')
|
|
||||||
.innerJoinAndSelect('logs.pilot', 'pilot')
|
|
||||||
.orderBy('logs.date', 'DESC')
|
|
||||||
.skip(skip)
|
|
||||||
.take(take)
|
|
||||||
.getManyAndCount()
|
|
||||||
|
|
||||||
return {
|
|
||||||
entities,
|
|
||||||
total,
|
|
||||||
hasNextPage: skip + take < total
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findLogsWithTracks(skip?: number, take?: number): Promise<Logs> {
|
async create(log: Log): Promise<Log> {
|
||||||
const [entities, total] = await this.logRepository
|
log.partitionKey = 'log';
|
||||||
.createQueryBuilder('logs')
|
log.rowKey = uuidv4();
|
||||||
.innerJoinAndSelect('logs.tracks', 'track')
|
|
||||||
.innerJoinAndSelect('logs.pilot', 'pilot')
|
|
||||||
.orderBy('logs.date', 'DESC')
|
|
||||||
.skip(skip)
|
|
||||||
.take(take)
|
|
||||||
.getManyAndCount();
|
|
||||||
|
|
||||||
return {
|
return await this.logRepository.create(log);
|
||||||
entities,
|
|
||||||
total,
|
|
||||||
hasNextPage: skip + take < total
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(logDto: LogDto): Promise<LogDto> {
|
async update(partitionKey: string, rowKey: string, log: Log): Promise<Log> {
|
||||||
return this.logRepository.save(logDto);
|
return await this.logRepository.update(partitionKey, rowKey, log);
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, logDto: LogDto): Promise<LogDto> {
|
async delete(partitionKey: string, rowKey: string): Promise<void> {
|
||||||
const logEntity: LogEntity = await this.logRepository.findOneBy({ id });
|
await this.logRepository.delete(partitionKey, rowKey);
|
||||||
const logEntityUpdated = Object.assign(logEntity, logDto)
|
|
||||||
|
|
||||||
delete logEntityUpdated.tracks;
|
|
||||||
|
|
||||||
return await this.logRepository.save(logEntityUpdated);
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(id: string): Promise<DeleteResult> {
|
|
||||||
await this.fileService.deleteFolder('tracks', id);
|
|
||||||
|
|
||||||
return await this.logRepository.delete({ id });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
import { LogEntity } from "src/log/log.entity";
|
|
||||||
|
|
||||||
export interface Logs {
|
|
||||||
entities: LogEntity[],
|
|
||||||
total: number,
|
|
||||||
hasNextPage: boolean
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,7 @@ import { AppModule } from './app.module';
|
|||||||
import { HttpService } from '@nestjs/axios';
|
import { HttpService } from '@nestjs/axios';
|
||||||
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
||||||
import { InternalServerErrorException } from '@nestjs/common';
|
import { InternalServerErrorException } from '@nestjs/common';
|
||||||
import * as session from 'express-session';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const httpService = new HttpService();
|
const httpService = new HttpService();
|
||||||
@@ -12,13 +12,6 @@ async function bootstrap() {
|
|||||||
app.enableCors();
|
app.enableCors();
|
||||||
app.setGlobalPrefix('api');
|
app.setGlobalPrefix('api');
|
||||||
app.useGlobalFilters(new HttpExceptionFilter());
|
app.useGlobalFilters(new HttpExceptionFilter());
|
||||||
app.use(
|
|
||||||
session({
|
|
||||||
secret: process.env.SESSION_SECRET,
|
|
||||||
resave: false,
|
|
||||||
saveUninitialized: false
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
httpService.axiosRef.interceptors.response.use(
|
httpService.axiosRef.interceptors.response.use(
|
||||||
(response) => {
|
(response) => {
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
import { Body, Controller, Delete, Get, HttpException, Param, Post, Put, UseGuards } from "@nestjs/common";
|
|
||||||
import { MedicalService } from "./medical.service";
|
|
||||||
import { CustomError } from "src/error/customError";
|
|
||||||
import { MedicalDto } from "./medical.dto";
|
|
||||||
import { AuthGuard } from "@noahspan/noahspan-modules";
|
|
||||||
|
|
||||||
|
|
||||||
@Controller('medical')
|
|
||||||
export class MedicalController {
|
|
||||||
constructor(private readonly medicalService: MedicalService) {}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Get(':id')
|
|
||||||
async find(@Param('id') id: string) {
|
|
||||||
try {
|
|
||||||
return await this.medicalService.find(id)
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Get()
|
|
||||||
async findAll() {
|
|
||||||
try {
|
|
||||||
return await this.medicalService.findAll();
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Post()
|
|
||||||
async create(@Body() medicalDto: MedicalDto) {
|
|
||||||
try {
|
|
||||||
return await this.medicalService.create(medicalDto);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Put(':id')
|
|
||||||
async update(@Param('id') id: string, @Body() medicalDto: MedicalDto) {
|
|
||||||
try {
|
|
||||||
return await this.medicalService.update(id, medicalDto);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Delete(':id')
|
|
||||||
async delete(@Param('id') id: string) {
|
|
||||||
try {
|
|
||||||
return await this.medicalService.delete(id);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
export class MedicalDto {
|
|
||||||
id: string;
|
|
||||||
class: string;
|
|
||||||
expirationDate: string;
|
|
||||||
pilotId: string;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
|
||||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
|
||||||
|
|
||||||
@Entity({ name: 'medical' })
|
|
||||||
export class MedicalEntity {
|
|
||||||
@PrimaryGeneratedColumn('uuid')
|
|
||||||
id: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
class: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
expirationDate: Date;
|
|
||||||
|
|
||||||
@ManyToOne(() => PilotEntity, (pilot: PilotEntity) => pilot.medical, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'pilotId' })
|
|
||||||
pilot: PilotEntity;
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
|
|
||||||
import { MedicalEntity } from "./medical.entity";
|
|
||||||
import { Injectable } from "@nestjs/common";
|
|
||||||
import { InjectRepository } from "@nestjs/typeorm";
|
|
||||||
import { DeleteResult, InsertResult, Repository, UpdateResult } from 'typeorm';
|
|
||||||
import { MedicalDto } from "./medical.dto";
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class MedicalService {
|
|
||||||
constructor(
|
|
||||||
@InjectRepository(MedicalEntity) private readonly medicalRepository: Repository<MedicalEntity>
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async find(id: string): Promise<MedicalEntity> {
|
|
||||||
return await this.medicalRepository.findOneBy({ id });
|
|
||||||
}
|
|
||||||
|
|
||||||
async findAll(): Promise<MedicalEntity[]> {
|
|
||||||
return await this.medicalRepository.find();
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(medical: MedicalDto): Promise<InsertResult> {
|
|
||||||
return await this.medicalRepository.insert(medical)
|
|
||||||
}
|
|
||||||
|
|
||||||
async update(id: string, medical: MedicalDto): Promise<UpdateResult> {
|
|
||||||
return await this.medicalRepository.update(id, medical);
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(id: string): Promise<DeleteResult> {
|
|
||||||
return await this.medicalRepository.delete({ id });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
5
api/src/pilot/certificate/certificate.entity.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export class Certificate {
|
||||||
|
type: string;
|
||||||
|
issueDate: string;
|
||||||
|
number: string;
|
||||||
|
}
|
||||||
4
api/src/pilot/endorsement/endorsement.entity.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export class Endorsement {
|
||||||
|
type: string;
|
||||||
|
issueDate: Date;
|
||||||
|
}
|
||||||
35
api/src/pilot/interceptors/pilot.interceptor.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
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,
|
||||||
|
certificates: pilot.certificates,
|
||||||
|
endorsements: pilot.endorsements
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return pilots;
|
||||||
|
} else {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler.handle().pipe(map((data) => data));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,260 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { PilotController } from "./pilot.controller";
|
|
||||||
import { PilotService } from './pilot.service';
|
|
||||||
import { PilotDto } from './pilot.dto';
|
|
||||||
import { PilotEntity } from './pilot.entity';
|
|
||||||
import { CustomError } from '../error/customError';
|
|
||||||
import { HttpException } from '@nestjs/common';
|
|
||||||
|
|
||||||
describe('PilotController', () => {
|
|
||||||
let controller: PilotController;
|
|
||||||
|
|
||||||
const mockPilotService = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findAll: jest.fn(),
|
|
||||||
update: jest.fn(),
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [PilotController],
|
|
||||||
providers: [
|
|
||||||
{
|
|
||||||
provide: PilotService,
|
|
||||||
useValue: mockPilotService
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<PilotController>(PilotController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('find => should find a pilot by id', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity;
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'find').mockReturnValue(pilot);
|
|
||||||
|
|
||||||
const result = await controller.find(id);
|
|
||||||
|
|
||||||
expect(result).toEqual(pilot);
|
|
||||||
expect(mockPilotService.find).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.find).toHaveBeenCalledWith(id)
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
it('find => should fail to find a pilot by id', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'find').mockRejectedValue(new Error('Pilot not found'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.find(id);
|
|
||||||
|
|
||||||
fail('find did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error)
|
|
||||||
expect(mockPilotService.find).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.find).toHaveBeenCalledWith(id);
|
|
||||||
expect(mockPilotService.find).rejects.toThrow('Pilot not found')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should find all pilots', async () => {
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity;
|
|
||||||
const pilots = [pilot];
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'findAll').mockReturnValue(pilots);
|
|
||||||
|
|
||||||
const result = await controller.findAll();
|
|
||||||
|
|
||||||
expect(result).toEqual(pilots);
|
|
||||||
expect(mockPilotService.findAll).toHaveBeenCalled();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should fail to find all pilots', async () => {
|
|
||||||
jest.spyOn(mockPilotService, 'findAll').mockRejectedValue(new Error('Pilots not found'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.findAll();
|
|
||||||
|
|
||||||
fail('findAll did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error)
|
|
||||||
expect(mockPilotService.findAll).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.findAll).rejects.toThrow('Pilots not found')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should create a new pilot', async () => {
|
|
||||||
const pilotDto = {
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234'
|
|
||||||
} as PilotDto
|
|
||||||
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'create').mockReturnValue(pilot);
|
|
||||||
|
|
||||||
const result = await controller.create(pilotDto);
|
|
||||||
|
|
||||||
expect(mockPilotService.create).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.create).toHaveBeenCalledWith(pilotDto)
|
|
||||||
expect(result).toEqual(pilot);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should fail to create a new pilot', async () => {
|
|
||||||
const pilotDto = {
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234'
|
|
||||||
} as PilotDto
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'create').mockRejectedValue(new Error('Pilot failed to create'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.create(pilotDto);
|
|
||||||
|
|
||||||
fail('create did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error)
|
|
||||||
expect(mockPilotService.create).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.create).toHaveBeenCalledWith(pilotDto);
|
|
||||||
expect(mockPilotService.create).rejects.toThrow('Pilot failed to create')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should update an existing pilot', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
const pilotDto = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234'
|
|
||||||
} as PilotDto
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'update').mockReturnValue(pilotDto);
|
|
||||||
|
|
||||||
const result = await controller.update(id, pilotDto);
|
|
||||||
|
|
||||||
expect(result).toEqual(pilotDto);
|
|
||||||
expect(mockPilotService.update).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.update).toHaveBeenCalledWith(id, pilotDto)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should fail to update an existing pilot', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
const pilotDto = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234'
|
|
||||||
} as PilotDto
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'update').mockRejectedValue(new Error('Pilot failed to update'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.update(id, pilotDto);
|
|
||||||
|
|
||||||
fail('update function did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error)
|
|
||||||
expect(mockPilotService.update).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.update).toHaveBeenCalledWith(id, pilotDto);
|
|
||||||
expect(mockPilotService.update).rejects.toThrow('Pilot failed to update')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should delete an existing pilot', async () => {
|
|
||||||
const id = '39465ae3-7947-4cc2-b565-ca00a982fdd8';
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'delete');
|
|
||||||
|
|
||||||
const result = await controller.delete(id);
|
|
||||||
|
|
||||||
expect(mockPilotService.delete).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.delete).toHaveBeenCalledWith(id);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should fail to delete an existing pilot', async () => {
|
|
||||||
const id = '39465ae3-7947-4cc2-b565-ca00a982fdd8';
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotService, 'delete').mockRejectedValue(new Error('Pilot failed to delete'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.delete(id);
|
|
||||||
|
|
||||||
fail('delete function did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error)
|
|
||||||
expect(mockPilotService.delete).toHaveBeenCalled();
|
|
||||||
expect(mockPilotService.delete).toHaveBeenCalledWith(id);
|
|
||||||
expect(mockPilotService.delete).rejects.toThrow('Pilot failed to delete')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -11,24 +11,24 @@ import {
|
|||||||
UseInterceptors,
|
UseInterceptors,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { PilotDto } from './pilot.dto';
|
import { PilotDto } from './pilot.dto';
|
||||||
|
import { Pilot } from './pilot.entity';
|
||||||
import { PilotService } from './pilot.service';
|
import { PilotService } from './pilot.service';
|
||||||
import { CustomError } from '../error/customError';
|
import { CustomError } from '../error/customError';
|
||||||
import { PilotInterceptor } from './pilot.interceptor';
|
import { AuthGuard } from '@noahspan/noahspan-modules'
|
||||||
import { AuthGuard, Public } from '@noahspan/noahspan-modules';
|
import { PilotInterceptor } from './interceptors/pilot.interceptor';
|
||||||
import { Reflector } from '@nestjs/core';
|
|
||||||
|
|
||||||
const reflector = new Reflector();
|
|
||||||
|
|
||||||
@Controller('pilots')
|
@Controller('pilots')
|
||||||
@UseInterceptors(new PilotInterceptor(reflector))
|
@UseInterceptors(new PilotInterceptor())
|
||||||
export class PilotController {
|
export class PilotController {
|
||||||
constructor(private readonly pilotService: PilotService) {}
|
constructor(private readonly pilotService: PilotService) {}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':partitionKey/:rowKey')
|
||||||
@Public()
|
async find(
|
||||||
async find(@Param('id') id: string) {
|
@Param('partitionKey') partitionKey: string,
|
||||||
|
@Param('rowKey') rowKey: string
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
return await this.pilotService.find(id);
|
return await this.pilotService.find(partitionKey, rowKey);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
@@ -37,7 +37,6 @@ export class PilotController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@Public()
|
|
||||||
async findAll() {
|
async findAll() {
|
||||||
try {
|
try {
|
||||||
return await this.pilotService.findAll();
|
return await this.pilotService.findAll();
|
||||||
@@ -48,11 +47,30 @@ export class PilotController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
async create(@Body() pilotDto: PilotDto): Promise<PilotDto> {
|
@Post()
|
||||||
|
async create(@Body() pilotDto: PilotDto) {
|
||||||
try {
|
try {
|
||||||
return await this.pilotService.create(pilotDto);
|
let pilot = new Pilot();
|
||||||
|
|
||||||
|
pilot = {
|
||||||
|
partitionKey: pilotDto.partitionKey,
|
||||||
|
rowKey: pilotDto.rowKey,
|
||||||
|
id: pilotDto.id,
|
||||||
|
name: pilotDto.name,
|
||||||
|
address: pilotDto.address,
|
||||||
|
city: pilotDto.city,
|
||||||
|
state: pilotDto.state,
|
||||||
|
postalCode: pilotDto.postalCode,
|
||||||
|
email: pilotDto.email,
|
||||||
|
phone: pilotDto.phone,
|
||||||
|
medicalClass: pilotDto.medicalClass,
|
||||||
|
medicalExpiration: pilotDto.medicalExpiration,
|
||||||
|
certificates: JSON.stringify(pilotDto.certificates),
|
||||||
|
endorsements: JSON.stringify(pilotDto.endorsements)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.pilotService.create(pilot);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
@@ -60,14 +78,34 @@ export class PilotController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put(':id')
|
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
|
@Put(':partitionKey/:rowKey')
|
||||||
async update(
|
async update(
|
||||||
@Param('id') id: string,
|
@Param('partitionKey') partitionKey: string,
|
||||||
|
@Param('rowKey') rowKey: string,
|
||||||
@Body() pilotDto: PilotDto
|
@Body() pilotDto: PilotDto
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
return await this.pilotService.update(id, pilotDto);
|
let pilot = new Pilot();
|
||||||
|
|
||||||
|
pilot = {
|
||||||
|
partitionKey: pilotDto.partitionKey,
|
||||||
|
rowKey: pilotDto.rowKey,
|
||||||
|
id: pilotDto.id,
|
||||||
|
name: pilotDto.name,
|
||||||
|
address: pilotDto.address,
|
||||||
|
city: pilotDto.city,
|
||||||
|
state: pilotDto.state,
|
||||||
|
postalCode: pilotDto.postalCode,
|
||||||
|
email: pilotDto.email,
|
||||||
|
phone: pilotDto.phone,
|
||||||
|
medicalClass: pilotDto.medicalClass,
|
||||||
|
medicalExpiration: pilotDto.medicalExpiration,
|
||||||
|
certificates: JSON.stringify(pilotDto.certificates),
|
||||||
|
endorsements: JSON.stringify(pilotDto.endorsements)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.pilotService.update(partitionKey, rowKey, pilot);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
@@ -75,13 +113,14 @@ export class PilotController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
|
@Delete(':partitionKey/:rowKey')
|
||||||
async delete(
|
async delete(
|
||||||
@Param('id') id: string,
|
@Param('partitionKey') partitionKey: string,
|
||||||
|
@Param('rowKey') rowKey: string
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
return await this.pilotService.delete(id);
|
return await this.pilotService.delete(partitionKey, rowKey);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const customError = error as CustomError;
|
const customError = error as CustomError;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
|
import { Certificate } from "./certificate/certificate.entity";
|
||||||
|
import { Endorsement } from "./endorsement/endorsement.entity";
|
||||||
|
|
||||||
export class PilotDto {
|
export class PilotDto {
|
||||||
|
partitionKey: string;
|
||||||
|
rowKey: string;
|
||||||
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
address: string;
|
address: string;
|
||||||
city: string;
|
city: string;
|
||||||
@@ -6,4 +12,8 @@ export class PilotDto {
|
|||||||
postalCode: string;
|
postalCode: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
phone?: string;
|
phone?: string;
|
||||||
|
medicalClass?: string;
|
||||||
|
medicalExpiration?: string;
|
||||||
|
certificates: Certificate;
|
||||||
|
endorsements: Endorsement
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +1,18 @@
|
|||||||
import { LogEntity } from '../log/log.entity';
|
import { EntityString } from '@noahspan/azure-database';
|
||||||
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
|
|
||||||
import { CertificateEntity } from '../certificate/certificate.entity';
|
|
||||||
import { EndorsementEntity } from '../endorsement/endorsement.entity';
|
|
||||||
import { MedicalEntity } from '../medical/medical.entity';
|
|
||||||
|
|
||||||
@Entity({ name: 'pilots' })
|
export class Pilot {
|
||||||
export class PilotEntity {
|
@EntityString() partitionKey: string;
|
||||||
@PrimaryGeneratedColumn('uuid')
|
@EntityString() rowKey: string;
|
||||||
id: string;
|
@EntityString() id: string;
|
||||||
|
@EntityString() name: string;
|
||||||
@Column()
|
@EntityString() address?: string;
|
||||||
name: string;
|
@EntityString() city?: string;
|
||||||
|
@EntityString() state?: string;
|
||||||
@Column()
|
@EntityString() postalCode?: string;
|
||||||
address: string
|
@EntityString() email?: string;
|
||||||
|
@EntityString() phone?: string;
|
||||||
@Column()
|
@EntityString() medicalClass?: string;
|
||||||
city: string;
|
@EntityString() medicalExpiration: string;
|
||||||
|
@EntityString() certificates: string;
|
||||||
@Column()
|
@EntityString() endorsements: string;
|
||||||
state: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
postalCode: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
email: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
phone: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
userId: string | null;
|
|
||||||
|
|
||||||
@OneToMany(() => LogEntity, (log: LogEntity) => log.pilot)
|
|
||||||
logs: LogEntity[];
|
|
||||||
|
|
||||||
@OneToMany(() => CertificateEntity, (certificate: CertificateEntity) => certificate.pilot)
|
|
||||||
certificates: CertificateEntity[];
|
|
||||||
|
|
||||||
@OneToMany(() => EndorsementEntity, (endorsement: EndorsementEntity) => endorsement.pilot)
|
|
||||||
endorsements: EndorsementEntity[];
|
|
||||||
|
|
||||||
@OneToMany(() => MedicalEntity, (medical: MedicalEntity) => medical.pilot)
|
|
||||||
medical: MedicalEntity[];
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
|
||||||
import { jwtDecode } from 'jwt-decode';
|
|
||||||
import { Observable, map } from 'rxjs';
|
|
||||||
import { PilotEntity } from './pilot.entity';
|
|
||||||
import { IS_PUBLIC_KEY } from '@noahspan/noahspan-modules';
|
|
||||||
import { Reflector } from '@nestjs/core';
|
|
||||||
|
|
||||||
export class PilotInterceptor implements NestInterceptor {
|
|
||||||
constructor(private reflector: Reflector) {}
|
|
||||||
|
|
||||||
intercept(context: ExecutionContext, handler: CallHandler): Observable<any> {
|
|
||||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
||||||
context.getHandler(),
|
|
||||||
context.getClass(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return handler.handle().pipe(
|
|
||||||
map((data: PilotEntity[]) => {
|
|
||||||
const req = context.switchToHttp().getRequest();
|
|
||||||
const limitData = (data) => {
|
|
||||||
return data.map((pilot: PilotEntity) => {
|
|
||||||
return {
|
|
||||||
id: pilot.id,
|
|
||||||
name: pilot.name
|
|
||||||
};
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.headers.authorization) {
|
|
||||||
const authHeader = req.headers.authorization;
|
|
||||||
const token = authHeader && authHeader.split(' ')[1];
|
|
||||||
|
|
||||||
const jwtPayload = jwtDecode(token);
|
|
||||||
const rolesKeyName = Object.keys(jwtPayload).find((key) => key.includes('roles'));
|
|
||||||
|
|
||||||
if (jwtPayload[rolesKeyName].includes('Flying.Read')) {
|
|
||||||
const pilots = limitData(data)
|
|
||||||
|
|
||||||
return pilots;
|
|
||||||
} else {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
} else if (!req.headers.authorization && isPublic) {
|
|
||||||
const publicData = limitData(data);
|
|
||||||
const logs = publicData.slice(0,5)
|
|
||||||
|
|
||||||
return logs;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,41 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { PilotController } from './pilot.controller';
|
import { PilotController } from './pilot.controller';
|
||||||
import { PilotService } from './pilot.service';
|
import { PilotService } from './pilot.service';
|
||||||
import { PilotEntity } from './pilot.entity';
|
import { AzureTableStorageModule } from '@noahspan/azure-database';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { Pilot } from './pilot.entity';
|
||||||
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
import { Log } from 'src/log/log.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forFeature([PilotEntity])
|
AzureTableStorageModule.forRootAsync({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
useFactory: async (configService: ConfigService) => {
|
||||||
|
return {
|
||||||
|
connectionString: configService.get<string>('azureStorageConnectionString')
|
||||||
|
};
|
||||||
|
},
|
||||||
|
inject: [ConfigService]
|
||||||
|
}),
|
||||||
|
AzureTableStorageModule.forFeature(Log, {
|
||||||
|
createTableIfNotExists: false,
|
||||||
|
table: 'logs'
|
||||||
|
}),
|
||||||
|
AzureTableStorageModule.forRootAsync({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
useFactory: async (configService: ConfigService) => {
|
||||||
|
return {
|
||||||
|
connectionString: configService.get<string>('azureStorageConnectionString')
|
||||||
|
};
|
||||||
|
},
|
||||||
|
inject: [ConfigService]
|
||||||
|
}),
|
||||||
|
AzureTableStorageModule.forFeature(Pilot, {
|
||||||
|
createTableIfNotExists: false,
|
||||||
|
table: 'pilots'
|
||||||
|
})
|
||||||
],
|
],
|
||||||
controllers: [PilotController],
|
controllers: [PilotController],
|
||||||
exports: [PilotService],
|
|
||||||
providers: [PilotService]
|
providers: [PilotService]
|
||||||
})
|
})
|
||||||
export class PilotModule {}
|
export class PilotModule {}
|
||||||
|
|||||||
@@ -1,206 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { PilotService } from './pilot.service';
|
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
||||||
import { PilotEntity } from './pilot.entity';
|
|
||||||
import { PilotDto } from './pilot.dto';
|
|
||||||
import { CustomError } from '../error/customError';
|
|
||||||
|
|
||||||
describe('PilotService', () => {
|
|
||||||
let service: PilotService;
|
|
||||||
|
|
||||||
const mockPilotRepository = {
|
|
||||||
delete: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findOneBy: jest.fn(),
|
|
||||||
save: jest.fn(),
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [
|
|
||||||
PilotService,
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(PilotEntity),
|
|
||||||
useValue: mockPilotRepository
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<PilotService>(PilotService);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('find => should find one pilot by id', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity;
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotRepository, 'findOneBy').mockReturnValue(pilot);
|
|
||||||
|
|
||||||
const result = await service.find(id);
|
|
||||||
|
|
||||||
expect(result).toEqual(pilot);
|
|
||||||
expect(mockPilotRepository.findOneBy).toHaveBeenCalled();
|
|
||||||
expect(mockPilotRepository.findOneBy).toHaveBeenCalledWith({ id: id });
|
|
||||||
})
|
|
||||||
|
|
||||||
it('find => should fail to find one pilot by id', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8';
|
|
||||||
const mockCustomError = new CustomError('Pilot not found', 'Not found', 404)
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotRepository, 'findOneBy').mockReturnValue(undefined);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.find(id);
|
|
||||||
|
|
||||||
fail('find did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(CustomError);
|
|
||||||
expect(mockPilotRepository.findOneBy).toHaveBeenCalled();
|
|
||||||
expect(mockPilotRepository.findOneBy).toHaveBeenCalledWith({ id: id });
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should find all pilots', async () => {
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity;
|
|
||||||
const pilots = [pilot];
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotRepository, 'find').mockReturnValue(pilots);
|
|
||||||
|
|
||||||
const result = await service.findAll();
|
|
||||||
|
|
||||||
expect(result).toEqual(pilots);
|
|
||||||
expect(mockPilotRepository.find).toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should create a new pilot', async () => {
|
|
||||||
const pilotDto = {
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234'
|
|
||||||
} as PilotDto
|
|
||||||
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotRepository, 'save').mockReturnValue(pilot);
|
|
||||||
|
|
||||||
const result = await service.create(pilotDto);
|
|
||||||
|
|
||||||
expect(mockPilotRepository.save).toHaveBeenCalled();
|
|
||||||
expect(mockPilotRepository.save).toHaveBeenCalledWith(pilotDto);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should update a pilot', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
const pilotDto = {
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234'
|
|
||||||
} as PilotDto
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotRepository, 'findOneBy').mockReturnValue(pilot)
|
|
||||||
jest.spyOn(mockPilotRepository, 'save').mockReturnValue(pilotDto);
|
|
||||||
|
|
||||||
const result = await service.update(id, pilotDto)
|
|
||||||
|
|
||||||
expect(result).toEqual(pilotDto);
|
|
||||||
expect(mockPilotRepository.findOneBy).toHaveBeenCalled();
|
|
||||||
expect(mockPilotRepository.findOneBy).toHaveBeenCalledWith({ id: id });
|
|
||||||
expect(mockPilotRepository.save).toHaveBeenCalled();
|
|
||||||
expect(mockPilotRepository.save).toHaveBeenCalledWith(pilotDto)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should delete a pilot', async () => {
|
|
||||||
const id: string = '39465ae3-7947-4cc2-b565-ca00a982fdd8'
|
|
||||||
const pilot = {
|
|
||||||
id: '39465ae3-7947-4cc2-b565-ca00a982fdd8',
|
|
||||||
name: 'Test pilot',
|
|
||||||
address: '123 Any Street',
|
|
||||||
city: 'Any Town',
|
|
||||||
state: 'Minnesota',
|
|
||||||
postalCode: '55123',
|
|
||||||
email: 'user@example.com',
|
|
||||||
phone: '555-555-1234',
|
|
||||||
userId: 'user@example.com',
|
|
||||||
logs: [],
|
|
||||||
certificates: [],
|
|
||||||
endorsements: [],
|
|
||||||
medical: []
|
|
||||||
} as PilotEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockPilotRepository, 'delete').mockReturnValue(pilot);
|
|
||||||
|
|
||||||
const result = await service.delete(id);
|
|
||||||
|
|
||||||
expect(result).toEqual(pilot);
|
|
||||||
expect(mockPilotRepository.delete).toHaveBeenCalled();
|
|
||||||
expect(mockPilotRepository.delete).toHaveBeenCalledWith({ id: id });
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,49 +1,54 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { InjectRepository, Repository } from '@noahspan/azure-database';
|
||||||
import { PilotEntity } from './pilot.entity';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { Pilot } from './pilot.entity';
|
||||||
import { DeleteResult, InsertResult, Repository, UpdateResult } from 'typeorm';
|
import { Log } from 'src/log/log.entity';
|
||||||
import { PilotDto } from './pilot.dto';
|
import { LogService } from 'src/log/log.service';
|
||||||
import { CustomError } from '../error/customError';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PilotService {
|
export class PilotService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(PilotEntity) private readonly pilotRepository: Repository<PilotEntity>
|
@InjectRepository(Pilot) private readonly pilotRepository: Repository<Pilot>,
|
||||||
|
@InjectRepository(Log) private readonly logRepository: Repository<Log>
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async find(id: string): Promise<PilotEntity> {
|
async find(partitionKey: string, rowKey: string): Promise<Pilot> {
|
||||||
try {
|
return await this.pilotRepository.find(partitionKey, rowKey);
|
||||||
const pilotEntity: PilotEntity = await this.pilotRepository.findOneBy({ id });
|
|
||||||
|
|
||||||
if (pilotEntity) {
|
|
||||||
return pilotEntity
|
|
||||||
} else {
|
|
||||||
throw new CustomError('Pilot not found', 'Not found', 404)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAll(): Promise<PilotEntity[]> {
|
async findAll(): Promise<Pilot[]> {
|
||||||
return await this.pilotRepository.find();
|
return await this.pilotRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(pilot: PilotDto): Promise<PilotDto> {
|
async create(pilot: Pilot): Promise<Pilot> {
|
||||||
return await this.pilotRepository.save(pilot);
|
// try {
|
||||||
|
// return await this.pilotRepository.create(pilot);
|
||||||
|
// } catch (error) {
|
||||||
|
// throw new Error(error);
|
||||||
|
// }
|
||||||
|
return await this.pilotRepository.create(pilot);
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
id: string,
|
partitionKey: string,
|
||||||
pilotDto: PilotDto
|
rowKey: string,
|
||||||
): Promise<PilotDto> {
|
pilot: Pilot
|
||||||
const pilotEntity: PilotEntity = await this.pilotRepository.findOneBy({ id })
|
): Promise<Pilot> {
|
||||||
const pilotEntityUpdated = Object.assign(pilotEntity, pilotDto)
|
return await this.pilotRepository.update(partitionKey, rowKey, pilot);
|
||||||
|
|
||||||
return await this.pilotRepository.save(pilotEntityUpdated);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string): Promise<DeleteResult> {
|
async delete(partitionKey: string, rowKey: string): Promise<void> {
|
||||||
return await this.pilotRepository.delete({ id });
|
const pilotLogs: Log[] = await this.logRepository.findAll({
|
||||||
|
queryOptions: {
|
||||||
|
filter: `pilotId eq '${rowKey}'`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const pilotLog of pilotLogs) {
|
||||||
|
await this.logRepository.delete(pilotLog.partitionKey, pilotLog.rowKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.pilotRepository.delete(partitionKey, rowKey);
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,218 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { TrackController } from './track.controller';
|
|
||||||
import { TrackService } from './track.service';
|
|
||||||
import { TrackDto } from './track.dto';
|
|
||||||
import { TrackEntity } from './track.entity';
|
|
||||||
import { FileService } from '../file/file.service';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { HttpException } from '@nestjs/common';
|
|
||||||
import { Readable } from 'stream';
|
|
||||||
import { DeleteResult, InsertResult } from 'typeorm';
|
|
||||||
|
|
||||||
describe('TrackController', () => {
|
|
||||||
let controller: TrackController;
|
|
||||||
|
|
||||||
const mockTrackService = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
downloadTrackFile: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findAll: jest.fn(),
|
|
||||||
update: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [TrackController],
|
|
||||||
providers: [
|
|
||||||
ConfigService,
|
|
||||||
FileService,
|
|
||||||
{
|
|
||||||
provide: TrackService,
|
|
||||||
useValue: mockTrackService
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<TrackController>(TrackController);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should find all tracks by log id', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const track = {
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde',
|
|
||||||
url: 'http://track.example.com',
|
|
||||||
order: 1
|
|
||||||
} as TrackEntity
|
|
||||||
const tracks = [track]
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'findAll').mockReturnValue(tracks);
|
|
||||||
|
|
||||||
const result = await controller.findAll(logId);
|
|
||||||
|
|
||||||
expect(result).toEqual(tracks);
|
|
||||||
expect(mockTrackService.findAll).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.findAll).toHaveBeenLastCalledWith(logId);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should fail to find all tracks by log id', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'findAll').mockRejectedValue(new Error('Tracks not found'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.findAll(logId);
|
|
||||||
|
|
||||||
fail('findAll did not throw error');
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(HttpException);
|
|
||||||
expect(mockTrackService.findAll).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.findAll).rejects.toThrow('Tracks not found')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => create a track by a log id', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const order = 1;
|
|
||||||
const file: Express.Multer.File = {
|
|
||||||
fieldname: 'file',
|
|
||||||
originalname: 'test_track.kml',
|
|
||||||
encoding: 'utf-8',
|
|
||||||
mimetype: 'application/vnd.google-earth.kml+xml',
|
|
||||||
size: 12345,
|
|
||||||
destination: '/tmp/uploads',
|
|
||||||
filename: 'unique-filename-123.kml',
|
|
||||||
path: '/tmp/uploads/unique-filename-123.kml',
|
|
||||||
buffer: Buffer.from('<kml xmlns="http://www.opengis.net"></kml>'),
|
|
||||||
stream: new Readable()
|
|
||||||
}
|
|
||||||
const mockInsertResult: InsertResult = {
|
|
||||||
identifiers: [
|
|
||||||
{
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
generatedMaps: [
|
|
||||||
{
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde',
|
|
||||||
createdAd: new Date()
|
|
||||||
}
|
|
||||||
],
|
|
||||||
raw: {
|
|
||||||
affectedRows: 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'create').mockReturnValue(mockInsertResult);
|
|
||||||
|
|
||||||
const result = await controller.create(logId, order, file);
|
|
||||||
|
|
||||||
expect(result).toEqual(mockInsertResult);
|
|
||||||
expect(mockTrackService.create).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.create).toHaveBeenCalledWith(logId, order, file)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should fail to create a track by log id', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const order = 1;
|
|
||||||
const file: Express.Multer.File = {
|
|
||||||
fieldname: 'file',
|
|
||||||
originalname: 'test_track.kml',
|
|
||||||
encoding: 'utf-8',
|
|
||||||
mimetype: 'application/vnd.google-earth.kml+xml',
|
|
||||||
size: 12345,
|
|
||||||
destination: '/tmp/uploads',
|
|
||||||
filename: 'unique-filename-123.kml',
|
|
||||||
path: '/tmp/uploads/unique-filename-123.kml',
|
|
||||||
buffer: Buffer.from('<kml xmlns="http://www.opengis.net"></kml>'),
|
|
||||||
stream: new Readable()
|
|
||||||
}
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'create').mockRejectedValue(new Error('Track failed to create'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.create(logId, order, file)
|
|
||||||
|
|
||||||
fail('create did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(HttpException);
|
|
||||||
expect(mockTrackService.create).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.create).toHaveBeenCalledWith(logId, order, file);
|
|
||||||
expect(mockTrackService.create).rejects.toThrow('Track failed to create')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should delete a track by log id', async () => {
|
|
||||||
const id = 'a6973d91-629f-4e10-aa28-016076a21fde'
|
|
||||||
const logId: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const fileName = 'test_track.kml';
|
|
||||||
const mockDeleteResult: DeleteResult ={
|
|
||||||
raw: [],
|
|
||||||
affected: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'delete').mockReturnValue(mockDeleteResult);
|
|
||||||
|
|
||||||
const result = await controller.delete(id, fileName, logId);
|
|
||||||
|
|
||||||
expect(result).toEqual(mockDeleteResult);
|
|
||||||
expect(mockTrackService.delete).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.delete).toHaveBeenCalledWith(id, logId, fileName);
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should fail to delete a track by log id', async () => {
|
|
||||||
const id = 'a6973d91-629f-4e10-aa28-016076a21fde'
|
|
||||||
const logId: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const fileName = 'test_track.kml';
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'delete').mockRejectedValue(new Error('Track failed to delete'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.delete(id, fileName, logId);
|
|
||||||
|
|
||||||
fail('delete did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(HttpException);
|
|
||||||
expect(mockTrackService.delete).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.delete).toHaveBeenCalledWith(id, logId, fileName);
|
|
||||||
expect(mockTrackService.delete).rejects.toThrow('Track failed to delete')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('downloadTrack => should download a track by log id', async () => {
|
|
||||||
const logId: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const fileName = 'test_track.kml';
|
|
||||||
const mockStreamToBufferString = 'fake-stream-to-buffer-string'
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'downloadTrackFile').mockReturnValue(mockStreamToBufferString);
|
|
||||||
|
|
||||||
const result = await controller.downloadTrack(logId, fileName);
|
|
||||||
|
|
||||||
expect(result).toEqual(mockStreamToBufferString);
|
|
||||||
expect(mockTrackService.downloadTrackFile).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.downloadTrackFile).toHaveBeenCalledWith(logId, fileName)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('downloadTrack => should fail to download a track by log id', async () => {
|
|
||||||
const logId: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const fileName = 'test_track.kml';
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackService, 'downloadTrackFile').mockRejectedValue(new Error('Track failed to download'));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controller.downloadTrack(logId, fileName);
|
|
||||||
|
|
||||||
fail('downloadTrack did not throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(HttpException);
|
|
||||||
expect(mockTrackService.downloadTrackFile).toHaveBeenCalled();
|
|
||||||
expect(mockTrackService.downloadTrackFile).toHaveBeenCalledWith(logId, fileName);
|
|
||||||
expect(mockTrackService.downloadTrackFile).rejects.toThrow('Track failed to download')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
import {
|
|
||||||
Controller,
|
|
||||||
Delete,
|
|
||||||
Get,
|
|
||||||
HttpException,
|
|
||||||
Param,
|
|
||||||
Post,
|
|
||||||
Query,
|
|
||||||
UploadedFile,
|
|
||||||
UseGuards,
|
|
||||||
UseInterceptors
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { AuthGuard } from '@noahspan/noahspan-modules';
|
|
||||||
import { CustomError } from '../error/customError';
|
|
||||||
import { FileInterceptor } from '@nestjs/platform-express';
|
|
||||||
import { FileService } from '../file/file.service';
|
|
||||||
import { TrackService } from './track.service';
|
|
||||||
import { TrackEntity } from './track.entity';
|
|
||||||
import { DeleteResult, InsertResult } from 'typeorm';
|
|
||||||
|
|
||||||
@Controller('tracks')
|
|
||||||
export class TrackController {
|
|
||||||
constructor(
|
|
||||||
private readonly fileService: FileService,
|
|
||||||
private readonly trackService: TrackService
|
|
||||||
) {}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Get(':logId')
|
|
||||||
async findAll(@Param('logId') logId: string): Promise<TrackEntity[]> {
|
|
||||||
try {
|
|
||||||
return await this.trackService.findAll(logId);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Post(':logId/:order')
|
|
||||||
@UseInterceptors(FileInterceptor('file'))
|
|
||||||
async create(@Param('logId') logId: string, @Param('order') order: number, @UploadedFile() file: Express.Multer.File): Promise<InsertResult> {
|
|
||||||
try {
|
|
||||||
return await this.trackService.create(logId, order, file);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@Delete(':id/:filename/:logId')
|
|
||||||
async delete(@Param('id') id: string, @Query('fileName') filename: string, @Query('logId') logId: string): Promise<DeleteResult> {
|
|
||||||
try {
|
|
||||||
return await this.trackService.delete(id, logId, filename);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get(':logId/:fileName')
|
|
||||||
async downloadTrack(@Param('logId') logId: string, @Param('fileName') fileName: string): Promise<string> {
|
|
||||||
try {
|
|
||||||
return await this.trackService.downloadTrackFile(logId, fileName);
|
|
||||||
} catch (error) {
|
|
||||||
const customError = error as CustomError;
|
|
||||||
|
|
||||||
throw new HttpException(customError.message, customError.statusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
export class TrackDto {
|
|
||||||
logId: string;
|
|
||||||
order: number;
|
|
||||||
url?: string;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { LogEntity } from '../log/log.entity';
|
|
||||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
|
||||||
|
|
||||||
@Entity({ name: 'tracks' })
|
|
||||||
export class TrackEntity {
|
|
||||||
@PrimaryGeneratedColumn('uuid')
|
|
||||||
id: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
url: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
order: number;
|
|
||||||
|
|
||||||
@ManyToOne(() => LogEntity, (log: LogEntity) => log.tracks, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'logId' })
|
|
||||||
log: LogEntity;
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
|
||||||
import { TrackEntity } from "./track.entity";
|
|
||||||
import { TrackController } from "./track.controller";
|
|
||||||
import { FileService } from "../file/file.service";
|
|
||||||
import { TrackService } from './track.service';
|
|
||||||
import { LogModule } from '../log/log.module';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [
|
|
||||||
LogModule,
|
|
||||||
TypeOrmModule.forFeature([TrackEntity])
|
|
||||||
],
|
|
||||||
controllers: [TrackController],
|
|
||||||
providers: [
|
|
||||||
FileService,
|
|
||||||
TrackService
|
|
||||||
]
|
|
||||||
})
|
|
||||||
export class TrackModule {}
|
|
||||||
@@ -1,346 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { TrackService } from './track.service';
|
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
||||||
import { TrackEntity } from './track.entity'
|
|
||||||
import { TrackDto } from './track.dto';
|
|
||||||
import { FileService } from '../file/file.service';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { LogService } from '../log/log.service';
|
|
||||||
import { LogEntity } from '../log/log.entity';
|
|
||||||
import { PilotService } from '../pilot/pilot.service';
|
|
||||||
import { PilotEntity } from '../pilot/pilot.entity';
|
|
||||||
import { Readable } from 'stream';
|
|
||||||
import { DeleteResult, InsertResult, UpdateResult } from 'typeorm';
|
|
||||||
import { CustomError } from '../error/customError';
|
|
||||||
|
|
||||||
describe('TrackService', () => {
|
|
||||||
let service: TrackService;
|
|
||||||
|
|
||||||
const mockFileService = {
|
|
||||||
deleteFile: jest.fn(),
|
|
||||||
downloadFile: jest.fn(),
|
|
||||||
uploadFile: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockLogService = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findAll: jest.fn(),
|
|
||||||
update: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockPilotRepository = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
findAll: jest.fn(),
|
|
||||||
update: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockTrackRepository = {
|
|
||||||
create: jest.fn(),
|
|
||||||
delete: jest.fn(),
|
|
||||||
downloadTrackFile: jest.fn(),
|
|
||||||
findOneBy: jest.fn(),
|
|
||||||
find: jest.fn(),
|
|
||||||
insert: jest.fn(),
|
|
||||||
update: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [
|
|
||||||
ConfigService,
|
|
||||||
LogService,
|
|
||||||
PilotService,
|
|
||||||
TrackService,
|
|
||||||
{
|
|
||||||
provide: FileService,
|
|
||||||
useValue: mockFileService
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: LogService,
|
|
||||||
useValue: mockLogService
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(PilotEntity),
|
|
||||||
useValue: mockPilotRepository
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(TrackEntity),
|
|
||||||
useValue: mockTrackRepository
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}).compile()
|
|
||||||
|
|
||||||
service = module.get<TrackService>(TrackService);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should upload a track file', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const log = {
|
|
||||||
id: '094ec69c-72c4-4995-8821-79d5b79bedda',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity;
|
|
||||||
const order = 1;
|
|
||||||
const file: Express.Multer.File = {
|
|
||||||
fieldname: 'file',
|
|
||||||
originalname: 'test_track.kml',
|
|
||||||
encoding: 'utf-8',
|
|
||||||
mimetype: 'application/vnd.google-earth.kml+xml',
|
|
||||||
size: 12345,
|
|
||||||
destination: '/tmp/uploads',
|
|
||||||
filename: 'unique-filename-123.kml',
|
|
||||||
path: '/tmp/uploads/unique-filename-123.kml',
|
|
||||||
buffer: Buffer.from('<kml xmlns="http://www.opengis.net"></kml>'),
|
|
||||||
stream: new Readable()
|
|
||||||
}
|
|
||||||
const mockInsertResult: InsertResult = {
|
|
||||||
identifiers: [
|
|
||||||
{
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
generatedMaps: [
|
|
||||||
{
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde',
|
|
||||||
createdAd: new Date()
|
|
||||||
}
|
|
||||||
],
|
|
||||||
raw: {
|
|
||||||
affectedRows: 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const track = {
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde',
|
|
||||||
url: 'http://track.example.com',
|
|
||||||
order: 1
|
|
||||||
} as TrackEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'find').mockReturnValue(log)
|
|
||||||
jest.spyOn(mockTrackRepository, 'create').mockReturnValue(track)
|
|
||||||
jest.spyOn(mockTrackRepository, 'insert').mockReturnValue(mockInsertResult);
|
|
||||||
|
|
||||||
const result = await service.create(logId, order, file);
|
|
||||||
|
|
||||||
expect(result).toEqual(mockInsertResult)
|
|
||||||
expect(mockTrackRepository.insert).toHaveBeenCalled();
|
|
||||||
expect(mockTrackRepository.insert).toHaveBeenCalledWith({
|
|
||||||
id: track.id,
|
|
||||||
order: track.order,
|
|
||||||
url: track.url
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('create => should fail to find log', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const order = 1;
|
|
||||||
const file: Express.Multer.File = {
|
|
||||||
fieldname: 'file',
|
|
||||||
originalname: 'test_track.kml',
|
|
||||||
encoding: 'utf-8',
|
|
||||||
mimetype: 'application/vnd.google-earth.kml+xml',
|
|
||||||
size: 12345,
|
|
||||||
destination: '/tmp/uploads',
|
|
||||||
filename: 'unique-filename-123.kml',
|
|
||||||
path: '/tmp/uploads/unique-filename-123.kml',
|
|
||||||
buffer: Buffer.from('<kml xmlns="http://www.opengis.net"></kml>'),
|
|
||||||
stream: new Readable()
|
|
||||||
}
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'find').mockReturnValue(undefined);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.create(logId, order, file);
|
|
||||||
|
|
||||||
fail('find failed to throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(CustomError);
|
|
||||||
expect(mockLogService.find).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.find).toHaveBeenCalledWith(logId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should delete a track file', async () => {
|
|
||||||
const id = 'a6973d91-629f-4e10-aa28-016076a21fde';
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const fileName = 'test_track.kml'
|
|
||||||
const mockDeleteResult: DeleteResult ={
|
|
||||||
raw: [],
|
|
||||||
affected: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackRepository, 'delete').mockReturnValue(mockDeleteResult);
|
|
||||||
|
|
||||||
const result = await service.delete(id, logId, fileName)
|
|
||||||
|
|
||||||
expect(result).toEqual(mockDeleteResult);
|
|
||||||
expect(mockTrackRepository.delete).toHaveBeenCalled();
|
|
||||||
expect(mockTrackRepository.delete).toHaveBeenCalledWith({ id: id })
|
|
||||||
})
|
|
||||||
|
|
||||||
it('delete => should fail to delete file in file service', async () => {
|
|
||||||
const id = 'a6973d91-629f-4e10-aa28-016076a21fde';
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const fileName = 'test_track.kml'
|
|
||||||
|
|
||||||
jest.spyOn(mockFileService, 'deleteFile').mockRejectedValue(new Error('Failed to delete file'))
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.delete(id, logId, fileName)
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(Error);
|
|
||||||
expect(mockFileService.deleteFile).toHaveBeenCalled();
|
|
||||||
expect(mockFileService.deleteFile).toHaveBeenCalledWith('tracks', logId, fileName)
|
|
||||||
expect(mockFileService.deleteFile).rejects.toThrow('Failed to delete file')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('downloadTrackFile => should download a track file by log id and filename', async () => {
|
|
||||||
const logId: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
|
||||||
const fileName = 'test_track.kml';
|
|
||||||
const mockStreamToBufferString = 'fake-stream-to-buffer-string'
|
|
||||||
|
|
||||||
jest.spyOn(mockFileService, 'downloadFile').mockReturnValue(mockStreamToBufferString);
|
|
||||||
|
|
||||||
const result = await service.downloadTrackFile(logId, fileName);
|
|
||||||
|
|
||||||
expect(result).toEqual(mockStreamToBufferString);
|
|
||||||
expect(mockFileService.downloadFile).toHaveBeenCalled();
|
|
||||||
expect(mockFileService.downloadFile).toHaveBeenCalledWith('tracks', logId, fileName);
|
|
||||||
})
|
|
||||||
|
|
||||||
it('find => should find a track file by id', async () => {
|
|
||||||
const id = 'a6973d91-629f-4e10-aa28-016076a21fde';
|
|
||||||
const track = {
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde',
|
|
||||||
url: 'http://track.example.com',
|
|
||||||
order: 1
|
|
||||||
} as TrackEntity
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackRepository, 'findOneBy').mockReturnValue(track)
|
|
||||||
|
|
||||||
const result = await service.find(id);
|
|
||||||
|
|
||||||
expect(result).toEqual(track);
|
|
||||||
expect(mockTrackRepository.findOneBy).toHaveBeenCalled();
|
|
||||||
expect(mockTrackRepository.findOneBy).toHaveBeenCalledWith({ id: id })
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should find all track files by log id', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
const log = {
|
|
||||||
id: '094ec69c-72c4-4995-8821-79d5b79bedda',
|
|
||||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
|
||||||
date: new Date('2026-02-21'),
|
|
||||||
aircraftMakeModel: 'Cessna 172M',
|
|
||||||
aircraftIdentity: 'N12345',
|
|
||||||
routeFrom: 'KMSP',
|
|
||||||
routeTo: 'KMSP',
|
|
||||||
durationOfFlight: 1,
|
|
||||||
singleEngineLand: 1,
|
|
||||||
simulatorAtd: null,
|
|
||||||
landingsDay: 1,
|
|
||||||
landingsNight: null,
|
|
||||||
groundTrainingReceived: null,
|
|
||||||
flightTrainingReceived: null,
|
|
||||||
crossCountry: 1,
|
|
||||||
night: null,
|
|
||||||
solo: 1,
|
|
||||||
pilotInCommand: 1,
|
|
||||||
instrumentActual: null,
|
|
||||||
instrumentSimulated: null,
|
|
||||||
instrumentApproaches: null,
|
|
||||||
instrumentHolds: null,
|
|
||||||
instrumentNavTrack: null,
|
|
||||||
notes: 'This is a test',
|
|
||||||
tracks: []
|
|
||||||
} as LogEntity;
|
|
||||||
const track = {
|
|
||||||
id: 'a6973d91-629f-4e10-aa28-016076a21fde',
|
|
||||||
url: 'http://track.example.com',
|
|
||||||
order: 1
|
|
||||||
} as TrackEntity;
|
|
||||||
const tracks = [track]
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'find').mockReturnValue(log)
|
|
||||||
jest.spyOn(mockTrackRepository, 'find').mockReturnValue(tracks);
|
|
||||||
|
|
||||||
const result = await service.findAll(logId);
|
|
||||||
|
|
||||||
expect(result).toEqual(tracks);
|
|
||||||
expect(mockTrackRepository.find).toHaveBeenCalled();
|
|
||||||
expect(mockTrackRepository.find).toHaveBeenCalledWith(
|
|
||||||
{
|
|
||||||
where: {
|
|
||||||
log: {
|
|
||||||
id: logId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('findAll => should fail to find log', async () => {
|
|
||||||
const logId = '094ec69c-72c4-4995-8821-79d5b79bedda';
|
|
||||||
|
|
||||||
jest.spyOn(mockLogService, 'find').mockReturnValue(undefined);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.findAll(logId);
|
|
||||||
|
|
||||||
fail('find failed to throw error')
|
|
||||||
} catch (error) {
|
|
||||||
expect(error).toBeInstanceOf(CustomError);
|
|
||||||
expect(mockLogService.find).toHaveBeenCalled();
|
|
||||||
expect(mockLogService.find).toHaveBeenCalledWith(logId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('update => should update a track', async () => {
|
|
||||||
const id = 'a6973d91-629f-4e10-aa28-016076a21fde';
|
|
||||||
const track = {
|
|
||||||
|
|
||||||
} as TrackDto;
|
|
||||||
const mockUpdateResult: UpdateResult = {
|
|
||||||
affected: 1,
|
|
||||||
raw: [],
|
|
||||||
generatedMaps: []
|
|
||||||
}
|
|
||||||
|
|
||||||
jest.spyOn(mockTrackRepository, 'update').mockReturnValue(mockUpdateResult)
|
|
||||||
|
|
||||||
const result = await service.update(id, track);
|
|
||||||
|
|
||||||
expect(result).toEqual(mockUpdateResult);
|
|
||||||
expect(mockTrackRepository.update).toHaveBeenCalled();
|
|
||||||
expect(mockTrackRepository.update).toHaveBeenCalledWith(id, track)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
import { DeleteResult, InsertResult, Repository, UpdateResult } from "typeorm";
|
|
||||||
import { TrackDto } from "./track.dto";
|
|
||||||
import { TrackEntity } from "./track.entity";
|
|
||||||
import { InjectRepository } from "@nestjs/typeorm";
|
|
||||||
import { Injectable } from "@nestjs/common";
|
|
||||||
import { LogService } from "../log/log.service";
|
|
||||||
import { LogEntity } from "../log/log.entity";
|
|
||||||
import { CustomError } from "../error/customError";
|
|
||||||
import { FileService } from "../file/file.service";
|
|
||||||
import { Logs } from "src/log/logs.interface";
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class TrackService {
|
|
||||||
private readonly containerName: string = 'tracks'
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
@InjectRepository(TrackEntity) private readonly trackRepository: Repository<TrackEntity>,
|
|
||||||
private readonly fileService: FileService,
|
|
||||||
private readonly logService: LogService
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async find(id: string): Promise<TrackEntity> {
|
|
||||||
return await this.trackRepository.findOneBy({ id });
|
|
||||||
}
|
|
||||||
|
|
||||||
async findAll(logId: string): Promise<TrackEntity[]> {
|
|
||||||
try {
|
|
||||||
const logEntity: LogEntity = await this.logService.find(logId);
|
|
||||||
|
|
||||||
if (logEntity) {
|
|
||||||
const tracks = await this.trackRepository.find({
|
|
||||||
where: { log:
|
|
||||||
{
|
|
||||||
id: logEntity.id
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return tracks;
|
|
||||||
} else {
|
|
||||||
throw new CustomError('Tracks not found', 'Not found', 404);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(logId: string, order: number, file: Express.Multer.File): Promise<InsertResult> {
|
|
||||||
try {
|
|
||||||
const logEntity: LogEntity = await this.logService.find(logId);
|
|
||||||
|
|
||||||
if (logEntity) {
|
|
||||||
const url = await this.fileService.uploadFile(file, this.containerName, logId);
|
|
||||||
const track = this.trackRepository.create({
|
|
||||||
log: logEntity,
|
|
||||||
order: order,
|
|
||||||
url: url
|
|
||||||
});
|
|
||||||
|
|
||||||
return await this.trackRepository.insert(track);
|
|
||||||
} else {
|
|
||||||
throw new CustomError('Log not found', 'Not found', 404)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async update(id: string, track: TrackDto): Promise<UpdateResult> {
|
|
||||||
return await this.trackRepository.update(id, track);
|
|
||||||
}
|
|
||||||
|
|
||||||
async downloadTrackFile(logId: string, fileName: string): Promise<string> {
|
|
||||||
return await this.fileService.downloadFile(this.containerName, logId, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(id: string, logId: string, fileName: string): Promise<DeleteResult> {
|
|
||||||
try {
|
|
||||||
await this.fileService.deleteFile(this.containerName, logId, fileName);
|
|
||||||
|
|
||||||
return await this.trackRepository.delete({ id });
|
|
||||||
} catch (error) {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
export class ColumnNumericTransformer {
|
|
||||||
to(data: number): number {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
from (data: string): number {
|
|
||||||
return parseFloat(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
0
client/.gitignore → app/.gitignore
vendored
@@ -2,9 +2,9 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/png" href="/noahspan-logo.png" />
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Flying</title>
|
<title>Vite + React + TS</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "client",
|
"name": "app",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "2.1.5",
|
"version": "1.3.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
@@ -11,31 +11,25 @@
|
|||||||
"serve": "serve -s dist"
|
"serve": "serve -s dist"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fortawesome/fontawesome-svg-core": "^7.1.0",
|
"@azure/msal-browser": "^4.0.1",
|
||||||
"@fortawesome/free-solid-svg-icons": "^7.1.0",
|
"@azure/msal-react": "^3.0.1",
|
||||||
"@fortawesome/react-fontawesome": "^3.1.0",
|
"@noahspan/noahspan-components": "1.6.0",
|
||||||
"@tailwindcss/typography": "^0.5.19",
|
|
||||||
"@tailwindcss/vite": "^4.1.13",
|
|
||||||
"@tanstack/react-table": "^8.21.3",
|
|
||||||
"axios": "^1.7.2",
|
"axios": "^1.7.2",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"leaflet": "^1.9.4",
|
"leaflet": "^1.9.4",
|
||||||
"oidc-spa": "^7.2.4",
|
"react": "19.0.0-rc.1",
|
||||||
"react": "^19.1.1",
|
"react-dom": "19.0.0-rc.1",
|
||||||
"react-dom": "^19.1.1",
|
|
||||||
"react-hook-form": "^7.51.4",
|
"react-hook-form": "^7.51.4",
|
||||||
"react-leaflet": "^4",
|
"react-leaflet": "^5.0.0",
|
||||||
"react-leaflet-kml": "^2.1.2",
|
"react-leaflet-kml": "^2.1.2",
|
||||||
"react-oidc-context": "^3.3.0",
|
|
||||||
"react-router-dom": "^6.23.0",
|
"react-router-dom": "^6.23.0",
|
||||||
"swiper": "^11.2.6",
|
"swiper": "^11.2.6"
|
||||||
"tailwindcss": "^4.1.13"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@microsoft/microsoft-graph-types": "^2.40.0",
|
"@microsoft/microsoft-graph-types": "^2.40.0",
|
||||||
"@types/leaflet": "^1.9.16",
|
"@types/leaflet": "^1.9.16",
|
||||||
"@types/react": "^19.1.12",
|
"@types/react": "^18.2.66",
|
||||||
"@types/react-dom": "^19.1.8",
|
"@types/react-dom": "^18.2.22",
|
||||||
"@vitejs/plugin-react": "^4.2.1",
|
"@vitejs/plugin-react": "^4.2.1",
|
||||||
"typescript": "^5.2.2",
|
"typescript": "^5.2.2",
|
||||||
"vite": "^5.2.0"
|
"vite": "^5.2.0"
|
||||||
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 696 B After Width: | Height: | Size: 696 B |
|
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 618 B After Width: | Height: | Size: 618 B |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
@@ -1,22 +1,31 @@
|
|||||||
import { Route, Routes } from 'react-router-dom';
|
import { Navigate, Route, Routes } from 'react-router-dom';
|
||||||
import Flights from './components/flights/Flights';
|
import Flights from './components/flights/Flights';
|
||||||
import Logbook from './components/logbook/Logbook';
|
import Logbook from './components/logbook/Logbook';
|
||||||
import Pilots from './components/pilots/Pilots';
|
import Pilots from './components/pilots/Pilots';
|
||||||
import SiteNav from './components/siteNav/SiteNav';
|
import SiteNav from './components/siteNav/SiteNav';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useIsAuthenticated } from '@azure/msal-react';
|
||||||
|
|
||||||
|
interface ProtectedRouteProps {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const navigate = useNavigate();
|
const isAuthenticated = useIsAuthenticated()
|
||||||
|
|
||||||
|
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||||||
|
return isAuthenticated ? children : <Navigate to='/' />
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='bg-[#f5f5f5]' data-theme="lofi">
|
<>
|
||||||
<SiteNav />
|
<SiteNav />
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path='/' element={<Flights />} />
|
<Route path='/' element={<Flights />} />
|
||||||
<Route path="/logbook" element={<Logbook />} />
|
<Route path="/logbook" element={<Logbook />} />
|
||||||
<Route path="/pilots" element={<Pilots />} />
|
<Route path="/pilots" element={<Pilots />} />
|
||||||
|
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
70
app/src/auth/msalConfig.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { LogLevel } from '@azure/msal-browser';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration object to be passed to MSAL instance on creation.
|
||||||
|
* For a full list of MSAL.js configuration parameters, visit:
|
||||||
|
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/configuration.md
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const msalConfig = {
|
||||||
|
auth: {
|
||||||
|
clientId: import.meta.env.VITE_CLIENT_ID, // This is the ONLY mandatory field that you need to supply.
|
||||||
|
authority: `https://login.microsoftonline.com/${import.meta.env.VITE_TENANT_ID}`, // Replace the placeholder with your tenant subdomain
|
||||||
|
redirectUri: import.meta.env.VITE_REDIRECT_URL, // Points to window.location.origin. You must register this URI on Microsoft Entra admin center/App Registration.
|
||||||
|
postLogoutRedirectUri: '/', // Indicates the page to navigate after logout.
|
||||||
|
navigateToLoginRequestUrl: false, // If "true", will navigate back to the original request location before processing the auth code response.
|
||||||
|
},
|
||||||
|
cache: {
|
||||||
|
cacheLocation: 'sessionStorage', // Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO between tabs.
|
||||||
|
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
|
||||||
|
},
|
||||||
|
system: {
|
||||||
|
loggerOptions: {
|
||||||
|
loggerCallback: (level: any, message: any, containsPii: any) => {
|
||||||
|
if (containsPii) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (level) {
|
||||||
|
case LogLevel.Error:
|
||||||
|
console.error(message);
|
||||||
|
return;
|
||||||
|
case LogLevel.Info:
|
||||||
|
console.info(message);
|
||||||
|
return;
|
||||||
|
case LogLevel.Verbose:
|
||||||
|
console.debug(message);
|
||||||
|
return;
|
||||||
|
case LogLevel.Warning:
|
||||||
|
console.warn(message);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scopes you add here will be prompted for user consent during sign-in.
|
||||||
|
* By default, MSAL.js will add OIDC scopes (openid, profile, email) to any login request.
|
||||||
|
* For more information about OIDC scopes, visit:
|
||||||
|
* https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#openid-connect-scopes
|
||||||
|
*/
|
||||||
|
export const loginRequest = {
|
||||||
|
scopes: [`api://${import.meta.env.VITE_CLIENT_ID}/user_impersonation`],
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An optional silentRequest object can be used to achieve silent SSO
|
||||||
|
* between applications by providing a "login_hint" property.
|
||||||
|
*/
|
||||||
|
// export const silentRequest = {
|
||||||
|
// scopes: ["openid", "profile"],
|
||||||
|
// loginHint: "example@domain.net"
|
||||||
|
// };
|
||||||
81
app/src/components/actionMenu/ActionMenu.tsx
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import { IActionMenuProps } from './IActionMenuProps';
|
||||||
|
import {
|
||||||
|
IconButton,
|
||||||
|
Icon,
|
||||||
|
IconName,
|
||||||
|
ListItemIcon,
|
||||||
|
ListItemText,
|
||||||
|
Menu,
|
||||||
|
MenuItem
|
||||||
|
} from '@noahspan/noahspan-components';
|
||||||
|
import { FormMode } from '../../enums/formMode';
|
||||||
|
import { useIsAuthenticated } from '@azure/msal-react';
|
||||||
|
|
||||||
|
const ActionMenu = ({ id, onDelete, onOpenCloseForm, onOpenCloseTracks }: IActionMenuProps) => {
|
||||||
|
const [anchorElAction, setAnchorElAction] = useState<null | HTMLElement>(
|
||||||
|
null
|
||||||
|
);
|
||||||
|
const isAuthenticated = useIsAuthenticated();
|
||||||
|
|
||||||
|
const onOpenActionMenu = (event: React.MouseEvent<HTMLElement>) => {
|
||||||
|
setAnchorElAction(event.currentTarget);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onCloseActionMenu = () => {
|
||||||
|
setAnchorElAction(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<IconButton onClick={onOpenActionMenu}>
|
||||||
|
<Icon iconName={IconName.ELLIPSIS_VERTICAL} size="sm" />
|
||||||
|
</IconButton>
|
||||||
|
<Menu
|
||||||
|
anchorEl={anchorElAction}
|
||||||
|
keepMounted
|
||||||
|
open={Boolean(anchorElAction)}
|
||||||
|
onClose={onCloseActionMenu}
|
||||||
|
>
|
||||||
|
{isAuthenticated &&
|
||||||
|
<>
|
||||||
|
<MenuItem onClick={() => onOpenCloseForm(FormMode.EDIT, id)}>
|
||||||
|
<ListItemIcon>
|
||||||
|
<Icon iconName={IconName.PEN} size="lg" />
|
||||||
|
</ListItemIcon>
|
||||||
|
<ListItemText>Edit</ListItemText>
|
||||||
|
</MenuItem>
|
||||||
|
{onOpenCloseTracks &&
|
||||||
|
<MenuItem onClick={() => onOpenCloseTracks!(FormMode.EDIT, id)}>
|
||||||
|
<ListItemIcon>
|
||||||
|
<Icon iconName={IconName.MAP_LOCATION_DOT} size="lg" />
|
||||||
|
</ListItemIcon>
|
||||||
|
<ListItemText>Tracks</ListItemText>
|
||||||
|
</MenuItem>
|
||||||
|
}
|
||||||
|
</>
|
||||||
|
|
||||||
|
}
|
||||||
|
<MenuItem onClick={() => onOpenCloseForm(FormMode.VIEW, id)}>
|
||||||
|
<ListItemIcon>
|
||||||
|
<Icon iconName={IconName.EYE} size="lg" />
|
||||||
|
</ListItemIcon>
|
||||||
|
<ListItemText>View</ListItemText>
|
||||||
|
</MenuItem>
|
||||||
|
{isAuthenticated &&
|
||||||
|
<>
|
||||||
|
<hr className="my-3" />
|
||||||
|
<MenuItem onClick={() => onDelete(id)}>
|
||||||
|
<ListItemIcon>
|
||||||
|
<Icon iconName={IconName.TRASH} size="lg" />
|
||||||
|
</ListItemIcon>
|
||||||
|
<ListItemText>Delete</ListItemText>
|
||||||
|
</MenuItem>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
</Menu>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ActionMenu;
|
||||||
8
app/src/components/actionMenu/IActionMenuProps.tsx
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { FormMode } from '../../enums/formMode';
|
||||||
|
|
||||||
|
export interface IActionMenuProps {
|
||||||
|
id: string;
|
||||||
|
onDelete: (entryId: string) => void;
|
||||||
|
onOpenCloseForm: (formMode: FormMode, id: string) => void;
|
||||||
|
onOpenCloseTracks?: (formMode: FormMode, id: string) => void;
|
||||||
|
}
|
||||||
52
app/src/components/confirmationDialog/ConfirmationDialog.tsx
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import {
|
||||||
|
Box,
|
||||||
|
Button,
|
||||||
|
Dialog,
|
||||||
|
DialogActions,
|
||||||
|
DialogContent,
|
||||||
|
DialogContentText,
|
||||||
|
DialogTitle,
|
||||||
|
Icon,
|
||||||
|
IconName,
|
||||||
|
Spinner
|
||||||
|
} from '@noahspan/noahspan-components';
|
||||||
|
import { IDialogConfirmationProps } from './IConfirmationDialogProps';
|
||||||
|
|
||||||
|
const ConfirmationDialog = ({
|
||||||
|
contentText,
|
||||||
|
isLoading,
|
||||||
|
isOpen,
|
||||||
|
onCancel,
|
||||||
|
onConfirm,
|
||||||
|
title
|
||||||
|
}: IDialogConfirmationProps) => {
|
||||||
|
return (
|
||||||
|
<Dialog
|
||||||
|
onClose={onCancel}
|
||||||
|
open={isOpen}
|
||||||
|
sx={{
|
||||||
|
'& .MuiDialog-paper': { width: '2000px' }
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DialogTitle>{title}</DialogTitle>
|
||||||
|
<DialogContent sx={{ textAlign: 'center' }}>
|
||||||
|
{!isLoading && <DialogContentText>{contentText}</DialogContentText>}
|
||||||
|
{isLoading && <Spinner />}
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={onCancel} variant="outlined" startIcon={<Icon iconName={IconName.XMARK} />}>
|
||||||
|
No
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={onConfirm}
|
||||||
|
variant="contained"
|
||||||
|
startIcon={<Icon iconName={IconName.CIRCLE_CHECK} />}
|
||||||
|
>
|
||||||
|
Yes
|
||||||
|
</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ConfirmationDialog;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export interface DialogConfirmationProps {
|
export interface IDialogConfirmationProps {
|
||||||
contentText: string;
|
contentText: string;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||