Search in sources :

Example 1 with ResourceList

use of org.sagebionetworks.bridge.models.ResourceList in project BridgeServer2 by Sage-Bionetworks.

the class AppController method getAppMemberships.

@GetMapping(path = { "/v1/apps/memberships", "/v3/studies/memberships" }, produces = { APPLICATION_JSON_UTF8_VALUE })
public String getAppMemberships() throws Exception {
    UserSession session = getAuthenticatedSession();
    if (session.getParticipant().getRoles().isEmpty()) {
        throw new UnauthorizedException(APP_ACCESS_EXCEPTION_MSG);
    }
    Stream<App> stream = null;
    if (!session.isSynapseAuthenticated()) {
        // If they have not signed in via Synapse, they cannot switch apps, so don't return any
        stream = ImmutableList.<App>of().stream();
    } else if (session.isInRole(SUPERADMIN)) {
        // Superadmins can see all apps and can switch between all apps.
        stream = appService.getApps().stream().filter(s -> s.isActive());
    } else {
        // Otherwise, apps are linked by Synapse user ID.
        List<String> appIds = accountService.getAppIdsForUser(session.getParticipant().getSynapseUserId());
        stream = appIds.stream().map(id -> appService.getApp(id)).filter(s -> s.isActive() && appIds.contains(s.getIdentifier()));
    }
    List<App> apps = stream.sorted(APP_COMPARATOR).collect(toList());
    return APP_LIST_WRITER.writeValueAsString(new ResourceList<App>(apps, true));
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) AppEmailType(org.sagebionetworks.bridge.services.AppEmailType) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ResourceList(org.sagebionetworks.bridge.models.ResourceList) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) APP_LIST_WRITER(org.sagebionetworks.bridge.models.apps.App.APP_LIST_WRITER) CmsPublicKey(org.sagebionetworks.bridge.models.CmsPublicKey) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PostMapping(org.springframework.web.bind.annotation.PostMapping) UploadCertificateService(org.sagebionetworks.bridge.services.UploadCertificateService) StatusMessage(org.sagebionetworks.bridge.models.StatusMessage) Set(java.util.Set) APPLICATION_JSON_UTF8_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE) RestController(org.springframework.web.bind.annotation.RestController) ADMIN(org.sagebionetworks.bridge.Roles.ADMIN) BridgeConfigFactory(org.sagebionetworks.bridge.config.BridgeConfigFactory) List(java.util.List) Stream(java.util.stream.Stream) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) BridgeUtils(org.sagebionetworks.bridge.BridgeUtils) UploadView(org.sagebionetworks.bridge.models.upload.UploadView) DEVELOPER(org.sagebionetworks.bridge.Roles.DEVELOPER) SynapseException(org.sagebionetworks.client.exceptions.SynapseException) EmailVerificationService(org.sagebionetworks.bridge.services.EmailVerificationService) CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) App(org.sagebionetworks.bridge.models.apps.App) GetMapping(org.springframework.web.bind.annotation.GetMapping) EmailVerificationStatusHolder(org.sagebionetworks.bridge.models.apps.EmailVerificationStatusHolder) WORKER(org.sagebionetworks.bridge.Roles.WORKER) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) APP_ACCESS_EXCEPTION_MSG(org.sagebionetworks.bridge.BridgeConstants.APP_ACCESS_EXCEPTION_MSG) SUPERADMIN(org.sagebionetworks.bridge.Roles.SUPERADMIN) DateTime(org.joda.time.DateTime) UnauthorizedException(org.sagebionetworks.bridge.exceptions.UnauthorizedException) EmailVerificationStatus(org.sagebionetworks.bridge.services.EmailVerificationStatus) HttpStatus(org.springframework.http.HttpStatus) AppAndUsers(org.sagebionetworks.bridge.models.apps.AppAndUsers) Collectors.toList(java.util.stream.Collectors.toList) UploadService(org.sagebionetworks.bridge.services.UploadService) VersionHolder(org.sagebionetworks.bridge.models.VersionHolder) Comparator(java.util.Comparator) SynapseProjectIdTeamIdHolder(org.sagebionetworks.bridge.models.apps.SynapseProjectIdTeamIdHolder) Collections(java.util.Collections) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) UnauthorizedException(org.sagebionetworks.bridge.exceptions.UnauthorizedException) ResourceList(org.sagebionetworks.bridge.models.ResourceList) List(java.util.List) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with ResourceList

use of org.sagebionetworks.bridge.models.ResourceList in project BridgeServer2 by Sage-Bionetworks.

the class StudyActivityEventService method getRecentStudyActivityEvents.

/**
 * Get a complete set of all events for this user, where the timestamp of each entry is the most recent timestamp
 * as determined by the <code>createdOn</code> value of the record (in other words, the time of creation as measured
 * by the server, and not the client or the time of the event).
 *
 * @param appId
 *      the appId of the study
 * @param studyId
 *      the study in which these events have occurred
 * @param userId
 *      the ID of the participant account
 * @return
 *      a complete set of event records for this user, including the most recent record for each event
 */
public ResourceList<StudyActivityEvent> getRecentStudyActivityEvents(String appId, String studyId, String userId) {
    checkNotNull(userId);
    checkNotNull(studyId);
    Account account = accountService.getAccount(AccountId.forId(appId, userId)).orElseThrow(() -> new EntityNotFoundException(Account.class));
    List<StudyActivityEvent> events = dao.getRecentStudyActivityEvents(userId, studyId);
    addEnrollmentIfMissing(account, events, studyId);
    // There are some global events related to authentication and account creation that
    // are useful when working with study-specific events, so add these from the global
    // system.
    Map<String, DateTime> map = activityEventService.getActivityEventMap(appId, account.getHealthCode());
    for (String fieldName : GLOBAL_EVENTS_OF_INTEREST) {
        addIfPresent(events, map, fieldName, true);
    }
    events.sort(Comparator.comparing(StudyActivityEvent::getEventId));
    return new ResourceList<>(events, true);
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) ResourceList(org.sagebionetworks.bridge.models.ResourceList) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) DateTime(org.joda.time.DateTime)

Example 3 with ResourceList

use of org.sagebionetworks.bridge.models.ResourceList in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceServiceTest method getEventStreamAdherenceReport.

@Test
public void getEventStreamAdherenceReport() {
    RequestContext.set(new RequestContext.Builder().withCallerUserId(TEST_USER_ID).withCallerEnrolledStudies(ImmutableSet.of(TEST_STUDY_ID)).build());
    Study study = Study.create();
    study.setScheduleGuid(SCHEDULE_GUID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    List<TimelineMetadata> metadata = ImmutableList.of();
    when(mockScheduleService.getScheduleMetadata(SCHEDULE_GUID)).thenReturn(metadata);
    ResourceList<StudyActivityEvent> events = new ResourceList<>(ImmutableList.of(), true);
    when(mockStudyActivityEventService.getRecentStudyActivityEvents(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID)).thenReturn(events);
    PagedResourceList<AdherenceRecord> adherencePage = new PagedResourceList<>(ImmutableList.of(), 0);
    when(mockRecordDao.getAdherenceRecords(any())).thenReturn(adherencePage);
    EventStreamAdherenceReport report = service.getEventStreamAdherenceReport(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID, EVENT_TS, TEST_CLIENT_TIME_ZONE, true);
    assertTrue(report.getStreams().isEmpty());
    assertEquals(report.getTimestamp(), EVENT_TS.withZone(DateTimeZone.forID(TEST_CLIENT_TIME_ZONE)));
    assertEquals(report.getAdherencePercent(), 100);
    verify(mockRecordDao).getAdherenceRecords(searchCaptor.capture());
    AdherenceRecordsSearch search = searchCaptor.getValue();
    assertTrue(search.getCurrentTimestampsOnly());
    assertTrue(search.getIncludeRepeats());
    assertEquals(search.getAdherenceRecordType(), AdherenceRecordType.SESSION);
    assertEquals(search.getStudyId(), TEST_STUDY_ID);
    assertEquals(search.getUserId(), TEST_USER_ID);
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) TimelineMetadata(org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) ResourceList(org.sagebionetworks.bridge.models.ResourceList) AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) EventStreamAdherenceReport(org.sagebionetworks.bridge.models.schedules2.adherence.eventstream.EventStreamAdherenceReport) AdherenceRecordsSearch(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecordsSearch) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) Test(org.testng.annotations.Test) StudyAdherenceReportGeneratorTest(org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)

Example 4 with ResourceList

use of org.sagebionetworks.bridge.models.ResourceList in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceServiceTest method getWeeklyAdherenceReport_studyHasNoSchedule.

@Test
public void getWeeklyAdherenceReport_studyHasNoSchedule() {
    RequestContext.set(new RequestContext.Builder().withCallerRoles(ImmutableSet.of(ADMIN)).build());
    Account account = Account.create();
    account.setAppId(TEST_APP_ID);
    account.setId(TEST_USER_ID);
    account.setFirstName("firstName");
    account.setLastName("lastName");
    account.setEmail(TestConstants.EMAIL);
    account.setPhone(TestConstants.PHONE);
    account.setClientTimeZone(TEST_CLIENT_TIME_ZONE);
    account.getEnrollments().add(Enrollment.create(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID, TEST_EXTERNAL_ID));
    Study study = Study.create();
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    List<StudyActivityEvent> events = ImmutableList.of();
    ResourceList<StudyActivityEvent> page = new ResourceList<>(events, true);
    when(mockStudyActivityEventService.getRecentStudyActivityEvents(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID)).thenReturn(page);
    PagedResourceList<AdherenceRecord> page2 = new PagedResourceList<>(ImmutableList.of(), 0);
    when(mockRecordDao.getAdherenceRecords(any())).thenReturn(page2);
    WeeklyAdherenceReport retValue = service.getWeeklyAdherenceReport(TEST_APP_ID, TEST_STUDY_ID, account);
    assertEquals(retValue.getAppId(), TEST_APP_ID);
    assertEquals(retValue.getStudyId(), TEST_STUDY_ID);
    assertEquals(retValue.getUserId(), TEST_USER_ID);
    assertEquals(retValue.getClientTimeZone(), TEST_CLIENT_TIME_ZONE);
    assertEquals(retValue.getCreatedOn(), MODIFIED_ON.withZone(DateTimeZone.forID(TEST_CLIENT_TIME_ZONE)));
    assertNull(retValue.getWeeklyAdherencePercent());
    assertEquals(retValue.getParticipant().getIdentifier(), TEST_USER_ID);
    verify(mockReportDao).saveWeeklyAdherenceReport(retValue);
// The contents of the weekly report are tested separately by testing the generator
}
Also used : WeeklyAdherenceReport(org.sagebionetworks.bridge.models.schedules2.adherence.weekly.WeeklyAdherenceReport) Account(org.sagebionetworks.bridge.models.accounts.Account) Study(org.sagebionetworks.bridge.models.studies.Study) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) ResourceList(org.sagebionetworks.bridge.models.ResourceList) AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) RequestContext(org.sagebionetworks.bridge.RequestContext) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) Test(org.testng.annotations.Test) StudyAdherenceReportGeneratorTest(org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)

Example 5 with ResourceList

use of org.sagebionetworks.bridge.models.ResourceList in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceServiceTest method getStudyAdherenceReport.

@Test
public void getStudyAdherenceReport() throws Exception {
    RequestContext.set(new RequestContext.Builder().withCallerUserId("id").withOrgSponsoredStudies(ImmutableSet.of(TEST_STUDY_ID)).withCallerRoles(ImmutableSet.of(STUDY_COORDINATOR)).build());
    Account account = Account.create();
    account.setAppId(TEST_APP_ID);
    account.setDataGroups(ImmutableSet.of(BridgeConstants.TEST_USER_GROUP));
    account.setId(TEST_USER_ID);
    account.setClientTimeZone("America/Chicago");
    Study study = Study.create();
    study.setScheduleGuid(SCHEDULE_GUID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    when(mockScheduleService.getScheduleMetadata(SCHEDULE_GUID)).thenReturn(StudyAdherenceReportGeneratorTest.createTimelineMetadata());
    ResourceList<StudyActivityEvent> events = new ResourceList<>(StudyAdherenceReportGeneratorTest.createEvents());
    when(mockStudyActivityEventService.getRecentStudyActivityEvents(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID)).thenReturn(events);
    StudyBurst burst = new StudyBurst();
    burst.setIdentifier("Study Burst");
    burst.setOriginEventId("timeline_retrieved");
    burst.setUpdateType(IMMUTABLE);
    burst.setDelay(Period.parse("P1W"));
    burst.setOccurrences(3);
    burst.setInterval(Period.parse("P1W"));
    StudyActivityEventIdsMap map = new StudyActivityEventIdsMap();
    map.addStudyBursts(ImmutableList.of(burst));
    when(mockStudyService.getStudyActivityEventIdsMap(TEST_APP_ID, TEST_STUDY_ID)).thenReturn(map);
    PagedResourceList<AdherenceRecord> records = new PagedResourceList<>(StudyAdherenceReportGeneratorTest.createAdherenceRecords(), 10);
    when(mockRecordDao.getAdherenceRecords(any())).thenReturn(records);
    StudyAdherenceReport report = service.getStudyAdherenceReport(TEST_APP_ID, TEST_STUDY_ID, account);
    assertEquals(report.getParticipant().getIdentifier(), TEST_USER_ID);
    assertTrue(report.isTestAccount());
    assertEquals(report.getCreatedOn(), MODIFIED_ON.withZone(DateTimeZone.forID("America/Chicago")));
    assertEquals(report.getClientTimeZone(), "America/Chicago");
    verify(mockReportDao).saveWeeklyAdherenceReport(weeklyReportCaptor.capture());
    WeeklyAdherenceReport weeklyReport = weeklyReportCaptor.getValue();
    assertEquals(weeklyReport.getAppId(), TEST_APP_ID);
    assertEquals(weeklyReport.getStudyId(), TEST_STUDY_ID);
    assertEquals(weeklyReport.getUserId(), TEST_USER_ID);
    assertEquals(weeklyReport.getParticipant(), report.getParticipant());
    assertEquals(weeklyReport.getProgression(), ParticipantStudyProgress.IN_PROGRESS);
    assertTrue(weeklyReport.isTestAccount());
    assertEquals(weeklyReport.getClientTimeZone(), "America/Chicago");
    assertEquals(weeklyReport.getCreatedOn(), MODIFIED_ON.withZone(DateTimeZone.forID("America/Chicago")));
    assertEquals(weeklyReport.getNextActivity().getSessionGuid(), "initialSurveyGuid");
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) Study(org.sagebionetworks.bridge.models.studies.Study) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) StudyAdherenceReport(org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReport) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) WeeklyAdherenceReport(org.sagebionetworks.bridge.models.schedules2.adherence.weekly.WeeklyAdherenceReport) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) ResourceList(org.sagebionetworks.bridge.models.ResourceList) AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) RequestContext(org.sagebionetworks.bridge.RequestContext) Test(org.testng.annotations.Test) StudyAdherenceReportGeneratorTest(org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)

Aggregations

ResourceList (org.sagebionetworks.bridge.models.ResourceList)34 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)19 GetMapping (org.springframework.web.bind.annotation.GetMapping)19 PagedResourceList (org.sagebionetworks.bridge.models.PagedResourceList)17 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)11 Test (org.testng.annotations.Test)10 Account (org.sagebionetworks.bridge.models.accounts.Account)9 Study (org.sagebionetworks.bridge.models.studies.Study)8 App (org.sagebionetworks.bridge.models.apps.App)6 AdherenceRecord (org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord)6 StudyAdherenceReportGeneratorTest (org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)6 RequestContext (org.sagebionetworks.bridge.RequestContext)5 ForwardCursorPagedResourceList (org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)5 WeeklyAdherenceReport (org.sagebionetworks.bridge.models.schedules2.adherence.weekly.WeeklyAdherenceReport)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 DateTime (org.joda.time.DateTime)2 DynamoSchedulePlan (org.sagebionetworks.bridge.dynamodb.DynamoSchedulePlan)2 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)2 UnauthorizedException (org.sagebionetworks.bridge.exceptions.UnauthorizedException)2 NotificationRegistration (org.sagebionetworks.bridge.models.notifications.NotificationRegistration)2