From c2f3fea07125670a6ceeb694e97c047e2657af03 Mon Sep 17 00:00:00 2001 From: PhilippFruh Date: Wed, 13 Jul 2022 18:12:11 +0200 Subject: [PATCH 1/2] feat: add booking link to booking object Signed-off-by: PhilippFruh --- api/appointmentService.yaml | 3 ++ .../controller/AskerController.java | 26 +++++++---------- .../controller/ConsultantController.java | 8 ++--- .../helper/RescheduleHelper.java | 28 ++++++++++++++++++ .../service/CalComBookingService.java | 29 ++++++++++++++----- 5 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java diff --git a/api/appointmentService.yaml b/api/appointmentService.yaml index 16306f6..933d437 100644 --- a/api/appointmentService.yaml +++ b/api/appointmentService.yaml @@ -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: diff --git a/src/main/java/com/vi/appointmentservice/controller/AskerController.java b/src/main/java/com/vi/appointmentservice/controller/AskerController.java index 355421a..ee0217c 100644 --- a/src/main/java/com/vi/appointmentservice/controller/AskerController.java +++ b/src/main/java/com/vi/appointmentservice/controller/AskerController.java @@ -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; @@ -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 @@ -50,6 +43,9 @@ public ResponseEntity> 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); } @@ -62,7 +58,7 @@ public ResponseEntity> getAllBookingsOfAsker(String askerId) public ResponseEntity 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); diff --git a/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java b/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java index 33ac91d..88d746e 100644 --- a/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java +++ b/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java @@ -244,12 +244,8 @@ public ResponseEntity addEventTypeToConsultant(String userId, C @Override public ResponseEntity> getAllBookingsOfConsultant(String userId) { try { - Integer calcomUserId = Math.toIntExact(calcomUserToConsultantRepository.findByConsultantId(userId).getCalComUserId()); - - List bookings = calComBookingService.getBookings().stream() - .filter(o -> o.getUserId().equals(calcomUserId)) - .collect(Collectors.toList()); - + Long calcomUserId = calcomUserToConsultantRepository.findByConsultantId(userId).getCalComUserId(); + List bookings = calComBookingService.getAllBookingsForConsultant(calcomUserId); return new ResponseEntity<>(bookings, HttpStatus.OK); } catch (Exception e) { log.error(e.getMessage()); diff --git a/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java b/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java new file mode 100644 index 0000000..d4d6338 --- /dev/null +++ b/src/main/java/com/vi/appointmentservice/helper/RescheduleHelper.java @@ -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; + } + +} diff --git a/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java b/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java index d446d8a..e293bc7 100644 --- a/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java +++ b/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java @@ -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; @@ -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 getBookings() throws JsonProcessingException { + public List 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 result = List.of(Objects.requireNonNull(mapper.readValue(response, CalcomBooking[].class))); + log.info("All bookings: {}", result); + return result; } + public List getAllBookingsForConsultant(Long userId) throws JsonProcessingException { + List allBookings = this.getAllBookings(); + List 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(); From 3b3b2620e7fca30a4122f5cde62411917702a742 Mon Sep 17 00:00:00 2001 From: PhilippFruh Date: Thu, 14 Jul 2022 12:37:30 +0200 Subject: [PATCH 2/2] fix: add some error handling to consultant getAllBookings Signed-off-by: PhilippFruh --- .../controller/ConsultantController.java | 10 +++++++--- .../service/CalComBookingService.java | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java b/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java index 88d746e..3f7191c 100644 --- a/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java +++ b/src/main/java/com/vi/appointmentservice/controller/ConsultantController.java @@ -244,9 +244,13 @@ public ResponseEntity addEventTypeToConsultant(String userId, C @Override public ResponseEntity> getAllBookingsOfConsultant(String userId) { try { - Long calcomUserId = calcomUserToConsultantRepository.findByConsultantId(userId).getCalComUserId(); - List bookings = calComBookingService.getAllBookingsForConsultant(calcomUserId); - return new ResponseEntity<>(bookings, HttpStatus.OK); + if(calcomUserToConsultantRepository.existsByConsultantId(userId)){ + Long calcomUserId = calcomUserToConsultantRepository.findByConsultantId(userId).getCalComUserId(); + List bookings = calComBookingService.getAllBookingsForConsultant(calcomUserId); + return new ResponseEntity<>(bookings, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } } catch (Exception e) { log.error(e.getMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); diff --git a/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java b/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java index e293bc7..54b9536 100644 --- a/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java +++ b/src/main/java/com/vi/appointmentservice/service/CalComBookingService.java @@ -39,7 +39,7 @@ public List getAllBookings() throws JsonProcessingException { response = jsonObject.getJSONArray("bookings").toString(); ObjectMapper mapper = new ObjectMapper(); List result = List.of(Objects.requireNonNull(mapper.readValue(response, CalcomBooking[].class))); - log.info("All bookings: {}", result); + log.info("Found total of {} bookings", result.size()); return result; } @@ -47,7 +47,7 @@ public List getAllBookingsForConsultant(Long userId) throws JsonP List allBookings = this.getAllBookings(); List filteredBookings = allBookings.stream() .filter(booking -> Integer.valueOf(userId.intValue()).equals(booking.getUserId())).collect(Collectors.toList()); - log.info("Found bookings: {}", filteredBookings); + log.info("Found {} bookings for user {}", filteredBookings.size(), userId); for(CalcomBooking booking : filteredBookings){ rescheduleHelper.attachRescheduleLink(booking); }