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 f495a14..b51f1b5 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 @@ -6,6 +6,7 @@ import com.vi.appointmentservice.api.calcom.repository.MembershipsRepository; import com.vi.appointmentservice.api.calcom.repository.WebhookRepository; import com.vi.appointmentservice.api.facade.AppointmentType; +import com.vi.appointmentservice.api.facade.DefaultTextConstants; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -173,4 +174,14 @@ public void deleteEventType(Long eventTypeId) { eventTypeRepository.removeTeamEventTypeMembershipsForEventType(eventTypeId); eventTypeRepository.removeTeamEventHostsForEventType(eventTypeId); } + + public void updateEventTypeTitle(Long calComUserId, String displayName) { + CalcomEventType eventTypeByUserId = getEventTypeByUserId(calComUserId); + if (eventTypeByUserId.getTitle().contains(DefaultTextConstants.BERATUNG_MIT)) { + eventTypeByUserId.setTitle(DefaultTextConstants.BERATUNG_MIT_DEM_DER_BERATER_IN + " " + displayName); + eventTypeRepository.updateEventType(eventTypeByUserId); + } else { + log.warn("Skipping update of EventType because event type for the user {} does not contain text {}", calComUserId, DefaultTextConstants.BERATUNG_MIT_DEM_DER_BERATER_IN); + } + } } diff --git a/src/main/java/com/vi/appointmentservice/api/facade/ConsultantFacade.java b/src/main/java/com/vi/appointmentservice/api/facade/ConsultantFacade.java index f2c57b2..1344c22 100644 --- a/src/main/java/com/vi/appointmentservice/api/facade/ConsultantFacade.java +++ b/src/main/java/com/vi/appointmentservice/api/facade/ConsultantFacade.java @@ -1,5 +1,7 @@ package com.vi.appointmentservice.api.facade; +import com.google.common.collect.Lists; +import com.vi.appointmentservice.api.calcom.model.CalcomEventType; import com.vi.appointmentservice.api.calcom.model.CalcomUser; import com.vi.appointmentservice.api.calcom.repository.AvailabilityRepository; import com.vi.appointmentservice.api.calcom.repository.BookingRepository; @@ -76,8 +78,11 @@ public void patchAppointmentUser(String consultantId, PatchConsultantDTO consult var name = getDisplayNameOrFallbackToFirstname(consultant); Optional userConsultant = userToConsultantRepository .findByConsultantId(consultantId); + Long calComUserId = userConsultant.orElseThrow().getCalComUserId(); calComUserService - .updateUsername(userConsultant.orElseThrow().getCalComUserId(), name); + .updateUsername(calComUserId, name); + + calComEventTypeService.updateEventTypeTitle(calComUserId, name); } private void linkConsultantToAppointmentUser( @@ -90,7 +95,7 @@ private void linkConsultantToAppointmentUser( void setupDefaultScheduleAndEventType(CalcomUser calcomUser) { Long defaultScheduleId = scheduleRepository.createDefaultSchedule(calcomUser.getId()); AppointmentType defaultAppointmentType = appointmentService.createDefaultAppointmentType(); - defaultAppointmentType.setTitle("Beratung mit dem / der Berater:in"); + defaultAppointmentType.setTitle(DefaultTextConstants.BERATUNG_MIT_DEM_DER_BERATER_IN); calComEventTypeService .createEventType(calcomUser, defaultAppointmentType, defaultScheduleId); diff --git a/src/main/java/com/vi/appointmentservice/api/facade/DefaultTextConstants.java b/src/main/java/com/vi/appointmentservice/api/facade/DefaultTextConstants.java new file mode 100644 index 0000000..ca58b22 --- /dev/null +++ b/src/main/java/com/vi/appointmentservice/api/facade/DefaultTextConstants.java @@ -0,0 +1,11 @@ +package com.vi.appointmentservice.api.facade; + +public class DefaultTextConstants { + + private DefaultTextConstants() { + // private constructor to hide the implicit public one + } + + public static final String BERATUNG_MIT = "Beratung mit "; + public static final String BERATUNG_MIT_DEM_DER_BERATER_IN = "Beratung mit dem / der Berater:in"; +} 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 d4e245c..e8c6602 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 @@ -1,5 +1,6 @@ package com.vi.appointmentservice.api.service.calcom; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +13,7 @@ import org.assertj.core.util.Lists; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; @@ -63,4 +65,23 @@ public void shouldCreateEventTypeAndNotUpdateLocationsIfAppointmentTypeDoesNotCo Mockito.verify(eventTypeRepository, Mockito.never()).updateLocations(Mockito.anyInt(), Mockito.anyString()); } + @Test + public void shouldUpdateEventTypeTitle() { + // given + var eventType = new CalcomEventType(); + eventType.setId(1); + eventType.setTitle("Beratung mit dem / der Berater:in ConsultantFirstname"); + + Long calcomUserId = 2L; + when(eventTypeRepository.getEventTypeByUserId(calcomUserId)).thenReturn(eventType); + + // when + calcomEventTypeService.updateEventTypeTitle(calcomUserId, "ConsultantDisplayName"); + // then + + ArgumentCaptor captor = ArgumentCaptor.forClass(CalcomEventType.class); + Mockito.verify(eventTypeRepository).updateEventType(captor.capture()); + assertThat(captor.getValue().getTitle()).isEqualTo("Beratung mit dem / der Berater:in ConsultantDisplayName"); + } + } \ No newline at end of file