Skip to content

Commit

Permalink
fix build without pg promise variables (#2996)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BobanL authored Dec 10, 2024
1 parent a2ef121 commit bc6cf1e
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 35 deletions.
10 changes: 10 additions & 0 deletions containers/ecr-viewer/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.next
.github
.env
docker-compose.yml
Dockerfile
node_modules
seed-scripts
cypress
design-review
.swc
16 changes: 0 additions & 16 deletions containers/ecr-viewer/.env

This file was deleted.

3 changes: 0 additions & 3 deletions containers/ecr-viewer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 1 addition & 2 deletions containers/ecr-viewer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions containers/ecr-viewer/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions containers/ecr-viewer/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */
const path = require("path");
const basePath = "/ecr-viewer";

const nextConfig = {
sassOptions: {
Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -435,6 +434,7 @@ export const saveMetadataToPostgres = async (
metadata: BundleMetadata,
ecrId: string,
) => {
const { database, pgPromise } = getDB();
const { ParameterizedQuery: PQ } = pgPromise;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
7 changes: 5 additions & 2 deletions containers/ecr-viewer/src/app/services/listEcrDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -96,6 +94,7 @@ async function listEcrDataPostgres(
searchTerm?: string,
filterConditions?: string[],
): Promise<EcrDisplay[]> {
const { database } = getDB();
const list = await database.manyOrNone<CoreMetadataModel>(
"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",
{
Expand Down Expand Up @@ -236,6 +235,7 @@ const getTotalEcrCountPostgres = async (
searchTerm?: string,
filterConditions?: string[],
): Promise<number> => {
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]",
{
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -352,6 +353,7 @@ export const generateFilterConditionsStatement = (
) => ({
rawType: true,
toPostgres: () => {
const { pgPromise } = getDB();
if (!filterConditions) {
return pgPromise.as.format("NULL IS NULL");
}
Expand Down Expand Up @@ -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"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,7 +16,11 @@ describe("Snapshot test for EcrLoadingSkeleton", () => {
});
window.IntersectionObserver = mockIntersectionObserver;

container = render(<EcrLoadingSkeleton />).container;
({ container } = render(<EcrLoadingSkeleton />));
});

afterAll(() => {
process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER = "false";
});
it("should match snapshot", () => {
expect(container).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions containers/orchestration/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bc6cf1e

Please sign in to comment.