Support original EzDent patient creation and lookup
This commit is contained in:
1 parent
56fa065b77
commit
fd09b81a7c
3 files changed
+74
-16
No files matched your search
@@ -150,7 +150,7 @@ final class LocalServer {
|
||||
let reply: (Data) -> Void = { [self] body in
|
||||
let response = webConsole.reply(path: route, method: method, body: body,
|
||||
authorization: bearer, query: target.queryItems ?? [],
|
||||
store: library.store)
|
||||
queryPathLast: segments.last, store: library.store)
|
||||
guard let json = try? JSONSerialization.data(withJSONObject: response.body) else {
|
||||
connection.cancel()
|
||||
return
|
||||
|
||||
@@ -56,6 +56,10 @@ final class ServerStore {
|
||||
""")
|
||||
}
|
||||
try execute("""
|
||||
CREATE TABLE IF NOT EXISTS legacy_patient_fields (
|
||||
patient_id TEXT PRIMARY KEY NOT NULL REFERENCES patients(id) ON DELETE CASCADE,
|
||||
fields TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS password_policy (
|
||||
key TEXT PRIMARY KEY NOT NULL,
|
||||
value TEXT NOT NULL
|
||||
@@ -155,11 +159,19 @@ final class ServerStore {
|
||||
""", values: [.integer(limit), .integer(offset)], map: { try self.patient($0) })
|
||||
}
|
||||
|
||||
func patient(chartNumber: String) throws -> PatientRecord? {
|
||||
func patient(chartNumber: String) throws -> (record: PatientRecord, fields: [String: String])? {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return try query("SELECT id, name, chart_number FROM patients WHERE chart_number = ? COLLATE BINARY",
|
||||
values: [.text(chartNumber)], map: { try self.patient($0) }).first
|
||||
return try query("""
|
||||
SELECT patients.id, patients.name, patients.chart_number, legacy_patient_fields.fields
|
||||
FROM patients LEFT JOIN legacy_patient_fields ON legacy_patient_fields.patient_id = patients.id
|
||||
WHERE patients.chart_number = ? COLLATE BINARY
|
||||
""", values: [.text(chartNumber)]) { row in
|
||||
let record = try self.patient(row)
|
||||
let fields = sqlite3_column_type(row, 3) == SQLITE_NULL ? [:]
|
||||
: try JSONDecoder().decode([String: String].self, from: Data(self.text(row, 3).utf8))
|
||||
return (record, fields)
|
||||
}.first
|
||||
}
|
||||
|
||||
func patient(_ id: UUID) throws -> PatientRecord? {
|
||||
@@ -188,7 +200,7 @@ final class ServerStore {
|
||||
}.first ?? 0
|
||||
}
|
||||
|
||||
func createPatient(name: String, chartNumber: String) throws -> PatientRecord {
|
||||
func createPatient(name: String, chartNumber: String, legacyFields: [String: String]? = nil) throws -> PatientRecord {
|
||||
let name = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let chart = chartNumber.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard (1...200).contains(name.count), (1...100).contains(chart.count),
|
||||
@@ -196,7 +208,16 @@ final class ServerStore {
|
||||
let patient = PatientRecord(id: UUID(), name: name, chartNumber: chart)
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
try insert(patient)
|
||||
if let legacyFields {
|
||||
let json = try JSONEncoder().encode(legacyFields)
|
||||
try transaction {
|
||||
try insert(patient)
|
||||
try run("INSERT INTO legacy_patient_fields(patient_id, fields) VALUES (?, ?)",
|
||||
[.text(patient.id.uuidString), .text(String(decoding: json, as: UTF8.self))])
|
||||
}
|
||||
} else {
|
||||
try insert(patient)
|
||||
}
|
||||
return patient
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,11 @@ final class WebConsoleAPI {
|
||||
}
|
||||
|
||||
private static let admin = "Master Admin"
|
||||
private static let legacyPatientFields = [
|
||||
"strLastName", "strFirstName", "strMiddleName", "strGender", "strAge", "dtBirthdate",
|
||||
"strEmail", "strSocialID", "strPhone", "strPhotoIDName", "dtRegisteredDateTime",
|
||||
"strZipCode", "strAddress", "strMobile", "strRace", "strClassification", "strJob", "eSourceType",
|
||||
]
|
||||
private static let wireKey = Data(base64Encoded: "fCVpkpDy3QII1bJ0uklkvQ==")!
|
||||
private static let wireIV = Data(base64Encoded: "p+O8XWH7eWQw1xifYtxx5Q==")!
|
||||
private let passwordDigest = SHA256.hash(data: Data(
|
||||
@@ -41,6 +46,7 @@ final class WebConsoleAPI {
|
||||
("e2ds", "useraccountsbynamer2", "GET"),
|
||||
("e2ds", "useraccountsfornames", "GET"),
|
||||
("e2ds", "patientsbychartno", "GET"),
|
||||
("e2ds", "patients", "POST"),
|
||||
("e2ds", "doctors", "GET"),
|
||||
("e2ds", "smartpayaccountsbyidandqueryuserid", "GET"),
|
||||
("e2ds", "patientsbynumlistforrecentlyacquired", "POST"),
|
||||
@@ -66,7 +72,7 @@ final class WebConsoleAPI {
|
||||
func allowsNetworkAccess(_ path: [String], method: String) -> Bool {
|
||||
switch (path, method) {
|
||||
case (["auth", "apploginr2"], "POST"), (["auth", "applogin"], "POST"),
|
||||
(["e2ds", "patientsbynumlistforrecentlyacquired"], "POST"):
|
||||
(["e2ds", "patientsbynumlistforrecentlyacquired"], "POST"), (["e2ds", "patients"], "POST"):
|
||||
return true
|
||||
case (let route, "GET"):
|
||||
return route.starts(with: ["e2ds", "patientsbychartno"]) || isBootstrapRead(route)
|
||||
@@ -81,7 +87,7 @@ final class WebConsoleAPI {
|
||||
path == ["e2ds", "doctors"]
|
||||
}
|
||||
|
||||
func reply(path: [String], method: String, body: Data, authorization: String?, query: [URLQueryItem], store: ServerStore) -> Reply {
|
||||
func reply(path: [String], method: String, body: Data, authorization: String?, query: [URLQueryItem], queryPathLast: String?, store: ServerStore) -> Reply {
|
||||
if path == ["auth", "apploginr2"] || path == ["auth", "applogin"] {
|
||||
return login(body)
|
||||
}
|
||||
@@ -91,7 +97,9 @@ final class WebConsoleAPI {
|
||||
return Reply(["errorMessage": "Auth token has expired"], status: "401 Unauthorized", errorCode: -8001511)
|
||||
}
|
||||
let applicationBootstrap = (method == "GET" && isBootstrapRead(path))
|
||||
|| (method == "POST" && path == ["e2ds", "patientsbynumlistforrecentlyacquired"])
|
||||
|| (method == "GET" && path.starts(with: ["e2ds", "patientsbychartno"]))
|
||||
|| (method == "POST" && (path == ["e2ds", "patientsbynumlistforrecentlyacquired"]
|
||||
|| path == ["e2ds", "patients"]))
|
||||
guard session.administrator || applicationBootstrap else {
|
||||
return Reply(["errorMessage": "Administrator login required"],
|
||||
status: "403 Forbidden", errorCode: -1013)
|
||||
@@ -139,8 +147,40 @@ final class WebConsoleAPI {
|
||||
} catch {
|
||||
return Reply(status: "503 Service Unavailable", errorCode: -1005)
|
||||
}
|
||||
case ("e2ds", "patients", "POST") where path.count == 2:
|
||||
guard let chartNumber = input["strChartNo"] as? String,
|
||||
!chartNumber.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
}
|
||||
var fields: [String: String] = [:]
|
||||
for field in Self.legacyPatientFields {
|
||||
guard let raw = input[field], !(raw is NSNull) else { continue }
|
||||
guard let value = raw as? String, value.utf8.count <= 4096, !value.contains("\0") else {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
}
|
||||
fields[field] = value
|
||||
}
|
||||
let gender = fields["strGender"]?.prefix(1).uppercased() ?? ""
|
||||
guard ["", "O", "M", "F"].contains(gender) else {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
}
|
||||
fields["strGender"] = gender.isEmpty ? "O" : gender
|
||||
let displayName = ["strLastName", "strFirstName", "strMiddleName"].compactMap { fields[$0] }
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.joined(separator: " ")
|
||||
do {
|
||||
_ = try store.createPatient(name: displayName.isEmpty ? chartNumber : displayName,
|
||||
chartNumber: chartNumber, legacyFields: fields)
|
||||
return Reply()
|
||||
} catch StoreError.conflict {
|
||||
return Reply(errorCode: -100029)
|
||||
} catch StoreError.invalid {
|
||||
return Reply(status: "400 Bad Request", errorCode: -1021)
|
||||
} catch {
|
||||
return Reply(status: "503 Service Unavailable", errorCode: -1005)
|
||||
}
|
||||
case ("e2ds", "patientsbychartno", "GET"):
|
||||
let argument = query.first(where: { $0.name == "chartno" })?.value
|
||||
?? (path.count == 4 && path[2] == "chartno" ? queryPathLast : nil)
|
||||
// CodeIgniter receives an already decoded query value and the controller calls urldecode again.
|
||||
let chartNumber = argument
|
||||
.map { $0.replacingOccurrences(of: "+", with: " ") }
|
||||
@@ -149,15 +189,12 @@ final class WebConsoleAPI {
|
||||
guard !chartNumber.isEmpty, let patient = try store.patient(chartNumber: chartNumber) else {
|
||||
return Reply(["patientInfo": []], errorCode: -100037)
|
||||
}
|
||||
// Imported Mac records store one display name without separate family / given names.
|
||||
let emptyFields = ["strFirstName", "strMiddleName", "strGender", "strAge", "dtBirthdate",
|
||||
"strEmail", "strSocialID", "strPhone", "strPhotoIDName", "dtRegisteredDateTime",
|
||||
"strZipCode", "strAddress", "strMobile", "strRace", "strClassification", "strJob"]
|
||||
var info: [String: Any] = Dictionary(uniqueKeysWithValues: emptyFields.map { ($0, "") })
|
||||
info["strChartNo"] = patient.chartNumber
|
||||
info["strLastName"] = patient.name
|
||||
var info: [String: Any] = Dictionary(uniqueKeysWithValues: Self.legacyPatientFields.map { ($0, "") })
|
||||
info["strChartNo"] = patient.record.chartNumber
|
||||
info["strLastName"] = patient.record.name
|
||||
info["strlDoctor"] = [String]()
|
||||
info["eSourceType"] = "eUnknownSourceType"
|
||||
for (field, value) in patient.fields { info[field] = value }
|
||||
return Reply(["patientInfo": info])
|
||||
} catch {
|
||||
return Reply(status: "503 Service Unavailable", errorCode: -1005)
|
||||
|
||||
Reference in new issue
Block a user