Skip to content

Commit

Permalink
Merge pull request #8 from Onlineberatung/feature/VIC-903_reschedule_…
Browse files Browse the repository at this point in the history
…link

feat: add booking link to booking object
  • Loading branch information
adnanalicic authored Jul 13, 2022
2 parents fb711f0 + c2f3fea commit 83bad5d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
3 changes: 3 additions & 0 deletions api/appointmentService.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ components:
endTime:
type: string
example: 2022-06-16T00:00:00.000
rescheduleLink:
type: string
example: /consultant-slug/event-typ-slug?rescheduleUid=myUId
ArrayOfCalcomBookings:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.vi.appointmentservice.api.model.CalcomWebhookPayload;
import com.vi.appointmentservice.api.model.MeetingSlug;
import com.vi.appointmentservice.generated.api.controller.AskersApi;
import com.vi.appointmentservice.helper.RescheduleHelper;
import com.vi.appointmentservice.model.CalcomBookingToAsker;
import com.vi.appointmentservice.repository.CalcomBookingToAskerRepository;
import com.vi.appointmentservice.service.CalComBookingService;
import io.swagger.annotations.Api;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -23,22 +26,12 @@
@RestController
@Api(tags = "asker")
@Slf4j
@RequiredArgsConstructor
public class AskerController implements AskersApi {

CalComBookingService calComBookingService;



CalcomBookingToAskerRepository calcomBookingToAskerRepository;

@Autowired
public AskerController(
CalComBookingService calComBookingService,
CalcomBookingToAskerRepository calcomBookingToAskerRepository)
{
this.calComBookingService = calComBookingService;
this.calcomBookingToAskerRepository = calcomBookingToAskerRepository;
}
private final @NonNull CalComBookingService calComBookingService;
private final @NonNull RescheduleHelper rescheduleHelper;
private final @NonNull CalcomBookingToAskerRepository calcomBookingToAskerRepository;


@Override
Expand All @@ -50,6 +43,9 @@ public ResponseEntity<List<CalcomBooking>> getAllBookingsOfAsker(String askerId)
for (CalcomBookingToAsker bookingId : bookingIds) {
bookings.add(calComBookingService.getBookingById(bookingId.getCalcomBookingId()));
}
for(CalcomBooking booking : bookings){
rescheduleHelper.attachRescheduleLink(booking);
}

return new ResponseEntity<>(bookings, HttpStatus.OK);
}
Expand All @@ -62,7 +58,7 @@ public ResponseEntity<List<CalcomBooking>> getAllBookingsOfAsker(String askerId)
public ResponseEntity<CalcomBooking> getBookingDetails(String bookingId) {
try {
CalcomBooking booking = calComBookingService.getBookingById(Long.valueOf(bookingId));
return new ResponseEntity<>(booking, HttpStatus.OK);
return new ResponseEntity<>(rescheduleHelper.attachRescheduleLink(booking), HttpStatus.OK);
}
catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,8 @@ public ResponseEntity<CalcomEventType> addEventTypeToConsultant(String userId, C
@Override
public ResponseEntity<List<CalcomBooking>> getAllBookingsOfConsultant(String userId) {
try {
Integer calcomUserId = Math.toIntExact(calcomUserToConsultantRepository.findByConsultantId(userId).getCalComUserId());

List<CalcomBooking> bookings = calComBookingService.getBookings().stream()
.filter(o -> o.getUserId().equals(calcomUserId))
.collect(Collectors.toList());

Long calcomUserId = calcomUserToConsultantRepository.findByConsultantId(userId).getCalComUserId();
List<CalcomBooking> bookings = calComBookingService.getAllBookingsForConsultant(calcomUserId);
return new ResponseEntity<>(bookings, HttpStatus.OK);
} catch (Exception e) {
log.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.vi.appointmentservice.helper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.vi.appointmentservice.api.model.CalcomBooking;
import com.vi.appointmentservice.service.CalComEventTypeService;
import com.vi.appointmentservice.service.CalComUserService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class RescheduleHelper {

private final @NonNull CalComEventTypeService eventTypeService;
private final @NonNull CalComUserService calComUserService;

public CalcomBooking attachRescheduleLink(CalcomBooking calcomBooking)
throws JsonProcessingException {
String userSlug = this.calComUserService.getUserById(Long.valueOf(calcomBooking.getUserId())).getUsername();
String eventTypeSlug = this.eventTypeService.getEventTypeById(
Long.valueOf(calcomBooking.getEventTypeId())).getSlug();
calcomBooking.setRescheduleLink("/" + userSlug + "/" + eventTypeSlug + "?rescheduleUid=" + calcomBooking.getUid());
return calcomBooking;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vi.appointmentservice.api.model.CalcomBooking;
import com.vi.appointmentservice.helper.RescheduleHelper;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,24 +23,36 @@
@Slf4j
public class CalComBookingService extends CalComService {

private final @NonNull RescheduleHelper rescheduleHelper;

@Autowired
public CalComBookingService(RestTemplate restTemplate, @Value("${calcom.apiUrl}") String calcomApiUrl, @Value("${calcom.apiKey}") String calcomApiKey) {
public CalComBookingService(RestTemplate restTemplate, @Value("${calcom.apiUrl}") String calcomApiUrl, @Value("${calcom.apiKey}") String calcomApiKey,
@NonNull RescheduleHelper rescheduleHelper) {
super(restTemplate, calcomApiUrl, calcomApiKey);
this.rescheduleHelper = rescheduleHelper;
}

// Booking
public List<CalcomBooking> getBookings() throws JsonProcessingException {
public List<CalcomBooking> getAllBookings() throws JsonProcessingException {
String response = this.restTemplate.getForObject(String.format(this.buildUri("/v1/bookings"), calcomApiUrl, calcomApiKey), String.class);
JSONObject jsonObject = new JSONObject(response);
log.debug(String.valueOf(jsonObject));
response = jsonObject.getJSONArray("bookings").toString();
log.debug(response);
ObjectMapper mapper = new ObjectMapper();
CalcomBooking[] result = mapper.readValue(response, CalcomBooking[].class);

return List.of(Objects.requireNonNull(result));
List<CalcomBooking> result = List.of(Objects.requireNonNull(mapper.readValue(response, CalcomBooking[].class)));
log.info("All bookings: {}", result);
return result;
}

public List<CalcomBooking> getAllBookingsForConsultant(Long userId) throws JsonProcessingException {
List<CalcomBooking> allBookings = this.getAllBookings();
List<CalcomBooking> filteredBookings = allBookings.stream()
.filter(booking -> Integer.valueOf(userId.intValue()).equals(booking.getUserId())).collect(Collectors.toList());
log.info("Found bookings: {}", filteredBookings);
for(CalcomBooking booking : filteredBookings){
rescheduleHelper.attachRescheduleLink(booking);
}
return filteredBookings;
}

public CalcomBooking createBooking(CalcomBooking booking) {
HttpHeaders headers = new HttpHeaders();
Expand Down

0 comments on commit 83bad5d

Please sign in to comment.