patient table done, patient form done

This commit is contained in:
2025-07-12 22:54:57 +05:30
parent 27dc669aae
commit 324623f02b
9 changed files with 356 additions and 595 deletions

View File

@@ -7,7 +7,6 @@ import {
} from "@repo/db/usedSchemas";
import { z } from "zod";
import { extractDobParts } from "../utils/DobParts";
import { parseDobParts } from "../utils/DobPartsParsing";
const router = Router();
@@ -134,75 +133,18 @@ router.get("/search", async (req: Request, res: Response): Promise<any> => {
}
if (dob) {
const range = parseDobParts(dob);
if (range) {
if (!filters.AND) filters.AND = [];
// Check what kind of match this was
const fromYear = range.from.getUTCFullYear();
const toYear = range.to.getUTCFullYear();
const fromMonth = range.from.getUTCMonth() + 1; // Prisma months: 1-12
const toMonth = range.to.getUTCMonth() + 1;
const fromDay = range.from.getUTCDate();
const toDay = range.to.getUTCDate();
const isFullDate =
fromYear === toYear && fromMonth === toMonth && fromDay === toDay;
const isDayMonthOnly =
fromYear === 1900 &&
toYear === 2100 &&
fromMonth === toMonth &&
fromDay === toDay;
const isMonthOnly =
fromYear === 1900 &&
toYear === 2100 &&
fromDay === 1 &&
toDay >= 28 &&
fromMonth === toMonth &&
toMonth === toMonth;
const isYearOnly = fromMonth === 1 && toMonth === 12;
if (isFullDate) {
filters.AND.push({
dob_day: fromDay,
dob_month: fromMonth,
dob_year: fromYear,
});
} else if (isDayMonthOnly) {
filters.AND.push({
dob_day: fromDay,
dob_month: fromMonth,
});
} else if (isMonthOnly) {
filters.AND.push({
dob_month: fromMonth,
});
} else if (isYearOnly) {
filters.AND.push({
dob_year: fromYear,
});
} else {
// Fallback: search via dateOfBirth range
filters.AND.push({
dateOfBirth: {
gte: range.from,
lte: range.to,
},
});
}
} else {
const parsed = new Date(dob);
if (isNaN(parsed.getTime())) {
return res.status(400).json({
message: `Invalid date format for DOB. Try formats like "12 March", "March", "1980", "12/03/1980", etc.`,
message: "Invalid date format for DOB. Use format: YYYY-MM-DD",
});
}
// Match exact dateOfBirth (optional: adjust for timezone)
filters.dateOfBirth = parsed;
}
const [patients, totalCount] = await Promise.all([
storage.searchPatients({
storage.searchPatients({
filters,
limit: parseInt(limit),
offset: parseInt(offset),
@@ -259,13 +201,8 @@ router.post("/", async (req: Request, res: Response): Promise<any> => {
userId: req.user!.id,
});
// Extract dob_* from dateOfBirth
const dobParts = extractDobParts(new Date(patientData.dateOfBirth));
const patient = await storage.createPatient({
...patientData,
...dobParts, // adds dob_day/month/year
});
const patient = await storage.createPatient(patientData);
res.status(201).json(patient);
} catch (error) {
@@ -307,15 +244,8 @@ router.put(
// Validate request body
const patientData = updatePatientSchema.parse(req.body);
let dobParts = {};
if (patientData.dateOfBirth) {
dobParts = extractDobParts(new Date(patientData.dateOfBirth));
}
// Update patient
const updatedPatient = await storage.updatePatient(patientId, {
...patientData,
...dobParts,
});
const updatedPatient = await storage.updatePatient(patientId, patientData);
res.json(updatedPatient);
} catch (error) {
if (error instanceof z.ZodError) {

View File

@@ -1,81 +0,0 @@
export function parseDobParts(input: string): { from: Date; to: Date } | null {
if (!input || typeof input !== "string") return null;
const parts = input.trim().split(/[\s/-]+/).filter(Boolean);
if (parts.length === 1) {
const part = parts[0]?.toLowerCase();
// Year
if (part && /^\d{4}$/.test(part)) {
const year = parseInt(part);
return {
from: new Date(Date.UTC(year, 0, 1)),
to: new Date(Date.UTC(year, 11, 31, 23, 59, 59)),
};
}
// Month
const month = part ? parseMonth(part) : null;
if (month !== null) {
return {
from: new Date(Date.UTC(1900, month, 1)),
to: new Date(Date.UTC(2100, month + 1, 0, 23, 59, 59)),
};
}
return null;
}
if (parts.length === 2) {
const [part1, part2] = parts;
const day = tryParseInt(part1);
const month = part2 ? parseMonth(part2) : null;
if (day !== null && month !== null) {
return {
from: new Date(Date.UTC(1900, month, day)),
to: new Date(Date.UTC(2100, month, day, 23, 59, 59)),
};
}
return null;
}
if (parts.length === 3) {
const [part1, part2, part3] = parts;
const day = tryParseInt(part1);
const month = part2 ? parseMonth(part2) : null;
const year = tryParseInt(part3);
if (day !== null && month !== null && year !== null) {
return {
from: new Date(Date.UTC(year, month, day)),
to: new Date(Date.UTC(year, month, day, 23, 59, 59)),
};
}
return null;
}
return null;
}
function parseMonth(input: string): number | null {
const normalized = input.toLowerCase();
const months = [
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december",
];
const index = months.findIndex(
(m) => m === normalized || m.startsWith(normalized)
);
return index !== -1 ? index : null;
}
function tryParseInt(value?: string): number | null {
if (!value) return null;
const parsed = parseInt(value);
return isNaN(parsed) ? null : parsed;
}