From c80623050a4c8a0e8b2ef9b7ad2c3c6dfc5f68d8 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Fri, 31 May 2024 12:37:27 +0200 Subject: [PATCH] fix: appointments are loaded even if eventType is missing --- .../repository/EventTypeRepository.java | 12 +++ .../service/CalcomEventTypeService.java | 4 + .../helper/RescheduleHelper.java | 33 +++++--- .../CalcomEventTypeServiceTest.java | 8 ++ .../helper/RescheduleHelperTest.java | 75 +++++++++++++++++++ 5 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/vi/appointmentservice/helper/RescheduleHelperTest.java diff --git a/src/main/java/com/vi/appointmentservice/api/calcom/repository/EventTypeRepository.java b/src/main/java/com/vi/appointmentservice/api/calcom/repository/EventTypeRepository.java index 17e9622..21dea40 100644 --- a/src/main/java/com/vi/appointmentservice/api/calcom/repository/EventTypeRepository.java +++ b/src/main/java/com/vi/appointmentservice/api/calcom/repository/EventTypeRepository.java @@ -3,6 +3,7 @@ import com.vi.appointmentservice.api.calcom.model.CalcomEventType; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import javax.sql.DataSource; import javax.validation.constraints.NotNull; @@ -39,6 +40,17 @@ public CalcomEventType getEventTypeById(Number eventTypeId) { return getCalcomEventType(result); } + public Optional findEventTypeById(Number eventTypeId) { + String selectEvent = "SELECT * FROM \"EventType\" WHERE id = :eventTypeId"; + SqlParameterSource parameters = new MapSqlParameterSource(EVENT_TYPE_ID, eventTypeId); + List> result = db.queryForList(selectEvent, parameters); + if (result.isEmpty()) { + return Optional.empty(); + } else { + return Optional.of(getCalcomEventType(result.get(0))); + } + } + public CalcomEventType getEventTypeByUserId(Number userId) { String selectEvent = "SELECT * FROM \"EventType\" WHERE \"userId\" = :userId"; SqlParameterSource parameters = new MapSqlParameterSource(USER_ID, userId); diff --git a/src/main/java/com/vi/appointmentservice/api/calcom/service/CalcomEventTypeService.java b/src/main/java/com/vi/appointmentservice/api/calcom/service/CalcomEventTypeService.java index b51f1b5..92412ce 100644 --- a/src/main/java/com/vi/appointmentservice/api/calcom/service/CalcomEventTypeService.java +++ b/src/main/java/com/vi/appointmentservice/api/calcom/service/CalcomEventTypeService.java @@ -43,6 +43,10 @@ public CalcomEventType getEventTypeById(Number eventTypeId) { return eventType; } + public Optional findEventTypeById(Number eventTypeId) { + return eventTypeRepository.findEventTypeById(eventTypeId); + } + public CalcomEventType getEventTypeByUserId(Number userId) { return eventTypeRepository.getEventTypeByUserId(userId); } diff --git a/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java b/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java index 015da75..f8f715b 100644 --- a/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java +++ b/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java @@ -38,25 +38,38 @@ public CalcomBooking attachRescheduleLink(CalcomBooking calcomBooking) { .getUserById(Long.valueOf(calcomBooking.getUserId())); var teamId = getTeamIdForBooking(calcomBooking); String slug = null; - if (teamId != null) { - CalcomTeam team = calComTeamService.getTeamById(teamId); + if (teamId.isPresent()) { + CalcomTeam team = calComTeamService.getTeamById(teamId.get()); slug = "team/" + team.getSlug(); } else { slug = registeredCalcomUser.getUsername(); } - String eventTypeSlug = this.calcomEventTypeService.getEventTypeById( - Long.valueOf(calcomBooking.getEventTypeId())).getSlug(); - calcomBooking.setRescheduleLink( - "/" + slug + "/" + eventTypeSlug + "?rescheduleUid=" + calcomBooking.getUid()); + return attachRescheduleLinkIfEventTypeIsFound(calcomBooking, slug); + } + + private CalcomBooking attachRescheduleLinkIfEventTypeIsFound(CalcomBooking calcomBooking, String slug) { + var optionalEventType = this.calcomEventTypeService.findEventTypeById( + Long.valueOf(calcomBooking.getEventTypeId())); + if (optionalEventType.isEmpty()) { + log.warn("EventType not found for bookingId " + calcomBooking.getId()); + return calcomBooking; + } else { + calcomBooking.setRescheduleLink( + "/" + slug + "/" + optionalEventType.get().getSlug() + "?rescheduleUid=" + calcomBooking.getUid()); + } return calcomBooking; } - private Number getTeamIdForBooking(CalcomBooking calcomBooking) { - CalcomEventType eventType = calcomEventTypeService - .getEventTypeById(Long.valueOf(calcomBooking.getEventTypeId())); - return eventType.getTeamId(); + private Optional getTeamIdForBooking(CalcomBooking calcomBooking) { + Optional eventType = calcomEventTypeService + .findEventTypeById(Long.valueOf(calcomBooking.getEventTypeId())); + if (eventType.isEmpty() || eventType.get().getTeamId() == null) { + return Optional.empty(); + } else { + return Optional.of(eventType.get().getTeamId()); + } } public void attachConsultantName(List bookings) { diff --git a/src/test/java/com.vi.appointmentservice.api.service.calcom/CalcomEventTypeServiceTest.java b/src/test/java/com.vi.appointmentservice.api.service.calcom/CalcomEventTypeServiceTest.java index e8c6602..6a88987 100644 --- a/src/test/java/com.vi.appointmentservice.api.service.calcom/CalcomEventTypeServiceTest.java +++ b/src/test/java/com.vi.appointmentservice.api.service.calcom/CalcomEventTypeServiceTest.java @@ -84,4 +84,12 @@ public void shouldUpdateEventTypeTitle() { assertThat(captor.getValue().getTitle()).isEqualTo("Beratung mit dem / der Berater:in ConsultantDisplayName"); } + @Test + public void shouldDelegateToRepositoryToFindEventType() { + // when + calcomEventTypeService.findEventTypeById(1L); + // then + Mockito.verify(eventTypeRepository).findEventTypeById(1L); + } + } \ No newline at end of file diff --git a/src/test/java/com/vi/appointmentservice/helper/RescheduleHelperTest.java b/src/test/java/com/vi/appointmentservice/helper/RescheduleHelperTest.java new file mode 100644 index 0000000..551a41b --- /dev/null +++ b/src/test/java/com/vi/appointmentservice/helper/RescheduleHelperTest.java @@ -0,0 +1,75 @@ +package com.vi.appointmentservice.helper; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +import com.vi.appointmentservice.api.calcom.model.CalcomEventType; +import com.vi.appointmentservice.api.calcom.model.CalcomUser; +import com.vi.appointmentservice.api.calcom.repository.EventTypeRepository; +import com.vi.appointmentservice.api.calcom.service.CalComTeamService; +import com.vi.appointmentservice.api.calcom.service.CalComUserService; +import com.vi.appointmentservice.api.calcom.service.CalcomEventTypeService; +import com.vi.appointmentservice.api.model.CalcomBooking; +import com.vi.appointmentservice.api.service.onlineberatung.AdminUserService; +import com.vi.appointmentservice.repository.CalcomBookingToAskerRepository; +import com.vi.appointmentservice.repository.UserToConsultantRepository; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class RescheduleHelperTest { + + @InjectMocks + RescheduleHelper rescheduleHelper; + + @Mock + CalComUserService calComUserService; + + @Mock + EventTypeRepository eventTypeRepository; + + @Mock + UserToConsultantRepository userToConsultantRepository; + + @Mock + AdminUserService adminUserService; + + @Mock + CalcomBookingToAskerRepository calcomBookingToAskerRepository; + + @Mock + CalComTeamService calComTeamService; + + @Mock + CalcomEventTypeService calcomEventTypeService; + + @Test + void shouldNotAttachRescheduleLink_When_EventTypeIsNotFound() { + // given + when(calComUserService.getUserById(Mockito.any())).thenReturn(new CalcomUser()); + + when(calcomEventTypeService.findEventTypeById(Mockito.any())).thenReturn(Optional.empty()); + // when + var result = rescheduleHelper.attachRescheduleLink(new CalcomBooking().userId(1).eventTypeId(2)); + // then + assertNull(result.getRescheduleLink()); + } + + @Test + void should_AttachRescheduleLink_When_EventTypeIsFound() { + // given + when(calComUserService.getUserById(Mockito.any())).thenReturn(new CalcomUser()); + CalcomEventType calcomEventType = new CalcomEventType(); + calcomEventType.setSlug("slug"); + when(calcomEventTypeService.findEventTypeById(Mockito.any())).thenReturn(Optional.of(calcomEventType)); + // when + var result = rescheduleHelper.attachRescheduleLink(new CalcomBooking().userId(1).eventTypeId(2)); + // then + assertNotNull(result.getRescheduleLink()); + } +} \ No newline at end of file