Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ob 10540 fix consultant deletion problem #122

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
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;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

@Repository
@Slf4j
public class ScheduleRepository {

private final @NotNull JdbcTemplate jdbcTemplate;
Expand All @@ -29,12 +44,40 @@ public ScheduleRepository(@Qualifier("dbTemplate") NamedParameterJdbcTemplate db
this.jdbcTemplate = jdbcTemplate;
}

public List<Integer> deleteUserSchedules(Long calcomUserId) {
public Set<Integer> 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<String> getTableNames(JdbcTemplate jdbcTemplate) throws SQLException {
List<String> 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<Integer> getScheduleIdsByUserId(NamedParameterJdbcTemplate jdbcTemplate, Long userId) {
Set<Integer> scheduleIds = Sets.newHashSet();
Map<String, Object> params = new HashMap<>();
params.put("userId", userId);
String sql = "SELECT \"id\" FROM \"Schedule\" WHERE \"userId\" = :userId";
try {
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, params);
for (Map<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -105,7 +105,7 @@ public void deleteConsultantHandler(String consultantId) {
// Delete personal event-types
calComEventTypeService.deleteAllEventTypesOfUser(calcomUserId);
// Delete schedules
List<Integer> deletedSchedules = scheduleRepository.deleteUserSchedules(calcomUserId);
Set<Integer> deletedSchedules = scheduleRepository.deleteUserSchedules(calcomUserId);
// Delete availabilities for schedules
for (Integer scheduleId : deletedSchedules) {
availabilityRepository.deleteAvailabilityByScheduleId(Long.valueOf(scheduleId));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-testing.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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"
+ ");");
}

}
Loading