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;
            }
        }
Ejemplo n.º 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());
        }