From 96cf5241427471db043599ed2794d32010813471 Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Tue, 19 Nov 2024 11:04:38 -0500 Subject: [PATCH 01/11] Conditions endpoint --- .../src/app/api/conditions/route.ts | 19 +++++ .../src/app/api/conditions/service.ts | 69 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 containers/ecr-viewer/src/app/api/conditions/route.ts create mode 100644 containers/ecr-viewer/src/app/api/conditions/service.ts diff --git a/containers/ecr-viewer/src/app/api/conditions/route.ts b/containers/ecr-viewer/src/app/api/conditions/route.ts new file mode 100644 index 000000000..3c5128eb7 --- /dev/null +++ b/containers/ecr-viewer/src/app/api/conditions/route.ts @@ -0,0 +1,19 @@ +import { NextResponse } from "next/server"; +import { get_postgres, get_sqlserver } from "./service"; + +/** + * Retrieves FHIR data from PostgreSQL database based on eCR ID. + * @returns A promise resolving to a NextResponse object. + */ +export async function GET() { + const metadataSaveLocation = process.env.METADATA_DATABASE_TYPE; + + switch (metadataSaveLocation) { + case "postgres": + return await get_postgres(); + case "sqlserver": + return await get_sqlserver(); + default: + return NextResponse.json({ message: "Invalid source" }, { status: 500 }); + } +} diff --git a/containers/ecr-viewer/src/app/api/conditions/service.ts b/containers/ecr-viewer/src/app/api/conditions/service.ts new file mode 100644 index 000000000..c270349f0 --- /dev/null +++ b/containers/ecr-viewer/src/app/api/conditions/service.ts @@ -0,0 +1,69 @@ +import pgPromise from "pg-promise"; +import { database } from "../services/db"; +import { NextResponse } from "next/server"; +import sql from "mssql"; +/** + * Retrieves all unique conditions from the ecr_rr_conditions table in the PostgreSQL database. + * @returns A promise resolving to a NextResponse object. + * @throws An error if the connection to the PostgreSQL database fails. + */ +export const get_postgres = async () => { + console.log("Get conditions from postgres"); + + const { ParameterizedQuery: PQ } = pgPromise; + + try { + const response = await database.tx(async (t) => { + const getConditions = new PQ({ + text: 'SELECT DISTINCT "condition" FROM ecr_rr_conditions', + }); + + const conditions = await t.any(getConditions); + console.log( + "Conditions:", + conditions.map((c) => c.condition), + ); + + return NextResponse.json( + conditions.map((c) => c.condition), + { status: 200 }, + ); + }); + + return response; + } catch (error: any) { + console.error("Error fetching data:", error); + return NextResponse.json({ message: error.message }, { status: 500 }); + } +}; + +/** + * Retrieves all unique conditions from the ecr_rr_conditions table in the SQL Server database. + * @returns A promise resolving to a NextResponse object. + * @throws An error if the connection to the SQL Server database fails. + */ +export const get_sqlserver = async () => { + let pool = await sql.connect({ + user: process.env.SQL_SERVER_USER, + password: process.env.SQL_SERVER_PASSWORD, + server: process.env.SQL_SERVER_HOST || "localhost", + options: { + trustServerCertificate: true, + }, + }); + + try { + const transaction = new sql.Transaction(pool); + const request = new sql.Request(transaction); + const result = await request.query( + "SELECT DISTINCT [condition] FROM ecr_rr_conditions", + ); + const conditions: string[] = result.recordset.map((row) => row.Condition); + + return NextResponse.json({ conditions }, { status: 200 }); + } catch (error: any) { + console.error("Error fetching data:", error); + return NextResponse.json({ message: error.message }, { status: 500 }); + } + console.log("I should not be here 2"); +}; From c73ecb5ccc0b3be31dcd263889f0d0fb795c0f2d Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Wed, 20 Nov 2024 14:26:05 -0500 Subject: [PATCH 02/11] Fixes to get SQLServer to run, and fix SQL query --- containers/ecr-viewer/docker-compose.yml | 4 + .../ecr-viewer/seed-scripts/sql/entrypoint.sh | 10 ++- .../ecr-viewer/seed-scripts/sql/extended.sql | 86 +++++++++++++++++++ .../src/app/api/conditions/service.ts | 14 ++- .../src/app/api/save-fhir-data/route.ts | 7 +- .../save-fhir-data/save-fhir-data-service.ts | 36 +++++--- .../philadelphia-ecr-viewer.json | 6 +- .../save-bundle-to-ecr-viewer.json | 3 - 8 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 containers/ecr-viewer/seed-scripts/sql/extended.sql diff --git a/containers/ecr-viewer/docker-compose.yml b/containers/ecr-viewer/docker-compose.yml index 589b8fdc4..a3bde9340 100644 --- a/containers/ecr-viewer/docker-compose.yml +++ b/containers/ecr-viewer/docker-compose.yml @@ -19,6 +19,10 @@ services: - 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} + - METADATA_DATABASE_SCHEMA=${METADATA_DATABASE_SCHEMA:-core} + - SQL_SERVER_USER=${SQL_SERVER_USER} + - SQL_SERVER_PASSWORD=${SQL_SERVER_PASSWORD} + - SQL_SERVER_HOST=${SQL_SERVER_HOST} profiles: - ecr-viewer diff --git a/containers/ecr-viewer/seed-scripts/sql/entrypoint.sh b/containers/ecr-viewer/seed-scripts/sql/entrypoint.sh index 5df20d28c..b072646b9 100755 --- a/containers/ecr-viewer/seed-scripts/sql/entrypoint.sh +++ b/containers/ecr-viewer/seed-scripts/sql/entrypoint.sh @@ -2,6 +2,14 @@ /opt/mssql/bin/sqlservr & +# Wait for SQL Server to start up +until /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $MSSQL_SA_PASSWORD -Q "SELECT 1" -C &>/dev/null; do + echo "Waiting for SQL Server to start..." + sleep 1 +done +echo "SQL Server ready." + +# Run your SQL script /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $MSSQL_SA_PASSWORD -d master -i /var/opt/mssql/scripts/extended.sql -C -wait \ No newline at end of file +wait diff --git a/containers/ecr-viewer/seed-scripts/sql/extended.sql b/containers/ecr-viewer/seed-scripts/sql/extended.sql new file mode 100644 index 000000000..1d13c5fa4 --- /dev/null +++ b/containers/ecr-viewer/seed-scripts/sql/extended.sql @@ -0,0 +1,86 @@ +CREATE TABLE ECR_DATA +( + eICR_ID VARCHAR(200) PRIMARY KEY, + set_id VARCHAR(255), + fhir_reference_link VARCHAR(255), + last_name VARCHAR(255), + first_name VARCHAR(255), + birth_date DATE, + gender VARCHAR(50), + birth_sex VARCHAR(50), + gender_identity VARCHAR(50), + race VARCHAR(255), + ethnicity VARCHAR(255), + street_address_1 VARCHAR(255), + street_address_2 VARCHAR(255), + state VARCHAR(50), + zip_code VARCHAR(20), + latitude FLOAT, + longitude FLOAT, + homelessness_status VARCHAR(255), + disabilities VARCHAR(255), + tribal_affiliation VARCHAR(255), + tribal_enrollment_status VARCHAR(255), + current_job_title VARCHAR(255), + current_job_industry VARCHAR(255), + usual_occupation VARCHAR(255), + usual_industry VARCHAR(255), + preferred_language VARCHAR(255), + pregnancy_status VARCHAR(255), + rr_id VARCHAR(255), + processing_status VARCHAR(255), + eicr_version_number VARCHAR(50), + authoring_date DATE, + authoring_time TIME, + authoring_provider VARCHAR(255), + provider_id VARCHAR(255), + facility_id VARCHAR(255), + facility_name VARCHAR(255), + encounter_type VARCHAR(255), + encounter_start_date DATE, + encounter_start_time TIME, + encounter_end_date DATE, + encounter_end_time TIME, + reason_for_visit VARCHAR(MAX), + active_problems VARCHAR(MAX) +); + +CREATE TABLE ecr_rr_conditions +( + UUID VARCHAR(200) PRIMARY KEY, + eICR_ID VARCHAR(200) NOT NULL REFERENCES ECR_DATA (eICR_ID), + condition VARCHAR(MAX) +); + +CREATE TABLE ecr_rr_rule_summaries +( + UUID VARCHAR(200) PRIMARY KEY, + ECR_RR_CONDITIONS_ID VARCHAR(200) REFERENCES ecr_rr_conditions (UUID), + rule_summary VARCHAR(MAX) +); + + +CREATE TABLE ecr_labs +( + UUID VARCHAR(200) PRIMARY KEY, + eICR_ID VARCHAR(200) REFERENCES ECR_DATA (eICR_ID), + test_type VARCHAR(255), + test_type_code VARCHAR(50), + test_type_system VARCHAR(MAX), + test_result_qualitative VARCHAR(MAX), + test_result_quantitative FLOAT, + test_result_units VARCHAR(50), + test_result_code VARCHAR(50), + test_result_code_display VARCHAR(255), + test_result_code_system VARCHAR(50), + test_result_interpretation VARCHAR(255), + test_result_interpretation_code VARCHAR(50), + test_result_interpretation_system VARCHAR(255), + test_result_reference_range_low_value FLOAT, + test_result_reference_range_low_units VARCHAR(50), + test_result_reference_range_high_value FLOAT, + test_result_reference_range_high_units VARCHAR(50), + specimen_type VARCHAR(255), + specimen_collection_date DATE, + performing_lab VARCHAR(255) +); diff --git a/containers/ecr-viewer/src/app/api/conditions/service.ts b/containers/ecr-viewer/src/app/api/conditions/service.ts index c270349f0..2ab6a4532 100644 --- a/containers/ecr-viewer/src/app/api/conditions/service.ts +++ b/containers/ecr-viewer/src/app/api/conditions/service.ts @@ -19,11 +19,6 @@ export const get_postgres = async () => { }); const conditions = await t.any(getConditions); - console.log( - "Conditions:", - conditions.map((c) => c.condition), - ); - return NextResponse.json( conditions.map((c) => c.condition), { status: 200 }, @@ -54,16 +49,17 @@ export const get_sqlserver = async () => { try { const transaction = new sql.Transaction(pool); + await transaction.begin(); const request = new sql.Request(transaction); + const result = await request.query( - "SELECT DISTINCT [condition] FROM ecr_rr_conditions", + "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ", ); - const conditions: string[] = result.recordset.map((row) => row.Condition); + const conditions: string[] = result.recordset.map((row) => row.condition); - return NextResponse.json({ conditions }, { status: 200 }); + return NextResponse.json(conditions, { status: 200 }); } catch (error: any) { console.error("Error fetching data:", error); return NextResponse.json({ message: error.message }, { status: 500 }); } - console.log("I should not be here 2"); }; diff --git a/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts b/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts index d5b7f12c7..03dc40a1a 100644 --- a/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts +++ b/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts @@ -10,13 +10,12 @@ import { S3_SOURCE, AZURE_SOURCE, POSTGRES_SOURCE } from "@/app/api/utils"; export async function POST(request: NextRequest) { let requestBody; let fhirBundle; - let saveSource; let ecrId; + const saveSource = process.env.SOURCE; try { requestBody = await request.json(); fhirBundle = requestBody.fhirBundle; - saveSource = requestBody.saveSource; ecrId = requestBody.fhirBundle.entry[0].resource.id; } catch (error: any) { console.error("Error reading request body:", error); @@ -36,10 +35,6 @@ export async function POST(request: NextRequest) { ); } - if (!saveSource) { - saveSource = process.env.SOURCE; - } - if ( [S3_SOURCE, AZURE_SOURCE, POSTGRES_SOURCE].includes(saveSource) == false ) { 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 31a139c68..828e7335d 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 @@ -175,14 +175,22 @@ export const saveMetadataToSqlServer = async ( metadata: BundleExtendedMetadata, ecrId: string, ) => { - let pool = await sql.connect({ - user: process.env.SQL_SERVER_USER, - password: process.env.SQL_SERVER_PASSWORD, - server: process.env.SQL_SERVER_HOST || "localhost", - options: { - trustServerCertificate: true, - }, - }); + let pool; + try { + pool = await sql.connect({ + user: process.env.SQL_SERVER_USER, + password: process.env.SQL_SERVER_PASSWORD, + server: process.env.SQL_SERVER_HOST || "localhost", + options: { + trustServerCertificate: true, + }, + }); + } catch (error: any) { + return NextResponse.json( + { message: "Failed to connect to SQL Server. " + error.message }, + { status: 500 }, + ); + } const transaction = new sql.Transaction(pool); await transaction.begin(); @@ -290,10 +298,14 @@ export const saveMetadataToSqlServer = async ( .input("eICR_ID", sql.VarChar(200), metadata.eicr_id) .input("test_type", sql.VarChar(200), lab.test_type) .input("test_type_code", sql.VarChar(50), lab.test_type_code) - .input("test_type_system", sql.VarChar(50), lab.test_type_system) + .input( + "test_type_system", + sql.VarChar(sql.MAX), + lab.test_type_system, + ) .input( "test_result_qualitative", - sql.VarChar(255), + sql.VarChar(sql.MAX), lab.test_result_qualitative, ) .input( @@ -383,6 +395,7 @@ export const saveMetadataToSqlServer = async ( { status: 200 }, ); } catch (error: any) { + console.log(metadata); console.error("Error inserting metadata to database:", error); // Rollback the transaction if any error occurs @@ -494,6 +507,7 @@ export const saveWithMetadata = async ( saveSource: string, metadata: BundleMetadata | BundleExtendedMetadata, ) => { + console.log("Saving FHIR data with metadata"); let fhirDataResult; let metadataResult; const metadataSaveLocation = process.env.METADATA_DATABASE_TYPE; @@ -501,11 +515,13 @@ export const saveWithMetadata = async ( fhirDataResult = await saveFhirData(fhirBundle, ecrId, saveSource); if (metadataSaveLocation == "postgres") { + console.log("Saving metadata to Postgres"); metadataResult = await saveMetadataToPostgres( metadata as BundleMetadata, ecrId, ); } else if (metadataSaveLocation == "sqlserver") { + console.log("Saving metadata to SQL Server"); metadataResult = await saveMetadataToSqlServer( metadata as BundleExtendedMetadata, ecrId, diff --git a/containers/orchestration/app/custom_configs/philadelphia-ecr-viewer.json b/containers/orchestration/app/custom_configs/philadelphia-ecr-viewer.json index 1ff12eca2..22a570eac 100644 --- a/containers/orchestration/app/custom_configs/philadelphia-ecr-viewer.json +++ b/containers/orchestration/app/custom_configs/philadelphia-ecr-viewer.json @@ -45,5 +45,9 @@ "stamped_ecr": "fhirBundle" } } - ] + ], + "outputs": [ + "metadata_values" + ], + "default-response": false } diff --git a/containers/orchestration/app/custom_configs/save-bundle-to-ecr-viewer.json b/containers/orchestration/app/custom_configs/save-bundle-to-ecr-viewer.json index 51ce1be9b..0734c3cd1 100644 --- a/containers/orchestration/app/custom_configs/save-bundle-to-ecr-viewer.json +++ b/containers/orchestration/app/custom_configs/save-bundle-to-ecr-viewer.json @@ -13,9 +13,6 @@ "name:": "save_bundle", "service": "save_bundle", "endpoint": "/api/save-fhir-data", - "params": { - "saveSource": "postgres" - }, "previous_response_to_param_mapping": { "metadata_values": "metadata" } From a88d141c68fb2b66cd8e7d8254cfa2ad9460e9c1 Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Wed, 20 Nov 2024 14:50:59 -0500 Subject: [PATCH 03/11] Unit tests --- .../src/app/api/conditions/route.ts | 6 +- .../src/app/api/conditions/service.ts | 4 +- .../src/app/api/tests/service.test.ts | 195 ++++++++++++++++++ 3 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 containers/ecr-viewer/src/app/api/tests/service.test.ts diff --git a/containers/ecr-viewer/src/app/api/conditions/route.ts b/containers/ecr-viewer/src/app/api/conditions/route.ts index 3c5128eb7..7ee5a410d 100644 --- a/containers/ecr-viewer/src/app/api/conditions/route.ts +++ b/containers/ecr-viewer/src/app/api/conditions/route.ts @@ -1,5 +1,5 @@ import { NextResponse } from "next/server"; -import { get_postgres, get_sqlserver } from "./service"; +import { get_conditions_postgres, get_conditions_sqlserver } from "./service"; /** * Retrieves FHIR data from PostgreSQL database based on eCR ID. @@ -10,9 +10,9 @@ export async function GET() { switch (metadataSaveLocation) { case "postgres": - return await get_postgres(); + return await get_conditions_postgres(); case "sqlserver": - return await get_sqlserver(); + return await get_conditions_sqlserver(); default: return NextResponse.json({ message: "Invalid source" }, { status: 500 }); } diff --git a/containers/ecr-viewer/src/app/api/conditions/service.ts b/containers/ecr-viewer/src/app/api/conditions/service.ts index 2ab6a4532..70e6b7b2e 100644 --- a/containers/ecr-viewer/src/app/api/conditions/service.ts +++ b/containers/ecr-viewer/src/app/api/conditions/service.ts @@ -7,7 +7,7 @@ import sql from "mssql"; * @returns A promise resolving to a NextResponse object. * @throws An error if the connection to the PostgreSQL database fails. */ -export const get_postgres = async () => { +export const get_conditions_postgres = async () => { console.log("Get conditions from postgres"); const { ParameterizedQuery: PQ } = pgPromise; @@ -37,7 +37,7 @@ export const get_postgres = async () => { * @returns A promise resolving to a NextResponse object. * @throws An error if the connection to the SQL Server database fails. */ -export const get_sqlserver = async () => { +export const get_conditions_sqlserver = async () => { let pool = await sql.connect({ user: process.env.SQL_SERVER_USER, password: process.env.SQL_SERVER_PASSWORD, diff --git a/containers/ecr-viewer/src/app/api/tests/service.test.ts b/containers/ecr-viewer/src/app/api/tests/service.test.ts new file mode 100644 index 000000000..3b1f90629 --- /dev/null +++ b/containers/ecr-viewer/src/app/api/tests/service.test.ts @@ -0,0 +1,195 @@ +import { + get_conditions_postgres, + get_conditions_sqlserver, +} from "@/app/api/conditions/service"; +import { database } from "@/app/api/services/db"; +import { NextResponse } from "next/server"; + +jest.mock("mssql", () => ({})); // Mock the mssql module + +jest.mock("mssql", () => { + const mssql = { + connect: jest.fn(), + Transaction: jest.fn(), + Request: jest.fn(), + // You can add other mocks if needed + }; + return mssql; +}); + +jest.mock("../services/db", () => ({ + database: { + tx: jest.fn(), + }, +})); + +jest.mock("next/server", () => ({ + NextResponse: { + json: jest.fn(), + }, +})); + +describe("get_conditions_postgres", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should return conditions when database query succeeds", async () => { + const mockConditions = [ + { condition: "condition1" }, + { condition: "condition2" }, + ]; + + (database.tx as jest.Mock).mockImplementation(async (callback: any) => { + const t = { + any: jest.fn().mockResolvedValue(mockConditions), + }; + return callback(t); + }); + + (NextResponse.json as jest.Mock).mockImplementation( + (data: any, options: any) => { + return { data, options }; + }, + ); + + const response = await get_conditions_postgres(); + + expect(response).toEqual({ + data: ["condition1", "condition2"], + options: { status: 200 }, + }); + expect(NextResponse.json).toHaveBeenCalledWith( + ["condition1", "condition2"], + { status: 200 }, + ); + }); + + it("should handle error when database query fails", async () => { + const errorMessage = "Database error"; + + (database.tx as jest.Mock).mockImplementation(async () => { + throw new Error(errorMessage); + }); + + (NextResponse.json as jest.Mock).mockImplementation( + (data: any, options: any) => { + return { data, options }; + }, + ); + + const response = await get_conditions_postgres(); + + expect(response).toEqual({ + data: { message: errorMessage }, + options: { status: 500 }, + }); + expect(NextResponse.json).toHaveBeenCalledWith( + { message: errorMessage }, + { status: 500 }, + ); + }); +}); + +describe("get_conditions_sqlserver", () => { + const sql = require("mssql"); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should return conditions when database query succeeds", async () => { + const mockConditions = [ + { condition: "condition1" }, + { condition: "condition2" }, + ]; + + // Mock sql.connect + sql.connect.mockResolvedValue({}); + + // Mock Transaction + const mockTransaction = { + begin: jest.fn().mockResolvedValue(undefined), + commit: jest.fn().mockResolvedValue(undefined), + rollback: jest.fn().mockResolvedValue(undefined), + }; + sql.Transaction.mockImplementation(() => mockTransaction); + + // Mock Request + const mockRequest = { + query: jest.fn().mockResolvedValue({ recordset: mockConditions }), + }; + sql.Request.mockImplementation(() => mockRequest); + + // Mock NextResponse.json + (NextResponse.json as jest.Mock).mockImplementation( + (data: any, options: any) => { + return { data, options }; + }, + ); + + const response = await get_conditions_sqlserver(); + + expect(response).toEqual({ + data: ["condition1", "condition2"], + options: { status: 200 }, + }); + + expect(NextResponse.json).toHaveBeenCalledWith( + ["condition1", "condition2"], + { status: 200 }, + ); + + // Verify that the SQL methods were called as expected + expect(sql.connect).toHaveBeenCalled(); + expect(mockTransaction.begin).toHaveBeenCalled(); + expect(mockRequest.query).toHaveBeenCalledWith( + "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ", + ); + }); + + it("should handle error when database query fails", async () => { + const errorMessage = "Database error"; + + // Mock sql.connect + sql.connect.mockResolvedValue({}); + + // Mock Transaction that will throw an error + const mockTransaction = { + begin: jest.fn().mockResolvedValue(undefined), + }; + sql.Transaction.mockImplementation(() => mockTransaction); + + // Mock Request that throws an error + const mockRequest = { + query: jest.fn().mockRejectedValue(new Error(errorMessage)), + }; + sql.Request.mockImplementation(() => mockRequest); + + // Mock NextResponse.json + (NextResponse.json as jest.Mock).mockImplementation( + (data: any, options: any) => { + return { data, options }; + }, + ); + + const response = await get_conditions_sqlserver(); + + expect(response).toEqual({ + data: { message: errorMessage }, + options: { status: 500 }, + }); + + expect(NextResponse.json).toHaveBeenCalledWith( + { message: errorMessage }, + { status: 500 }, + ); + + // Verify that the SQL methods were called as expected + expect(sql.connect).toHaveBeenCalled(); + expect(mockTransaction.begin).toHaveBeenCalled(); + expect(mockRequest.query).toHaveBeenCalledWith( + "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ", + ); + }); +}); From 6a3d4755ddd3d26cb3a99e92fe847feb84a00fcf Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Wed, 20 Nov 2024 14:56:58 -0500 Subject: [PATCH 04/11] Revert the complete removal of saveSource --- containers/ecr-viewer/src/app/api/save-fhir-data/route.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts b/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts index 03dc40a1a..98dca77de 100644 --- a/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts +++ b/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts @@ -11,7 +11,6 @@ export async function POST(request: NextRequest) { let requestBody; let fhirBundle; let ecrId; - const saveSource = process.env.SOURCE; try { requestBody = await request.json(); @@ -35,6 +34,8 @@ export async function POST(request: NextRequest) { ); } + let saveSource = requestBody.saveSource || process.env.SOURCE; + if ( [S3_SOURCE, AZURE_SOURCE, POSTGRES_SOURCE].includes(saveSource) == false ) { From 34a880f7f6962e083e9cf119db0b3668caf7df13 Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Wed, 20 Nov 2024 18:16:26 -0500 Subject: [PATCH 05/11] Remove debug statements, reduce size of test_type_system --- containers/ecr-viewer/docker-compose.yml | 2 +- containers/ecr-viewer/seed-scripts/sql/extended.sql | 2 +- .../ecr-viewer/src/app/api/conditions/service.ts | 6 ++---- .../ecr-viewer/src/app/api/save-fhir-data/route.ts | 2 +- .../app/api/save-fhir-data/save-fhir-data-service.ts | 10 +--------- 5 files changed, 6 insertions(+), 16 deletions(-) diff --git a/containers/ecr-viewer/docker-compose.yml b/containers/ecr-viewer/docker-compose.yml index a3bde9340..771a49db1 100644 --- a/containers/ecr-viewer/docker-compose.yml +++ b/containers/ecr-viewer/docker-compose.yml @@ -22,7 +22,7 @@ services: - METADATA_DATABASE_SCHEMA=${METADATA_DATABASE_SCHEMA:-core} - SQL_SERVER_USER=${SQL_SERVER_USER} - SQL_SERVER_PASSWORD=${SQL_SERVER_PASSWORD} - - SQL_SERVER_HOST=${SQL_SERVER_HOST} + - SQL_SERVER_HOST=${SQL_SERVER_HOST:-sqlserver} profiles: - ecr-viewer diff --git a/containers/ecr-viewer/seed-scripts/sql/extended.sql b/containers/ecr-viewer/seed-scripts/sql/extended.sql index 1d13c5fa4..dd606bd30 100644 --- a/containers/ecr-viewer/seed-scripts/sql/extended.sql +++ b/containers/ecr-viewer/seed-scripts/sql/extended.sql @@ -66,7 +66,7 @@ CREATE TABLE ecr_labs eICR_ID VARCHAR(200) REFERENCES ECR_DATA (eICR_ID), test_type VARCHAR(255), test_type_code VARCHAR(50), - test_type_system VARCHAR(MAX), + test_type_system VARCHAR(255), test_result_qualitative VARCHAR(MAX), test_result_quantitative FLOAT, test_result_units VARCHAR(50), diff --git a/containers/ecr-viewer/src/app/api/conditions/service.ts b/containers/ecr-viewer/src/app/api/conditions/service.ts index 70e6b7b2e..ad121d4de 100644 --- a/containers/ecr-viewer/src/app/api/conditions/service.ts +++ b/containers/ecr-viewer/src/app/api/conditions/service.ts @@ -8,14 +8,12 @@ import sql from "mssql"; * @throws An error if the connection to the PostgreSQL database fails. */ export const get_conditions_postgres = async () => { - console.log("Get conditions from postgres"); - const { ParameterizedQuery: PQ } = pgPromise; try { const response = await database.tx(async (t) => { const getConditions = new PQ({ - text: 'SELECT DISTINCT "condition" FROM ecr_rr_conditions', + text: 'SELECT DISTINCT "condition" FROM ecr_rr_conditions ORDER BY "condition"', }); const conditions = await t.any(getConditions); @@ -53,7 +51,7 @@ export const get_conditions_sqlserver = async () => { const request = new sql.Request(transaction); const result = await request.query( - "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ", + "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ORDER BY erc.[condition]", ); const conditions: string[] = result.recordset.map((row) => row.condition); diff --git a/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts b/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts index 98dca77de..1f51ff90a 100644 --- a/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts +++ b/containers/ecr-viewer/src/app/api/save-fhir-data/route.ts @@ -34,7 +34,7 @@ export async function POST(request: NextRequest) { ); } - let saveSource = requestBody.saveSource || process.env.SOURCE; + const saveSource = requestBody.saveSource || process.env.SOURCE; if ( [S3_SOURCE, AZURE_SOURCE, POSTGRES_SOURCE].includes(saveSource) == false 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 828e7335d..d02f16ab1 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 @@ -298,11 +298,7 @@ export const saveMetadataToSqlServer = async ( .input("eICR_ID", sql.VarChar(200), metadata.eicr_id) .input("test_type", sql.VarChar(200), lab.test_type) .input("test_type_code", sql.VarChar(50), lab.test_type_code) - .input( - "test_type_system", - sql.VarChar(sql.MAX), - lab.test_type_system, - ) + .input("test_type_system", sql.VarChar(255), lab.test_type_system) .input( "test_result_qualitative", sql.VarChar(sql.MAX), @@ -395,7 +391,6 @@ export const saveMetadataToSqlServer = async ( { status: 200 }, ); } catch (error: any) { - console.log(metadata); console.error("Error inserting metadata to database:", error); // Rollback the transaction if any error occurs @@ -507,7 +502,6 @@ export const saveWithMetadata = async ( saveSource: string, metadata: BundleMetadata | BundleExtendedMetadata, ) => { - console.log("Saving FHIR data with metadata"); let fhirDataResult; let metadataResult; const metadataSaveLocation = process.env.METADATA_DATABASE_TYPE; @@ -515,13 +509,11 @@ export const saveWithMetadata = async ( fhirDataResult = await saveFhirData(fhirBundle, ecrId, saveSource); if (metadataSaveLocation == "postgres") { - console.log("Saving metadata to Postgres"); metadataResult = await saveMetadataToPostgres( metadata as BundleMetadata, ecrId, ); } else if (metadataSaveLocation == "sqlserver") { - console.log("Saving metadata to SQL Server"); metadataResult = await saveMetadataToSqlServer( metadata as BundleExtendedMetadata, ecrId, From b22d6e7196e479f286800d3d3edd9338ac064000 Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Wed, 20 Nov 2024 18:28:27 -0500 Subject: [PATCH 06/11] Update tests with order by --- containers/ecr-viewer/src/app/api/tests/service.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/containers/ecr-viewer/src/app/api/tests/service.test.ts b/containers/ecr-viewer/src/app/api/tests/service.test.ts index 3b1f90629..2317e9d00 100644 --- a/containers/ecr-viewer/src/app/api/tests/service.test.ts +++ b/containers/ecr-viewer/src/app/api/tests/service.test.ts @@ -144,7 +144,7 @@ describe("get_conditions_sqlserver", () => { expect(sql.connect).toHaveBeenCalled(); expect(mockTransaction.begin).toHaveBeenCalled(); expect(mockRequest.query).toHaveBeenCalledWith( - "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ", + "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ORDER BY erc.[condition]", ); }); @@ -189,7 +189,7 @@ describe("get_conditions_sqlserver", () => { expect(sql.connect).toHaveBeenCalled(); expect(mockTransaction.begin).toHaveBeenCalled(); expect(mockRequest.query).toHaveBeenCalledWith( - "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ", + "SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ORDER BY erc.[condition]", ); }); }); From 92fc7234e40b7565ea58227cc3048294c8728a46 Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Mon, 25 Nov 2024 11:16:35 -0500 Subject: [PATCH 07/11] Centralize SQL Server pool, and extended.sql --- containers/ecr-viewer/docker-compose.yml | 1 + .../ecr-viewer/seed-scripts/sql/extended.sql | 86 ------------------- containers/ecr-viewer/sql/extended.sql | 4 +- .../src/app/api/conditions/route.ts | 5 +- .../src/app/api/conditions/service.ts | 20 ++--- .../app/api/fhir-data/fhir-data-service.ts | 2 +- .../save-fhir-data/save-fhir-data-service.ts | 19 ++-- .../app/api/services/listEcrDataService.ts | 2 +- .../src/app/api/services/postgress_db.ts | 4 + .../src/app/api/services/sqlserver_db.ts | 17 ++++ .../app/api/tests/listEcrDataService.test.tsx | 2 +- .../src/app/api/tests/service.test.ts | 2 +- .../ecr-viewer/src/app/view-data/page.tsx | 6 +- 13 files changed, 50 insertions(+), 120 deletions(-) delete mode 100644 containers/ecr-viewer/seed-scripts/sql/extended.sql create mode 100644 containers/ecr-viewer/src/app/api/services/postgress_db.ts create mode 100644 containers/ecr-viewer/src/app/api/services/sqlserver_db.ts diff --git a/containers/ecr-viewer/docker-compose.yml b/containers/ecr-viewer/docker-compose.yml index 771a49db1..be351a2cb 100644 --- a/containers/ecr-viewer/docker-compose.yml +++ b/containers/ecr-viewer/docker-compose.yml @@ -35,6 +35,7 @@ services: - ./sql/core.sql:/docker-entrypoint-initdb.d/core.sql - ./seed-scripts/sql/.pgpass/:/usr/local/lib/.pgpass - db:/var/lib/postgresql/data + - ./sql/extended.sql:/var/opt/mssql/scripts/extended.sql environment: - POSTGRES_USER=postgres - PGUSER=postgres diff --git a/containers/ecr-viewer/seed-scripts/sql/extended.sql b/containers/ecr-viewer/seed-scripts/sql/extended.sql deleted file mode 100644 index dd606bd30..000000000 --- a/containers/ecr-viewer/seed-scripts/sql/extended.sql +++ /dev/null @@ -1,86 +0,0 @@ -CREATE TABLE ECR_DATA -( - eICR_ID VARCHAR(200) PRIMARY KEY, - set_id VARCHAR(255), - fhir_reference_link VARCHAR(255), - last_name VARCHAR(255), - first_name VARCHAR(255), - birth_date DATE, - gender VARCHAR(50), - birth_sex VARCHAR(50), - gender_identity VARCHAR(50), - race VARCHAR(255), - ethnicity VARCHAR(255), - street_address_1 VARCHAR(255), - street_address_2 VARCHAR(255), - state VARCHAR(50), - zip_code VARCHAR(20), - latitude FLOAT, - longitude FLOAT, - homelessness_status VARCHAR(255), - disabilities VARCHAR(255), - tribal_affiliation VARCHAR(255), - tribal_enrollment_status VARCHAR(255), - current_job_title VARCHAR(255), - current_job_industry VARCHAR(255), - usual_occupation VARCHAR(255), - usual_industry VARCHAR(255), - preferred_language VARCHAR(255), - pregnancy_status VARCHAR(255), - rr_id VARCHAR(255), - processing_status VARCHAR(255), - eicr_version_number VARCHAR(50), - authoring_date DATE, - authoring_time TIME, - authoring_provider VARCHAR(255), - provider_id VARCHAR(255), - facility_id VARCHAR(255), - facility_name VARCHAR(255), - encounter_type VARCHAR(255), - encounter_start_date DATE, - encounter_start_time TIME, - encounter_end_date DATE, - encounter_end_time TIME, - reason_for_visit VARCHAR(MAX), - active_problems VARCHAR(MAX) -); - -CREATE TABLE ecr_rr_conditions -( - UUID VARCHAR(200) PRIMARY KEY, - eICR_ID VARCHAR(200) NOT NULL REFERENCES ECR_DATA (eICR_ID), - condition VARCHAR(MAX) -); - -CREATE TABLE ecr_rr_rule_summaries -( - UUID VARCHAR(200) PRIMARY KEY, - ECR_RR_CONDITIONS_ID VARCHAR(200) REFERENCES ecr_rr_conditions (UUID), - rule_summary VARCHAR(MAX) -); - - -CREATE TABLE ecr_labs -( - UUID VARCHAR(200) PRIMARY KEY, - eICR_ID VARCHAR(200) REFERENCES ECR_DATA (eICR_ID), - test_type VARCHAR(255), - test_type_code VARCHAR(50), - test_type_system VARCHAR(255), - test_result_qualitative VARCHAR(MAX), - test_result_quantitative FLOAT, - test_result_units VARCHAR(50), - test_result_code VARCHAR(50), - test_result_code_display VARCHAR(255), - test_result_code_system VARCHAR(50), - test_result_interpretation VARCHAR(255), - test_result_interpretation_code VARCHAR(50), - test_result_interpretation_system VARCHAR(255), - test_result_reference_range_low_value FLOAT, - test_result_reference_range_low_units VARCHAR(50), - test_result_reference_range_high_value FLOAT, - test_result_reference_range_high_units VARCHAR(50), - specimen_type VARCHAR(255), - specimen_collection_date DATE, - performing_lab VARCHAR(255) -); diff --git a/containers/ecr-viewer/sql/extended.sql b/containers/ecr-viewer/sql/extended.sql index bf345d6b5..dd606bd30 100644 --- a/containers/ecr-viewer/sql/extended.sql +++ b/containers/ecr-viewer/sql/extended.sql @@ -66,8 +66,8 @@ CREATE TABLE ecr_labs eICR_ID VARCHAR(200) REFERENCES ECR_DATA (eICR_ID), test_type VARCHAR(255), test_type_code VARCHAR(50), - test_type_system VARCHAR(50), - test_result_qualitative VARCHAR(255), + test_type_system VARCHAR(255), + test_result_qualitative VARCHAR(MAX), test_result_quantitative FLOAT, test_result_units VARCHAR(50), test_result_code VARCHAR(50), diff --git a/containers/ecr-viewer/src/app/api/conditions/route.ts b/containers/ecr-viewer/src/app/api/conditions/route.ts index 7ee5a410d..cc1206d46 100644 --- a/containers/ecr-viewer/src/app/api/conditions/route.ts +++ b/containers/ecr-viewer/src/app/api/conditions/route.ts @@ -14,6 +14,9 @@ export async function GET() { case "sqlserver": return await get_conditions_sqlserver(); default: - return NextResponse.json({ message: "Invalid source" }, { status: 500 }); + return NextResponse.json( + { message: "Invalid metadata save location: " + metadataSaveLocation }, + { status: 500 }, + ); } } diff --git a/containers/ecr-viewer/src/app/api/conditions/service.ts b/containers/ecr-viewer/src/app/api/conditions/service.ts index ad121d4de..f7caa6be0 100644 --- a/containers/ecr-viewer/src/app/api/conditions/service.ts +++ b/containers/ecr-viewer/src/app/api/conditions/service.ts @@ -1,7 +1,8 @@ import pgPromise from "pg-promise"; -import { database } from "../services/db"; +import { database } from "../services/postgress_db"; import { NextResponse } from "next/server"; import sql from "mssql"; +import { get_pool } from "../services/sqlserver_db"; /** * Retrieves all unique conditions from the ecr_rr_conditions table in the PostgreSQL database. * @returns A promise resolving to a NextResponse object. @@ -36,16 +37,15 @@ export const get_conditions_postgres = async () => { * @throws An error if the connection to the SQL Server database fails. */ export const get_conditions_sqlserver = async () => { - let pool = await sql.connect({ - user: process.env.SQL_SERVER_USER, - password: process.env.SQL_SERVER_PASSWORD, - server: process.env.SQL_SERVER_HOST || "localhost", - options: { - trustServerCertificate: true, - }, - }); - try { + let pool = await get_pool(); + if (!pool) { + return NextResponse.json( + { message: "Failed to connect to SQL Server." }, + { status: 500 }, + ); + } + const transaction = new sql.Transaction(pool); await transaction.begin(); const request = new sql.Request(transaction); 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 289e782bf..f5c1d66c6 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 @@ -7,7 +7,7 @@ import { BlobServiceClient, } from "@azure/storage-blob"; import { loadYamlConfig, streamToJson } from "../utils"; -import { database } from "../services/db"; +import { database } from "../services/postgress_db"; import { s3Client } from "../services/s3Client"; /** 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 d02f16ab1..067707472 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 @@ -1,7 +1,7 @@ import { BlobServiceClient } from "@azure/storage-blob"; import { NextResponse } from "next/server"; import pgPromise from "pg-promise"; -import { database, db_url } from "../services/db"; +import { database, db_url } from "../services/postgress_db"; import { PutObjectCommand, PutObjectCommandOutput } from "@aws-sdk/client-s3"; import { Bundle } from "fhir/r4"; import { S3_SOURCE, AZURE_SOURCE, POSTGRES_SOURCE } from "@/app/api/utils"; @@ -9,6 +9,7 @@ import sql from "mssql"; import { randomUUID } from "crypto"; import { BundleExtendedMetadata, BundleMetadata } from "./types"; import { s3Client } from "../services/s3Client"; +import { get_pool } from "../services/sqlserver_db"; /** * Saves a FHIR bundle to a postgres database. @@ -175,19 +176,11 @@ export const saveMetadataToSqlServer = async ( metadata: BundleExtendedMetadata, ecrId: string, ) => { - let pool; - try { - pool = await sql.connect({ - user: process.env.SQL_SERVER_USER, - password: process.env.SQL_SERVER_PASSWORD, - server: process.env.SQL_SERVER_HOST || "localhost", - options: { - trustServerCertificate: true, - }, - }); - } catch (error: any) { + let pool = await get_pool(); + + if (!pool) { return NextResponse.json( - { message: "Failed to connect to SQL Server. " + error.message }, + { message: "Failed to connect to SQL Server." }, { status: 500 }, ); } diff --git a/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts b/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts index b3ca56c76..960b79fb7 100644 --- a/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts +++ b/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts @@ -1,4 +1,4 @@ -import { database } from "@/app/api/services/db"; +import { database } from "@/app/api/services/postgress_db"; import { convertUTCToLocalString, formatDate, diff --git a/containers/ecr-viewer/src/app/api/services/postgress_db.ts b/containers/ecr-viewer/src/app/api/services/postgress_db.ts new file mode 100644 index 000000000..ef5455bc8 --- /dev/null +++ b/containers/ecr-viewer/src/app/api/services/postgress_db.ts @@ -0,0 +1,4 @@ +import pgp from "pg-promise"; + +export const db_url = process.env.DATABASE_URL || ""; +export const database = pgp()(db_url); diff --git a/containers/ecr-viewer/src/app/api/services/sqlserver_db.ts b/containers/ecr-viewer/src/app/api/services/sqlserver_db.ts new file mode 100644 index 000000000..916c54a46 --- /dev/null +++ b/containers/ecr-viewer/src/app/api/services/sqlserver_db.ts @@ -0,0 +1,17 @@ +import sql from "mssql"; + +/** + * Connect to the SQL Server database and return a connection pool. + * @returns A promise resolving to a connection pool. + */ +export const get_pool = async () => { + let pool = await sql.connect({ + user: process.env.SQL_SERVER_USER, + password: process.env.SQL_SERVER_PASSWORD, + server: process.env.SQL_SERVER_HOST || "localhost", + options: { + trustServerCertificate: true, + }, + }); + return pool; +}; diff --git a/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx b/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx index 15fbeae82..2d9658a3d 100644 --- a/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx +++ b/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx @@ -9,7 +9,7 @@ import { getTotalEcrCount, generateSearchStatement, } from "@/app/api/services/listEcrDataService"; -import { database } from "../services/db"; +import { database } from "../services/postgress_db"; import { convertUTCToLocalString, formatDate, diff --git a/containers/ecr-viewer/src/app/api/tests/service.test.ts b/containers/ecr-viewer/src/app/api/tests/service.test.ts index 2317e9d00..bbcc0e14e 100644 --- a/containers/ecr-viewer/src/app/api/tests/service.test.ts +++ b/containers/ecr-viewer/src/app/api/tests/service.test.ts @@ -2,7 +2,7 @@ import { get_conditions_postgres, get_conditions_sqlserver, } from "@/app/api/conditions/service"; -import { database } from "@/app/api/services/db"; +import { database } from "@/app/api/services/postgress_db"; import { NextResponse } from "next/server"; jest.mock("mssql", () => ({})); // Mock the mssql module diff --git a/containers/ecr-viewer/src/app/view-data/page.tsx b/containers/ecr-viewer/src/app/view-data/page.tsx index 5a0bf683f..4e8e9865f 100644 --- a/containers/ecr-viewer/src/app/view-data/page.tsx +++ b/containers/ecr-viewer/src/app/view-data/page.tsx @@ -33,10 +33,8 @@ const ECRViewerPage: React.FC = () => { const [mappings, setMappings] = useState({}); const [errors, setErrors] = useState(); const searchParams = useSearchParams(); - const fhirId = searchParams ? (searchParams.get("id") ?? "") : ""; - const snomedCode = searchParams - ? (searchParams.get("snomed-code") ?? "") - : ""; + const fhirId = searchParams ? searchParams.get("id") ?? "" : ""; + const snomedCode = searchParams ? searchParams.get("snomed-code") ?? "" : ""; const isNonIntegratedViewer = process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER === "true"; From 3c7bf35407de2a8d4c45ff4cacea897b8c317407 Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Mon, 25 Nov 2024 11:17:06 -0500 Subject: [PATCH 08/11] Removed renamed db.ts --- containers/ecr-viewer/src/app/api/services/db.ts | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 containers/ecr-viewer/src/app/api/services/db.ts diff --git a/containers/ecr-viewer/src/app/api/services/db.ts b/containers/ecr-viewer/src/app/api/services/db.ts deleted file mode 100644 index ef5455bc8..000000000 --- a/containers/ecr-viewer/src/app/api/services/db.ts +++ /dev/null @@ -1,4 +0,0 @@ -import pgp from "pg-promise"; - -export const db_url = process.env.DATABASE_URL || ""; -export const database = pgp()(db_url); From 1e20e6cf360e4e72742a5ba7f46346dbc8e384ca Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Nov 2024 16:19:10 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit hooks --- containers/ecr-viewer/src/app/view-data/page.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/containers/ecr-viewer/src/app/view-data/page.tsx b/containers/ecr-viewer/src/app/view-data/page.tsx index 4e8e9865f..5a0bf683f 100644 --- a/containers/ecr-viewer/src/app/view-data/page.tsx +++ b/containers/ecr-viewer/src/app/view-data/page.tsx @@ -33,8 +33,10 @@ const ECRViewerPage: React.FC = () => { const [mappings, setMappings] = useState({}); const [errors, setErrors] = useState(); const searchParams = useSearchParams(); - const fhirId = searchParams ? searchParams.get("id") ?? "" : ""; - const snomedCode = searchParams ? searchParams.get("snomed-code") ?? "" : ""; + const fhirId = searchParams ? (searchParams.get("id") ?? "") : ""; + const snomedCode = searchParams + ? (searchParams.get("snomed-code") ?? "") + : ""; const isNonIntegratedViewer = process.env.NEXT_PUBLIC_NON_INTEGRATED_VIEWER === "true"; From c3d0c507ae889dbb7550649017a4c03c3100a2bb Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Mon, 25 Nov 2024 11:24:13 -0500 Subject: [PATCH 10/11] Update postgres_db name --- containers/ecr-viewer/src/app/api/tests/service.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/containers/ecr-viewer/src/app/api/tests/service.test.ts b/containers/ecr-viewer/src/app/api/tests/service.test.ts index bbcc0e14e..598a02ac6 100644 --- a/containers/ecr-viewer/src/app/api/tests/service.test.ts +++ b/containers/ecr-viewer/src/app/api/tests/service.test.ts @@ -17,7 +17,7 @@ jest.mock("mssql", () => { return mssql; }); -jest.mock("../services/db", () => ({ +jest.mock("../services/postgres_db", () => ({ database: { tx: jest.fn(), }, From 8e4b2dd125095a23c6de9817ade8adb3e789608e Mon Sep 17 00:00:00 2001 From: Josh Nygaard Date: Mon, 25 Nov 2024 11:35:08 -0500 Subject: [PATCH 11/11] Correct postgres spelling --- containers/ecr-viewer/src/app/api/conditions/service.ts | 2 +- .../ecr-viewer/src/app/api/fhir-data/fhir-data-service.ts | 2 +- .../src/app/api/save-fhir-data/save-fhir-data-service.ts | 2 +- .../ecr-viewer/src/app/api/services/listEcrDataService.ts | 2 +- .../src/app/api/services/{postgress_db.ts => postgres_db.ts} | 0 .../ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx | 2 +- containers/ecr-viewer/src/app/api/tests/service.test.ts | 2 +- 7 files changed, 6 insertions(+), 6 deletions(-) rename containers/ecr-viewer/src/app/api/services/{postgress_db.ts => postgres_db.ts} (100%) diff --git a/containers/ecr-viewer/src/app/api/conditions/service.ts b/containers/ecr-viewer/src/app/api/conditions/service.ts index f7caa6be0..d1f10707a 100644 --- a/containers/ecr-viewer/src/app/api/conditions/service.ts +++ b/containers/ecr-viewer/src/app/api/conditions/service.ts @@ -1,5 +1,5 @@ import pgPromise from "pg-promise"; -import { database } from "../services/postgress_db"; +import { database } from "../services/postgres_db"; import { NextResponse } from "next/server"; import sql from "mssql"; import { get_pool } from "../services/sqlserver_db"; 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 f5c1d66c6..d1b47767c 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 @@ -7,7 +7,7 @@ import { BlobServiceClient, } from "@azure/storage-blob"; import { loadYamlConfig, streamToJson } from "../utils"; -import { database } from "../services/postgress_db"; +import { database } from "../services/postgres_db"; import { s3Client } from "../services/s3Client"; /** 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 c9a1a3a8a..374af5e69 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 @@ -1,7 +1,7 @@ import { BlobServiceClient } from "@azure/storage-blob"; import { NextResponse } from "next/server"; import pgPromise from "pg-promise"; -import { database, db_url } from "../services/postgress_db"; +import { database, db_url } from "../services/postgres_db"; import { PutObjectCommand, PutObjectCommandOutput } from "@aws-sdk/client-s3"; import { Bundle } from "fhir/r4"; import { S3_SOURCE, AZURE_SOURCE, POSTGRES_SOURCE } from "@/app/api/utils"; diff --git a/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts b/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts index 392cc1533..20e5e1e9a 100644 --- a/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts +++ b/containers/ecr-viewer/src/app/api/services/listEcrDataService.ts @@ -1,4 +1,4 @@ -import { database } from "@/app/api/services/postgress_db"; +import { database } from "@/app/api/services/postgres_db"; import { convertUTCToLocalString, formatDate, diff --git a/containers/ecr-viewer/src/app/api/services/postgress_db.ts b/containers/ecr-viewer/src/app/api/services/postgres_db.ts similarity index 100% rename from containers/ecr-viewer/src/app/api/services/postgress_db.ts rename to containers/ecr-viewer/src/app/api/services/postgres_db.ts diff --git a/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx b/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx index 269cd6a90..36e24e365 100644 --- a/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx +++ b/containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx @@ -11,7 +11,7 @@ import { generateFilterConditionsStatement, generateWhereStatement, } from "@/app/api/services/listEcrDataService"; -import { database } from "../services/postgress_db"; +import { database } from "../services/postgres_db"; import { convertUTCToLocalString, formatDate, diff --git a/containers/ecr-viewer/src/app/api/tests/service.test.ts b/containers/ecr-viewer/src/app/api/tests/service.test.ts index 598a02ac6..ff47937d6 100644 --- a/containers/ecr-viewer/src/app/api/tests/service.test.ts +++ b/containers/ecr-viewer/src/app/api/tests/service.test.ts @@ -2,7 +2,7 @@ import { get_conditions_postgres, get_conditions_sqlserver, } from "@/app/api/conditions/service"; -import { database } from "@/app/api/services/postgress_db"; +import { database } from "@/app/api/services/postgres_db"; import { NextResponse } from "next/server"; jest.mock("mssql", () => ({})); // Mock the mssql module