UNPKG

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

Version:

A plugin for patient record

519 lines (513 loc) 24.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _objectDestructuringEmpty2 = _interopRequireDefault(require("@babel/runtime/helpers/objectDestructuringEmpty")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); 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 _medusaCoreUtils = require("medusa-core-utils"); var _examinationResult = require("../models/examination-result"); var _referenceValue = require("../models/reference-value"); 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 LaboratoryTempResultService = /*#__PURE__*/function (_TransactionBaseServi) { function LaboratoryTempResultService(_ref) { var _this; var laboratoryTempResultRepository = _ref.laboratoryTempResultRepository, examinationResultService = _ref.examinationResultService, eventBusService = _ref.eventBusService, customerService = _ref.customerService; (0, _classCallCheck2["default"])(this, LaboratoryTempResultService); // eslint-disable-next-line prefer-rest-params _this = _callSuper(this, LaboratoryTempResultService, [arguments[0]]); _this.laboratoryTempResultRepository_ = laboratoryTempResultRepository; _this.examinationResultService_ = examinationResultService; _this.eventBus_ = eventBusService; _this.customerService_ = customerService; return _this; } (0, _inherits2["default"])(LaboratoryTempResultService, _TransactionBaseServi); return (0, _createClass2["default"])(LaboratoryTempResultService, [{ key: "list", value: function () { var _list = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(selector) { var config, _yield$this$listAndCo, _yield$this$listAndCo2, laboratoryTempResult, _args = arguments; return _regenerator["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: config = _args.length > 1 && _args[1] !== undefined ? _args[1] : { skip: 0, take: 50, relations: [] }; _context.next = 3; return this.listAndCount(selector, config); case 3: _yield$this$listAndCo = _context.sent; _yield$this$listAndCo2 = (0, _slicedToArray2["default"])(_yield$this$listAndCo, 1); laboratoryTempResult = _yield$this$listAndCo2[0]; return _context.abrupt("return", laboratoryTempResult); case 7: case "end": return _context.stop(); } }, _callee, this); })); function list(_x) { return _list.apply(this, arguments); } return list; }() }, { key: "listAndCount", value: function () { var _listAndCount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(selector) { var config, laboratoryTempResultRepo, query, _args2 = arguments; return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: config = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : { skip: 0, take: 50, relations: [] }; laboratoryTempResultRepo = this.activeManager_.withRepository(this.laboratoryTempResultRepository_); query = (0, _medusa.buildQuery)(selector, config); return _context2.abrupt("return", laboratoryTempResultRepo.findAndCount(query)); case 4: case "end": return _context2.stop(); } }, _callee2, this); })); function listAndCount(_x2) { return _listAndCount.apply(this, arguments); } return listAndCount; }() }, { key: "retrieve", value: function () { var _retrieve = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(laboratoryTempResultId, config) { var laboratoryTempResult; return _regenerator["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.retrieve_({ id: laboratoryTempResultId }, config); case 2: laboratoryTempResult = _context3.sent; if (laboratoryTempResult) { _context3.next = 5; break; } throw new _medusaCoreUtils.MedusaError(_medusaCoreUtils.MedusaError.Types.NOT_FOUND, "LaboratoryTempResult with ".concat(laboratoryTempResultId, " was not found")); case 5: return _context3.abrupt("return", laboratoryTempResult); case 6: case "end": return _context3.stop(); } }, _callee3, this); })); function retrieve(_x3, _x4) { return _retrieve.apply(this, arguments); } return retrieve; }() }, { key: "retrieve_", value: function () { var _retrieve_ = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(selector, config) { var laboratoryTempResultRepo, query, laboratoryTempResult; return _regenerator["default"].wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: laboratoryTempResultRepo = this.activeManager_.withRepository(this.laboratoryTempResultRepository_); query = (0, _medusa.buildQuery)(selector, config); _context4.next = 4; return laboratoryTempResultRepo.findOne(query); case 4: laboratoryTempResult = _context4.sent; return _context4.abrupt("return", laboratoryTempResult); case 6: case "end": return _context4.stop(); } }, _callee4, this); })); function retrieve_(_x5, _x6) { return _retrieve_.apply(this, arguments); } return retrieve_; }() }, { key: "create", value: function () { var _create = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(laboratoryTempResultObject) { var _this2 = this; return _regenerator["default"].wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return this.atomicPhase_( /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(manager) { var laboratoryTempResultRepo, laboratoryTempResult, result; return _regenerator["default"].wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: laboratoryTempResultRepo = manager.withRepository(_this2.laboratoryTempResultRepository_); _context5.prev = 1; laboratoryTempResult = laboratoryTempResultRepo.create(laboratoryTempResultObject); _context5.next = 5; return laboratoryTempResultRepo.save(laboratoryTempResult); case 5: laboratoryTempResult = _context5.sent; _context5.next = 8; return _this2.retrieve(laboratoryTempResult.id, { relations: [] }); case 8: result = _context5.sent; _context5.next = 11; return _this2.eventBus_.withTransaction(manager).emit(LaboratoryTempResultService.Events.CREATED, { id: result.id }); case 11: return _context5.abrupt("return", result); case 14: _context5.prev = 14; _context5.t0 = _context5["catch"](1); throw (0, _medusa.formatException)(_context5.t0); case 17: case "end": return _context5.stop(); } }, _callee5, null, [[1, 14]]); })); return function (_x8) { return _ref2.apply(this, arguments); }; }()); case 2: return _context6.abrupt("return", _context6.sent); case 3: case "end": return _context6.stop(); } }, _callee6, this); })); function create(_x7) { return _create.apply(this, arguments); } return create; }() }, { key: "delete", value: function () { var _delete2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(laboratoryTempResultId) { var _this3 = this; return _regenerator["default"].wrap(function _callee8$(_context8) { while (1) switch (_context8.prev = _context8.next) { case 0: _context8.next = 2; return this.atomicPhase_( /*#__PURE__*/function () { var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(manager) { var laboratoryTempResultRepo, laboratoryTempResult; return _regenerator["default"].wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: laboratoryTempResultRepo = manager.withRepository(_this3.laboratoryTempResultRepository_); _context7.next = 3; return laboratoryTempResultRepo.findOne({ where: { id: laboratoryTempResultId } }); case 3: laboratoryTempResult = _context7.sent; if (laboratoryTempResult) { _context7.next = 6; break; } return _context7.abrupt("return"); case 6: _context7.next = 8; return laboratoryTempResultRepo.softRemove(laboratoryTempResult); case 8: _context7.next = 10; return _this3.eventBus_.withTransaction(manager).emit(LaboratoryTempResultService.Events.DELETED, { id: laboratoryTempResultId }); case 10: return _context7.abrupt("return", Promise.resolve()); case 11: case "end": return _context7.stop(); } }, _callee7); })); return function (_x10) { return _ref3.apply(this, arguments); }; }()); case 2: return _context8.abrupt("return", _context8.sent); case 3: case "end": return _context8.stop(); } }, _callee8, this); })); function _delete(_x9) { return _delete2.apply(this, arguments); } return _delete; }() }, { key: "update", value: function () { var _update2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(laboratoryTempResultId, _update) { var _this4 = this; return _regenerator["default"].wrap(function _callee10$(_context10) { while (1) switch (_context10.prev = _context10.next) { case 0: _context10.next = 2; return this.atomicPhase_( /*#__PURE__*/function () { var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(manager) { var laboratoryTempResultRepo, laboratoryTempResult, rest, _i, _Object$entries, _Object$entries$_i, key, value, result; return _regenerator["default"].wrap(function _callee9$(_context9) { while (1) switch (_context9.prev = _context9.next) { case 0: laboratoryTempResultRepo = manager.withRepository(_this4.laboratoryTempResultRepository_); _context9.next = 3; return _this4.retrieve(laboratoryTempResultId, {}); case 3: laboratoryTempResult = _context9.sent; rest = (0, _extends2["default"])({}, ((0, _objectDestructuringEmpty2["default"])(_update), _update)); for (_i = 0, _Object$entries = Object.entries(rest); _i < _Object$entries.length; _i++) { _Object$entries$_i = (0, _slicedToArray2["default"])(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1]; if ((0, _typeof2["default"])(value) !== "undefined") { laboratoryTempResult[key] = value; } } _context9.next = 8; return laboratoryTempResultRepo.save(laboratoryTempResult); case 8: result = _context9.sent; _context9.next = 11; return _this4.eventBus_.withTransaction(manager).emit(LaboratoryTempResultService.Events.UPDATED, { id: result.id, fields: Object.keys(_update) }); case 11: return _context9.abrupt("return", result); case 12: case "end": return _context9.stop(); } }, _callee9); })); return function (_x13) { return _ref4.apply(this, arguments); }; }()); case 2: return _context10.abrupt("return", _context10.sent); case 3: case "end": return _context10.stop(); } }, _callee10, this); })); function update(_x11, _x12) { return _update2.apply(this, arguments); } return update; }() }, { key: "assign", value: function () { var _assign = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11(payload) { var customer_id, appointment_id, id, customer, resultEntity, tree, befundSatz, labResults, ergebnisBericht, currentNature, blood_analytes, resultsInput; return _regenerator["default"].wrap(function _callee11$(_context11) { while (1) switch (_context11.prev = _context11.next) { case 0: console.log("[LDT3-SERVICE] ::: assign", payload); customer_id = payload.customer_id, appointment_id = payload.appointment_id, id = payload.id; _context11.next = 4; return this.customerService_.retrieve(customer_id, {}); case 4: customer = _context11.sent; _context11.next = 7; return this.retrieve(id, {}); case 7: resultEntity = _context11.sent; tree = resultEntity.raw_file; befundSatz = tree.children.find(function (child) { return child.code === "8205"; }); labResults = befundSatz.children.find(function (child) { return child.code === "Obj_0035"; }); ergebnisBericht = labResults.children.filter(function (child) { return child.code === "Obj_0060"; }); currentNature = ""; // Variable to keep track of the current nature blood_analytes = ergebnisBericht.map(function (analyte, index) { // Check if this is a nature node and update currentNature if it is if (analyte.attributes["Testbezeichnung"] && analyte.attributes["Ergebnisstatus"] === "09") { // Check if the previous object is not a nature to update currentNature if (!(ergebnisBericht[index - 1] && ergebnisBericht[index - 1].attributes["Ergebnisstatus"] === "09")) { currentNature = analyte.attributes["Testbezeichnung"]; } // Skip the current nature object return null; } // Skip if the nature has not been set yet or if it is a subsequent nature object if (!currentNature) { return null; } var nominator = ""; var denominator = ""; if (analyte.attributes["Maßeinheit des Messwertes / Wertes"]) { var _analyte$attributes$M = analyte.attributes["Maßeinheit des Messwertes / Wertes"].split("/"), _analyte$attributes$M2 = (0, _slicedToArray2["default"])(_analyte$attributes$M, 2), nominator_ = _analyte$attributes$M2[0], denominator_ = _analyte$attributes$M2[1]; nominator = nominator_ ? nominator_.trim() : ""; denominator = denominator_ ? denominator_.trim() : ""; if (nominator === denominator) { denominator = ""; } } var value = parseFloat(analyte.attributes["Ergebnis-Wert"]); var status = isNaN(value) ? _examinationResult.ExaminationResultStatus.PENDING : _examinationResult.ExaminationResultStatus.COMPLETED; var referenceValues = getReferenceValues(analyte.children.filter(function (child) { return child.code === "Obj_0042"; })); // TODO: Remove this, This is just for testing // const optionsCondition = [ // ["Non Smoker"], // ["Night"], // ["non smoker", "good morning"], // ["non SMoKer", "gOOd mOrNIng", "nIghT"], // ]; // const randomIndex = Math.floor(Math.random() * optionsCondition.length); return { unit: { nominator: nominator, denominator: denominator }, examination_result_title: { title: analyte.attributes["Testbezeichnung"], nature: currentNature, reference: { gender: customer === null || customer === void 0 ? void 0 : customer.gender, hint: null, type: null, unit: { nominator: nominator, denominator: denominator }, // conditions: ["testing", "Test", "teST"], // TODO: Change this value, This is just for testing values: referenceValues } }, status: status, value: value, hint: "" }; }).filter(Boolean); // Remove null values that represent skipped or nature nodes console.log("[LDT3-SERVICE] ::: blood_analytes", blood_analytes); resultsInput = { customer_id: customer_id, appointment_id: appointment_id, reference_id: resultEntity.external_id, blood_analytes: blood_analytes, laboratory_provider_id: "Labor Froreich", // FIXME: Make this dynamic order_entry_date: resultEntity === null || resultEntity === void 0 ? void 0 : resultEntity.order_entry_date, lab_result_datetime: resultEntity === null || resultEntity === void 0 ? void 0 : resultEntity.lab_result_datetime }; _context11.next = 18; return this.examinationResultService_.createResults(resultsInput); case 18: case "end": return _context11.stop(); } }, _callee11, this); })); function assign(_x14) { return _assign.apply(this, arguments); } return assign; }() }]); }(_medusa.TransactionBaseService); // eslint-disable-next-line max-len (0, _defineProperty2["default"])(LaboratoryTempResultService, "IndexName", "laboratoryTempResults"); (0, _defineProperty2["default"])(LaboratoryTempResultService, "Events", { UPDATED: "laboratoryTempResult.updated", CREATED: "laboratoryTempResult.created", DELETED: "laboratoryTempResult.deleted" }); var getReferenceValues = function getReferenceValues(nodes) { var referenceValues = nodes.map(function (node, index) { var normalizedString = node.attributes["Normalwert-Text"].replace(/(nüchtern:|Graubereich|\s+)/gi, "").trim(); // Attempt to match the string against known patterns var rangeMatch = normalizedString.match(/^(\d+(\.\d+)?)-(\d+(\.\d+)?)$/); var lessThanMatch = normalizedString.match(/^<\s*(\d+(\.\d+)?)$/); var greaterThanMatch = normalizedString.match(/^>\s*(\d+(\.\d+)?)$/); if (rangeMatch) { // Range format, e.g., "3.6-10.0" return { low_value: parseFloat(rangeMatch[1]), high_value: parseFloat(rangeMatch[3]), value_type: _referenceValue.ReferenceValueType.GOOD, order: index }; } else if (lessThanMatch) { // Less than format, e.g., "< 15.0" return { high_value: parseFloat(lessThanMatch[1]), unlimited_low_value: true, value_type: _referenceValue.ReferenceValueType.GOOD, order: index }; } else if (greaterThanMatch) { // Greater than format, e.g., "> 80" return { low_value: parseFloat(greaterThanMatch[1]), unlimited_high_value: true, value_type: _referenceValue.ReferenceValueType.GOOD, order: index }; } // If the format doesn't match any known pattern, return null return null; }); return referenceValues.filter(Boolean); // Remove null values }; var _default = exports["default"] = LaboratoryTempResultService;