Skip to content

Commit

Permalink
Merge branch 'develop' into feat/member-join
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu committed Mar 22, 2024
2 parents 4f0826e + 4e33e5b commit 84e8e79
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ enum class AgeCondition(
AGE40TO49("만 40-49세", 0b001000),
AGE50TO59("만 50-59세", 0b000100),
AGE60TO64("만 60-64세", 0b000010),
AGEOVER65("만 65세 이상", 0b000001),
;

fun isMatching(value: Int): Boolean {
return value and this.value == this.value
}
AGEOVER65("만 65세 이상", 0b000001);

companion object {
fun getConditions(value: Int): List<AgeCondition> {
return entries.filter { it.isMatching(value) }
fun getAgeCondition(value: Int): AgeCondition {
return when {
value in 19..29 -> AGE19TO29
value in 30..39 -> AGE30TO39
value in 40..49 -> AGE40TO49
value in 50..59 -> AGE50TO59
value in 60..64 -> AGE60TO64
value >= 65 -> AGEOVER65
else -> AGE19TO29
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class CustomRequestMatcher {

fun userEndpoints(): RequestMatcher {
return OrRequestMatcher(
AntPathRequestMatcher("/api/v1/inoculation/**")
AntPathRequestMatcher("/api/v1/inoculation/**"),
AntPathRequestMatcher("/api/v1/search/**"),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ interface InoculationRepository : JpaRepository<Inoculation, UUID> {
"and i.vaccination.vaccinationType = :vaccinationType")
fun findInoculationsByMemberIdAndVaccinationType(memberId: UUID, vaccinationType: VaccinationType): List<Inoculation>

@Query("select distinct i.vaccination.diseaseName " +
"from Inoculation i " +
"where i.member.id = :memberId")
fun findDistinctDiseaseNameByMemberId(memberId: UUID): List<String>

@Query("select i " +
"from Inoculation i " +
"where i.member.id = :memberId and i.vaccination.diseaseName = :diseaseName " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import com.vacgom.backend.disease.application.DiseaseService
import com.vacgom.backend.disease.domain.Disease
import com.vacgom.backend.disease.domain.constants.AgeCondition
import com.vacgom.backend.disease.domain.constants.HealthCondition
import com.vacgom.backend.global.exception.error.BusinessException
import com.vacgom.backend.inoculation.domain.Vaccination
import com.vacgom.backend.inoculation.domain.constants.VaccinationType
import com.vacgom.backend.inoculation.infrastructure.persistence.InoculationRepository
import com.vacgom.backend.inoculation.infrastructure.persistence.VaccinationRepository
import com.vacgom.backend.member.exception.MemberError
import com.vacgom.backend.member.infrastructure.persistence.MemberRepository
import com.vacgom.backend.search.application.dto.DiseaseSearchResponse
import com.vacgom.backend.search.application.dto.VaccinationSearchResponse
import org.springframework.stereotype.Service
import java.time.LocalDate
import java.util.*

@Service
class SearchService(
val vaccinationRepository: VaccinationRepository,
val inoculationRepository: InoculationRepository,
val memberRepository: MemberRepository,
val diseaseService: DiseaseService,
) {
private fun findAllVaccinations(): List<Vaccination> {
Expand Down Expand Up @@ -45,6 +53,36 @@ class SearchService(
}.map { VaccinationSearchResponse.of(it) }
}

fun searchRecommendVaccination(memberId: UUID): List<VaccinationSearchResponse> {
val member =
memberRepository.findById(memberId).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}

val ageCondition =
AgeCondition.getAgeCondition(member.memberDetails?.birthday?.year!!.minus(LocalDate.now().year))
val vaccinations = findAllVaccinations()
val inoculatedDiseaseName =
inoculationRepository.findDistinctDiseaseNameByMemberId(memberId).flatMap { it.split("·") }.toSet()
val recommendedVaccinations =
vaccinations.filter { vaccination -> !inoculatedDiseaseName.contains(vaccination.vaccineName) }

val healthProfiles = member.healthProfiles.map { it.healthCondition }.toList()

val diseases =
this.searchDisease(listOf(ageCondition), healthProfiles).filter { response ->
!inoculatedDiseaseName.contains(response.name)
}.toList()
return filterByDisease(recommendedVaccinations, diseases)
}

private fun filterByDisease(
vaccinations: List<Vaccination>,
diseases: List<DiseaseSearchResponse>,
) = vaccinations.filter {
diseases.any { disease -> it.diseaseName.contains(disease.name) }
}.map { VaccinationSearchResponse.of(it) }

fun isMatched(
disease: Disease,
age: List<AgeCondition>,
Expand Down

0 comments on commit 84e8e79

Please sign in to comment.