diff --git a/api/statisticsservice.yaml b/api/statisticsservice.yaml index 62d988f..d3b3f0c 100644 --- a/api/statisticsservice.yaml +++ b/api/statisticsservice.yaml @@ -109,6 +109,7 @@ components: enum: - "ASSIGN_SESSION" - "ARCHIVE_SESSION" + - "DELETE_ACCOUNT" - "CREATE_MESSAGE" - "START_VIDEO_CALL" - "STOP_VIDEO_CALL" @@ -165,7 +166,7 @@ components: description: The id of the session example: 12345 - ArchiveSessionStatisticsEventMessage: + ArchiveOrDeleteSessionStatisticsEventMessage: type: object required: - sessionId @@ -188,6 +189,28 @@ components: type: string example: '2022-08-15T21:11:29' + DeleteAccountStatisticsEventMessage: + type: object + required: + - sessionId + - endDate + allOf: + - $ref: '#/components/schemas/StatisticsEventMessage' + - type: object + properties: + tenantId: + type: integer + format: int64 + description: The id of the tenant + example: 1 + userId: + type: string + description: The keycloak id of the consultant + example: d63f4cc0-215d-40e2-a866-2d3e910f0590 + deleteDate: + type: string + example: '2022-08-15T21:11:29' + CreateMessageStatisticsEventMessage: type: object required: @@ -205,6 +228,14 @@ components: type: boolean description: indicates whether the message has an attachment example: true + receiverId: + type: string + description: receiving user id of the message (taken from session). Can be null for groupchat. + tenantId: + type: integer + format: int64 + description: The id of the tenant + example: 1 StartVideoCallStatisticsEventMessage: type: object @@ -220,10 +251,17 @@ components: format: int64 description: The id of the session example: 12345 + adviceSeekerId: + type: string + description: The id of the adviceseeker videoCallUuid: type: string description: The uuid of the video call example: 123e4567-e89b-12d3-a456-556642440000 + tenantId: + type: integer + format: int64 + description: The id of the tenant StopVideoCallStatisticsEventMessage: type: object @@ -287,6 +325,9 @@ components: agencyName: type: string example: "Dortmund Beratungstelle" + referer: + type: string + example: "referer" RegistrationStatisticsListResponseDTO: type: object @@ -336,6 +377,19 @@ components: agencyName: type: string example: "Dortmund Beratungstelle" + referer: + type: string + example: "referer" + appointmentsBookedCount: + type: integer + example: "4" + attendedVideoCallsCount: + type: integer + example: "6" + consultantMessagesCount: + type: integer + example: "72" + description: "Total number of messages sent by all consultants to this adviceseeker" BookingCreatedStatisticsEventMessage: type: object @@ -355,6 +409,12 @@ components: type: string bookingId: type: integer + adviceSeekerId: + type: string + tenantId: + type: integer + format: int64 + description: The id of the tenant BookingRescheduledStatisticsEventMessage: type: object diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java index 706d9ac..bb87237 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java @@ -4,10 +4,14 @@ import static java.util.Objects.nonNull; import de.caritas.cob.statisticsservice.api.model.RegistrationStatisticsResponseDTO; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticEventsContainer; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.AdviceSeekerAwareMetaData; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.ArchiveMetaData; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.CreateMessageMetaData; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.DeleteAccountMetaData; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.RegistrationMetaData; -import java.util.List; +import java.util.Collection; import java.util.Optional; import org.springframework.stereotype.Component; @@ -15,8 +19,11 @@ public class RegistrationStatisticsDTOConverter { public RegistrationStatisticsResponseDTO convertStatisticsEvent( - StatisticsEvent rawEvent, List archiveSessionEvents) { + StatisticsEvent rawEvent, StatisticEventsContainer statisticEventsContainer) { RegistrationMetaData metadata = (RegistrationMetaData) rawEvent.getMetaData(); + String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(), statisticEventsContainer.getArchiveSessionEvents()); + String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(), statisticEventsContainer.getDeleteAccountEvents()); + return new RegistrationStatisticsResponseDTO() .userId(rawEvent.getUser().getId()) .registrationDate(metadata.getRegistrationDate()) @@ -27,11 +34,63 @@ public RegistrationStatisticsResponseDTO convertStatisticsEvent( .counsellingRelation(metadata.getCounsellingRelation()) .mainTopicInternalAttribute(metadata.getMainTopicInternalAttribute()) .topicsInternalAttributes(metadata.getTopicsInternalAttributes()) - .endDate(findEndDate(rawEvent.getSessionId(), archiveSessionEvents)) - .postalCode(metadata.getPostalCode()); + .endDate(getEndDate(maxArchiveDate, deleteAccountDate)) + .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())); + } + + private Integer countEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId, + Collection events) { + + return events != null + ? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadata(adviceSeekerId, events) : 0; + } + + private Integer countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String adviceSeekerId, + Collection events) { + + return events != null + ? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId, events) : 0; + } + + private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId, + Collection statisticsEvents) { + return statisticsEvents.stream() + .filter(event -> { + AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData(); + return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId() + .equals(adviceSeekerId); + }) + .count(); + } + + private long getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String adviceSeekerId, + Collection createMessageEvents) { + return createMessageEvents.stream() + .filter(event -> { + CreateMessageMetaData metaData = (CreateMessageMetaData) event.getMetaData(); + return metaData.getReceiverId() != null && metaData.getReceiverId() + .equals(adviceSeekerId); + }) + .count(); + } + + private String getEndDate(String maxArchiveDate, String deleteAccountDate) { + return deleteAccountDate != null ? deleteAccountDate : maxArchiveDate; + } + + private String findDeleteAccountDate(String userId, Collection 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 findEndDate(Long sessionId, List archiveSessionEvents) { + private String findMaxArchiveDate(Long sessionId, Collection archiveSessionEvents) { var maxArchiveEvent = findMaxArchiveSessionEvent(sessionId, archiveSessionEvents); if (maxArchiveEvent.isPresent()) { ArchiveMetaData metaData = (ArchiveMetaData) maxArchiveEvent.get().getMetaData(); @@ -40,9 +99,9 @@ private String findEndDate(Long sessionId, List archiveSessionE return null; } - private Optional findMaxArchiveSessionEvent(Long sessionId, List archiveSessionEvents) { + private Optional findMaxArchiveSessionEvent(Long sessionId, Collection archiveSessionEvents) { return nonNull(archiveSessionEvents) ? archiveSessionEvents.stream() - .filter(event -> event.getSessionId() == sessionId) + .filter(event -> event.getSessionId() != null && event.getSessionId().equals(sessionId)) .max(comparing(StatisticsEvent::getTimestamp)) : Optional.empty(); } } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsService.java b/src/main/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsService.java index 05ac9a4..787bce7 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsService.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsService.java @@ -1,8 +1,8 @@ package de.caritas.cob.statisticsservice.api.service; import de.caritas.cob.statisticsservice.api.service.securityheader.TenantHeaderSupplier; +import de.caritas.cob.statisticsservice.config.apiclient.UserStatisticsApiControllerFactory; import de.caritas.cob.statisticsservice.config.cache.CacheManagerConfig; -import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.UserStatisticsControllerApi; import de.caritas.cob.statisticsservice.api.service.securityheader.SecurityHeaderSupplier; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; import lombok.NonNull; @@ -14,7 +14,7 @@ @RequiredArgsConstructor public class UserStatisticsService { - private final @NonNull UserStatisticsControllerApi userStatisticsControllerApi; + private final @NonNull UserStatisticsApiControllerFactory userStatisticsApiControllerFactory; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; @@ -41,6 +41,7 @@ public SessionStatisticsResultDTO retrieveSessionViaRcGroupId(String rcGroupId) } private SessionStatisticsResultDTO retrieveSession(Long sessionId, String rcGroupId) { + var userStatisticsControllerApi = userStatisticsApiControllerFactory.createControllerApi(); addDefaultHeaders(userStatisticsControllerApi.getApiClient()); return userStatisticsControllerApi .getSession(sessionId, rcGroupId); diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/service/securityheader/SecurityHeaderSupplier.java b/src/main/java/de/caritas/cob/statisticsservice/api/service/securityheader/SecurityHeaderSupplier.java index cd10b2a..727a116 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/service/securityheader/SecurityHeaderSupplier.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/service/securityheader/SecurityHeaderSupplier.java @@ -21,35 +21,16 @@ public class SecurityHeaderSupplier { @Value("${csrf.cookie.property}") private String csrfCookieProperty; - /** - * Creates the headers containing keycloak token and csrf headers {@link HttpHeaders} object. - * - * @return the created {@link HttpHeaders} - */ - public HttpHeaders getKeycloakAndCsrfHttpHeaders() { - var header = getCsrfHttpHeaders(); - this.addKeycloakAuthorizationHeader(header); - - return header; - } - public HttpHeaders getCsrfHttpHeaders() { var httpHeaders = new HttpHeaders(); - return this.addCsrfValues(httpHeaders); } private HttpHeaders addCsrfValues(HttpHeaders httpHeaders) { var csrfToken = UUID.randomUUID().toString(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.add("Cookie", csrfCookieProperty + "=" + csrfToken); httpHeaders.add(csrfHeaderProperty, csrfToken); - return httpHeaders; } - - private void addKeycloakAuthorizationHeader(HttpHeaders httpHeaders) { - httpHeaders.add("Authorization", "Bearer " + authenticatedUser.getAccessToken()); - } } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveSessionListener.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListener.java similarity index 81% rename from src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveSessionListener.java rename to src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListener.java index 57619f4..2ec00ab 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveSessionListener.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListener.java @@ -1,6 +1,6 @@ package de.caritas.cob.statisticsservice.api.statistics.listener; -import de.caritas.cob.statisticsservice.api.model.ArchiveSessionStatisticsEventMessage; +import de.caritas.cob.statisticsservice.api.model.ArchiveOrDeleteSessionStatisticsEventMessage; import de.caritas.cob.statisticsservice.api.service.UserStatisticsService; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEventBuilder; @@ -16,7 +16,7 @@ */ @Service @RequiredArgsConstructor -public class ArchiveSessionListener { +public class ArchiveOrDeleteSessionListener { private final @NonNull MongoTemplate mongoTemplate; private final @NonNull UserStatisticsService userStatisticsService; @@ -24,13 +24,13 @@ public class ArchiveSessionListener { /** * Consumer for archive session message statistics event. * - * @param eventMessage the {@link ArchiveSessionStatisticsEventMessage} instance + * @param eventMessage the {@link ArchiveOrDeleteSessionStatisticsEventMessage} instance */ @RabbitListener( id = "archive-session-event-listener", queues = "#{rabbitMqConfig.QUEUE_NAME_ARCHIVE_SESSION}", containerFactory = "simpleRabbitListenerContainerFactory") - public void receiveMessage(ArchiveSessionStatisticsEventMessage eventMessage) { + public void receiveMessage(ArchiveOrDeleteSessionStatisticsEventMessage eventMessage) { StatisticsEvent statisticsEvent = StatisticsEventBuilder.getInstance( @@ -45,7 +45,7 @@ public void receiveMessage(ArchiveSessionStatisticsEventMessage eventMessage) { mongoTemplate.insert(statisticsEvent); } - private ArchiveMetaData buildMetaData(ArchiveSessionStatisticsEventMessage eventMessage) { + private ArchiveMetaData buildMetaData(ArchiveOrDeleteSessionStatisticsEventMessage eventMessage) { return ArchiveMetaData.builder() .endDate(eventMessage.getEndDate()) .tenantId(eventMessage.getTenantId()) diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/BookingCreatedListener.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/BookingCreatedListener.java index 24ecb03..b5be145 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/BookingCreatedListener.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/BookingCreatedListener.java @@ -51,6 +51,8 @@ private BookingCreatedMetaData buildMetaData(BookingCreatedStatisticsEventMessag .uid(eventMessage.getUid()) .bookingId(eventMessage.getBookingId()) .currentBookingId(eventMessage.getBookingId()) + .adviceSeekerId(eventMessage.getAdviceSeekerId()) + .tenantId(eventMessage.getTenantId()) .build(); } } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListener.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListener.java index 7923e76..64eb8d8 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListener.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListener.java @@ -45,6 +45,10 @@ public void receiveMessage(CreateMessageStatisticsEventMessage eventMessage) { } private CreateMessageMetaData buildMetaData(CreateMessageStatisticsEventMessage eventMessage) { - return CreateMessageMetaData.builder().hasAttachment(eventMessage.getHasAttachment()).build(); + return CreateMessageMetaData.builder() + .receiverId(eventMessage.getReceiverId()) + .hasAttachment(eventMessage.getHasAttachment()) + .tenantId(eventMessage.getTenantId()) + .build(); } } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/DeleteAccountSessionListener.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/DeleteAccountSessionListener.java new file mode 100644 index 0000000..55bfa54 --- /dev/null +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/DeleteAccountSessionListener.java @@ -0,0 +1,52 @@ +package de.caritas.cob.statisticsservice.api.statistics.listener; + +import de.caritas.cob.statisticsservice.api.model.DeleteAccountStatisticsEventMessage; +import de.caritas.cob.statisticsservice.api.service.UserStatisticsService; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.User; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.DeleteAccountMetaData; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; + +/** + * AMQP Listener for archive session statistics event. + */ +@Service +@RequiredArgsConstructor +public class DeleteAccountSessionListener { + + private final @NonNull MongoTemplate mongoTemplate; + private final @NonNull UserStatisticsService userStatisticsService; + + /** + * Consumer for archive session message statistics event. + * + * @param eventMessage the {@link de.caritas.cob.statisticsservice.api.model.ArchiveOrDeleteSessionStatisticsEventMessage} instance + */ + @RabbitListener( + id = "delete-account-event-listener", + queues = "#{rabbitMqConfig.QUEUE_NAME_DELETE_ACCOUNT}", + containerFactory = "simpleRabbitListenerContainerFactory") + public void receiveMessage(DeleteAccountStatisticsEventMessage eventMessage) { + + StatisticsEvent statisticsEvent = StatisticsEvent.builder() + .eventType(eventMessage.getEventType()) + .timestamp(eventMessage.getTimestamp().toInstant()) + .user(User.builder().userRole(eventMessage.getUserRole()).id(eventMessage.getUserId()) + .build()) + .metaData(buildMetaData(eventMessage)) + .build(); + + mongoTemplate.insert(statisticsEvent); + } + + private DeleteAccountMetaData buildMetaData(DeleteAccountStatisticsEventMessage eventMessage) { + return DeleteAccountMetaData.builder() + .deleteDate(eventMessage.getDeleteDate()) + .tenantId(eventMessage.getTenantId()) + .build(); + } +} diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListener.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListener.java index 6ffd112..e0b30df 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListener.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListener.java @@ -58,6 +58,7 @@ private RegistrationMetaData buildMetaData(RegistrationStatisticsEventMessage ev .topicsInternalAttributes(eventMessage.getTopicsInternalAttributes()) .mainTopicInternalAttribute(eventMessage.getMainTopicInternalAttribute()) .postalCode(eventMessage.getPostalCode()) + .referer(eventMessage.getReferer()) .build(); } } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListener.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListener.java index 2dd8965..b732416 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListener.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListener.java @@ -52,9 +52,11 @@ public void receiveMessage(StartVideoCallStatisticsEventMessage eventMessage) { private StartVideoCallMetaData buildMetaData(StartVideoCallStatisticsEventMessage eventMessage) { return StartVideoCallMetaData.builder() .videoCallUuid(eventMessage.getVideoCallUuid()) + .tenantId(eventMessage.getTenantId()) .duration(0) .timestampStop(null) .status(VideoCallStatus.ONGOING) + .adviceSeekerId(eventMessage.getAdviceSeekerId()) .build(); } } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/StatisticEventsContainer.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/StatisticEventsContainer.java new file mode 100644 index 0000000..17efb61 --- /dev/null +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/StatisticEventsContainer.java @@ -0,0 +1,21 @@ +package de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent; + +import java.util.Collection; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class StatisticEventsContainer { + + private Collection archiveSessionEvents; + private Collection deleteAccountEvents; + private Collection videoCallStartedEvents; + private Collection bookingCreatedEvents; + private Collection consultantMessageCreatedEvents; + +} diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/AdviceSeekerAwareMetaData.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/AdviceSeekerAwareMetaData.java new file mode 100644 index 0000000..efa4a82 --- /dev/null +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/AdviceSeekerAwareMetaData.java @@ -0,0 +1,6 @@ +package de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta; + +public interface AdviceSeekerAwareMetaData { + + String getAdviceSeekerId(); +} diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/BookingCreatedMetaData.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/BookingCreatedMetaData.java index 04816ba..47e6188 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/BookingCreatedMetaData.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/BookingCreatedMetaData.java @@ -10,7 +10,7 @@ @Data @NoArgsConstructor @AllArgsConstructor -public class BookingCreatedMetaData { +public class BookingCreatedMetaData implements AdviceSeekerAwareMetaData { private String type; private String title; @@ -19,5 +19,7 @@ public class BookingCreatedMetaData { private String uid; private Integer bookingId; private Integer currentBookingId; + private String adviceSeekerId; + private Long tenantId; } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/CreateMessageMetaData.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/CreateMessageMetaData.java index 1adf636..45af492 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/CreateMessageMetaData.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/CreateMessageMetaData.java @@ -12,5 +12,8 @@ public class CreateMessageMetaData { private boolean hasAttachment; + private String receiverId; + + private Long tenantId; } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/DeleteAccountMetaData.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/DeleteAccountMetaData.java new file mode 100644 index 0000000..1689458 --- /dev/null +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/DeleteAccountMetaData.java @@ -0,0 +1,12 @@ +package de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +public class DeleteAccountMetaData { + + private String deleteDate; + private Long tenantId; +} diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/RegistrationMetaData.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/RegistrationMetaData.java index c222708..d0bf119 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/RegistrationMetaData.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/RegistrationMetaData.java @@ -19,8 +19,9 @@ public class RegistrationMetaData { private Integer age; private String gender; private String counsellingRelation; - private List topicsInternalAttributes = null; + private List topicsInternalAttributes; private String mainTopicInternalAttribute; private String postalCode; + private String referer; } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/StartVideoCallMetaData.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/StartVideoCallMetaData.java index 000ae9a..3fe9fd1 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/StartVideoCallMetaData.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/model/statisticsevent/meta/StartVideoCallMetaData.java @@ -10,11 +10,14 @@ @Data @NoArgsConstructor @AllArgsConstructor -public class StartVideoCallMetaData { +public class StartVideoCallMetaData implements AdviceSeekerAwareMetaData { private String videoCallUuid; private long duration; private Instant timestampStop; private VideoCallStatus status; + private String adviceSeekerId; + + private Long tenantId; } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepository.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepository.java index 4ae98f7..e69c4c1 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepository.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepository.java @@ -83,9 +83,21 @@ class Duration { @Query(value = "{'eventType': 'REGISTRATION'}") List getAllRegistrationStatistics(); + @Query(value = "{'eventType': 'START_VIDEO_CALL'}") + List getAllStartVideoCallSessionEvents(); + @Query(value = "{'eventType': 'ARCHIVE_SESSION'}") List getAllArchiveSessionEvents(); + @Query(value = "{'eventType': 'BOOKING_CREATED'}") + List getAllBookingCreatedEvents(); + + @Query(value = "{'eventType': 'DELETE_ACCOUNT'}") + List getAllDeleteAccountSessionEvents(); + + @Query(value = "{'eventType': 'CREATE_MESSAGE', 'user.userRole': 'CONSULTANT'}") + List getConsultantMessageCreatedEvents(); + /** * Calculate the number of done appointments. * Done mean that the endTime of the appointment or the endTime of the latest reschedule has been reached, and it was not canceled diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepository.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepository.java index b8e6a80..1d48f7f 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepository.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepository.java @@ -12,4 +12,16 @@ public interface StatisticsEventTenantAwareRepository extends MongoRepository getAllArchiveSessionEvents(Long tenantId); + + @Query(value = "{'eventType': 'DELETE_ACCOUNT', 'metaData.tenantId': ?0}") + List getAllDeleteAccountSessionEvents(Long tenantId); + + @Query(value = "{'eventType': 'START_VIDEO_CALL', 'metaData.tenantId': ?0}") + List getAllStartVideoCallSessionEvents(Long tenantId); + + @Query(value = "{'eventType': 'BOOKING_CREATED', 'metaData.tenantId': ?0}") + List getAllBookingCreatedEvents(Long currentTenant); + + @Query(value = "{'eventType': 'CREATE_MESSAGE', 'user.userRole': 'CONSULTANT', 'metaData.tenantId': ?0}") + List getConsultantMessageCreatedEvents(Long currentTenant); } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsService.java b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsService.java index 2c880cf..2662e22 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsService.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsService.java @@ -1,11 +1,14 @@ package de.caritas.cob.statisticsservice.api.statistics.service; import de.caritas.cob.statisticsservice.api.helper.RegistrationStatisticsDTOConverter; +import de.caritas.cob.statisticsservice.api.model.EventType; import de.caritas.cob.statisticsservice.api.model.RegistrationStatisticsListResponseDTO; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticEventsContainer; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventRepository; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventTenantAwareRepository; import de.caritas.cob.statisticsservice.api.tenant.TenantContext; +import java.util.Collection; import java.util.List; import lombok.NonNull; import lombok.RequiredArgsConstructor; @@ -35,17 +38,56 @@ public RegistrationStatisticsListResponseDTO fetchRegistrationStatisticsData() { } private RegistrationStatisticsListResponseDTO buildResponseDTO() { - List archiveSessionEvents = getArchiveSessionEvents(); + StatisticEventsContainer statisticEventsContainer = new StatisticEventsContainer( + getArchiveSessionEvents(), getDeleteAccountEvents(), getVideoCallStartedEvents(), + getAllBookingCreatedEvents(), getAllMessageCreatedEvents()); RegistrationStatisticsListResponseDTO registrationStatisticsList = new RegistrationStatisticsListResponseDTO(); getRegistrationStatistics() .stream() - .map(rawEvent -> registrationStatisticsDTOConverter.convertStatisticsEvent(rawEvent, archiveSessionEvents)) + .map(rawEvent -> registrationStatisticsDTOConverter.convertStatisticsEvent(rawEvent, + statisticEventsContainer)) .forEach(registrationStatisticsList::addRegistrationStatisticsItem); return registrationStatisticsList; } + private Collection getAllMessageCreatedEvents() { + if (isAllTenantAccessContext()) { + return getMessageCreatedEventsForAllTenants(); + } else { + return getMessageCreatedEventsForCurrentTenant(); + } + } + + private Collection getAllBookingCreatedEvents() { + if (isAllTenantAccessContext()) { + return getBookingCreatedEventsForAllTenants(); + } else { + return getBookingCreatedEventsForCurrentTenant(); + } + } + + private List getVideoCallStartedEvents() { + if (isAllTenantAccessContext()) { + return getVideoCallStartedEventsAllTenants(); + } else { + return getVideoCallStartedEventsForCurrentTenant(); + } + } + + private List getVideoCallStartedEventsForCurrentTenant() { + log.info("Gathering video call started events for all tenants"); + return statisticsEventTenantAwareRepository.getAllStartVideoCallSessionEvents( + TenantContext.getCurrentTenant()); + } + + private List getVideoCallStartedEventsAllTenants() { + log.info("Gathering video call started events for all tenants"); + return statisticsEventRepository.getAllStartVideoCallSessionEvents(); + + } + private List getRegistrationStatistics() { if (isAllTenantAccessContext()) { return getRegistrationStatisticsForAllTenants(); @@ -62,8 +104,28 @@ private List getArchiveSessionEvents() { } } + private List getDeleteAccountEvents() { + if (isAllTenantAccessContext()) { + return getDeleteAccountEventsForAllTenants(); + } else { + return getDeleteAccountEventsForCurrentTenant(); + } + } + + private List getDeleteAccountEventsForAllTenants() { + log.info("Gathering delete account events for all tenants"); + return statisticsEventRepository.getAllDeleteAccountSessionEvents(); + } + + private List getDeleteAccountEventsForCurrentTenant() { + log.info("Gathering delete account events for current tenant"); + return statisticsEventTenantAwareRepository.getAllDeleteAccountSessionEvents( + TenantContext.getCurrentTenant()); + } + private List getRegistrationStatisticsForCurrentTenant() { - log.info("Gathering registration statistics for tenant with id {}", TenantContext.getCurrentTenant()); + log.info("Gathering registration statistics for tenant with id {}", + TenantContext.getCurrentTenant()); return statisticsEventTenantAwareRepository.getAllRegistrationStatistics( TenantContext.getCurrentTenant()); } @@ -78,9 +140,34 @@ private List getArchiveSessionEventsForAllTenants() { return statisticsEventRepository.getAllArchiveSessionEvents(); } + private List getMessageCreatedEventsForAllTenants() { + log.info("Gathering message created events for all tenants"); + return statisticsEventRepository.getConsultantMessageCreatedEvents(); + } + + private List getBookingCreatedEventsForAllTenants() { + log.info("Gathering booked appointments events for all tenants"); + return statisticsEventRepository.getAllBookingCreatedEvents(); + } + + private List getMessageCreatedEventsForCurrentTenant() { + log.info("Gathering message created events for tenant with id {}", + TenantContext.getCurrentTenant()); + return statisticsEventTenantAwareRepository.getConsultantMessageCreatedEvents(TenantContext.getCurrentTenant()); + } + + private List getBookingCreatedEventsForCurrentTenant() { + log.info("Gathering booked appointments events for tenant with id {}", + TenantContext.getCurrentTenant()); + return statisticsEventTenantAwareRepository.getAllBookingCreatedEvents( + TenantContext.getCurrentTenant()); + } + private List getArchiveSessionEventsForCurrentTenant() { - log.info("Gathering archive session events for tenant with id {}", TenantContext.getCurrentTenant()); - return statisticsEventTenantAwareRepository.getAllArchiveSessionEvents(TenantContext.getCurrentTenant()); + log.info("Gathering archive session events for tenant with id {}", + TenantContext.getCurrentTenant()); + return statisticsEventTenantAwareRepository.getAllArchiveSessionEvents( + TenantContext.getCurrentTenant()); } private boolean isAllTenantAccessContext() { @@ -91,3 +178,4 @@ private boolean multitenancyIsDisabled() { return !multitenancyEnabled; } } + diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/RabbitMqConfig.java b/src/main/java/de/caritas/cob/statisticsservice/config/RabbitMqConfig.java index 08d341c..c860a3f 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/config/RabbitMqConfig.java +++ b/src/main/java/de/caritas/cob/statisticsservice/config/RabbitMqConfig.java @@ -53,6 +53,8 @@ public class RabbitMqConfig { public static final String QUEUE_NAME_BOOKING_RESCHEDULED = QUEUE_PREFIX + EventType.BOOKING_RESCHEDULED; public static final String QUEUE_NAME_BOOKING_CANCELED = QUEUE_PREFIX + EventType.BOOKING_CANCELLED; + public static final String QUEUE_NAME_DELETE_ACCOUNT = QUEUE_PREFIX + EventType.DELETE_ACCOUNT; + @Value("${spring.rabbitmq.listener.simple.retry.max-attempts}") private int retryMaxAttempts; @Value("${spring.rabbitmq.listener.simple.retry.initial-interval}") @@ -75,6 +77,8 @@ public Declarables topicBindings() { var bookingCreatedStatisticsEventQueue = buildQueue(QUEUE_NAME_BOOKING_CREATED); var bookingRescheduledStatisticsEventQueue = buildQueue(QUEUE_NAME_BOOKING_RESCHEDULED); var bookingCanceledStatisticsEventQueue = buildQueue(QUEUE_NAME_BOOKING_CANCELED); + var deleteAccountStatisticsEventQueue = buildQueue(QUEUE_NAME_DELETE_ACCOUNT); + var deadLetterExchange = new DirectExchange(DEAD_LETTER_EXCHANGE_NAME, true, false); var topicExchange = new TopicExchange(STATISTICS_EXCHANGE_NAME, true, false); @@ -119,7 +123,12 @@ public Declarables topicBindings() { bookingCanceledStatisticsEventQueue, BindingBuilder.bind(bookingCanceledStatisticsEventQueue) .to(topicExchange) - .with(EventType.BOOKING_CANCELLED)); + .with(EventType.BOOKING_CANCELLED), + deleteAccountStatisticsEventQueue, + BindingBuilder.bind(deleteAccountStatisticsEventQueue) + .to(topicExchange) + .with(EventType.DELETE_ACCOUNT) + ); } private Queue buildQueue(String queueName) { diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/apiclient/UserStatisticsApiControllerFactory.java b/src/main/java/de/caritas/cob/statisticsservice/config/apiclient/UserStatisticsApiControllerFactory.java new file mode 100644 index 0000000..35dca2c --- /dev/null +++ b/src/main/java/de/caritas/cob/statisticsservice/config/apiclient/UserStatisticsApiControllerFactory.java @@ -0,0 +1,28 @@ +package de.caritas.cob.statisticsservice.config.apiclient; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.UserStatisticsControllerApi; +import de.caritas.cob.statisticsservice.userstatisticsservice.generated.ApiClient; + +@Component +@RequiredArgsConstructor +public class UserStatisticsApiControllerFactory { + + @Value("${user.statistics.service.api.url}") + private String userStatisticsServiceApiUrl; + + @Autowired + private RestTemplate restTemplate; + + private final ObjectMapper objectMapper; + + public UserStatisticsControllerApi createControllerApi() { + var apiClient = new ApiClient(restTemplate).setBasePath(this.userStatisticsServiceApiUrl); + return new UserStatisticsControllerApi(apiClient); + } +} diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/apiclient/UserStatisticsControllerApiConfig.java b/src/main/java/de/caritas/cob/statisticsservice/config/apiclient/UserStatisticsControllerApiConfig.java deleted file mode 100644 index db039f5..0000000 --- a/src/main/java/de/caritas/cob/statisticsservice/config/apiclient/UserStatisticsControllerApiConfig.java +++ /dev/null @@ -1,41 +0,0 @@ -package de.caritas.cob.statisticsservice.config.apiclient; - -import de.caritas.cob.statisticsservice.userstatisticsservice.generated.ApiClient; -import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.UserStatisticsControllerApi; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class UserStatisticsControllerApiConfig { - - @Value("${user.statistics.service.api.url}") - private String userStatisticsServiceApiUrl; - - /** - * UserStatisticsService controller bean. - * - * @param apiClient {@link ApiClient} - * @return the UserStatisticsService controller {@link UserStatisticsControllerApi} - */ - @Bean - public UserStatisticsControllerApi userStatisticsControllerApi( - ApiClient apiClient) { - return new UserStatisticsControllerApi(apiClient); - } - - /** - * ConsultingTypeService API client bean. - * - * @param restTemplate {@link RestTemplate} - * @return the ConsultingTypeService {@link ApiClient} - */ - @Bean - @Primary - public ApiClient userStatisticsApiClient(RestTemplate restTemplate) { - return new ApiClient(restTemplate).setBasePath(this.userStatisticsServiceApiUrl); - } - -} diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java index 43eedbd..0b82549 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java @@ -12,9 +12,11 @@ import de.caritas.cob.statisticsservice.api.model.UserRole; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.Agency; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.ConsultingType; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticEventsContainer; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.User; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.ArchiveMetaData; +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.List; @@ -34,6 +36,8 @@ class RegistrationStatisticsDTOConverterTest { private List archiveSessionEvents; + private List deleteAccountEvents; + @AfterEach void teardownEach() { testEvent = null; @@ -46,8 +50,7 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() { givenValidStatisticEvent(1L); // when - RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( - testEvent, null); + RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(testEvent, new StatisticEventsContainer()); // then assertThat(result.getUserId(), is(ASKER_ID)); @@ -66,6 +69,8 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() { is("tenantName")); assertThat(result.getAgencyName(), is("agencyName")); + assertThat(result.getReferer(), + is("aReferer")); } @Test @@ -75,7 +80,7 @@ void convertStatisticsEvent_Should_notFail_When_archiveSessionEventsAreNull() { // when RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( - testEvent, null); + testEvent, new StatisticEventsContainer()); // then assertThat(result.getEndDate(), is(nullValue())); @@ -89,12 +94,31 @@ void convertStatisticsEvent_Should_addNewestArchiveSessionEndDate_When_multipleA // when RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( - testEvent, archiveSessionEvents); + testEvent, StatisticEventsContainer.builder() + .archiveSessionEvents(archiveSessionEvents).build()); // then assertThat(result.getEndDate(), is("2 end date for session 1")); } + @Test + void convertStatisticsEvent_Should_takeDeleteDateAsSessionEndDate_When_multipleArchiveSessionEventsAreAvailableAndDeleteDateExists() { + // given + givenValidStatisticEvent(1L); + givenValidArchiveStatisticEvents(); + givenAccountDeleteStatisticEvents(); + + // when + RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( + testEvent, StatisticEventsContainer.builder() + .archiveSessionEvents(archiveSessionEvents) + .deleteAccountEvents(deleteAccountEvents) + .build()); + + // then + assertThat(result.getEndDate(), is("delete date for user 1")); + } + @Test void convertStatisticsEvent_Should_addArchiveSessionEndDate_When_onlyOneArchiveSessionEventIsAvailable() { // given @@ -103,7 +127,9 @@ void convertStatisticsEvent_Should_addArchiveSessionEndDate_When_onlyOneArchiveS // when RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( - testEvent, archiveSessionEvents); + testEvent, StatisticEventsContainer.builder() + .archiveSessionEvents(archiveSessionEvents) + .build()); // then assertThat(result.getEndDate(), is("end date for session 2")); @@ -117,7 +143,9 @@ void convertStatisticsEvent_Should_notAddArchiveSessionEndDate_When_noMatchingAr // when RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( - testEvent, archiveSessionEvents); + testEvent, StatisticEventsContainer.builder() + .archiveSessionEvents(archiveSessionEvents) + .build()); // then assertThat(result.getEndDate(), is(nullValue())); @@ -135,6 +163,7 @@ private void givenValidStatisticEvent(Long sessionId) { .counsellingRelation("SELF_COUNSELLING") .tenantName("tenantName") .agencyName("agencyName") + .referer("aReferer") .build(); testEvent = StatisticsEvent.builder() .sessionId(sessionId) @@ -147,6 +176,12 @@ private void givenValidStatisticEvent(Long sessionId) { .build(); } + private void givenAccountDeleteStatisticEvents() { + 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"), archiveEvent(1L, "2022-10-18T10:00:00.00Z", "2 end date for session 1"), @@ -158,4 +193,11 @@ private StatisticsEvent archiveEvent(Long sessionId, String timestampString, Str Object metaData = ArchiveMetaData.builder().endDate(endDate).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(); + } } diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveSessionListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java similarity index 88% rename from src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveSessionListenerTest.java rename to src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java index f1e7b99..7e57a23 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveSessionListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java @@ -11,7 +11,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import de.caritas.cob.statisticsservice.api.model.ArchiveSessionStatisticsEventMessage; +import de.caritas.cob.statisticsservice.api.model.ArchiveOrDeleteSessionStatisticsEventMessage; import de.caritas.cob.statisticsservice.api.model.EventType; import de.caritas.cob.statisticsservice.api.model.UserRole; import de.caritas.cob.statisticsservice.api.service.UserStatisticsService; @@ -29,10 +29,10 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -public class ArchiveSessionListenerTest { +public class ArchiveOrDeleteSessionListenerTest { @InjectMocks - ArchiveSessionListener archiveSessionListener; + ArchiveOrDeleteSessionListener archiveSessionListener; @Mock MongoTemplate mongoTemplate; @Mock @@ -48,7 +48,7 @@ public void receiveMessage_Should_saveEventToMongoDb() { .thenReturn(sessionStatisticsResultDTO); OffsetDateTime timestamp = OffsetDateTime.now(); - ArchiveSessionStatisticsEventMessage archiveSessionStatisticsEventMessage = buildEventMessage(timestamp); + ArchiveOrDeleteSessionStatisticsEventMessage archiveSessionStatisticsEventMessage = buildEventMessage(timestamp); // when archiveSessionListener.receiveMessage(archiveSessionStatisticsEventMessage); @@ -76,8 +76,8 @@ private SessionStatisticsResultDTO buildResultDto() { .rcGroupId(RC_GROUP_ID); } - private ArchiveSessionStatisticsEventMessage buildEventMessage(OffsetDateTime timestamp) { - return new ArchiveSessionStatisticsEventMessage() + private ArchiveOrDeleteSessionStatisticsEventMessage buildEventMessage(OffsetDateTime timestamp) { + return new ArchiveOrDeleteSessionStatisticsEventMessage() .sessionId(SESSION_ID) .tenantId(TENANT_ID) .eventType(EventType.ARCHIVE_SESSION) diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java index 9c2303b..25273ed 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java @@ -95,6 +95,7 @@ private RegistrationStatisticsEventMessage buildEventMessage() { .topicsInternalAttributes(List.of("angeho01", "angeho13")) .mainTopicInternalAttribute("angeho01") .postalCode("99999") + .referer("aReferer") .timestamp(OffsetDateTime.now()); } @@ -109,6 +110,7 @@ private RegistrationMetaData buildMetaData(RegistrationStatisticsEventMessage ev .topicsInternalAttributes(eventMessage.getTopicsInternalAttributes()) .mainTopicInternalAttribute(eventMessage.getMainTopicInternalAttribute()) .postalCode(eventMessage.getPostalCode()) + .referer(eventMessage.getReferer()) .build(); } diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java index a7e00b7..5129040 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java @@ -17,9 +17,11 @@ import de.caritas.cob.statisticsservice.api.model.UserRole; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.Agency; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.ConsultingType; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticEventsContainer; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.User; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.ArchiveMetaData; +import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.DeleteAccountMetaData; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.RegistrationMetaData; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventRepository; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventTenantAwareRepository; @@ -93,7 +95,8 @@ void fetchRegistrationStatisticsData_Should_RetrieveExpectedData_When_matchingSt var result = registrationStatisticsService.fetchRegistrationStatisticsData(); // then - verify(registrationStatisticsDTOConverter).convertStatisticsEvent(any(StatisticsEvent.class), anyList()); + verify(registrationStatisticsDTOConverter).convertStatisticsEvent(any(StatisticsEvent.class), any( + StatisticEventsContainer.class)); assertThat(result.getRegistrationStatistics().get(0).getUserId(), is(ASKER_ID)); assertThat(result.getRegistrationStatistics().get(0).getRegistrationDate(), @@ -119,7 +122,7 @@ void fetchRegistrationStatisticsData_Should_addEndDate() { var result = registrationStatisticsService.fetchRegistrationStatisticsData(); // then - verify(registrationStatisticsDTOConverter).convertStatisticsEvent(any(StatisticsEvent.class), anyList()); + verify(registrationStatisticsDTOConverter).convertStatisticsEvent(any(StatisticsEvent.class), any(StatisticEventsContainer.class)); assertThat(result.getRegistrationStatistics().get(0).getEndDate(), is("end date 1")); @@ -157,8 +160,21 @@ private void givenArchiveSessionEvents() { when(statisticsEventRepository.getAllArchiveSessionEvents()).thenReturn(archiveEvents); } + private void givenDeleteSessionEvents() { + List deleteAccountEvents = List.of(archiveSessionEvent(1L, "end date 1"), + archiveSessionEvent(99L, "end date 2")); + when(statisticsEventRepository.getAllDeleteAccountSessionEvents()).thenReturn(deleteAccountEvents); + } + private StatisticsEvent archiveSessionEvent(Long sessionId, String endDate) { Object metaData = ArchiveMetaData.builder().endDate(endDate).build(); return StatisticsEvent.builder().sessionId(sessionId).metaData(metaData).build(); } + + private StatisticsEvent deleteAccountEvents(String userId, String deleteDate) { + User user = new User(); + user.setId(userId); + Object metaData = DeleteAccountMetaData.builder().deleteDate(deleteDate).build(); + return StatisticsEvent.builder().user(user).metaData(metaData).build(); + } } diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/testhelper/TestConstants.java b/src/test/java/de/caritas/cob/statisticsservice/api/testhelper/TestConstants.java index d47c25f..5598cc3 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/testhelper/TestConstants.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/testhelper/TestConstants.java @@ -50,6 +50,7 @@ private TestConstants() {} .postalCode("99999") .tenantName("tenanName") .agencyName("agencyName") + .referer("https://www.caritas.de/") ).addRegistrationStatisticsItem(new RegistrationStatisticsResponseDTO() .userId(ASKER_ID) @@ -62,6 +63,7 @@ private TestConstants() {} .postalCode("11111") .tenantName("tenanName") .agencyName("agencyName") + .referer("https://www.caritas.de/") ); }