Skip to content
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
31 changes: 19 additions & 12 deletions src/main/kotlin/io/github/devcavin/gatelog/visitors/VisitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -25,36 +27,37 @@ 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
fun register(
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
)

val profile =
visitorProfileRepository.findBySiteIdAndPhoneNumber(
siteId,
targetSite.id!!,
request.phone
) ?: visitorProfileRepository.save(
VisitorProfile(
name = request.name,
phoneNumber = request.phone,
site = site
site = targetSite
)
)

Expand All @@ -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 =
Expand All @@ -83,7 +90,7 @@ class VisitService(

val visit = Visit(
visitorProfile = profile,
site = site,
site = targetSite,
zone = zone,
createdBy = requestedBy,
visitStatus = checkedInStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ data class RegisterVisitRequest(

val purpose: String = "General visit",

val siteId: UUID,

val zoneId: UUID
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading