-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The API that creates the schedule must know about the user time zone.…
… Because for the same user in different time zones the schedule is different.
- Loading branch information
Showing
14 changed files
with
93 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package org.motivepick.domain.model | ||
|
||
import java.time.LocalDateTime | ||
import java.time.ZonedDateTime | ||
|
||
data class Schedule(val week: Map<LocalDateTime, List<Task>>, val overdue: List<Task>, val future: List<Task>) | ||
data class Schedule(val week: Map<ZonedDateTime, List<ScheduledTask>>, val overdue: List<ScheduledTask>, val future: List<ScheduledTask>) |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/org/motivepick/domain/model/ScheduledTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.motivepick.domain.model | ||
|
||
import org.motivepick.domain.entity.TaskEntity | ||
import java.time.LocalDateTime | ||
|
||
data class ScheduledTask(val id: Long, val name: String, val description: String, val dueDate: LocalDateTime, val closed: Boolean) { | ||
|
||
companion object { | ||
fun from(entity: TaskEntity): ScheduledTask = ScheduledTask(entity.id, entity.name, entity.description ?: "", entity.dueDate!!, entity.closed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package org.motivepick.domain.view | ||
|
||
import java.time.OffsetDateTime | ||
import java.time.ZonedDateTime | ||
|
||
data class ScheduleView(val week: Map<OffsetDateTime, List<TaskView>>, val overdue: List<TaskView>, val future: List<TaskView>) | ||
data class ScheduleView(val week: Map<ZonedDateTime, List<ScheduledTaskView>>, val overdue: List<ScheduledTaskView>, val future: List<ScheduledTaskView>) |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/org/motivepick/domain/view/ScheduledTaskView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.motivepick.domain.view | ||
|
||
import java.time.OffsetDateTime | ||
|
||
data class ScheduledTaskView(val id: Long, val name: String, val description: String, val dueDate: OffsetDateTime, val closed: Boolean) |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/org/motivepick/extensions/ClockExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.motivepick.extensions | ||
|
||
import java.time.Clock | ||
import java.time.ZonedDateTime | ||
import java.time.temporal.ChronoUnit.DAYS | ||
|
||
object ClockExtensions { | ||
|
||
fun Clock.endOfToday(): ZonedDateTime = ZonedDateTime.now(this).truncatedTo(DAYS).plusDays(1).minusNanos(1) | ||
} |
12 changes: 0 additions & 12 deletions
12
src/main/kotlin/org/motivepick/extensions/LocalDateTimeExtensions.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/org/motivepick/extensions/ScheduledTaskExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.motivepick.extensions | ||
|
||
import org.motivepick.domain.model.ScheduledTask | ||
import org.motivepick.domain.view.ScheduledTaskView | ||
import java.time.ZoneOffset | ||
|
||
object ScheduledTaskExtensions { | ||
|
||
fun ScheduledTask.view(): ScheduledTaskView = | ||
ScheduledTaskView(this.id, this.name, this.description, this.dueDate.atOffset(ZoneOffset.UTC), this.closed) | ||
} |
3 changes: 0 additions & 3 deletions
3
src/main/kotlin/org/motivepick/extensions/TaskEntityExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
package org.motivepick.extensions | ||
|
||
import org.motivepick.domain.entity.TaskEntity | ||
import org.motivepick.domain.model.Task | ||
import org.motivepick.domain.view.TaskView | ||
import java.time.ZoneOffset | ||
|
||
internal object TaskEntityExtensions { | ||
|
||
fun TaskEntity.model(): Task = Task(this.id, this.name, this.description ?: "", this.dueDate, this.closed) | ||
|
||
fun TaskEntity.view(): TaskView = TaskView(this.id, this.name, this.description ?: "", this.dueDate?.atOffset(ZoneOffset.UTC), this.closed) | ||
} |
54 changes: 21 additions & 33 deletions
54
src/main/kotlin/org/motivepick/service/ScheduleFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,35 @@ | ||
package org.motivepick.service | ||
|
||
import org.motivepick.domain.model.Schedule | ||
import org.motivepick.domain.model.Task | ||
import org.motivepick.extensions.LocalDateTimeExtensions.isSameDayAs | ||
import org.motivepick.domain.model.ScheduledTask | ||
import org.motivepick.extensions.ClockExtensions.endOfToday | ||
import org.springframework.stereotype.Component | ||
import java.time.Clock | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime.MAX | ||
import kotlin.collections.set | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
import java.time.temporal.ChronoUnit.DAYS | ||
|
||
@Component | ||
class ScheduleFactory(private val clock: Clock) { | ||
class ScheduleFactory { | ||
|
||
fun scheduleFor(tasksWithDueDate: List<Task>): Schedule { | ||
val week: MutableMap<LocalDateTime, List<Task>> = week() | ||
for (dayOfWeek in week.keys) { | ||
val tasksOfTheDay = tasksWithDueDate.filter { dayOfWeek.isSameDayAs(it.dueDate!!) } | ||
week[dayOfWeek] = tasksOfTheDay | ||
} | ||
val startOfToday = LocalDate.now(clock).atStartOfDay() | ||
val overdue = tasksWithDueDate.filter { it.dueDate!!.isBefore(startOfToday) } | ||
|
||
val startOfFuture = startOfToday.plusDays(7) | ||
|
||
return tasksWithDueDate.asSequence() | ||
.filter { it.dueDate!!.isAfter(startOfFuture) } | ||
.sortedBy { it.dueDate } | ||
.firstOrNull() | ||
?.let { task -> tasksWithDueDate.filter { task.dueDate!!.isSameDayAs(it.dueDate!!) } } | ||
?.let { Schedule(week, overdue, it) } | ||
?: Schedule(week, overdue, listOf()) | ||
fun scheduleFor(tasksWithDueDate: List<ScheduledTask>, timeZone: ZoneId): Schedule { | ||
val endOfToday = Clock.system(timeZone).endOfToday() | ||
val week: Map<ZonedDateTime, List<ScheduledTask>> = weekDays(endOfToday) | ||
.associateWith { endOfDay -> tasksWithDueDate.filter { it.dueDate.atZone(timeZone) in startOfDayFrom(endOfDay)..endOfDay } } | ||
val startOfToday = startOfDayFrom(endOfToday) | ||
val overdue = tasksWithDueDate.filter { it.dueDate.atZone(timeZone).isBefore(startOfToday) } | ||
val endOfWeek = endOfToday.plusDays(6) | ||
val futureTasks = tasksWithDueDate.filter { it.dueDate.atZone(timeZone).isAfter(endOfWeek) }.sortedBy { it.dueDate } | ||
return Schedule(week, overdue, futureTasks) | ||
} | ||
|
||
private fun week(): MutableMap<LocalDateTime, List<Task>> = | ||
LocalDate | ||
.now(clock) | ||
.atTime(MAX) | ||
.let { endOfToday -> | ||
private fun weekDays(endOfToday: ZonedDateTime): List<ZonedDateTime> = | ||
endOfToday | ||
.let { value -> | ||
(0..6) | ||
.map { it.toLong() } | ||
.map { endOfToday.plusDays(it) } | ||
.associateBy({ it }, { ArrayList<Task>() }) | ||
.toMutableMap() | ||
.map { value.plusDays(it) } | ||
} | ||
|
||
private fun startOfDayFrom(dateTime: ZonedDateTime): ZonedDateTime = dateTime.truncatedTo(DAYS) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/kotlin/org/motivepick/extensions/ClockExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.motivepick.extensions | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.motivepick.extensions.ClockExtensions.endOfToday | ||
import java.time.Clock | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
|
||
class ClockExtensionsTest { | ||
|
||
@Test | ||
fun `endOfToday should return the end of the day in the given zone`() { | ||
val clock = Clock.fixed(Instant.parse("2024-08-25T11:52:00.00Z"), ZoneId.of("GMT+3")) | ||
assertThat(clock.endOfToday()).isEqualTo(ZonedDateTime.parse("2024-08-25T23:59:59.999999999+03:00")) | ||
} | ||
} |