Exemple #1
0
        public async SystemTasks.Task <NrlsCreateResponse> GenerateAndCreatePointer(NrlsPointerRequest pointerRequest)
        {
            //update to allow seperate org codes for custodians and authors
            var pointer = NrlsPointer.Generate(_spineSettings.NrlsDefaultprofile, pointerRequest.CustodianOrgCode, pointerRequest.NhsNumber, pointerRequest.RecordUrl, pointerRequest.RecordContentType, pointerRequest.TypeCode, pointerRequest.TypeDisplay);

            var newPointer = await CreatePointer(pointerRequest, pointer);

            return(newPointer);
        }
        public async SystemTasks.Task <IEnumerable <PointerViewModel> > GetPointers(RequestViewModel request)
        {
            var pointerViewModels = new List <PointerViewModel>();

            var pointerRequest = NrlsPointerRequest.Search(request.OrgCode, request.Id, null, request.Asid, null);

            var pointerResponse = await _docRefService.GetPointersBundle(pointerRequest);

            if (pointerResponse.ResourceType.Equals(ResourceType.OperationOutcome))
            {
                throw new HttpFhirException("Invalid Fhir Request", (OperationOutcome)pointerResponse, null);
            }

            var pointerBundle = pointerResponse as Bundle;

            //need a more slick solution for getting related references
            //we are connecting to NRLS so will only get Pointers back - a complete Fhir server would allow for includes
            var patients = await _patientService.GetPatients();                 //In live this could be lots

            var organisations = await _organisationServices.GetOrganisations(); //In live this could be lots

            var pointers = ListEntries <DocumentReference>(pointerBundle.Entry, ResourceType.DocumentReference);

            //var patients = ListEntries<Patient>(entries, ResourceType.Patient); // If we could do includes take from bundle
            //var organisations = ListEntries<Organization>(entries, ResourceType.Organization); // If we could do includes take from bundle

            foreach (var pointer in pointers)
            {
                var pointerViewModel = pointer.ToViewModel(DefaultUrlBase, PointerUrlBase);
                var patientNhsNumber = pointerViewModel.Subject?.Reference?.Replace(FhirConstants.SystemPDS, "");
                var authorOrgCode    = pointerViewModel.Author?.Reference?.Replace(FhirConstants.SystemODS, "");
                var custodianOrgCode = pointerViewModel.Custodian?.Reference?.Replace(FhirConstants.SystemODS, "");

                //This assumes the resource is relative
                //In reality it does not make sense to attach a patient because a GET to NRLS should be in the patient context anyway!
                var subject = patients.FirstOrDefault(s => s.Identifier.FirstOrDefault(t => !string.IsNullOrEmpty(patientNhsNumber) && !string.IsNullOrEmpty(t.System) && t.System.Equals(FhirConstants.SystemNhsNumber) && !string.IsNullOrEmpty(t.Value) && t.Value.Equals(patientNhsNumber)) != null);
                pointerViewModel.SubjectViewModel = subject?.ToViewModel(null);

                //This assumes the resource is relative
                var custodian = organisations.FirstOrDefault(s => s.Identifier.FirstOrDefault(t => !string.IsNullOrEmpty(custodianOrgCode) && !string.IsNullOrEmpty(t.System) && t.System.Equals(FhirConstants.SystemOrgCode) && !string.IsNullOrEmpty(t.Value) && t.Value.Equals(custodianOrgCode)) != null);
                pointerViewModel.CustodianViewModel = custodian?.ToViewModel(FhirConstants.SystemOrgCode);

                var author = organisations.FirstOrDefault(s => s.Identifier.FirstOrDefault(t => !string.IsNullOrEmpty(authorOrgCode) && !string.IsNullOrEmpty(t.System) && t.System.Equals(FhirConstants.SystemOrgCode) && !string.IsNullOrEmpty(t.Value) && t.Value.Equals(authorOrgCode)) != null);
                pointerViewModel.AuthorViewModel = author?.ToViewModel(FhirConstants.SystemOrgCode);

                pointerViewModels.Add(pointerViewModel);
            }

            if (pointers.Any())
            {
                CachePointers(request.Id, pointerViewModels);
            }

            return(pointerViewModels);
        }
Exemple #3
0
        public async SystemTasks.Task <NrlsCreateResponse> CreatePointer(NrlsPointerRequest pointerRequest, DocumentReference pointer)
        {
            var newPointer = await _fhirConnector.RequestOne <CommandRequest, FhirResponse>(BuildPostRequest(pointerRequest.Asid, pointerRequest.JwtOrgCode, pointer));

            var createResponse = new NrlsCreateResponse
            {
                Resource         = newPointer.GetResource <OperationOutcome>(),
                ResponseLocation = newPointer.ResponseLocation
            };

            return(createResponse);
        }
Exemple #4
0
        public void NrlsPointerRequest_Read_Valid()
        {
            var viewModel = NrlsPointerRequest.Read("id001", "0001", "org1");

            var expectedViewModel = new NrlsPointerRequest
            {
                Asid       = "0001",
                PointerId  = "id001",
                JwtOrgCode = "org1"
            };

            Assert.Equal(expectedViewModel, viewModel, Comparers.ModelComparer <NrlsPointerRequest>());
        }
        public async SystemTasks.Task <NrlsCreateResponse> CreatePointer(NrlsPointerRequest pointerRequest, DocumentReference pointer)
        {
            var pointerJson = new FhirJsonSerializer().SerializeToString(pointer);
            var content     = new StringContent(pointerJson, Encoding.UTF8, $"{ContentType.JSON_CONTENT_HEADER }; charset={Encoding.UTF8.WebName}");

            var newPointer = await new FhirConnector().RequestOne(BuildPostRequest(pointerRequest.Asid, pointerRequest.JwtOrgCode, content));

            var createResponse = new NrlsCreateResponse
            {
                Resource         = newPointer.GetResource <OperationOutcome>(),
                ResponseLocation = newPointer.ResponseLocation
            };

            return(createResponse);
        }
Exemple #6
0
        public void NrlsPointerRequest_Search_Valid()
        {
            var viewModel = NrlsPointerRequest.Search("Test", "0001", "0001", "0001", null);

            var expectedViewModel = new NrlsPointerRequest
            {
                Asid             = "0001",
                TypeCode         = "0001",
                JwtOrgCode       = "Test",
                NhsNumber        = "0001",
                CustodianOrgCode = null
            };

            Assert.Equal(expectedViewModel, viewModel, Comparers.ModelComparer <NrlsPointerRequest>());
        }
        public async SystemTasks.Task <CrisisPlanViewModel> SavePlan(CrisisPlanViewModel crisisPlan)
        {
            try
            {
                crisisPlan = crisisPlan.Cleaned();

                var currentPlan = await this.GetForPatient(crisisPlan.PatientNhsNumber, true);

                int version = 1;

                if (currentPlan != null)
                {
                    version          = currentPlan.Version + 1;
                    currentPlan.Asid = crisisPlan.Asid;
                    //Should really archive plans after update rather than mark as delete
                    //Delete only when an active delete request is made
                    var deleted = await DeletePlan(currentPlan);
                }

                crisisPlan.Version = version;
                crisisPlan.Active  = true;

                //Create new plan
                var newCrisisPlan = CrisisPlan.ToModel(crisisPlan);

                _context.CrisisPlans.InsertOne(newCrisisPlan);

                //Hardcoded values - this is a demo with urls to ficticious documents
                var pointerRequest = NrlsPointerRequest.Create(crisisPlan.OrgCode, crisisPlan.OrgCode, crisisPlan.PatientNhsNumber, $"https://spine-proxy.national.ncrs.nhs.uk/{newCrisisPlan.Id}/mental-health-care-plan.pdf", "application/pdf", MentalHealthCrisisPlanTypeCode, MentalHealthCrisisPlanTypeDisplay, crisisPlan.Asid);

                //Create new NRLS pointer
                var newPointer = await _documentReferenceServices.GenerateAndCreatePointer(pointerRequest);

                //Create map between NRLS Pointer and Medical Record
                _pointerMapService.CreatePointerMap(newPointer.ResourceId, newCrisisPlan.Id, RecordType.MentalHealthCrisisPlan);

                return(await SystemTasks.Task.Run(() => CrisisPlan.ToViewModel(newCrisisPlan)));
            }
            catch (Exception ex)
            {
                // log or manage the exception
                throw ex;
            }
        }
Exemple #8
0
        public void NrlsPointerRequest_Create_Valid()
        {
            var viewModel = NrlsPointerRequest.Create("TestOrg", "TestCustodian", "0001", "https://test.com/test/pdf", "test/test", "0001", "test", "0001");

            var expectedViewModel = new NrlsPointerRequest
            {
                Asid              = "0001",
                NhsNumber         = "0001",
                JwtOrgCode        = "TestOrg",
                CustodianOrgCode  = "TestCustodian",
                PointerId         = null,
                RecordContentType = "test/test",
                RecordUrl         = "https://test.com/test/pdf",
                TypeCode          = "0001",
                TypeDisplay       = "test"
            };

            Assert.Equal(expectedViewModel, viewModel, Comparers.ModelComparer <NrlsPointerRequest>());
        }
        public async SystemTasks.Task <bool> DeletePlan(RequestViewModel request)
        {
            try
            {
                //Find and delete Medical Record
                var update   = new UpdateDefinitionBuilder <CrisisPlan>().Set(n => n.Active, false);
                var previous = _context.CrisisPlans.UpdateOne(item => item.Id == new ObjectId(request.Id) && item.RecordType == RecordType.MentalHealthCrisisPlan.ToString(), update);

                //Find pointer id from local map
                var pointerMap = await _pointerMapService.FindPointerMap(request.Id, RecordType.MentalHealthCrisisPlan);

                //Delete pointer from NRLS
                var deletedPointer = true;
                if (pointerMap != null && !string.IsNullOrEmpty(pointerMap.NrlsPointerId))
                {
                    var pointerRequest = NrlsPointerRequest.Delete(pointerMap.NrlsPointerId, request.Asid, request.OrgCode);

                    var outcome = await _documentReferenceServices.DeletePointer(pointerRequest);

                    deletedPointer = (outcome != null && outcome.Success);

                    if (deletedPointer)
                    {
                        var delMap = _pointerMapService.DeletePointerMap(pointerMap.Id);
                    }
                }


                return(await SystemTasks.Task.Run(() => previous.IsAcknowledged && previous.ModifiedCount > 0 && deletedPointer));
            }
            catch (Exception ex)
            {
                // log or manage the exception
                throw ex;
            }
        }
Exemple #10
0
        public async Task<CommandResult<string>> RunCommand()
        {
            
            var jsonSerializer = new FhirJsonSerializer();

            var jsonResponse = string.Empty;
            NrlsPointerRequest pointerRequest;

            var idParam = _parameters?.FirstOrDefault(n => n.Key.Equals("_id"));
            var asid = _headers.FirstOrDefault(n => n.Key.Equals("fromasid"));
            var orgCode = _headers.FirstOrDefault(n => n.Key.Equals("orgcode"));

            //Massive Try/catch

            try
            {
                if (_method == "get")
                {
                    var patientParam = _parameters.FirstOrDefault(n => n.Key.Equals("subject"));
                    var custodianParam = _parameters.FirstOrDefault(n => n.Key.Equals("custodian"));
                    var typeParam = _parameters.FirstOrDefault(n => n.Key.Equals("type"));

                    if (!string.IsNullOrEmpty(idParam.Value.Value))
                    {
                        pointerRequest = NrlsPointerRequest.Read(idParam.Value.Value, asid.Value, orgCode.Value);
                    }
                    else
                    {
                        pointerRequest = NrlsPointerRequest.Search(orgCode.Value, patientParam.Value, typeParam.Value, asid.Value, custodianParam.Value);
                    }

                    var pointers = await _docRefService.GetPointersBundle(pointerRequest);

                    jsonResponse = jsonSerializer.SerializeToString(pointers);
                }

                if (_method == "post")
                {
                    OperationOutcome documentOutcome;

                    if (_pointerBody != null)
                    {
                        pointerRequest = NrlsPointerRequest.Create(orgCode.Value, _pointerBody.OrgCode, _pointerBody.NhsNumber, _pointerBody.Url, _pointerBody.ContentType, _pointerBody.TypeCode, _pointerBody.TypeDisplay, asid.Value);

                        var response = await _docRefService.GenerateAndCreatePointer(pointerRequest);

                        documentOutcome = response.Resource as OperationOutcome;
                    }
                    else
                    {
                        pointerRequest = NrlsPointerRequest.Create(orgCode.Value, null, null, null, null, null, null, asid.Value);

                        var response = await _docRefService.CreatePointer(pointerRequest, _pointer);

                        documentOutcome = response.Resource as OperationOutcome;
                    }


                    jsonResponse = jsonSerializer.SerializeToString(documentOutcome);
                }

                if (_method == "put")
                {
                    //not implemented
                }

                if (_method == "delete")
                {

                    pointerRequest = NrlsPointerRequest.Delete(idParam.Value.Value, asid.Value, orgCode.Value);

                    var outcome = await _docRefService.DeletePointer(pointerRequest);

                    jsonResponse = jsonSerializer.SerializeToString(outcome);
                }
            }
            catch(Exception ex)
            {
                jsonResponse = ex.Message;
            }
            


            return CommandResult<string>.Set(true, "Success", jsonResponse.ToString());
        }
Exemple #11
0
        public async SystemTasks.Task <OperationOutcome> DeletePointer(NrlsPointerRequest pointerRequest)
        {
            var pointer = await _fhirConnector.RequestOneFhir <CommandRequest, OperationOutcome>(BuildDeleteRequest(pointerRequest.Asid, pointerRequest.JwtOrgCode, pointerRequest.PointerId));

            return(pointer);
        }
Exemple #12
0
        public async SystemTasks.Task <Resource> GetPointersBundle(NrlsPointerRequest pointerRequest)
        {
            var pointers = await _fhirConnector.RequestOneFhir <CommandRequest, Resource>(BuildGetRequest(pointerRequest.Asid, pointerRequest.NhsNumber, pointerRequest.CustodianOrgCode, pointerRequest.PointerId, pointerRequest.TypeCode, pointerRequest.JwtOrgCode));

            return(pointers);
        }