Skip to content

Commit

Permalink
2557 [BE] delete autotest contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrsam committed Jul 25, 2024
1 parent b18f286 commit 4e002a5
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/com/epam/brn/controller/UserDetailController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ class UserDetailController(
@Operation(summary = "Delete doctor from patient")
fun deleteDoctorFromPatient(@PathVariable patientId: Long) =
doctorService.deleteDoctorFromPatientAsPatient(patientId)

@DeleteMapping("/del")
@Operation(summary = "Delete all auto test users")
@ResponseStatus(HttpStatus.OK)
fun deleteAutoTestUsers() = ResponseEntity.ok()
.body(userAccountService.deleteAutoTestUsers())

@DeleteMapping("/del/{email}")
@Operation(summary = "Delete all auto test users")
@ResponseStatus(HttpStatus.OK)
fun deleteAutoTestUserByEmail(@PathVariable("email") email: String) = ResponseEntity.ok()
.body(userAccountService.deleteAutoTestUserByEmail(email))
}
8 changes: 8 additions & 0 deletions src/main/kotlin/com/epam/brn/dto/UserDeletionDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.epam.brn.dto

/**
* UserDeletionDto.
*
* @author Andrey Samoylov
*/
data class UserDeletionDto(val count: Long)
4 changes: 2 additions & 2 deletions src/main/kotlin/com/epam/brn/model/StudyHistory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class StudyHistory(
val id: Long? = null,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JoinColumn(name = "user_id", insertable = true, updatable = false)
var userAccount: UserAccount,

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -44,7 +44,7 @@ data class StudyHistory(

) {
override fun toString() =
"StudyHistory(id=$id, userAccount=$userAccount, exercise=$exercise, startTime=$startTime, endTime=$endTime, spentTime=$spentTimeInSeconds, tasksCount=$tasksCount, wrongAnswers=$wrongAnswers)"
"StudyHistory(id=$id, userAccount=$userAccount, startTime=$startTime, endTime=$endTime, spentTime=$spentTimeInSeconds, tasksCount=$tasksCount, wrongAnswers=$wrongAnswers)"

fun toDto() = StudyHistoryDto(
id = this.id,
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/epam/brn/repo/UserAccountRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ interface UserAccountRepository : JpaRepository<UserAccount, Long> {
where u.email = :email"""
)
fun updateLastVisitByEmail(email: String, lastVisit: LocalDateTime)

fun countByEmailStartsWith(prefix: String): Long

fun countByEmailIs(email: String): Long

@Transactional
fun deleteUserAccountsByEmailStartsWith(prefix: String)

@Transactional
fun deleteUserAccountByEmailIs(email: String)
}
5 changes: 4 additions & 1 deletion src/main/kotlin/com/epam/brn/service/UserAccountService.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.epam.brn.service

import com.epam.brn.dto.HeadphonesDto
import com.epam.brn.dto.request.UserAccountChangeRequest
import com.epam.brn.dto.UserAccountDto
import com.epam.brn.dto.UserDeletionDto
import com.epam.brn.dto.request.UserAccountChangeRequest
import com.epam.brn.model.UserAccount
import com.google.firebase.auth.UserRecord
import org.springframework.data.domain.Pageable
Expand Down Expand Up @@ -36,4 +37,6 @@ interface UserAccountService {
fun getPatientsForDoctor(doctorId: Long): List<UserAccountDto>

fun markVisitForCurrentUser()
fun deleteAutoTestUsers(): UserDeletionDto
fun deleteAutoTestUserByEmail(email: String): UserDeletionDto
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.epam.brn.service.impl

import com.epam.brn.dto.HeadphonesDto
import com.epam.brn.dto.UserAccountDto
import com.epam.brn.dto.UserDeletionDto
import com.epam.brn.dto.request.UserAccountChangeRequest
import com.epam.brn.enums.BrnRole
import com.epam.brn.exception.EntityNotFoundException
Expand All @@ -14,6 +15,7 @@ import com.epam.brn.service.RoleService
import com.epam.brn.service.TimeService
import com.epam.brn.service.UserAccountService
import com.google.firebase.auth.UserRecord
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.domain.Pageable
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder
Expand All @@ -26,8 +28,10 @@ class UserAccountServiceImpl(
private val userAccountRepository: UserAccountRepository,
private val roleService: RoleService,
private val headphonesService: HeadphonesService,
private val timeService: TimeService
private val timeService: TimeService,
) : UserAccountService {
@Value("\${users.delete.prefix}")
private lateinit var prefix: String

override fun findUserByEmail(email: String): UserAccountDto =
userAccountRepository.findUserAccountByEmail(email)
Expand Down Expand Up @@ -172,4 +176,16 @@ class UserAccountServiceImpl(

private fun getDefaultRoleSet(): MutableSet<Role> =
setOf(BrnRole.USER).mapTo(mutableSetOf()) { roleService.findByName(it) }

override fun deleteAutoTestUsers(): UserDeletionDto {
val usersCountBefore = userAccountRepository.countByEmailStartsWith(prefix)
userAccountRepository.deleteUserAccountsByEmailStartsWith(prefix)
return UserDeletionDto(usersCountBefore)
}

override fun deleteAutoTestUserByEmail(email: String): UserDeletionDto {
val count = userAccountRepository.countByEmailIs(email)
userAccountRepository.deleteUserAccountByEmailIs(email)
return UserDeletionDto(count)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ class InitialDataLoader(
val autoTestUser = createUser("autoTestUser", AUTO_TEST_USER_EMAIL, setOf(userRole))
val autoTestSpecialist =
createUser("autoTestSpecialist", AUTO_TEST_SPECIALIST_EMAIL, setOf(userRole, specialistRole))
val listOfUsers = listOf(admin, firstUser, secondUser, autoTestUser, autoTestSpecialist)

val listOfUsers =
listOf(admin, firstUser, secondUser, autoTestUser, autoTestSpecialist)
userAccountRepository.saveAll(listOfUsers)
} else {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ github.contributors.sync.repository-names=brn, mobile, mobile-ios, mobile-androi
github.contributors.default-page-size=30
github.contributors.bot-logins=dependabot-preview[bot],snyk-bot,dependabot[bot]
github.api.type-token=${API_GITHUB_TYPE_TOKEN:token}
github.api.token=${API_GITHUB_TOKEN}
github.api.token=
github.api.codec-max-size=16777216
github.api.logging-enabled=false
github.api.connection-timeout=15000
Expand All @@ -152,6 +152,8 @@ github.api.url.base=https://api.github.com
github.api.url.path.contributors=/repos/{OWNER}/{REPO}/contributors
github.api.url.path.users=/users/{username}

users.delete.prefix=autotest

# Swagger
springdoc.swagger-ui.tagsSorter=alpha
springdoc.writer-with-order-by-keys=true
127 changes: 126 additions & 1 deletion src/test/kotlin/com/epam/brn/integration/StudyHistoryIT.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.epam.brn.integration

import com.epam.brn.enums.BrnGender
import com.epam.brn.enums.BrnRole
import com.epam.brn.model.Exercise
import com.epam.brn.model.ExerciseGroup
import com.epam.brn.enums.BrnGender
import com.epam.brn.model.Series
import com.epam.brn.model.StudyHistory
import com.epam.brn.model.SubGroup
Expand All @@ -14,6 +14,7 @@ import com.epam.brn.repo.SeriesRepository
import com.epam.brn.repo.StudyHistoryRepository
import com.epam.brn.repo.SubGroupRepository
import com.epam.brn.repo.UserAccountRepository
import com.epam.brn.service.UserAccountService
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -54,6 +55,12 @@ class StudyHistoryIT : BaseIT() {
@Autowired
lateinit var exerciseGroupRepository: ExerciseGroupRepository

// @Autowired
// lateinit var roleRepository: RoleRepository

@Autowired
lateinit var userAccountService: UserAccountService

@AfterEach
fun deleteAfterTest() {
studyHistoryRepository.deleteAll()
Expand All @@ -62,6 +69,7 @@ class StudyHistoryIT : BaseIT() {
seriesRepository.deleteAll()
exerciseGroupRepository.deleteAll()
userAccountRepository.deleteAll()
// roleRepository.deleteAll()
}

@Test
Expand Down Expand Up @@ -226,6 +234,115 @@ class StudyHistoryIT : BaseIT() {
assertEquals(0, result)
}

// @Test
// fun `test delete study history when delete autotest users`() {
// // GIVEN
// val roleUser = insertRole(BrnRole.USER)
//
// val user1 = UserAccount(
// fullName = "autotest_n1",
// email = "autotest_n@1704819771.8820736.com",
// gender = BrnGender.MALE.toString(),
// bornYear = 2000,
// active = true,
// )
// user1.roleSet = mutableSetOf(roleUser)
//
// val user2 = UserAccount(
// fullName = "autotest_n1",
// email = "autotest_n@170472339.1784415.com",
// gender = BrnGender.MALE.toString(),
// bornYear = 2000,
// active = true,
// )
// user2.roleSet = mutableSetOf(roleUser)
//
// userAccountRepository.saveAll(listOf(user1, user2))
//
// val existingSeries = insertSeries()
// val subGroup = insertSubGroup(existingSeries)
// val existingExerciseFirst = insertExercise("FirstName", subGroup)
// val existingExerciseSecond = insertExercise("SecondName", subGroup)
// val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
// val historyFirstExerciseOne = insertStudyHistory(user1, existingExerciseFirst, now)
// val historySecondExerciseOne = insertStudyHistory(user2, existingExerciseSecond, now)
//
// studyHistoryRepository
// .saveAll(
// listOf(
// historyFirstExerciseOne,
// historySecondExerciseOne,
// )
// )
//
// // WHEN
// userAccountService.deleteAutoTestUsers()
//
// val result1 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// val result2 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// // THEN
// assertEquals(0, result1?.size)
// assertEquals(0, result2?.size)
// }
//
// @Test
// fun `test delete study history when delete single autotest user`() {
// // GIVEN
// val roleUser = insertRole(BrnRole.USER)
//
// val user1 = UserAccount(
// fullName = "autotest_n1",
// email = "autotest_n@1704819771.8820736.com",
// gender = BrnGender.MALE.toString(),
// bornYear = 2000,
// active = true,
// )
// user1.roleSet = mutableSetOf(roleUser)
// userAccountRepository.save(user1)
//
// val exerciseFirstName = "FirstName"
// val existingSeries = insertSeries()
// val subGroup = insertSubGroup(existingSeries)
// val existingExerciseFirst = insertExercise(exerciseFirstName, subGroup)
// val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
// val historyFirstExerciseTwo = insertStudyHistory(user1, existingExerciseFirst, now)
//
// studyHistoryRepository.save(historyFirstExerciseTwo)
//
// // WHEN
// userAccountService.deleteAutoTestUsers()
//
// val result1 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// val result2 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// // THEN
// assertEquals(0, result1?.size)
// assertEquals(0, result2?.size)
// }

private fun insertStudyHistory(
existingUser: UserAccount,
existingExercise: Exercise,
Expand Down Expand Up @@ -289,4 +406,12 @@ class StudyHistoryIT : BaseIT() {
)
)
}

// private fun insertRole(roleName: String): Role {
// return roleRepository.save(
// Role(
// name = roleName
// )
// )
// }
}
Loading

0 comments on commit 4e002a5

Please sign in to comment.