Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/refactor_calcom_usage' into refa…
Browse files Browse the repository at this point in the history
…ctor_calcom_usage
  • Loading branch information
tkuzynow committed Feb 28, 2023
2 parents e7fb52d + efdc608 commit d194cdb
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 87 deletions.
2 changes: 2 additions & 0 deletions api/appointmentService.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ components:
type: string
videoAppointmentId:
type: string
location:
type: string
ArrayOfCalcomBookings:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vi.appointmentservice.api.calcom.model;

import com.vi.appointmentservice.api.model.Location;
import com.vi.appointmentservice.api.model.TeamEventTypeConsultant;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,26 +17,18 @@ public class CalcomEventType {

private Integer length;

private Boolean hidden;

private Integer userId;

private Number teamId;

private String eventName;

private String timeZone;

private String periodType;

private String periodStartDate;

private String periodEndDate;

private Integer periodDays;

private Boolean periodCountCalendarDays;

private Boolean requiresConfirmation;

private String recurringEvent;
Expand All @@ -54,20 +45,14 @@ public class CalcomEventType {

private String schedulingType;

private Integer price;

private String currency;

private Integer slotInterval;

private String successRedirectUrl;

private String description;

private List<Location> locations = null;

private String metadata;

private String locations;

private String type;

private List<TeamEventTypeConsultant> consultants = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.vi.appointmentservice.api.calcom.model;

public enum LocationType {
VIDEO_CALL,
IN_PERSON,
PHONE_CALL,
CHAT
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.vi.appointmentservice.api.calcom.repository;

import com.vi.appointmentservice.api.calcom.model.CalcomEventType;
import com.vi.appointmentservice.api.model.TeamEventTypeConsultant;
import com.vi.appointmentservice.model.CalcomUserToConsultant;
import com.vi.appointmentservice.repository.UserToConsultantRepository;
import java.util.ArrayList;
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 @@ -81,25 +77,8 @@ public void addUserEventTypeRelation(Number eventTypeId, Number calcomUserId) {
} catch (Exception e) {
//do nothing in case relation existss
}

}

public void updateUsersOfEventType(Long eventTypeId,
List<TeamEventTypeConsultant> eventTypeConsultants) {
String DELETE_QUERY = "delete from \"_user_eventtype\" where \"A\"=" + eventTypeId;
jdbcTemplate.update(DELETE_QUERY);
eventTypeConsultants.forEach(eventTypeConsultant -> {
Optional<CalcomUserToConsultant> calcomUserToConsultant = userToConsultantRepository
.findByConsultantId(eventTypeConsultant.getConsultantId());
if (calcomUserToConsultant.isPresent()) {
Long calcomUserId = calcomUserToConsultant.get().getCalComUserId();
String INSERT_QUERY = "insert into \"_user_eventtype\" (\"A\", \"B\") values ($eventTypeIdParam, $userIdParam)";
INSERT_QUERY = INSERT_QUERY.replace("$eventTypeIdParam", eventTypeId.toString())
.replace("$userIdParam", calcomUserId.toString());
jdbcTemplate.update(INSERT_QUERY);
}
});
}

public List<Long> getUserIdsOfEventTypeMembers(Number eventTypeId) {
String QUERY = "SELECT \"B\" FROM \"_user_eventtype\" WHERE \"A\" = " + eventTypeId;
Expand All @@ -113,16 +92,16 @@ public void updateEventTypeScheduleId(Number eventTypeId, Number scheduleId) {
jdbcTemplate.update(UPDATE_QUERY);
}

@Deprecated
public void setPeriodType(Number eventTypeId) {
String UPDATE_QUERY = "update \"EventType\" set \"periodType\"='rolling' where \"id\" = eventTypeId";
public void markAsRoundRobin(Number eventTypeId) {
String UPDATE_QUERY = "update \"EventType\" set \"schedulingType\"='roundRobin' where \"id\" = eventTypeId";
UPDATE_QUERY = UPDATE_QUERY.replace("eventTypeId", eventTypeId.toString());
jdbcTemplate.update(UPDATE_QUERY);
}

public void markAsRoundRobin(Number eventTypeId) {
String UPDATE_QUERY = "update \"EventType\" set \"schedulingType\"='roundRobin' where \"id\" = eventTypeId";
UPDATE_QUERY = UPDATE_QUERY.replace("eventTypeId", eventTypeId.toString());
public void updateLocations(Number eventTypeId, String locations) {
String UPDATE_QUERY = "update \"EventType\" set \"locations\"='locationsParam' where \"id\" = eventTypeId";
UPDATE_QUERY = UPDATE_QUERY.replace("eventTypeId", eventTypeId.toString())
.replace("locationsParam", locations);
jdbcTemplate.update(UPDATE_QUERY);
}

Expand All @@ -144,10 +123,10 @@ public CalcomEventType createEventType(CalcomEventType eventType) {
.addValue("slug", eventType.getSlug())
.addValue("description", eventType.getDescription())
.addValue("length", eventType.getLength())
.addValue("hidden", eventType.getHidden())
.addValue("hidden", false)
.addValue("userId", eventType.getUserId())
.addValue("eventName", eventType.getEventName())
.addValue("periodCountCalendarDays", eventType.getPeriodCountCalendarDays())
.addValue("periodCountCalendarDays", true)
.addValue("periodDays", eventType.getPeriodDays())
.addValue("periodEndDate", eventType.getPeriodEndDate())
.addValue("periodStartDate", eventType.getPeriodStartDate())
Expand All @@ -156,15 +135,17 @@ public CalcomEventType createEventType(CalcomEventType eventType) {
.addValue("schedulingType", eventType.getSchedulingType())
.addValue("teamId", eventType.getTeamId())
.addValue("disableGuests", eventType.getDisableGuests())
.addValue("periodType", eventType.getPeriodType())
.addValue("periodType", "rolling")
.addValue("slotInterval", eventType.getSlotInterval())
.addValue("metadata", eventType.getMetadata())
.addValue("afterEventBuffer", eventType.getAfterEventBuffer())
.addValue("beforeEventBuffer", eventType.getBeforeEventBuffer())
.addValue("hideCalendarNotes", eventType.getHideCalendarNotes());
db.update(INSERT_EVENT_TYPE, parameters, generatedKeyHolder);
return getEventTypeById(Integer.valueOf((Integer) generatedKeyHolder.getKeys().get("id")));

db.update(INSERT_EVENT_TYPE, parameters, generatedKeyHolder);
var eventTypeId = Integer.valueOf((Integer) generatedKeyHolder.getKeys().get("id"));
updateLocations(eventTypeId, eventType.getLocations());
return getEventTypeById(eventTypeId);
}

public void updateEventType(CalcomEventType eventType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package com.vi.appointmentservice.api.calcom.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vi.appointmentservice.api.calcom.repository.BookingRepository;
import com.vi.appointmentservice.api.exception.httpresponses.CalComApiErrorException;
import com.vi.appointmentservice.api.model.CalcomBooking;
import com.vi.appointmentservice.api.calcom.repository.BookingRepository;
import com.vi.appointmentservice.helper.RescheduleHelper;
import com.vi.appointmentservice.model.CalcomBookingToAsker;
import com.vi.appointmentservice.repository.CalcomBookingToAskerRepository;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -26,6 +20,7 @@ public class CalComBookingService {
private final @NonNull RescheduleHelper rescheduleHelper;
private final @NonNull BookingRepository bookingRepository;
private final @NonNull CalcomBookingToAskerRepository calcomBookingToAskerRepository;
private final @NonNull CalcomLocationsService calcomLocationsService;

public List<CalcomBooking> getConsultantActiveBookings(Long consultantId) {
return enrichConsultantResultSet(bookingRepository.getConsultantActiveBookings(consultantId));
Expand All @@ -52,6 +47,7 @@ private List<CalcomBooking> enrichConsultantResultSet(List<CalcomBooking> bookin
CalcomBookingToAsker entity = calcomBookingAsker.get();
booking.setVideoAppointmentId(entity.getVideoAppointmentId());
booking.setAskerId(entity.getAskerId());
booking.setLocation(calcomLocationsService.resolveLocationType(booking));
rescheduleHelper.attachRescheduleLink(booking);
}
rescheduleHelper.attachAskerNames(bookings);
Expand All @@ -73,6 +69,7 @@ List<CalcomBooking> enrichAskerResultSet(List<CalcomBooking> bookings) {
}
CalcomBookingToAsker entity = calcomBookingAsker.get();
booking.setAskerId(entity.getAskerId());
booking.setLocation(calcomLocationsService.resolveLocationType(booking));
booking.setVideoAppointmentId(entity.getVideoAppointmentId());
rescheduleHelper.attachRescheduleLink(booking);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
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.model.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -26,6 +24,8 @@ public class CalcomEventTypeService {

private final @NonNull WebhookRepository webhookRepository;

private final @NonNull CalcomLocationsService calcomLocationsService;

@Value("${app.base.url}")
private String appBaseUrl;

Expand Down Expand Up @@ -73,18 +73,12 @@ public CalcomEventType buildEventType(AppointmentType appointmentType) {
eventType.setAfterEventBuffer(appointmentType.getAfterEventBuffer());
eventType.setSlotInterval(appointmentType.getSlotInterval());
eventType.setSlug(UUID.randomUUID().toString());
eventType.setHidden(false);
eventType.setEventName(appointmentType.getTitle() + " {ATTENDEE} mit {HOST}");
eventType.setRequiresConfirmation(false);
eventType.setDisableGuests(true);
eventType.setHideCalendarNotes(true);
eventType.setPeriodDays(30);
eventType.setPeriodCountCalendarDays(true);
//TODO: do we need this
eventType.setPeriodType("rolling");

//TODO: leave this for now, since it is to complex
// eventType.setLocations(createDefaultLocations());
eventType.setLocations(calcomLocationsService.buildCalcomLocations());
return eventType;
}

Expand Down Expand Up @@ -130,27 +124,6 @@ public void addUser2Event(Long calComUserId, Number eventTypeId) {
eventTypeRepository.addRoundRobinHosts(eventTypeId, calComUserId);
}

private List<Location> createDefaultLocations() {
List<Location> locations = new ArrayList<>();
Location customVideoLink = new Location();
customVideoLink.setType("customVideoLink");
customVideoLink.setLink(appBaseUrl);
locations.add(customVideoLink);
Location link = new Location();
link.setType("link");
link.setLink(appBaseUrl);
locations.add(link);
Location userPhone = new Location();
userPhone.setType("userPhone");
userPhone.setHostPhoneNumber(appBaseUrl);
locations.add(userPhone);
Location inPerson = new Location();
inPerson.setType("inPerson");
inPerson.setAddress("Die Adresse der Beratungsstelle teilt Ihnen ihr:e Berater:in im Chat mit");
locations.add(inPerson);
return locations;
}

public List<CalcomEventType> getAllEventTypesOfTeam(Long teamid) {
var eventTypes = eventTypeRepository.getEventTypes4Team(teamid);
eventTypes.forEach(eventType -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.vi.appointmentservice.api.calcom.service;

import com.vi.appointmentservice.api.model.CalcomBooking;
import com.vi.appointmentservice.api.calcom.model.LocationType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
public class CalcomLocationsService {

private static final String IN_PERSON_MEETING_MESSAGE = "Die Adresse der Beratungsstelle teilt Ihnen ihr:e Berater:in im Chat mit";

private static final String PHONE_CALL_MEETING_MESSAGE = "Die Telefonnummer teilt Ihnen ihr:e Berater:in im Chat mit";

private static final String VIDEO_CALL = "integrations:daily";

private static final String LINK = "https://app.suchtberatung.digital";

public String resolveLocationType(CalcomBooking booking) {
if (IN_PERSON_MEETING_MESSAGE.equals(booking.getLocation())) {
return LocationType.IN_PERSON.name();
} else if (PHONE_CALL_MEETING_MESSAGE.equals(booking.getLocation())) {
return LocationType.PHONE_CALL.name();
} else if (VIDEO_CALL.equals(booking.getLocation())) {
return LocationType.VIDEO_CALL.name();
} else if (LINK.equals(booking.getLocation())) {
return LocationType.CHAT.name();
}
throw new IllegalStateException("Unknown location type");
}

public String buildCalcomLocations() {
return "[{\"type\": \"integrations:daily\"},"
+ "{\"type\": \"inPerson\",\"address\": \"" + IN_PERSON_MEETING_MESSAGE + "\"},"
+ "{\"link\": \"" + LINK + "\",\"type\": \"link\"},"
+ "{\"type\": \"userPhone\",\"hostPhoneNumber\": \"" + PHONE_CALL_MEETING_MESSAGE + "\"}]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,32 @@ protected void configure(HttpSecurity http) throws Exception {

httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.sessionAuthenticationStrategy(sessionAuthenticationStrategy()).and().authorizeRequests()
.anyRequest().permitAll();

.antMatchers(WHITE_LIST).permitAll()

.antMatchers(HttpMethod.GET, "/consultants/**/meetingSlug")
.hasAnyAuthority(AuthorityValue.USER_DEFAULT, AuthorityValue.CONSULTANT_DEFAULT)

.antMatchers(HttpMethod.GET, "/agencies/**/initialMeetingSlug")
.hasAnyAuthority(AuthorityValue.USER_DEFAULT, AuthorityValue.CONSULTANT_DEFAULT)

.antMatchers(HttpMethod.GET, "/consultants", "/consultants/**","/consultants/token")
.hasAnyAuthority(AuthorityValue.CONSULTANT_DEFAULT)

.antMatchers(HttpMethod.GET, "/askers", "/askers/**")
.hasAnyAuthority(AuthorityValue.USER_DEFAULT)
.antMatchers(HttpMethod.PATCH, "/askers/**")
.hasAnyAuthority(AuthorityValue.USER_DEFAULT)

.antMatchers(HttpMethod.GET, "/caldav/hasAccount")
.hasAuthority(AuthorityValue.CONSULTANT_DEFAULT)

.antMatchers(HttpMethod.POST, "/askers/processBooking", "/processBooking")
.permitAll() // auth handeled via hmac in controller

.anyRequest()
.hasAnyAuthority(AuthorityValue.SINGLE_TENANT_ADMIN, AuthorityValue.TENANT_ADMIN,
AuthorityValue.TECHNICAL_DEFAULT);
}

/**
Expand Down

0 comments on commit d194cdb

Please sign in to comment.