public void FindsAConsentedPersonWhenConsentIsRequired()
        {
            var personOneAgain = createContext.Find <PersonEntity>(personOne.Id);
            var study          = new StudyEntity {
                Name = Random.String()
            };
            var subject = new StudySubjectEntity
            {
                Person = personOneAgain, Study = study, SubjectIdentifier = Random.String()
            };
            var consent = new ConsentEntity
            {
                DateProvided = DateTime.Today, GivenBy = personOneAgain, StudySubject = subject
            };

            createContext.AddRange(study, subject, consent);
            createContext.SaveChanges();

            repository.FindPersonBy(
                new PersonIdentifierSpecification(Identifiers.NhsNumber(personOneNhsNumber))
                )?.Id.Should().Be(personOne.Id);

            repository.FindPersonBy(
                new CompositePersonSpecification(
                    new PersonIdentifierSpecification(Identifiers.NhsNumber(personOneNhsNumber)),
                    new ConsentedPersonSpecification(new StudyIdentity(study.Id))
                    )
                ).Should().Be((PersonIdentity)personOne.Id);
        }
        public async Task <IActionResult> Get(string clientId, string objectId)
        {
            ResponseContent responseContent = new ResponseContent()
            {
                version  = "1.0.0",
                status   = (int)HttpStatusCode.OK,
                clientId = clientId,
                objectId = objectId
            };

            CloudTable consentTable = _cloudTableClient.GetTableReference(TABLE_NAME);
            bool       exists       = await consentTable.ExistsAsync();

            if (exists)
            {
                TableOperation retrieveOperation = TableOperation.Retrieve <ConsentEntity>(clientId, objectId);
                TableResult    result            = await consentTable.ExecuteAsync(retrieveOperation);

                ConsentEntity consentEntity = result.Result as ConsentEntity;
                if (consentEntity != null)
                {
                    if (consentEntity.ConsentDateTime >= DateTime.Now)
                    {
                        responseContent.hasConsented = "true";
                    }
                    else
                    {
                        responseContent.hasConsented = "false";
                    }
                }
                else
                {
                    responseContent.hasConsented = "false";
                }
                responseContent.userRole = await GetUserRole(objectId);

                return(new OkObjectResult(responseContent));
            }
            else
            {
                return(new BadRequestObjectResult($"Table {TABLE_NAME} does not exist."));
            }
        }
        public async Task <IActionResult> SaveConsent(string clientId, string objectId)
        {
            ResponseContent responseContent = new ResponseContent()
            {
                version  = "1.0.0",
                status   = (int)HttpStatusCode.OK,
                clientId = clientId,
                objectId = objectId
            };

            CloudTable consentTable = _cloudTableClient.GetTableReference(TABLE_NAME);
            bool       exists       = await consentTable.ExistsAsync();

            if (exists)
            {
                ConsentEntity consentEntity = new ConsentEntity()
                {
                    ClientId        = clientId,
                    ObjectId        = objectId,
                    ConsentDateTime = DateTime.Now.AddMonths(CONSENT_MONTHS)
                };
                TableOperation updateOperation = TableOperation.InsertOrReplace(consentEntity);
                TableResult    result          = await consentTable.ExecuteAsync(updateOperation);

                if (result.HttpStatusCode == (int)HttpStatusCode.NoContent)
                {
                    responseContent.hasConsented = "true";
                    return(new OkObjectResult(responseContent));
                }
                else
                {
                    return(new BadRequestObjectResult($"Failed to update consent information in table {TABLE_NAME}. HTTP status code is {result.HttpStatusCode}."));
                }
            }
            else
            {
                return(new BadRequestObjectResult($"Table {TABLE_NAME} does not exist."));
            }
        }
Example #4
0
        /// <inheritdoc />
        public ConsentRepositoryTests(ITestOutputHelper outputHelper, DatabaseFixture fixture) : base(outputHelper, fixture)
        {
            readContext   = CreateNewContextInSameTransaction();
            updateContext = CreateNewContextInSameTransaction();
            createContext = CreateNewContextInSameTransaction();


            study = createContext.Studies.Add(new StudyEntity {
                Name = Random.String()
            }).Entity;

            var consentedPerson = createContext.People.Add(new PersonEntity()).Entity;

            consentedStudySubject = createContext.Add(new StudySubjectEntity {
                Study = study, Person = consentedPerson, SubjectIdentifier = "Consented"
            }).Entity;
            createContext.Add(
                new ConsentEntity
            {
                StudySubject  = consentedStudySubject,
                DateProvided  = 1.December(1965),
                DateWithdrawn = 1.January(1966),
                GivenBy       = consentedPerson
            });

            activeConsent = createContext.Add(
                new ConsentEntity
            {
                StudySubject = consentedStudySubject,
                DateProvided = 1.February(1966),
                GivenBy      = consentedPerson
            }).Entity;

            var unconsentedPerson = createContext.People.Add(new PersonEntity()).Entity;

            createContext.Add(
                new StudySubjectEntity {
                Study = study, Person = unconsentedPerson, SubjectIdentifier = "Unconsented"
            });

            var withdrawnConsent = createContext.Add(new PersonEntity()).Entity;
            var withdrawnSubject = createContext.Add(
                new StudySubjectEntity {
                Study = study, Person = withdrawnConsent, SubjectIdentifier = "Withdrawn"
            }).Entity;

            withdrawnSubjectWithdrawnDate = 5.January(2018);
            createContext.Add(new ConsentEntity
            {
                StudySubject  = withdrawnSubject,
                DateProvided  = 1.December(2017),
                DateWithdrawn = withdrawnSubjectWithdrawnDate,
                GivenBy       = withdrawnConsent
            });


            createContext.SaveChanges();

            studyId             = new StudyIdentity(study.Id);
            consentedPersonId   = consentedPerson;
            unconsentedPersonId = unconsentedPerson;
            withdrawnPersonId   = withdrawnConsent;

            consents = CreateConsentRepository(readContext);

            studySubjects = CreateStudySubjectRepository(readContext);
            studies       = CreateStudyRepository(readContext);
        }