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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.github.devcavin.gatelog.common.time

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.time.Clock
import java.time.ZoneOffset
import java.time.ZoneId

@Configuration
class TimeConfig {
class TimeConfig(
@Value($$"${gatelog.timezone}")
private val timeZone: String
) {

@Bean
fun clock(): Clock =
Clock.system(ZoneOffset.UTC)
Clock.system(ZoneId.of(timeZone))
}

This file was deleted.

20 changes: 8 additions & 12 deletions src/main/kotlin/io/github/devcavin/gatelog/common/time/TimeUtil.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package io.github.devcavin.gatelog.common.time

import org.springframework.stereotype.Component
import java.time.Clock
import java.time.LocalDate
import java.time.OffsetDateTime
import java.time.ZoneOffset

object TimeUtil {
@Component
class TimeUtil(private val clock: Clock) {

fun timeNow(): OffsetDateTime =
OffsetDateTime.now(ZoneOffset.UTC)
OffsetDateTime.now(clock)

fun startOfToday(): OffsetDateTime =
timeNow()
.toLocalDate()
.atStartOfDay()
.atOffset(ZoneOffset.UTC)
LocalDate.now(clock).atStartOfDay(clock.zone).toOffsetDateTime()

fun startOfTomorrow(): OffsetDateTime =
startOfToday().plusDays(1)

fun endOfToday(): OffsetDateTime =
startOfTomorrow()
fun endOfToday(): OffsetDateTime = startOfToday().plusDays(1)

fun isOvernight(
checkInTime: OffsetDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import io.github.devcavin.gatelog.dashboard.dto.DashboardFeed
import io.github.devcavin.gatelog.dashboard.dto.DashboardSummary
import io.github.devcavin.gatelog.users.User
import io.github.devcavin.gatelog.visitors.VisitRepository
import io.github.devcavin.gatelog.visitors.VisitResponseMapper
import io.github.devcavin.gatelog.visitors.VisitStatusRepository
import io.github.devcavin.gatelog.visitors.dto.toResponse
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -17,7 +17,9 @@ import org.springframework.transaction.annotation.Transactional
class DashboardService(
private val visitRepository: VisitRepository,
private val visitStatusRepository: VisitStatusRepository,
private val authorizationService: AuthorizationService
private val authorizationService: AuthorizationService,
private val timeUtil: TimeUtil,
private val visitResponseMapper: VisitResponseMapper
) {

@Transactional(readOnly = true)
Expand All @@ -26,8 +28,8 @@ class DashboardService(
val scope = authorizationService.scopeFor(requestedBy)
val siteId = scope.siteIdOrNull

val startOfToday = TimeUtil.startOfToday()
val endOfTheDay = TimeUtil.endOfToday()
val startOfToday = timeUtil.startOfToday()
val endOfTheDay = timeUtil.endOfToday()

val checkedInStatus =
visitStatusRepository.findByName("CHECKED_IN")
Expand Down Expand Up @@ -118,12 +120,13 @@ class DashboardService(
checkedInToday = checkedInToday,
checkedOutToday = checkedOutToday,
overdueCount = overdueCount,
overnightCount = overnightCount
overnightCount = overnightCount,
asOf = timeUtil.timeNow()
),
activeVisitors = activeVisitors.map { it.toResponse() },
overdueVisitors = overdueVisitors.map { it.toResponse() },
overnightVisitors = overnightVisitors.map { it.toResponse() },
recentlyCheckedOut = recentlyCheckedOut.map { it.toResponse() }
activeVisitors = activeVisitors.map(visitResponseMapper::toResponse),
overdueVisitors = overdueVisitors.map(visitResponseMapper::toResponse),
overnightVisitors = overnightVisitors.map(visitResponseMapper::toResponse),
recentlyCheckedOut = recentlyCheckedOut.map(visitResponseMapper::toResponse),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.devcavin.gatelog.dashboard.dto

import io.github.devcavin.gatelog.common.time.TimeUtil
import io.github.devcavin.gatelog.visitors.dto.VisitResponse
import java.time.OffsetDateTime

Expand All @@ -10,7 +9,7 @@ data class DashboardSummary(
val checkedOutToday: Long,
val overdueCount: Long,
val overnightCount: Long,
val asOf: OffsetDateTime = TimeUtil.timeNow()
val asOf: OffsetDateTime
)

data class DashboardFeed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import java.time.format.DateTimeFormatter
@Service
class ReportService(
private val visitRepository: VisitRepository,
private val authorizationService: AuthorizationService
private val authorizationService: AuthorizationService,
private val timeUtil: TimeUtil
) {

private val formatter = DateTimeFormatter.ofPattern(
Expand Down Expand Up @@ -82,7 +83,7 @@ class ReportService(
?.let(formatter::format)
?: "",
durationMinutes(visit),
TimeUtil.isOvernight(visit.checkInTime).toString()
timeUtil.isOvernight(visit.checkInTime).toString()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class OverdueVisitJob(
private val visitRepository: VisitRepository,
private val visitorStatusRepository: VisitStatusRepository,
private val siteRepository: SiteRepository,
private val timeUtil: TimeUtil,

@Value($$"${gatelog.scheduler.overdue-threshold-hours:2}")
private val overdueThresholdHours: Long
Expand All @@ -35,7 +36,7 @@ class OverdueVisitJob(
)

val threshold =
TimeUtil.timeNow().minusHours(overdueThresholdHours)
timeUtil.timeNow().minusHours(overdueThresholdHours)

val sites = siteRepository.findAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Visit(
var purpose: String = "General Visit",

@Column(name = "check_in_time", nullable = false, updatable = false)
var checkInTime: OffsetDateTime = TimeUtil.timeNow(),
var checkInTime: OffsetDateTime,

@Column(name = "check_out_time")
var checkOutTime: OffsetDateTime? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.devcavin.gatelog.visitors

import io.github.devcavin.gatelog.common.time.TimeUtil
import io.github.devcavin.gatelog.visitors.dto.VisitResponse
import io.github.devcavin.gatelog.visitors.dto.VisitorProfileSummary
import org.springframework.stereotype.Component

@Component
class VisitResponseMapper(
private val timeUtil: TimeUtil
) {

fun toResponse(visit: Visit): VisitResponse {
val profile = visit.visitorProfile

return VisitResponse(
id = requireNotNull(visit.id),
profile = VisitorProfileSummary(
id = requireNotNull(profile.id),
name = profile.name,
phoneNumber = profile.phoneNumber
),
visitorType = visit.visitorType,
purpose = visit.purpose,
status = visit.visitStatus.name,
siteId = requireNotNull(visit.site.id),
zoneId = visit.zone?.id,
zoneName = visit.zone?.name,
createdById = requireNotNull(visit.createdBy.id),
createdByName = visit.createdBy.name,
checkInTime = visit.checkInTime,
checkOutTime = visit.checkOutTime,
overnight = visit.checkOutTime == null && timeUtil.isOvernight(visit.checkInTime)
)
}
}
33 changes: 17 additions & 16 deletions src/main/kotlin/io/github/devcavin/gatelog/visitors/VisitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.github.devcavin.gatelog.auth.AuthorizationService
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.TimeProvider
import io.github.devcavin.gatelog.common.time.TimeUtil
import io.github.devcavin.gatelog.users.User
import io.github.devcavin.gatelog.visitors.dto.*
import io.github.devcavin.gatelog.zones.ZoneRepository
Expand All @@ -24,7 +24,8 @@ class VisitService(
private val zoneRepository: ZoneRepository,
private val visitorProfileRepository: VisitorProfileRepository,
private val authorizationService: AuthorizationService,
private val timeProvider: TimeProvider
private val timeUtil: TimeUtil,
private val visitResponseMapper: VisitResponseMapper
) {

@Transactional
Expand Down Expand Up @@ -88,23 +89,23 @@ class VisitService(
visitStatus = checkedInStatus,
visitorType = request.visitorType,
purpose = request.purpose,
checkInTime = timeProvider.timeNow()
checkInTime = timeUtil.timeNow()
)

return visitRepository
.save(visit)
.toResponse()
val saved = visitRepository.save(visit)

return visitResponseMapper.toResponse(saved)
}

@Transactional(readOnly = true)
fun getById(
requestedBy: User,
visitId: UUID
): VisitResponse =
findAccessibleVisit(
requestedBy,
visitId
).toResponse()
): VisitResponse {
val visit = findAccessibleVisit(requestedBy, visitId)

return visitResponseMapper.toResponse(visit)
}

@Transactional(readOnly = true)
fun search(
Expand All @@ -122,7 +123,7 @@ class VisitService(
),
pageable
)
.map { it.toResponse() }
.map(visitResponseMapper::toResponse)
}

@Transactional
Expand Down Expand Up @@ -153,11 +154,11 @@ class VisitService(
)

visit.visitStatus = checkedOutStatus
visit.checkOutTime = timeProvider.timeNow()
visit.checkOutTime = timeUtil.timeNow()

return visitRepository
.save(visit)
.toResponse()
val checkedOut = visitRepository.save(visit)

return visitResponseMapper.toResponse(checkedOut)
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.github.devcavin.gatelog.visitors.dto

import io.github.devcavin.gatelog.common.time.TimeUtil
import io.github.devcavin.gatelog.visitors.Visit
import io.github.devcavin.gatelog.visitors.VisitorProfile
import java.time.OffsetDateTime
import java.util.UUID
import java.util.*

data class VisitorProfileSummary(
val id: UUID,
Expand Down Expand Up @@ -52,31 +51,6 @@ data class VisitSummary(
val checkOutTime: OffsetDateTime?
)

fun Visit.toResponse(): VisitResponse {
val profile = visitorProfile

return VisitResponse(
id = requireNotNull(id),
profile = VisitorProfileSummary(
id = requireNotNull(profile.id),
name = profile.name,
phoneNumber = profile.phoneNumber
),
visitorType = visitorType,
purpose = purpose,
status = visitStatus.name,
siteId = requireNotNull(site.id),
zoneId = zone?.id,
zoneName = zone?.name,
createdById = requireNotNull(createdBy.id),
createdByName = createdBy.name,
checkInTime = checkInTime,
checkOutTime = checkOutTime,
overnight = checkOutTime == null &&
TimeUtil.isOvernight(checkInTime)
)
}

fun VisitorProfile.toResponse(
visitCount: Long
): VisitorProfileResponse =
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ gatelog:

scheduler:
overdue-threshold-hours: 2
overdue-job-rate-ms: 900000
overdue-job-rate-ms: 900000

timezone: ${GATELOG_TIMEZONE}
Loading