UNPKG

@ai-ecom/medusa-plugin-patient-record

Version:

A plugin for patient record

239 lines (231 loc) 12.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _medusa = require("@medusajs/medusa"); var _laboratoryTempResult = require("../models/laboratory-temp-result"); var TypeORM = _interopRequireWildcard(require("typeorm")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } var Ldt3Service = /*#__PURE__*/function (_TransactionBaseServi) { function Ldt3Service(_ref) { var _this; var eventBusService = _ref.eventBusService, examinationResultService = _ref.examinationResultService, laboratoryTempResultService = _ref.laboratoryTempResultService, customerService = _ref.customerService, customerRepository = _ref.customerRepository, appointmentRepository = _ref.appointmentRepository; (0, _classCallCheck2["default"])(this, Ldt3Service); // eslint-disable-next-line prefer-rest-params _this = _callSuper(this, Ldt3Service, [arguments[0]]); _this.eventBus_ = eventBusService; _this.examinationResultService_ = examinationResultService; _this.laboratoryTempResultService_ = laboratoryTempResultService; _this.customerService_ = customerService; _this.customerRepository_ = customerRepository; _this.appointmentRepository_ = appointmentRepository; return _this; } (0, _inherits2["default"])(Ldt3Service, _TransactionBaseServi); return (0, _createClass2["default"])(Ldt3Service, [{ key: "processTree", value: function () { var _processTree = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(rootNode) { var befundSatz, patientInformation, patient, orderInformation, headerSatz, laboratoryIdentifier, laboratoryInformation, befundInformation, auftragsEingang, befundErstellung, lab, _yield$this$customerR, _yield$this$customerR2, customer, _appointments$0$id, _appointments$, tempResult, orderIds, startDate, endDate, appointments; return _regenerator["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: console.log("[PROCESS-TREE] - 1"); befundSatz = this.findChildByCode(rootNode, "8205"); patientInformation = this.findChildByCode(this.findChildByCode(befundSatz, "Obj_0045"), "Obj_0047"); patient = { firstName: patientInformation.attributes["Vorname"], lastName: patientInformation.attributes["Nachname"], birthday: formatDate(patientInformation.attributes["Geburtsdatum"]) }; console.log("[PROCESS-TREE] - 2 - patient", patient); orderInformation = this.findChildByCode(befundSatz, "Obj_0017"); headerSatz = this.findChildByCode(rootNode, "8220"); laboratoryIdentifier = this.findChildByCode(headerSatz, "Obj_0036"); laboratoryInformation = this.findChildByCode(laboratoryIdentifier, "Obj_0043"); befundInformation = this.findChildByCode(befundSatz, "Obj_0017"); auftragsEingang = this.findChildByName(befundInformation, "Timestamp_Auftragseingang"); befundErstellung = this.findChildByName(befundInformation, "Timestamp_Befunderstellung"); lab = { id: laboratoryIdentifier.attributes["ID eines Laborstandortes"], name: laboratoryInformation.attributes["Organisation/Firma"], orderId: this.findChildByCode(befundSatz, "Obj_0017").attributes["Befund-ID"], orderEntryDate: getFromDateAndTimestamp(auftragsEingang.attributes), resultDateTime: getFromDateAndTimestamp(befundErstellung.attributes) }; console.log("[PROCESS-TREE] - 3 - lab", lab); _context.next = 16; return this.customerRepository_.find({ where: { first_name: TypeORM.Raw(function (alias) { return "".concat(alias, " ILIKE :firstName"); }, { firstName: "%".concat(patient.firstName, "%") }), last_name: TypeORM.Raw(function (alias) { return "".concat(alias, " ILIKE :lastName"); }, { lastName: "%".concat(patient.lastName, "%") }), has_account: true, birthday: patient.birthday }, relations: ["orders"] }); case 16: _yield$this$customerR = _context.sent; _yield$this$customerR2 = (0, _slicedToArray2["default"])(_yield$this$customerR, 1); customer = _yield$this$customerR2[0]; console.log("[PROCESS-TREE] - 4 - customer", customer); _context.prev = 20; _context.next = 23; return this.laboratoryTempResultService_.create({ raw_file: rootNode, patient_first_name: patient.firstName, patient_last_name: patient.lastName, patient_birthday: patient.birthday, lab_id: lab.id, external_id: lab.orderId, lab_name: lab.name, order_entry_date: lab.orderEntryDate, lab_result_datetime: lab.resultDateTime, status: _laboratoryTempResult.LaboratoryTempResultStatus.UNASSIGNED }); case 23: tempResult = _context.sent; if (customer) { _context.next = 26; break; } return _context.abrupt("return", { status: "success", message: "LDT-3 processed successfully" }); case 26: orderIds = customer.orders.map(function (order) { return order.id; }); startDate = new Date(lab.orderEntryDate); startDate.setDate(startDate.getDate() - 3); // Subtract 3 days endDate = new Date(lab.resultDateTime); _context.next = 32; return this.appointmentRepository_.find({ where: { order: { id: TypeORM.In(orderIds) }, from: TypeORM.Between(startDate, endDate) } }); case 32: appointments = _context.sent; _context.next = 35; return this.laboratoryTempResultService_.assign({ appointment_id: (_appointments$0$id = (_appointments$ = appointments[0]) === null || _appointments$ === void 0 ? void 0 : _appointments$.id) !== null && _appointments$0$id !== void 0 ? _appointments$0$id : null, customer_id: customer.id, id: tempResult.id }); case 35: _context.next = 37; return this.laboratoryTempResultService_.update(tempResult.id, { status: _laboratoryTempResult.LaboratoryTempResultStatus.ASSIGNED }); case 37: return _context.abrupt("return", { status: "success", message: "LDT-3 processed successfully" }); case 40: _context.prev = 40; _context.t0 = _context["catch"](20); console.log("❌ [LDT3-SERVICE] ::: error:", _context.t0); return _context.abrupt("return", { status: "failed", message: "LDT-3 failed to process" }); case 44: case "end": return _context.stop(); } }, _callee, this, [[20, 40]]); })); function processTree(_x) { return _processTree.apply(this, arguments); } return processTree; }() }, { key: "findChildByCode", value: function findChildByCode(node, code) { return node.children.find(function (child) { return child.code === code; }); } }, { key: "findChildByName", value: function findChildByName(node, name) { return node.children.find(function (child) { return child.name === name; }); } }]); }(_medusa.TransactionBaseService); // eslint-disable-next-line max-len (0, _defineProperty2["default"])(Ldt3Service, "IndexName", "ldt3"); (0, _defineProperty2["default"])(Ldt3Service, "Events", { PROCESSED: "ldt3.processed" }); function formatDate(birthdayString) { console.log("🚀 ~ file: ldt-3.ts ~ line 139 ~ formatDate ~ birthdayString", birthdayString); // Extract year, month, and day from the string var year = birthdayString.substring(0, 4); var month = birthdayString.substring(4, 6); var day = birthdayString.substring(6, 8); // Create a new Date object (note: months are 0-indexed in JavaScript Date objects) var date = new Date(year, month - 1, day); return date; } function getFromDateAndTimestamp(timestampObj) { console.log("🚀 ~ file: ldt-3.ts ~ line 155 ~ getFromDateAndTimestamp ~ timestampObj", timestampObj); // Extracting date components var year = timestampObj["Datum des Timestamp"].substring(0, 4); var month = timestampObj["Datum des Timestamp"].substring(4, 6); var day = timestampObj["Datum des Timestamp"].substring(6, 8); // Providing default values for time components if "Uhrzeit des Timestamp" is missing var timeString = timestampObj["Uhrzeit des Timestamp"] || "000000"; // Default to "000000" if missing var hours = timeString.substring(0, 2); var minutes = timeString.substring(2, 4); var seconds = timeString.substring(4, 6); // Creating a date object in local time var date = new Date(year, month - 1, day, hours, minutes, seconds); // Providing a default value for "Zeitzone" if missing var timezoneString = timestampObj["Zeitzone"] || "UTC+0"; // Default to "UTC+0" if missing // Adjusting for timezone (assuming the timezone is always in the format "UTC±X") var timezoneOffset = parseInt(timezoneString.substring(3)) || 0; // Default to 0 if parsing fails var localTimezoneOffset = date.getTimezoneOffset() / 60; var totalOffset = timezoneOffset - localTimezoneOffset; // Adjust the date object for the timezone date.setHours(date.getHours() + totalOffset); return date; } var _default = exports["default"] = Ldt3Service;