Skip to content

Commit

Permalink
Add ECR Library sorting (#2812)
Browse files Browse the repository at this point in the history
* init

* Add ecrTableClient

* add handle sort onClick

* ooooohhhhh weeeeeee

* fix pagination

* workikng sort

* sort

* fetch data from serverv

* cleanup sort button

* update headers with state

* remove uneeded code

* things working good

* Attempt at styling

* latest

* merge with main

* rework

* update tests

* package lock

* Revert package-lock

* revert

* fix tests

* add next-router-mock

* add next-router-mock

* fix stupid mistake

* updoot snap

* PR requested changes

* fix snap
  • Loading branch information
lina-roth authored Nov 5, 2024
1 parent 4b91bdf commit b8f5d2a
Show file tree
Hide file tree
Showing 12 changed files with 1,536 additions and 2,685 deletions.
14 changes: 14 additions & 0 deletions containers/ecr-viewer/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ import "@testing-library/jest-dom";
import { toHaveNoViolations } from "jest-axe";
import * as matchers from "jest-extended";
import { TextEncoder } from "util";
import router from "next-router-mock";

global.TextEncoder = TextEncoder;

expect.extend(toHaveNoViolations);
expect.extend(matchers);

// Mocking `next/navigation` hooks
jest.mock("next/navigation", () => ({
useRouter: () => router,
usePathname: () => router.pathname,
useSearchParams: () => {
const params = new URLSearchParams(router.asPath.split("?")[1]);
return {
get: params.get.bind(params),
toString: () => params.toString(),
};
},
}));
1 change: 1 addition & 0 deletions containers/ecr-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"mssql": "^11.0.1",
"next": "^14.2.14",
"next-auth": "^4.24.7",
"next-router-mock": "^0.9.13",
"pg-promise": "^11.6.0",
"react": "^18",
"react-dom": "^18",
Expand Down
45 changes: 44 additions & 1 deletion containers/ecr-viewer/src/app/api/services/listEcrDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ export type EcrDisplay = {
/**
* @param startIndex - The index of the first item to fetch
* @param itemsPerPage - The number of items to fetch
* @param sortColumn - The column to sort by
* @param sortDirection - The direction to sort by
* @param searchTerm - The search term to use
* @returns A promise resolving to a list of eCR metadata
*/
export async function listEcrData(
startIndex: number,
itemsPerPage: number,
sortColumn: string,
sortDirection: string,
searchTerm?: string,
): Promise<EcrDisplay[]> {
const list = await database.manyOrNone<EcrMetadataModel>(
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] order by ed.report_date DESC OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
{
whereClause: generateSearchStatement(searchTerm),
startIndex,
itemsPerPage,
sortStatement: generateSortStatement(sortColumn, sortDirection),
},
);
return processMetadata(list);
Expand Down Expand Up @@ -121,3 +126,41 @@ export const generateSearchStatement = (searchTerm?: string) => ({
.join(" OR ");
},
});

/**
* A custom type format for sort statement
* @param columnName - The column to sort by
* @param direction - The direction to sort by
* @returns custom type format object for use by pg-promise
*/
export const generateSortStatement = (
columnName: string,
direction: string,
) => ({
rawType: true,
toPostgres: () => {
// Valid columns and directions
const validColumns = ["patient", "date_created", "report_date"];
const validDirections = ["ASC", "DESC"];

// Validation check
if (!validColumns.includes(columnName)) {
columnName = "date_created";
}
if (!validDirections.includes(direction)) {
direction = "DESC";
}

if (columnName === "patient") {
return pgPromise.as.format(
`ORDER BY ed.patient_name_last ${direction}, ed.patient_name_first ${direction}`,
{ direction },
);
}

// Default case for other columns
return pgPromise.as.format(`ORDER BY $[columnName:raw] ${direction}`, {
columnName,
});
},
});
62 changes: 52 additions & 10 deletions containers/ecr-viewer/src/app/api/tests/listEcrDataService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,23 @@ describe("listEcrDataService", () => {
it("should return empty array when no data is found", async () => {
let startIndex = 0;
let itemsPerPage = 25;
let columnName = "date_created";
let direction = "DESC";
database.manyOrNone = jest.fn(() => Promise.resolve([]));
const actual = await listEcrData(startIndex, itemsPerPage);
const actual = await listEcrData(
startIndex,
itemsPerPage,
columnName,
direction,
);
expect(database.manyOrNone).toHaveBeenCalledOnce();
expect(database.manyOrNone).toHaveBeenCalledWith(
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] order by ed.report_date DESC OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
{
whereClause: expect.any(Object),
startIndex,
itemsPerPage,
sortStatement: expect.any(Object),
},
);
expect(actual).toBeEmpty();
Expand All @@ -137,15 +145,23 @@ describe("listEcrDataService", () => {

let startIndex = 0;
let itemsPerPage = 25;
const actual: EcrDisplay[] = await listEcrData(startIndex, itemsPerPage);
let columnName = "date_created";
let direction = "DESC";
const actual: EcrDisplay[] = await listEcrData(
startIndex,
itemsPerPage,
columnName,
direction,
);

expect(database.manyOrNone).toHaveBeenCalledOnce();
expect(database.manyOrNone).toHaveBeenCalledWith(
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] order by ed.report_date DESC OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
{
whereClause: expect.any(Object),
startIndex,
itemsPerPage,
sortStatement: expect.any(Object),
},
);
expect(actual).toEqual([
Expand Down Expand Up @@ -183,14 +199,22 @@ describe("listEcrDataService", () => {

let startIndex = 0;
let itemsPerPage = 25;
const actual: EcrDisplay[] = await listEcrData(startIndex, itemsPerPage);
let columnName = "date_created";
let direction = "DESC";
const actual: EcrDisplay[] = await listEcrData(
startIndex,
itemsPerPage,
columnName,
direction,
);
expect(database.manyOrNone).toHaveBeenCalledOnce();
expect(database.manyOrNone).toHaveBeenCalledWith(
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] order by ed.report_date DESC OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
{
whereClause: expect.any(Object),
startIndex,
itemsPerPage,
sortStatement: expect.any(Object),
},
);
expect(actual).toEqual([
Expand All @@ -211,15 +235,24 @@ describe("listEcrDataService", () => {
database.manyOrNone = jest.fn(() => Promise.resolve([]));
let startIndex = 0;
let itemsPerPage = 25;
let columnName = "date_created";
let direction = "DESC";
let searchTerm = "abc";
await listEcrData(startIndex, itemsPerPage, searchTerm);
await listEcrData(
startIndex,
itemsPerPage,
columnName,
direction,
searchTerm,
);
expect(database.manyOrNone).toHaveBeenCalledOnce();
expect(database.manyOrNone).toHaveBeenCalledWith(
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] order by ed.report_date DESC OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
{
whereClause: expect.any(Object),
startIndex,
itemsPerPage,
sortStatement: expect.any(Object),
},
);
});
Expand All @@ -229,14 +262,23 @@ describe("listEcrDataService", () => {
let startIndex = 0;
let itemsPerPage = 25;
let searchTerm = "O'Riley";
await listEcrData(startIndex, itemsPerPage, searchTerm);
let columnName = "date_created";
let direction = "DESC";
await listEcrData(
startIndex,
itemsPerPage,
searchTerm,
columnName,
direction,
);
expect(database.manyOrNone).toHaveBeenCalledOnce();
expect(database.manyOrNone).toHaveBeenCalledWith(
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] order by ed.report_date DESC OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
"SELECT ed.eICR_ID, ed.patient_name_first, ed.patient_name_last, ed.patient_birth_date, ed.date_created, ed.report_date, erc.condition, ers.rule_summary, ed.report_date 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] $[sortStatement] OFFSET $[startIndex] ROWS FETCH NEXT $[itemsPerPage] ROWS ONLY",
{
whereClause: expect.any(Object),
startIndex,
itemsPerPage,
sortStatement: expect.any(Object),
},
);
});
Expand Down
Loading

0 comments on commit b8f5d2a

Please sign in to comment.