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);
        }
Example #2
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());
        }