Tuesday, 3 May 2016

Adding minutes to appointment dates in Mirth Connect

Adding appointment duration to start date to get the end date

I've got an inbound HL7 appointment message that sends its appointments with a start date and a duration. However the system I'm integrating into needs to have a start date and an end date.

So, for you HL7 guys out there, that means I need to grab the start date from SCH-11.4 and add the duration mins in the SCH-9. It's probably worth noting here that the duration units in the SCH-10 is always going to be 'min' for me, so I can always treat the SCH-9 as the number of minutes - you might have to handle this too.

I've a .NET background, and if I had C# available this would be solved in a second, but as this is Mirth we're stuck with JavaScript.

After fiddling about for a bit I decided that doing this transformer in a Mapper's single line of expression was just too messy - it's possible, but the line is so long that it's impossible to work with. Instead I created a JavaScript Transformer step, basing it on a the mapper I started with as this provides me with all the try/catch syntax.

So first I grab out the appointment date and convert it from an HL7 string to a proper date type.

//Get the appointment startdate and convert it to a datetime
var appointmentDate = DateUtil.getDate("yyyyMMddHHmmss",msg['SCH']['SCH.11']['SCH.11.4'].toString());

Now I have a date I can manipulate it appropriately. I'll get the appointment duration as an integer too.

//Get the appointment length as an integer
var appointmentLength = parseInt(msg['SCH']['SCH.9']['SCH.9.1'].toString())

Online I found some very handy JavaScript functions for adding dates, GetMinutes and SetMinutes. They basically do what they say on the tin, but I did get tripped up there for a second. In my usual .NET coding I'd expect that dateVar.SetMinutes() would return the result of setting the minutes, but it actually just adds them to dateVar directly - no warnings or errors to help me through it, but I got their in the end.

//increase the start date by the appointment Length
appointmentDate.setMinutes(appointmentDate.getMinutes() + appointmentLength);

So that's it, I have the end date, now I just need to convert it back to and HL7 String (though you might not need too) and put it into a channel variable.

Here is the full transformer code.

var mapping;

try {
  //Get the appointment startdate and convert it to a datetime
  var appointmentDate = DateUtil.getDate("yyyyMMddHHmmss",msg['SCH']['SCH.11']['SCH.11.4'].toString());

  //Get the appointment length as an integer
  var appointmentLength = parseInt(msg['SCH']['SCH.9']['SCH.9.1'].toString())

  //increase the start date by the appointment Length
  appointmentDate.setMinutes(appointmentDate.getMinutes() + appointmentLength);

  //Convert the date back to an HL7 Formatted string.
  mapping = DateUtil.formatDate("yyyyMMddHHmmss", appointmentDate);
} catch (e) {
        logger.error(e);
        mapping = '';
}

//put the string into a channel varaiable.
channelMap.put('EndDate', validate( mapping , '', new Array()));

1 comment:

  1. If you use the momentjs, your life will be easier.

    ReplyDelete