From bc6cf1ea0c571ef50fadf7e20b64398385c6d36b Mon Sep 17 00:00:00 2001 From: Boban Date: Tue, 10 Dec 2024 15:55:50 -0500 Subject: [PATCH] fix build without pg promise variables (#2996) * remove .env * Remove app_env from dockerfile * make metadata env variable optional * fix tests without .env * move getDb from top level to be inside methods * add dockerignore * Remove NEXT_PUBLIC_BASEPATH since it is determined at build time anyways * Set base path as env variable again, but not configurable --- containers/ecr-viewer/.dockerignore | 10 ++++++++++ containers/ecr-viewer/.env | 16 ---------------- containers/ecr-viewer/Dockerfile | 3 --- containers/ecr-viewer/docker-compose.yml | 3 +-- containers/ecr-viewer/environment.d.ts | 4 ++-- containers/ecr-viewer/next.config.js | 9 +++++---- .../src/app/api/fhir-data/fhir-data-service.ts | 3 +-- .../api/save-fhir-data/save-fhir-data-service.ts | 4 ++-- .../src/app/components/EcrTableClient.tsx | 2 +- .../src/app/services/listEcrDataService.ts | 7 +++++-- .../tests/components/LoadingComponent.test.tsx | 7 ++++++- .../src/app/tests/components/SideNav.test.tsx | 8 ++++++++ .../src/app/tests/listEcrDataService.test.tsx | 6 ++++++ containers/orchestration/docker-compose.yml | 1 + 14 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 containers/ecr-viewer/.dockerignore delete mode 100644 containers/ecr-viewer/.env diff --git a/containers/ecr-viewer/.dockerignore b/containers/ecr-viewer/.dockerignore new file mode 100644 index 0000000000..6932304985 --- /dev/null +++ b/containers/ecr-viewer/.dockerignore @@ -0,0 +1,10 @@ +.next +.github +.env +docker-compose.yml +Dockerfile +node_modules +seed-scripts +cypress +design-review +.swc diff --git a/containers/ecr-viewer/.env b/containers/ecr-viewer/.env deleted file mode 100644 index 305ad86094..0000000000 --- a/containers/ecr-viewer/.env +++ /dev/null @@ -1,16 +0,0 @@ -DATABASE_URL=postgres://postgres:pw@localhost:5432/ecr_viewer_db -AWS_REGION=us-east-1 -SOURCE=postgres -NEXT_TELEMETRY_DISABLED=1 -NEXT_PUBLIC_NON_INTEGRATED_VIEWER=true -NEXT_PUBLIC_BASEPATH="/ecr-viewer" - -# Metadata Config -METADATA_DATABASE_TYPE=postgres -METADATA_DATABASE_SCHEMA=core -# METADATA_DATABASE_TYPE=sqlserver -# METADATA_DATABASE_SCHEMA=extended -# SQL_SERVER_USER=sa -# SQL_SERVER_PASSWORD=Password1! -# SQL_SERVER_SERVER=sqlserver - diff --git a/containers/ecr-viewer/Dockerfile b/containers/ecr-viewer/Dockerfile index 405806177f..d7abde5b43 100644 --- a/containers/ecr-viewer/Dockerfile +++ b/containers/ecr-viewer/Dockerfile @@ -1,8 +1,5 @@ FROM node:18-alpine AS base -ARG APP_ENV -ENV APP_ENV=${APP_ENV} - # Install dependencies only when needed FROM base AS deps # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. diff --git a/containers/ecr-viewer/docker-compose.yml b/containers/ecr-viewer/docker-compose.yml index ca23842093..c189c292b8 100644 --- a/containers/ecr-viewer/docker-compose.yml +++ b/containers/ecr-viewer/docker-compose.yml @@ -19,8 +19,7 @@ services: - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-xyz} - AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://azure-storage:10000/devstoreaccount1; - AZURE_CONTAINER_NAME=${AZURE_CONTAINER_NAME:-ecr-viewer-files} - - NEXT_PUBLIC_NON_INTEGRATED_VIEWER=${NEXT_PUBLIC_NON_INTEGRATED_VIEWER:-false} - - NEXT_PUBLIC_BASEPATH=${NEXT_PUBLIC_BASEPATH:-/ecr-viewer} + - NEXT_PUBLIC_NON_INTEGRATED_VIEWER=${NEXT_PUBLIC_NON_INTEGRATED_VIEWER:-true} - METADATA_DATABASE_SCHEMA=${METADATA_DATABASE_SCHEMA:-core} - SQL_SERVER_USER=${SQL_SERVER_USER} - SQL_SERVER_PASSWORD=${SQL_SERVER_PASSWORD} diff --git a/containers/ecr-viewer/environment.d.ts b/containers/ecr-viewer/environment.d.ts index a78fb66113..8c78070563 100644 --- a/containers/ecr-viewer/environment.d.ts +++ b/containers/ecr-viewer/environment.d.ts @@ -13,8 +13,8 @@ namespace NodeJS { ECR_BUCKET_NAME: string; GITHUB_ID: string; GITHUB_SECRET: string; - METADATA_DATABASE_SCHEMA: "core" | "extended"; - METADATA_DATABASE_TYPE: "postgres" | "sqlserver"; + METADATA_DATABASE_SCHEMA?: "core" | "extended"; + METADATA_DATABASE_TYPE?: "postgres" | "sqlserver"; NBS_PUB_KEY: string; NEXT_PUBLIC_NON_INTEGRATED_VIEWER: "true" | "false"; NEXT_RUNTIME: string; diff --git a/containers/ecr-viewer/next.config.js b/containers/ecr-viewer/next.config.js index 3646e53922..3dd060dd7a 100644 --- a/containers/ecr-viewer/next.config.js +++ b/containers/ecr-viewer/next.config.js @@ -1,5 +1,6 @@ /** @type {import('next').NextConfig} */ const path = require("path"); +const basePath = "/ecr-viewer"; const nextConfig = { sassOptions: { @@ -20,10 +21,10 @@ const nextConfig = { ]; }, output: "standalone", - basePath: - process.env.NODE_ENV === "production" - ? process.env.NEXT_PUBLIC_BASEPATH - : "", + basePath: process.env.NODE_ENV === "production" ? basePath : "", + env: { + BASE_PATH: basePath, + }, }; module.exports = nextConfig; diff --git a/containers/ecr-viewer/src/app/api/fhir-data/fhir-data-service.ts b/containers/ecr-viewer/src/app/api/fhir-data/fhir-data-service.ts index 264b2a1fab..94a09976e8 100644 --- a/containers/ecr-viewer/src/app/api/fhir-data/fhir-data-service.ts +++ b/containers/ecr-viewer/src/app/api/fhir-data/fhir-data-service.ts @@ -9,14 +9,13 @@ import { loadYamlConfig, streamToJson } from "../utils"; import { getDB } from "../services/postgres_db"; import { s3Client } from "../services/s3Client"; -const { database, pgPromise } = getDB(); - /** * Retrieves FHIR data from PostgreSQL database based on eCR ID. * @param request - The NextRequest object containing the request information. * @returns A promise resolving to a NextResponse object. */ export const get_postgres = async (request: NextRequest) => { + const { database, pgPromise } = getDB(); const params = request.nextUrl.searchParams; const ecr_id = params.get("id") ? params.get("id") : null; const mappings = loadYamlConfig(); diff --git a/containers/ecr-viewer/src/app/api/save-fhir-data/save-fhir-data-service.ts b/containers/ecr-viewer/src/app/api/save-fhir-data/save-fhir-data-service.ts index 45d8eef5ed..ec79f91b6f 100644 --- a/containers/ecr-viewer/src/app/api/save-fhir-data/save-fhir-data-service.ts +++ b/containers/ecr-viewer/src/app/api/save-fhir-data/save-fhir-data-service.ts @@ -10,8 +10,6 @@ import { BundleExtendedMetadata, BundleMetadata } from "./types"; import { s3Client } from "../services/s3Client"; import { get_pool } from "../services/sqlserver_db"; -const { database, pgPromise } = getDB(); - /** * Saves a FHIR bundle to a postgres database. * @async @@ -22,6 +20,7 @@ const { database, pgPromise } = getDB(); * @throws {Error} Throws an error if the FHIR bundle cannot be saved to postgress. */ export const saveToPostgres = async (fhirBundle: Bundle, ecrId: string) => { + const { database, pgPromise } = getDB(); const { ParameterizedQuery: PQ } = pgPromise; const addFhir = new PQ({ text: "INSERT INTO fhir VALUES ($1, $2) RETURNING ecr_id", @@ -435,6 +434,7 @@ export const saveMetadataToPostgres = async ( metadata: BundleMetadata, ecrId: string, ) => { + const { database, pgPromise } = getDB(); const { ParameterizedQuery: PQ } = pgPromise; try { diff --git a/containers/ecr-viewer/src/app/components/EcrTableClient.tsx b/containers/ecr-viewer/src/app/components/EcrTableClient.tsx index f06a4dcc08..5e4d6df58c 100644 --- a/containers/ecr-viewer/src/app/components/EcrTableClient.tsx +++ b/containers/ecr-viewer/src/app/components/EcrTableClient.tsx @@ -9,7 +9,7 @@ import { range } from "../view-data/utils/utils"; import classNames from "classnames"; const basePath = - process.env.NODE_ENV === "production" ? process.env.NEXT_PUBLIC_BASEPATH : ""; + process.env.NODE_ENV === "production" ? process.env.BASE_PATH : ""; type EcrTableClientProps = { data: EcrDisplay[]; diff --git a/containers/ecr-viewer/src/app/services/listEcrDataService.ts b/containers/ecr-viewer/src/app/services/listEcrDataService.ts index 55956241a6..ddde222e7d 100644 --- a/containers/ecr-viewer/src/app/services/listEcrDataService.ts +++ b/containers/ecr-viewer/src/app/services/listEcrDataService.ts @@ -6,8 +6,6 @@ import { import { get_pool } from "../api/services/sqlserver_db"; import { getDB } from "../api/services/postgres_db"; -const { database, pgPromise } = getDB(); - export interface CoreMetadataModel { eicr_id: string; data_source: "DB" | "S3"; @@ -96,6 +94,7 @@ async function listEcrDataPostgres( searchTerm?: string, filterConditions?: string[], ): Promise { + const { database } = getDB(); const list = await database.manyOrNone( "SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, ed.report_date, ARRAY_AGG(DISTINCT erc.condition) AS conditions, ARRAY_AGG(DISTINCT ers.rule_summary) AS rule_summaries FROM ecr_data ed LEFT JOIN ecr_rr_conditions erc ON ed.eICR_ID = erc.eICR_ID LEFT JOIN ecr_rr_rule_summaries ers ON erc.uuid = ers.ecr_rr_conditions_id WHERE $[whereClause] GROUP BY ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY", { @@ -236,6 +235,7 @@ const getTotalEcrCountPostgres = async ( searchTerm?: string, filterConditions?: string[], ): Promise => { + const { database } = getDB(); let number = await database.one( "SELECT count(DISTINCT ed.eICR_ID) FROM ecr_data as ed LEFT JOIN ecr_rr_conditions erc on ed.eICR_ID = erc.eICR_ID WHERE $[whereClause]", { @@ -315,6 +315,7 @@ const generateWhereStatementSqlServer = ( export const generateSearchStatement = (searchTerm?: string) => ({ rawType: true, toPostgres: () => { + const { pgPromise } = getDB(); const searchFields = ["ed.patient_name_first", "ed.patient_name_last"]; return searchFields .map((field) => { @@ -352,6 +353,7 @@ export const generateFilterConditionsStatement = ( ) => ({ rawType: true, toPostgres: () => { + const { pgPromise } = getDB(); if (!filterConditions) { return pgPromise.as.format("NULL IS NULL"); } @@ -396,6 +398,7 @@ export const generateSortStatement = ( ) => ({ rawType: true, toPostgres: () => { + const { pgPromise } = getDB(); // Valid columns and directions const validColumns = ["patient", "date_created", "report_date"]; const validDirections = ["ASC", "DESC"]; diff --git a/containers/ecr-viewer/src/app/tests/components/LoadingComponent.test.tsx b/containers/ecr-viewer/src/app/tests/components/LoadingComponent.test.tsx index a14bd1e5d4..24fca0001a 100644 --- a/containers/ecr-viewer/src/app/tests/components/LoadingComponent.test.tsx +++ b/containers/ecr-viewer/src/app/tests/components/LoadingComponent.test.tsx @@ -7,6 +7,7 @@ describe("Snapshot test for EcrLoadingSkeleton", () => { let container: HTMLElement; beforeAll(() => { + process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER = "true"; const mockIntersectionObserver = jest.fn(); mockIntersectionObserver.mockReturnValue({ observe: () => null, @@ -15,7 +16,11 @@ describe("Snapshot test for EcrLoadingSkeleton", () => { }); window.IntersectionObserver = mockIntersectionObserver; - container = render().container; + ({ container } = render()); + }); + + afterAll(() => { + process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER = "false"; }); it("should match snapshot", () => { expect(container).toMatchSnapshot(); diff --git a/containers/ecr-viewer/src/app/tests/components/SideNav.test.tsx b/containers/ecr-viewer/src/app/tests/components/SideNav.test.tsx index 69a7a23c5a..bf7e051c11 100644 --- a/containers/ecr-viewer/src/app/tests/components/SideNav.test.tsx +++ b/containers/ecr-viewer/src/app/tests/components/SideNav.test.tsx @@ -18,6 +18,14 @@ describe("SectionConfig", () => { window.IntersectionObserver = mockIntersectionObserver; }); + beforeAll(() => { + process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER = "true"; + }); + + afterAll(() => { + process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER = "false"; + }); + it("should create an instance with correct title and id", () => { const section = new SectionConfig("Test Section"); expect(section.title).toBe("Test Section"); diff --git a/containers/ecr-viewer/src/app/tests/listEcrDataService.test.tsx b/containers/ecr-viewer/src/app/tests/listEcrDataService.test.tsx index cd3200ede9..adeff19d6d 100644 --- a/containers/ecr-viewer/src/app/tests/listEcrDataService.test.tsx +++ b/containers/ecr-viewer/src/app/tests/listEcrDataService.test.tsx @@ -104,6 +104,12 @@ describe("listEcrDataService", () => { }); describe("list Ecr data with postgres", () => { + beforeAll(() => { + process.env.METADATA_DATABASE_TYPE = "postgres"; + }); + afterAll(() => { + delete process.env.METADATA_DATABASE_TYPE; + }); it("should return empty array when no data is found", async () => { let startIndex = 0; let itemsPerPage = 25; diff --git a/containers/orchestration/docker-compose.yml b/containers/orchestration/docker-compose.yml index bc6bee8e65..2afe03c0e6 100644 --- a/containers/orchestration/docker-compose.yml +++ b/containers/orchestration/docker-compose.yml @@ -33,6 +33,7 @@ services: - SOURCE=postgres - DATABASE_URL=postgres://postgres:pw@postgres:5432/ecr_viewer_db - APP_ENV=test + - NEXT_PUBLIC_NON_INTEGRATED_VIEWER=true orchestration-service: platform: linux/amd64 build: