Skip to content

Commit

Permalink
Merge pull request #4 from virtualidentityag/staging-2024-04-25
Browse files Browse the repository at this point in the history
Staging 2024 04 25
  • Loading branch information
Leandro13Silva13 authored Apr 25, 2024
2 parents 0ef1c49 + aeb70d7 commit f963f83
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public class RegistrationStatisticsDTOConverter {
public RegistrationStatisticsResponseDTO convertStatisticsEvent(
StatisticsEvent rawEvent, StatisticEventsContainer statisticEventsContainer) {
RegistrationMetaData metadata = (RegistrationMetaData) rawEvent.getMetaData();
String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(), statisticEventsContainer.getArchiveSessionEvents());
String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(), statisticEventsContainer.getDeleteAccountEvents());
String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(),
statisticEventsContainer.getArchiveSessionEvents());
String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(),
statisticEventsContainer.getDeleteAccountEvents());

return new RegistrationStatisticsResponseDTO()
.userId(rawEvent.getUser().getId())
Expand All @@ -38,9 +40,14 @@ public RegistrationStatisticsResponseDTO convertStatisticsEvent(
.postalCode(metadata.getPostalCode())
.referer(metadata.getReferer())
.attendedVideoCallsCount(
countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), statisticEventsContainer.getVideoCallStartedEvents()))
.appointmentsBookedCount(countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), statisticEventsContainer.getBookingCreatedEvents()))
.consultantMessagesCount(countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(rawEvent.getUser().getId(), statisticEventsContainer.getConsultantMessageCreatedEvents()));
countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(),
statisticEventsContainer.getVideoCallStartedEvents()))
.appointmentsBookedCount(
countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(),
statisticEventsContainer.getBookingCreatedEvents()))
.consultantMessagesCount(
countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(rawEvent.getUser().getId(),
statisticEventsContainer.getConsultantMessageCreatedEvents()));
}

private Integer countEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId,
Expand All @@ -54,23 +61,27 @@ private Integer countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String
Collection<StatisticsEvent> events) {

return events != null
? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId, events) : 0;
? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId,
events) : 0;
}

private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId,
Collection<StatisticsEvent> statisticsEvents) {
return statisticsEvents.stream()
.filter(event -> event.getMetaData() instanceof AdviceSeekerAwareMetaData)
.filter(event -> {
AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData();
return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId()
.equals(adviceSeekerId);

})
.count();
}

private long getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String adviceSeekerId,
Collection<StatisticsEvent> createMessageEvents) {
return createMessageEvents.stream()
.filter(event -> event.getMetaData() instanceof CreateMessageMetaData)
.filter(event -> {
CreateMessageMetaData metaData = (CreateMessageMetaData) event.getMetaData();
return metaData.getReceiverId() != null && metaData.getReceiverId()
Expand All @@ -83,14 +94,16 @@ private String getEndDate(String maxArchiveDate, String deleteAccountDate) {
return deleteAccountDate != null ? deleteAccountDate : maxArchiveDate;
}

private String findDeleteAccountDate(String userId, Collection<StatisticsEvent> deleteAccountEvents) {
private String findDeleteAccountDate(String userId,
Collection<StatisticsEvent> deleteAccountEvents) {
return deleteAccountEvents != null ? deleteAccountEvents.stream()
.filter(event -> event.getUser() != null && event.getUser().getId().equals(userId))
.map(event -> ((DeleteAccountMetaData) event.getMetaData()).getDeleteDate())
.findFirst().orElse(null) : null;
}

private String findMaxArchiveDate(Long sessionId, Collection<StatisticsEvent> archiveSessionEvents) {
private String findMaxArchiveDate(Long sessionId,
Collection<StatisticsEvent> archiveSessionEvents) {
var maxArchiveEvent = findMaxArchiveSessionEvent(sessionId, archiveSessionEvents);
if (maxArchiveEvent.isPresent()) {
ArchiveMetaData metaData = (ArchiveMetaData) maxArchiveEvent.get().getMetaData();
Expand All @@ -99,7 +112,8 @@ private String findMaxArchiveDate(Long sessionId, Collection<StatisticsEvent> ar
return null;
}

private Optional<StatisticsEvent> findMaxArchiveSessionEvent(Long sessionId, Collection<StatisticsEvent> archiveSessionEvents) {
private Optional<StatisticsEvent> findMaxArchiveSessionEvent(Long sessionId,
Collection<StatisticsEvent> archiveSessionEvents) {
return nonNull(archiveSessionEvents) ? archiveSessionEvents.stream()
.filter(event -> event.getSessionId() != null && event.getSessionId().equals(sessionId))
.max(comparing(StatisticsEvent::getTimestamp)) : Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import com.google.common.collect.Lists;
import de.caritas.cob.statisticsservice.api.model.EventType;
import de.caritas.cob.statisticsservice.api.model.RegistrationStatisticsResponseDTO;
import de.caritas.cob.statisticsservice.api.model.UserRole;
Expand All @@ -19,6 +20,7 @@
import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.DeleteAccountMetaData;
import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.RegistrationMetaData;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -50,7 +52,52 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() {
givenValidStatisticEvent(1L);

// when
RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(testEvent, new StatisticEventsContainer());
RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(
testEvent, new StatisticEventsContainer());

// then
assertThat(result.getUserId(), is(ASKER_ID));
assertThat(result.getRegistrationDate(),
is("2022-09-15T09:14:45Z"));
assertThat(result.getAge(), is(26));
assertThat(result.getGender(), is("FEMALE"));
assertThat(result.getMainTopicInternalAttribute(),
is("alk"));
assertThat(result.getTopicsInternalAttributes(),
is(List.of("alk", "drogen")));
assertThat(result.getPostalCode(), is("12345"));
assertThat(result.getCounsellingRelation(),
is("SELF_COUNSELLING"));
assertThat(result.getTenantName(),
is("tenantName"));
assertThat(result.getAgencyName(),
is("agencyName"));
assertThat(result.getReferer(),
is("aReferer"));
}

@Test
void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse_SkipCounting() {
// given
givenValidStatisticEvent(1L);

var eventWithInvalidMetadataType = StatisticsEvent.builder()
.sessionId(1L)
.eventType(EventType.REGISTRATION)
.user(User.builder().userRole(UserRole.ASKER).id(ASKER_ID).build())
.consultingType(ConsultingType.builder().id(CONSULTING_TYPE_ID).build())
.agency(Agency.builder().id(AGENCY_ID).build())
.timestamp(Instant.now())
.metaData(new LinkedHashMap<>())
.build();

var statisticEventsContainer = StatisticEventsContainer.builder()
.videoCallStartedEvents(Lists.newArrayList(eventWithInvalidMetadataType))
.bookingCreatedEvents(Lists.newArrayList(eventWithInvalidMetadataType))
.consultantMessageCreatedEvents(Lists.newArrayList(eventWithInvalidMetadataType)).build();
// when
RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(
testEvent, statisticEventsContainer);

// then
assertThat(result.getUserId(), is(ASKER_ID));
Expand All @@ -73,6 +120,7 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() {
is("aReferer"));
}


@Test
void convertStatisticsEvent_Should_notFail_When_archiveSessionEventsAreNull() {
// given
Expand Down Expand Up @@ -177,27 +225,31 @@ private void givenValidStatisticEvent(Long sessionId) {
}

private void givenAccountDeleteStatisticEvents() {
deleteAccountEvents = List.of(deleteEvent(ASKER_ID, "2022-10-19T10:00:00.00Z", "delete date for user 1"),
deleteAccountEvents = List.of(
deleteEvent(ASKER_ID, "2022-10-19T10:00:00.00Z", "delete date for user 1"),
deleteEvent("user 2", "2022-10-17T10:00:00.00Z", "delete date for user 2"));
}


private void givenValidArchiveStatisticEvents() {
archiveSessionEvents = List.of(archiveEvent(1L, "2022-10-17T10:00:00.00Z", "1 end date for session 1"),
archiveSessionEvents = List.of(
archiveEvent(1L, "2022-10-17T10:00:00.00Z", "1 end date for session 1"),
archiveEvent(1L, "2022-10-18T10:00:00.00Z", "2 end date for session 1"),
archiveEvent(2L, "2022-10-18T10:00:00.00Z", "end date for session 2"),
archiveEvent(999L, "2022-10-19T10:00:00.00Z", "dummy end date"));
}

private StatisticsEvent archiveEvent(Long sessionId, String timestampString, String endDate) {
Object metaData = ArchiveMetaData.builder().endDate(endDate).build();
return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).sessionId(sessionId).metaData(metaData).build();
return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).sessionId(sessionId)
.metaData(metaData).build();
}

private StatisticsEvent deleteEvent(String userId, String timestampString, String deleteDate) {
Object metaData = DeleteAccountMetaData.builder().deleteDate(deleteDate).build();
User user = new User();
user.setId(userId);
return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).user(user).metaData(metaData).build();
return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).user(user)
.metaData(metaData).build();
}
}

0 comments on commit f963f83

Please sign in to comment.