From d45ae252f10ddfd18613d0393fbd99ab4df00d7e Mon Sep 17 00:00:00 2001 From: Cavin Date: Wed, 2 Sep 2026 23:27:57 +0300 Subject: [PATCH] chore(visit): resolve target site for visit creation --- .../devcavin/gatelog/visitors/VisitService.kt | 31 ++++++++++++------- .../gatelog/visitors/dto/VisitorRequests.kt | 2 ++ .../gatelog/visitors/dto/VisitorResponses.kt | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/io/github/devcavin/gatelog/visitors/VisitService.kt b/src/main/kotlin/io/github/devcavin/gatelog/visitors/VisitService.kt index 61ea216..5876994 100644 --- a/src/main/kotlin/io/github/devcavin/gatelog/visitors/VisitService.kt +++ b/src/main/kotlin/io/github/devcavin/gatelog/visitors/VisitService.kt @@ -5,6 +5,7 @@ import io.github.devcavin.gatelog.common.exception.ConflictException import io.github.devcavin.gatelog.common.exception.InvalidStateException import io.github.devcavin.gatelog.common.exception.ResourceNotFoundException import io.github.devcavin.gatelog.common.time.TimeUtil +import io.github.devcavin.gatelog.sites.SiteRepository import io.github.devcavin.gatelog.users.User import io.github.devcavin.gatelog.visitors.dto.* import io.github.devcavin.gatelog.zones.ZoneRepository @@ -16,6 +17,7 @@ import java.util.UUID private const val CHECKED_IN = "CHECKED_IN" private const val CHECKED_OUT = "CHECKED_OUT" +private const val OVERDUE = "OVERDUE" @Service class VisitService( @@ -25,7 +27,8 @@ class VisitService( private val visitorProfileRepository: VisitorProfileRepository, private val authorizationService: AuthorizationService, private val timeUtil: TimeUtil, - private val visitResponseMapper: VisitResponseMapper + private val visitResponseMapper: VisitResponseMapper, + private val siteRepository: SiteRepository ) { @Transactional @@ -33,14 +36,14 @@ class VisitService( requestedBy: User, request: RegisterVisitRequest ): VisitResponse { - val site = requestedBy.site - val siteId = requireNotNull(site.id) { - "Authenticated user has no site" - } + authorizationService.assertCovers(requestedBy, request.siteId) + + val targetSite = siteRepository.findById(request.siteId) + .orElseThrow { ResourceNotFoundException("Site", request.siteId) } val zone = zoneRepository.findByIdAndSiteId( request.zoneId, - siteId + targetSite.id!! ) ?: throw ResourceNotFoundException( "Zone", request.zoneId @@ -48,13 +51,13 @@ class VisitService( val profile = visitorProfileRepository.findBySiteIdAndPhoneNumber( - siteId, + targetSite.id!!, request.phone ) ?: visitorProfileRepository.save( VisitorProfile( name = request.name, phoneNumber = request.phone, - site = site + site = targetSite ) ) @@ -68,10 +71,14 @@ class VisitService( CHECKED_IN ) + val overdueVisit = visitRepository.findFirstByVisitorProfileIdAndVisitStatusName(profileId, OVERDUE) + if (checkedInVisit != null) { - throw ConflictException( - "Visitor is already checked in" - ) + throw ConflictException("Visitor already has an active visit with status CHECKED_IN") + } + + if (overdueVisit != null) { + throw ConflictException("Visitor already has an active visit with status OVERDUE") } val checkedInStatus = @@ -83,7 +90,7 @@ class VisitService( val visit = Visit( visitorProfile = profile, - site = site, + site = targetSite, zone = zone, createdBy = requestedBy, visitStatus = checkedInStatus, diff --git a/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorRequests.kt b/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorRequests.kt index 22f5817..5ce29b0 100644 --- a/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorRequests.kt +++ b/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorRequests.kt @@ -20,6 +20,8 @@ data class RegisterVisitRequest( val purpose: String = "General visit", + val siteId: UUID, + val zoneId: UUID ) diff --git a/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorResponses.kt b/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorResponses.kt index 67f6916..2ec1b00 100644 --- a/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorResponses.kt +++ b/src/main/kotlin/io/github/devcavin/gatelog/visitors/dto/VisitorResponses.kt @@ -18,6 +18,7 @@ data class VisitResponse( val purpose: String, val status: String, val siteId: UUID, + val siteName: String, val zoneId: UUID?, val zoneName: String?, val createdById: UUID,