Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug-Fix: Patient's Age parsing discrepancy during XLS export in Sample Management System #9247

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/components/Patient/SampleViewAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,30 @@ export default function SampleViewAdmin() {
});
};

const parseExportData = (data: string) =>
data
.trim()
.split("\n")
.map((row: string) =>
row
.trim()
.split(",")
.map((field: string) =>
new Date(field).toString() === "Invalid Date"
? field
: formatDateTime(field),
)
.join(","),
)
.join("\n");
const parseExportData = (data: string) => {
const rows = data.trim().split("\n");
const headerColumns = rows[0].split(",");

Comment on lines +120 to +123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation and error handling.

The function should validate input and handle edge cases:

  1. Empty/malformed input
  2. Missing required headers
 const parseExportData = (data: string) => {
+  if (!data?.trim()) {
+    throw new Error("Export data is empty or invalid");
+  }
+
   const rows = data.trim().split("\n");
+  if (rows.length < 2) {
+    throw new Error("Export data must contain headers and at least one row");
+  }
+
   const headerColumns = rows[0].split(",");
+  const requiredHeaders = ["Patient Age", "Date of Sample", "Date of Result"];
+  const missingHeaders = requiredHeaders.filter(
+    (header) => !headerColumns.some((h) => h.trim() === header)
+  );
+  if (missingHeaders.length > 0) {
+    throw new Error(`Missing required headers: ${missingHeaders.join(", ")}`);
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const parseExportData = (data: string) => {
const rows = data.trim().split("\n");
const headerColumns = rows[0].split(",");
const parseExportData = (data: string) => {
if (!data?.trim()) {
throw new Error("Export data is empty or invalid");
}
const rows = data.trim().split("\n");
if (rows.length < 2) {
throw new Error("Export data must contain headers and at least one row");
}
const headerColumns = rows[0].split(",");
const requiredHeaders = ["Patient Age", "Date of Sample", "Date of Result"];
const missingHeaders = requiredHeaders.filter(
(header) => !headerColumns.some((h) => h.trim() === header)
);
if (missingHeaders.length > 0) {
throw new Error(`Missing required headers: ${missingHeaders.join(", ")}`);
}

return [
rows[0],
...rows.slice(1).map((row) => {
const columns = row.split(",").map((field, index) => {
const header = headerColumns[index]?.trim();

if (header === "Patient Age") {
return field.trim();
}

if (header === "Date of Sample" || header === "Date of Result") {
return formatDateTime(field.trim());
}
return field.includes(",") ? `"${field.trim()}"` : field.trim();
});

return columns.join(",");
}),
].join("\n");
};

let sampleList: any[] = [];
if (sampeleData?.count) {
Expand Down