From 433723bf80f29d634812db1c0c2ec7b7f6985d51 Mon Sep 17 00:00:00 2001 From: aswincosq Date: Sun, 15 Dec 2024 00:43:52 +0530 Subject: [PATCH] Implemented DB population through the LIC service API --- Controllers/HL7Controller.cs | 97 +++++++++++++++++++++++++++++++++++ Services/PatientService.cs | 10 ++++ hl7.db-shm | Bin 0 -> 32768 bytes hl7.db-wal | Bin 0 -> 28872 bytes 4 files changed, 107 insertions(+) create mode 100644 Controllers/HL7Controller.cs create mode 100644 hl7.db-shm create mode 100644 hl7.db-wal diff --git a/Controllers/HL7Controller.cs b/Controllers/HL7Controller.cs new file mode 100644 index 0000000..99c1941 --- /dev/null +++ b/Controllers/HL7Controller.cs @@ -0,0 +1,97 @@ +using Microsoft.AspNetCore.Mvc; +using SimpleLIS.DTO; +using SimpleLIS.Services; +using SimpleLIS.Models; +using AutoMapper; +using System.Text.Json; + +namespace SimpleLIS.Controllers; + +[ApiController] +[Route("api/hl7")] +public class HL7Controller : ControllerBase +{ + private readonly MessageService _messageService; + private readonly PatientService _patientService; + private readonly IMapper _mapper; + + public HL7Controller( + MessageService messageService, + PatientService patientService, + IMapper mapper) + { + _messageService = messageService; + _patientService = patientService; + _mapper = mapper; + } + +[HttpPost("process")] +public async Task ProcessHL7Message([FromBody] JsonElement payload) +{ + try + { + // Create Patient + var patient = new Patient + { + HL7PatientId = payload.GetProperty("hl7PatientId").GetString(), + LastName = payload.GetProperty("lastName").GetString(), + FirstName = payload.GetProperty("firstName").GetString(), + DateOfBirth = payload.GetProperty("dateOfBirth").GetDateTime(), + Gender = payload.GetProperty("gender").GetString(), + Messages = new List() + }; + + // Check for existing patient + var existingPatient = await _patientService.GetPatientByNameAndDOB( + patient.FirstName!, + patient.LastName!, + patient.DateOfBirth + ); + + patient = existingPatient ?? await _patientService.CreatePatientAsync(patient); + + // Create Message + var message = new Message + { + ControlId = payload.GetProperty("controlId").GetString(), + MessageType = payload.GetProperty("messageType").GetString(), + SendingApp = payload.GetProperty("sendingApp").GetString(), + SendingFacility = payload.GetProperty("sendingFacility").GetString(), + ReceivingApp = payload.GetProperty("receivingApp").GetString(), + ReceivingFacility = payload.GetProperty("receivingFacility").GetString(), + Timestamp = payload.GetProperty("timestamp").GetDateTime(), + Version = payload.GetProperty("version").GetString(), + PatientId = patient.PatientId, + Patient = patient, + Observations = new List() + }; + + // Create Observations + var observationsArray = payload.GetProperty("observations"); + foreach (var obsElement in observationsArray.EnumerateArray()) + { + var observation = new Observation + { + ObservationCode = obsElement.GetProperty("ObservationCode").GetString(), + ObservationValue = obsElement.GetProperty("ObservationValue").GetString(), + Units = obsElement.GetProperty("Units").GetString(), + AbnormalFlag = obsElement.GetProperty("AbnormalFlag").GetString(), + ResultStatus = obsElement.GetProperty("ResultStatus").GetString() ?? "F", + Message = message + }; + message.Observations.Add(observation); + } + + var createdMessage = await _messageService.CreateMessageAsync(message); + + return Ok(new { + message = _mapper.Map(createdMessage), + patient = _mapper.Map(patient) + }); + } + catch (Exception ex) + { + return BadRequest(new { error = ex.Message + (ex.InnerException?.Message ?? "") }); + } +} +} diff --git a/Services/PatientService.cs b/Services/PatientService.cs index 3b0e23a..23df81b 100644 --- a/Services/PatientService.cs +++ b/Services/PatientService.cs @@ -52,4 +52,14 @@ public class PatientService { return await _context.Patients.AnyAsync(p => p.PatientId == id); } + public async Task GetPatientByNameAndDOB(string firstName, string lastName, DateTime dateOfBirth) + { + return await _context.Patients + .FirstOrDefaultAsync(p => + p.FirstName == firstName && + p.LastName == lastName && + p.DateOfBirth == dateOfBirth); + } + + } diff --git a/hl7.db-shm b/hl7.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..597eee266f86cc4818f21fac38c4fe9c8d312d58 GIT binary patch literal 32768 zcmeI*KMny=5C`D(-$p^H(+f&BaSIw3P-#>;h0b{-?%)jD^F~Cpvenw}mrUNvW-~ka z4lp~u&$CD~RU^wry>t#zZ7%!bJlQUGr{!(h&S!_#bzQ8E!|{*rr&g~-|HoI}k3UB@ z-QwGwRB`X8%2UNGLVy4P0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RmMOXjOG^ za}y{n(8$`QduTwQngX$jR0z~lAolADfqDwWW@#ZnfB=Dd3B=z8ga82o1PBlyK!5-N c0t5&UAV7cs0RjXF5FkK+009C72>hqO6Nc3y`2YX_ literal 0 HcmV?d00001 diff --git a/hl7.db-wal b/hl7.db-wal new file mode 100644 index 0000000000000000000000000000000000000000..dbd2e347fd06da95383e5bae5bb5628657c69fb5 GIT binary patch literal 28872 zcmeI)v1`;&9Ki9HM%#Oa`h;+x!(koHLA>K7y^FmoLY1o3_PEpN&_#$4s)KjflyfW; z3T}c3isB+J1&7WZiqt>AN$A|cNoe65E%?%UrQ*;z<@@887kJ5#-)DUMez~$ZxFkwj zXN5Q;(pY>Ge7kf)Uj9^g_v*#zZe+Fw5ARP*&po^UcC9;XHV4!Xp?YQ^5I_I{1Q0*~ z0R#|0009ILKp?{c&VXHzt!Uv%++Ii=%P!;^ags!Habk)ZGMCdoFOZ`f<;r+3O3wu> zwJA&z2q1s}0tg_000IagfB*srAdoSEi`J0HyZKSo(mI&z#CJN6+g@!c1db`=;J<^vmOnl%&!2z4-!HH()OyB?