public async Task <IActionResult> CompleteLabOrder([FromBody] CompleteLabOrderCommand completeLabOrder)
        {
            var labOrderResponse = await _mediator.Send(completeLabOrder).ConfigureAwait(false);

            if (labOrderResponse.IsValid)
            {
                return(Ok(labOrderResponse.Value));
            }
            return(BadRequest(labOrderResponse));
        }
Exemple #2
0
        public async Task <Result <CompleteLabOrderResponse> > Handle(CompleteLabOrderCommand request, CancellationToken cancellationToken)
        {
            using (var transaction = _labUnitOfwork.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                try
                {
                    DateTime resultDate = request.DateResultsCollected.HasValue
                        ? request.DateResultsCollected.Value
                        : DateTime.Now;

                    request.LabTestResults = string.IsNullOrEmpty(request.StrLabTestResults)
                        ? request.LabTestResults
                        : BuildLabTestResultCommandCollection(request.StrLabTestResults);

                    var submittedLabOrderTest = _labUnitOfwork.Repository <LabOrderTest>()
                                                .Get(x => x.Id == request.LabOrderTestId).FirstOrDefault();
                    if (submittedLabOrderTest == null)
                    {
                        return(Result <CompleteLabOrderResponse> .Invalid($"Lab order request with Id {request.LabOrderTestId} not found"));
                    }

                    if (!request.LabTestResults.Any())
                    {
                        return(Result <CompleteLabOrderResponse> .Invalid($"Submit atleast one lab result to complete the lab order"));
                    }

                    var labOrderTestResults = new List <LabOrderTestResult>();

                    foreach (var labTestResult in request.LabTestResults)
                    {
                        var parameterConfig = GetParamterConfigDetails(labTestResult.ParameterId);

                        var resultUnit = parameterConfig?.LabTestParameterConfig?.Unit;

                        var resultOption =
                            parameterConfig?.LabTestParameterResultOptions?.SingleOrDefault(x =>
                                                                                            x.Id == labTestResult.ResultOptionId)?.Value;

                        var labOrderTestResult = new LabOrderTestResult(request.LabOrderId, request.LabOrderTestId,
                                                                        request.LabTestId, labTestResult.ParameterId, labTestResult.ResultValue, labTestResult.ResultText,
                                                                        labTestResult.ResultOptionId, resultOption, resultUnit?.UnitName, resultUnit?.UnitId,
                                                                        request.UserId, labTestResult.Undetectable, resultDate, labTestResult.DetectionLimit);

                        labOrderTestResults.Add(labOrderTestResult);
                    }


                    await _labUnitOfwork.Repository <LabOrderTestResult>().AddRangeAsync(labOrderTestResults);

                    // PatientLabTracker is updated only for LabTests with only one parameter count
                    var labTestParameters = _labUnitOfwork.Repository <LabTestParameter>()
                                            .Get(x => x.LabTestId == request.LabTestId && x.DeleteFlag == false).ToList();

                    var totalLabTestParameterCount = labTestParameters.Count;

                    UpdatePatientLabTestTracker(labTestParameters[0].Id, request.LabOrderId, totalLabTestParameterCount,
                                                request.LabOrderTestId, labOrderTestResults.FirstOrDefault());
                    submittedLabOrderTest.ReceiveResult(request.UserId, resultDate);
                    submittedLabOrderTest.MarkAsReceived();

                    _labUnitOfwork.Repository <LabOrderTest>().Update(submittedLabOrderTest);

                    await _labUnitOfwork.SaveAsync();

                    var labOrderTestPendingSubmission = _labUnitOfwork.Repository <LabOrderTest>()
                                                        .Get(x => x.LabOrderId == request.LabOrderId && x.ResultStatus != ResultStatusEnum.Received.ToString()).Any();

                    if (!labOrderTestPendingSubmission)
                    {
                        var labOrder = _labUnitOfwork.Repository <LabOrder>().Get(x => x.Id == request.LabOrderId).SingleOrDefault();
                        labOrder?.CompleteOrder();
                        _labUnitOfwork.Repository <LabOrder>().Update(labOrder);
                        await _labUnitOfwork.SaveAsync();
                    }

                    transaction.Commit();

                    return(Result <CompleteLabOrderResponse> .Valid(new CompleteLabOrderResponse { LabOrderId = request.LabOrderId }));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }