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));
}
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);
}
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);
}
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
}
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");
}
Aggregations