Skip to content

Commit

Permalink
Merge pull request #126 from Onlineberatung/DELPHI-153
Browse files Browse the repository at this point in the history
fix: appointments are loaded even if eventType is missing
  • Loading branch information
tkuzynow authored May 31, 2024
2 parents cd47d53 + c806230 commit f4c4f4b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +40,17 @@ public CalcomEventType getEventTypeById(Number eventTypeId) {
return getCalcomEventType(result);
}

public Optional<CalcomEventType> findEventTypeById(Number eventTypeId) {
String selectEvent = "SELECT * FROM \"EventType\" WHERE id = :eventTypeId";
SqlParameterSource parameters = new MapSqlParameterSource(EVENT_TYPE_ID, eventTypeId);
List<Map<String, Object>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public CalcomEventType getEventTypeById(Number eventTypeId) {
return eventType;
}

public Optional<CalcomEventType> findEventTypeById(Number eventTypeId) {
return eventTypeRepository.findEventTypeById(eventTypeId);
}

public CalcomEventType getEventTypeByUserId(Number userId) {
return eventTypeRepository.getEventTypeByUserId(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Number> getTeamIdForBooking(CalcomBooking calcomBooking) {
Optional<CalcomEventType> 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<CalcomBooking> bookings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f4c4f4b

Please sign in to comment.