-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: server sends back proper response
* server can now can send the proper response back to the client if it's required. Closes #13
- Loading branch information
Showing
4 changed files
with
72 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,80 @@ | ||
import { Socket } from 'net' | ||
import { Message } from 'node-hl7-client' | ||
import { createHL7Date, Message } from 'node-hl7-client' | ||
|
||
/** | ||
* Send Response | ||
* @since 1.0.0 | ||
*/ | ||
export class SendResponse { | ||
/** @internal */ | ||
private readonly _ack: Message | ||
private _ack: Message | undefined | ||
/** @internal */ | ||
private readonly _socket: Socket | ||
/** @internal */ | ||
private readonly _message: Message | ||
/** @internal */ | ||
private _ackSent: boolean | ||
|
||
constructor (socket: Socket, ack: Message) { | ||
this._ack = ack | ||
socket.write(Buffer.from(ack.toString())) | ||
constructor (socket: Socket, message: Message) { | ||
this._ack = undefined | ||
this._ackSent = false | ||
this._message = message | ||
this._socket = socket | ||
} | ||
|
||
/** | ||
* Get Ack Message Object | ||
* Send Response back to End User | ||
* @since 1.0.0 | ||
* @see {@link https://hl7-definition.caristix.com/v2/HL7v2.1/Tables/0008} | ||
* @param type | ||
* @example | ||
* If you are to confirm to the end user (client) that the message they sent was good and processed successfully. | ||
* you would send an "AA" style message (Application Accept). | ||
* Otherwise, send an "AR" (Application Reject) to tell the client the data was | ||
* no accept.ed/processed. | ||
* ``` | ||
* const server = new Server({bindAddress: '0.0.0.0'}) | ||
* const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => { | ||
* const messageReq = req.getMessage() | ||
* await res.sendResponse("AA") | ||
* }) | ||
* ``` | ||
* "AE" (Application Error) will be sent if there is a problem creating either an "AA" or "AR" message from the orginial message sent. | ||
*/ | ||
getAckMessage (): Message { | ||
return this._ack | ||
async sendResponse (type: 'AA' | 'AR'): Promise<boolean> { | ||
try { | ||
this._ack = this._createAckMessage(type, this._message) | ||
this._socket.write(Buffer.from(this._ack.toString())) | ||
} catch (_e: any) { | ||
this._ack = this._createAckMessage('AE', this._message) | ||
this._socket.write(Buffer.from(this._ack.toString())) | ||
} | ||
|
||
this._ackSent = true | ||
|
||
return this._ackSent | ||
} | ||
|
||
/** @internal */ | ||
private _createAckMessage (type: string, message: Message): Message { | ||
const ackMessage = new Message({ | ||
messageHeader: { | ||
msh_9_1: 'ACK', | ||
msh_9_2: message.get('MSH.9.2').toString(), | ||
msh_10: `ACK${createHL7Date(new Date())}` | ||
} | ||
}) | ||
|
||
ackMessage.set('MSH.3', message.get('MSH.5').toRaw()) | ||
ackMessage.set('MSH.4', message.get('MSH.6').toRaw()) | ||
ackMessage.set('MSH.5', message.get('MSH.3').toRaw()) | ||
ackMessage.set('MSH.6', message.get('MSH.4').toRaw()) | ||
ackMessage.set('MSH.11', message.get('MSH.11').toRaw()) | ||
|
||
const segment = ackMessage.addSegment('MSA') | ||
segment.set('1', type) | ||
segment.set('2', message.get('MSH.10').toString()) | ||
|
||
return ackMessage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters