-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Conditions endpoint * Fixes to get SQLServer to run, and fix SQL query * Unit tests * Revert the complete removal of saveSource * Remove debug statements, reduce size of test_type_system * Update tests with order by * Centralize SQL Server pool, and extended.sql * Removed renamed db.ts * [pre-commit.ci] auto fixes from pre-commit hooks * Update postgres_db name * Correct postgres spelling --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Josh Nygaard <[email protected]>
- Loading branch information
1 parent
594ea46
commit 4bc70ca
Showing
14 changed files
with
316 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextResponse } from "next/server"; | ||
import { get_conditions_postgres, get_conditions_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_conditions_postgres(); | ||
case "sqlserver": | ||
return await get_conditions_sqlserver(); | ||
default: | ||
return NextResponse.json( | ||
{ message: "Invalid metadata save location: " + metadataSaveLocation }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import pgPromise from "pg-promise"; | ||
import { database } from "../services/postgres_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. | ||
* @throws An error if the connection to the PostgreSQL database fails. | ||
*/ | ||
export const get_conditions_postgres = async () => { | ||
const { ParameterizedQuery: PQ } = pgPromise; | ||
|
||
try { | ||
const response = await database.tx(async (t) => { | ||
const getConditions = new PQ({ | ||
text: 'SELECT DISTINCT "condition" FROM ecr_rr_conditions ORDER BY "condition"', | ||
}); | ||
|
||
const conditions = await t.any(getConditions); | ||
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_conditions_sqlserver = async () => { | ||
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); | ||
|
||
const result = await request.query( | ||
"SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ORDER BY erc.[condition]", | ||
); | ||
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 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
containers/ecr-viewer/src/app/api/services/listEcrDataService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
containers/ecr-viewer/src/app/api/services/sqlserver_db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.