diff --git a/src/main/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepository.java b/src/main/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepository.java index cfea888..e7faed3 100644 --- a/src/main/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepository.java +++ b/src/main/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepository.java @@ -1,9 +1,23 @@ package com.vi.appointmentservice.api.calcom.repository; +import static org.openapitools.codegen.meta.features.DataTypeFeature.Maps; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Set; import javax.validation.constraints.NotNull; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; @@ -11,6 +25,7 @@ import org.springframework.stereotype.Repository; @Repository +@Slf4j public class ScheduleRepository { private final @NotNull JdbcTemplate jdbcTemplate; @@ -29,12 +44,40 @@ public ScheduleRepository(@Qualifier("dbTemplate") NamedParameterJdbcTemplate db this.jdbcTemplate = jdbcTemplate; } - public List deleteUserSchedules(Long calcomUserId) { + public Set deleteUserSchedules(Long calcomUserId) { + var originalScheduleIds = getScheduleIdsByUserId(db, calcomUserId); String DELETE_SCHEDULE = "DELETE FROM \"Schedule\" where \"userId\" = :userId"; SqlParameterSource parameters = new MapSqlParameterSource("userId", calcomUserId); db.update(DELETE_SCHEDULE, parameters); - //TODO: return ids of removed schedules - return null; + var leftScheduleIds = getScheduleIdsByUserId(db, calcomUserId); + return Sets.difference(originalScheduleIds, leftScheduleIds); + } + + public List getTableNames(JdbcTemplate jdbcTemplate) throws SQLException { + List tableNames = new ArrayList<>(); + Connection connection = jdbcTemplate.getDataSource().getConnection(); + DatabaseMetaData metaData = connection.getMetaData(); + ResultSet rs = metaData.getTables(null, null, "%", null); + while (rs.next()) { + tableNames.add(rs.getString("TABLE_NAME")); + } + return tableNames; + } + public Set getScheduleIdsByUserId(NamedParameterJdbcTemplate jdbcTemplate, Long userId) { + Set scheduleIds = Sets.newHashSet(); + Map params = new HashMap<>(); + params.put("userId", userId); + String sql = "SELECT \"id\" FROM \"Schedule\" WHERE \"userId\" = :userId"; + try { + List> rows = jdbcTemplate.queryForList(sql, params); + for (Map row : rows) { + scheduleIds.add((Integer) row.get("id")); + } + } catch (DataAccessException e) { + log.error("Error while fetching schedule ids for user: {}", userId, e); + } + + return scheduleIds; } public Long createDefaultSchedule(Long calcomUserId) { 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 74212ec..f2c57b2 100644 --- a/src/main/java/com/vi/appointmentservice/api/facade/ConsultantFacade.java +++ b/src/main/java/com/vi/appointmentservice/api/facade/ConsultantFacade.java @@ -21,11 +21,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.Set; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -105,7 +105,7 @@ public void deleteConsultantHandler(String consultantId) { // Delete personal event-types calComEventTypeService.deleteAllEventTypesOfUser(calcomUserId); // Delete schedules - List deletedSchedules = scheduleRepository.deleteUserSchedules(calcomUserId); + Set deletedSchedules = scheduleRepository.deleteUserSchedules(calcomUserId); // Delete availabilities for schedules for (Integer scheduleId : deletedSchedules) { availabilityRepository.deleteAvailabilityByScheduleId(Long.valueOf(scheduleId)); diff --git a/src/main/resources/application-testing.properties b/src/main/resources/application-testing.properties index f56ed9c..4e082d8 100644 --- a/src/main/resources/application-testing.properties +++ b/src/main/resources/application-testing.properties @@ -22,7 +22,7 @@ keycloak.config.app-client-id=app-ci spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 spring.sql.init.schema-locations=classpath*:database/AppointmentServiceDatabase.sql -calcom.database.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +calcom.database.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;MODE=PostgreSQL calcom.database.username=appointmentservice calcom.database.password=appointmentservice calcom.database.driverClass=org.h2.Driver diff --git a/src/test/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepositoryTest.java b/src/test/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepositoryTest.java new file mode 100644 index 0000000..1856ef8 --- /dev/null +++ b/src/test/java/com/vi/appointmentservice/api/calcom/repository/ScheduleRepositoryTest.java @@ -0,0 +1,59 @@ +package com.vi.appointmentservice.api.calcom.repository; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Set; +import org.junit.Before; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@TestPropertySource(properties = "spring.profiles.active=testing") +@AutoConfigureTestDatabase(replace = Replace.ANY) +@ExtendWith(SpringExtension.class) +@SpringBootTest +class ScheduleRepositoryTest { + + @Autowired + ScheduleRepository scheduleRepository; + + @Autowired + JdbcTemplate jdbcTemplate; + + @Before + public void setUp() { + jdbcTemplate.execute("DROP TABLE IF EXISTS \"Schedule\""); + } + @Test + void deleteUserSchedules_Should_DeleteSchedulesPerUserId() { + // given + inititalizeDB(); + + jdbcTemplate.execute("INSERT INTO \"Schedule\" (\"id\", \"userId\", \"name\") VALUES (1, 1, 'DEFAULT_SCHEDULE')"); + jdbcTemplate.execute("INSERT INTO \"Schedule\" (\"id\", \"userId\", \"name\") VALUES (2, 1, 'DEFAULT_SCHEDULE')"); + // when + Set integers = scheduleRepository.deleteUserSchedules(1L); + // then + assertThat(integers).containsOnly(1, 2); + + } + + private void inititalizeDB() { + // we can't use @Sql annotation here because it's not visible in jdbcTemplate, + // probably because there are defined multiple jdbc templates in this project for different datasources + jdbcTemplate.execute("create table \"Schedule\"\n" + + "(\n" + + " \"id\" integer not null\n" + + " primary key,\n" + + " \"userId\" integer not null,\n" + + " \"name\" varchar(255) not null\n" + + ");"); + } + +} \ No newline at end of file