From 7ebddffc1a5d4f7ced31351bc0d039c3597120f7 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 09:39:01 +0200 Subject: [PATCH 1/8] Fix, give game notes null as pupilId Signed-off-by: sindrelm --- .../backend/bootstrap/DevDataBootstrap.kt | 16 +++++---- .../notebook/api/NotebookEntryApiMappings.kt | 2 +- .../notebook/api/NotebookEntryResponses.kt | 4 +-- .../notebook/application/NotebookService.kt | 5 +-- .../backend/notebook/domain/NotebookEntry.kt | 26 +++++++++++--- ...w_null_pupil_for_game_notebook_entries.sql | 13 +++++++ .../backend/notebook/NotebookServiceTests.kt | 34 ++++++++++++++++++- 7 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 src/main/resources/db/migration/V16__allow_null_pupil_for_game_notebook_entries.sql diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt index b1a4d9c5..b650ceee 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt @@ -368,7 +368,7 @@ class DevDataBootstrap( } activeStopsBySlug["postkontoret"]?.let { stop -> seedNotebookEntry( - pupil = pupilAda, + pupil = null, classroom = classroom, stop = stop, sourceType = NotebookSourceType.GAME, @@ -389,22 +389,26 @@ class DevDataBootstrap( } private fun seedNotebookEntry( - pupil: User, + pupil: User?, classroom: Classroom, stop: MapStop, sourceType: NotebookSourceType, title: String, content: String, ) { - val pupilId = requireNotNull(pupil.id) { "Pupil seed user must have an id" } val classroomId = classroom.id val stopId = requireNotNull(stop.id) { "Seeded map stop must have an id" } - val existingEntry = notebookEntryRepository.findAllByPupilId(pupilId).firstOrNull { - it.classroom.id == classroomId && it.stop.id == stopId && it.title == title + val existingEntry = notebookEntryRepository.findAll().firstOrNull { + it.classroom.id == classroomId && + it.stop.id == stopId && + it.title == title && + it.sourceType == sourceType && + it.pupil?.id == pupil?.id } if (existingEntry != null) { existingEntry.sourceType = sourceType existingEntry.content = content + existingEntry.pupil = pupil return } notebookEntryRepository.save( @@ -423,4 +427,4 @@ class DevDataBootstrap( HexFormat.of().formatHex( MessageDigest.getInstance("SHA-256").digest(rawToken.toByteArray(StandardCharsets.UTF_8)), ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryApiMappings.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryApiMappings.kt index d3ffd859..e10ba22a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryApiMappings.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryApiMappings.kt @@ -14,7 +14,7 @@ import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookEntry fun NotebookEntry.toResponse(): NotebookEntryResponse = NotebookEntryResponse( id = requireNotNull(id) { "Notebook entry is missing an id" }, - pupilUserId = requireNotNull(pupil.id) { "Notebook entry pupil is missing an id" }, + pupilUserId = pupil?.id, classroomId = classroom.id, stopId = requireNotNull(stop.id) { "Notebook entry stop is missing an id" }, sourceType = sourceType.name, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryResponses.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryResponses.kt index 216bc290..7817a3d6 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryResponses.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryResponses.kt @@ -6,7 +6,7 @@ import java.time.Instant * Serialized notebook entry returned by notebook endpoints. * * @property id notebook entry identifier - * @property pupilUserId owner pupil user id + * @property pupilUserId owner pupil user id when the entry originates from a pupil * @property classroomId classroom id for the entry context * @property stopId map stop id for the entry context * @property sourceType serialized notebook source type @@ -17,7 +17,7 @@ import java.time.Instant */ data class NotebookEntryResponse( val id: Long, - val pupilUserId: Long, + val pupilUserId: Long?, val classroomId: Long, val stopId: Long, val sourceType: String, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 2317c94d..dcd4e76f 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -4,6 +4,7 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepositor import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookEntry +import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookSourceType import edu.ntnu.idi.idatt.backend.notebook.infrastructure.NotebookEntryRepository import org.springframework.http.HttpStatus import org.springframework.stereotype.Service @@ -57,7 +58,7 @@ class NotebookService( } val notebookEntry = NotebookEntry( - pupil = pupil, + pupil = if (command.sourceType == NotebookSourceType.PUPIL) pupil else null, classroom = classroom, stop = stop, sourceType = command.sourceType, @@ -110,7 +111,7 @@ class NotebookService( ResponseStatusException(HttpStatus.NOT_FOUND, "Notebook entry not found") } - val ownerId = requireNotNull(notebookEntry.pupil.id) { "Notebook entry pupil is missing an id" } + val ownerId = notebookEntry.pupil?.id if (ownerId != resolvedPupilId) { throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot access another pupil's notebook entry") } diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/domain/NotebookEntry.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/domain/NotebookEntry.kt index 544379a3..188b394a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/domain/NotebookEntry.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/domain/NotebookEntry.kt @@ -13,6 +13,8 @@ import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne +import jakarta.persistence.PrePersist +import jakarta.persistence.PreUpdate import jakarta.persistence.Table import org.hibernate.annotations.CreationTimestamp import org.hibernate.annotations.UpdateTimestamp @@ -22,7 +24,7 @@ import java.time.Instant * Persisted notebook entry created for a pupil within a classroom and map stop context. * * @property id Database identifier. - * @property pupil Pupil that owns the notebook entry. + * @property pupil Pupil that owns the notebook entry when the source type is [NotebookSourceType.PUPIL]. * @property classroom Classroom the notebook entry belongs to. * @property stop Map stop the notebook entry is associated with. * @property sourceType Source that produced the entry content. @@ -37,9 +39,9 @@ class NotebookEntry( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, - @ManyToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "pupil_user_id", nullable = false) - var pupil: User, + @ManyToOne(fetch = FetchType.LAZY, optional = true) + @JoinColumn(name = "pupil_user_id", nullable = true) + var pupil: User?, @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "classroom_id", nullable = false) var classroom: Classroom, @@ -59,4 +61,18 @@ class NotebookEntry( @UpdateTimestamp @Column(name = "updated_at", nullable = false) var updatedAt: Instant? = null, -) +) { + @PrePersist + @PreUpdate + fun validatePupilOwnership() { + when (sourceType) { + NotebookSourceType.GAME -> require(pupil == null) { + "Notebook entries with source type GAME must not reference a pupil" + } + + NotebookSourceType.PUPIL -> requireNotNull(pupil) { + "Notebook entries with source type PUPIL must reference a pupil" + } + } + } +} diff --git a/src/main/resources/db/migration/V16__allow_null_pupil_for_game_notebook_entries.sql b/src/main/resources/db/migration/V16__allow_null_pupil_for_game_notebook_entries.sql new file mode 100644 index 00000000..b7d25ca4 --- /dev/null +++ b/src/main/resources/db/migration/V16__allow_null_pupil_for_game_notebook_entries.sql @@ -0,0 +1,13 @@ +ALTER TABLE notebook_entries + MODIFY COLUMN pupil_user_id BIGINT NULL; + +UPDATE notebook_entries +SET pupil_user_id = NULL +WHERE source_type = 'GAME'; + +ALTER TABLE notebook_entries + ADD CONSTRAINT chk_notebook_entries_source_type_pupil + CHECK ( + (source_type = 'GAME' AND pupil_user_id IS NULL) OR + (source_type = 'PUPIL' AND pupil_user_id IS NOT NULL) + ); diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt index a10a5b49..3e0aa6d6 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt @@ -83,6 +83,38 @@ class NotebookServiceTests { assertSame(saved, result) } + @Test + fun `create notebook entry with game source persists null pupil`() { + val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) + val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) + val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) + val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val currentUser = currentUser("42") + val pupil = pupilUser(42L) + val classroom = classroom(7L) + val stop = stop(9L) + val command = CreateNotebookEntryCommand( + currentUser = currentUser, + classroomId = 7L, + stopId = 9L, + sourceType = NotebookSourceType.GAME, + title = "Auto note", + content = "Generated note", + ) + + Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(pupil) + Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) + Mockito.`when`(mapStopsRepository.findById(9L)).thenReturn(Optional.of(stop)) + Mockito.`when`(notebookEntryRepository.save(Mockito.any(NotebookEntry::class.java))) + .thenAnswer { it.arguments[0] as NotebookEntry } + + service.createNotebookEntry(command) + val captor = ArgumentCaptor.forClass(NotebookEntry::class.java) + Mockito.verify(notebookEntryRepository).save(captor.capture()) + + assertEquals(null, captor.value.pupil) + } + @Test fun `create notebook entry rejects classroom outside active classroom`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) @@ -271,7 +303,7 @@ class NotebookServiceTests { title = "Stop $id", ) - private fun notebookEntry(id: Long, pupil: User) = NotebookEntry( + private fun notebookEntry(id: Long, pupil: User?) = NotebookEntry( id = id, pupil = pupil, classroom = classroom(7L), From d0644f614812f19757f64db7f3f6f892c3158564 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 09:47:18 +0200 Subject: [PATCH 2/8] Fix, pupils cannot create or delete entries with sourceType set to GAME Signed-off-by: sindrelm --- .../backend/notebook/application/NotebookService.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index dcd4e76f..62e342fa 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -48,6 +48,10 @@ class NotebookService( val pupil = currentUserResolver.requirePupil(command.currentUser.subject) val classroom = currentUserResolver.getClassroomByUser(command.currentUser) + if (command.sourceType == NotebookSourceType.GAME) { + throw ResponseStatusException(HttpStatus.FORBIDEN, "Pupil cannot create notes with sourceType set to GAME") + } + if (classroom.id != command.classroomId) { throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot create notebook entries outside the active classroom") } @@ -94,6 +98,11 @@ class NotebookService( fun deleteNotebookEntry(command: DeleteNotebookEntryCommand) { val pupil = currentUserResolver.requirePupil(command.currentUser.subject) val notebookEntry = verifyOwnership(pupil.id, command.notebookEntryId) + + if (notebookEntry.sourceType == NotebookSourceType.GAME) { + throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot create notebook entries outside the active classroom") + } + notebookEntryRepository.delete(notebookEntry) } From f6013971c792d68535bd1ba0cd12e246faa81151 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 09:48:52 +0200 Subject: [PATCH 3/8] Refactor sourceType verification into verify ownership Signed-off-by: sindrelm --- .../idatt/backend/notebook/application/NotebookService.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 62e342fa..0f2f159b 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -99,10 +99,6 @@ class NotebookService( val pupil = currentUserResolver.requirePupil(command.currentUser.subject) val notebookEntry = verifyOwnership(pupil.id, command.notebookEntryId) - if (notebookEntry.sourceType == NotebookSourceType.GAME) { - throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot create notebook entries outside the active classroom") - } - notebookEntryRepository.delete(notebookEntry) } @@ -120,6 +116,10 @@ class NotebookService( ResponseStatusException(HttpStatus.NOT_FOUND, "Notebook entry not found") } + if (notebookEntry.sourceType == NotebookSourceType.GAME) { + throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot create notebook entries outside the active classroom") + } + val ownerId = notebookEntry.pupil?.id if (ownerId != resolvedPupilId) { throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot access another pupil's notebook entry") From d89a74928ac928faad725dce187c48338a80ff72 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 09:53:52 +0200 Subject: [PATCH 4/8] Fix, shared notebook entries can be fetched by pupils Signed-off-by: sindrelm --- .../notebook/application/NotebookService.kt | 4 +++- .../infrastructure/NotebookEntryRepository.kt | 18 +++++++++++++++--- .../backend/notebook/NotebookServiceTests.kt | 4 +++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 0f2f159b..6cecac1a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -34,7 +34,9 @@ class NotebookService( fun getAllNotebookEntries(currentUser: CurrentUser): List { val pupil = currentUserResolver.requirePupil(currentUser.subject) val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" } - return notebookEntryRepository.findAllByPupilId(pupilId) + val classroom = currentUserResolver.getClassroomByUser(currentUser) + val classroomId = requireNotNull(classroom.id) { "Active classroom is missing an id" } + return notebookEntryRepository.findAllVisibleToPupil(pupilId, classroomId) } /** diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt index 4e249031..71e07bd7 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt @@ -2,6 +2,7 @@ package edu.ntnu.idi.idatt.backend.notebook.infrastructure import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookEntry import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository /** @@ -11,10 +12,21 @@ import org.springframework.stereotype.Repository interface NotebookEntryRepository : JpaRepository { /** - * Finds all entries owned by a given pupil + * Finds all notebook entries visible to a pupil: + * - pupil-authored entries owned by the pupil + * - game-authored entries for the pupil's active classroom * * @param pupilId the id of the pupil - * @return all entries connected to the pupil + * @param classroomId the id of the active classroom + * @return all visible notebook entries */ - fun findAllByPupilId(pupilId: Long): List + @Query( + """ + select notebookEntry + from NotebookEntry notebookEntry + where notebookEntry.pupil.id = :pupilId + or (notebookEntry.pupil is null and notebookEntry.classroom.id = :classroomId) + """ + ) + fun findAllVisibleToPupil(pupilId: Long, classroomId: Long): List } diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt index 3e0aa6d6..48f7f207 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt @@ -34,10 +34,12 @@ class NotebookServiceTests { val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) val currentUser = currentUser("42") val pupil = pupilUser(42L) + val classroom = classroom(7L) val entries = listOf(notebookEntry(1L, pupil), notebookEntry(2L, pupil)) Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(pupil) - Mockito.`when`(notebookEntryRepository.findAllByPupilId(42L)).thenReturn(entries) + Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) + Mockito.`when`(notebookEntryRepository.findAllVisibleToPupil(42L, 7L)).thenReturn(entries) val result = service.getAllNotebookEntries(currentUser) From 172b85d7ee9deb39ad1d27e9800895b5c20ee4b0 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 10:13:02 +0200 Subject: [PATCH 5/8] Fix typo Signed-off-by: sindrelm --- .../idi/idatt/backend/notebook/application/NotebookService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 6cecac1a..1401a3b0 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -51,7 +51,7 @@ class NotebookService( val classroom = currentUserResolver.getClassroomByUser(command.currentUser) if (command.sourceType == NotebookSourceType.GAME) { - throw ResponseStatusException(HttpStatus.FORBIDEN, "Pupil cannot create notes with sourceType set to GAME") + throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot create notes with sourceType set to GAME") } if (classroom.id != command.classroomId) { From edaef4ed68b9f5fe745f9128c684c6f944a8c76c Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 10:40:47 +0200 Subject: [PATCH 6/8] Fix only notes for unlocked stops are returned Signed-off-by: sindrelm --- .../notebook/application/NotebookService.kt | 9 +- .../infrastructure/NotebookEntryRepository.kt | 7 +- .../backend/notebook/NotebookServiceTests.kt | 92 +++++++++++++++---- 3 files changed, 84 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 1401a3b0..1e5a65e5 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt.backend.notebook.application +import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser @@ -23,6 +24,7 @@ class NotebookService( private val notebookEntryRepository: NotebookEntryRepository, private val currentUserResolver: CurrentUserResolver, private val mapStopsRepository: MapStopsRepository, + private val pupilGameProgressRepository: PupilGameProgressRepository, ) { /** * Returns all notebook entries owned by the authenticated pupil. @@ -34,9 +36,14 @@ class NotebookService( fun getAllNotebookEntries(currentUser: CurrentUser): List { val pupil = currentUserResolver.requirePupil(currentUser.subject) val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" } + val classroom = currentUserResolver.getClassroomByUser(currentUser) val classroomId = requireNotNull(classroom.id) { "Active classroom is missing an id" } - return notebookEntryRepository.findAllVisibleToPupil(pupilId, classroomId) + + val gameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) + val completedStopsCount = gameProgress?.completedStopsCount ?: 0 + + return notebookEntryRepository.findAllVisibleToPupil(pupilId, classroomId, completedStopsCount) } /** diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt index 71e07bd7..bf6cd2b1 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/infrastructure/NotebookEntryRepository.kt @@ -24,9 +24,10 @@ interface NotebookEntryRepository : JpaRepository { """ select notebookEntry from NotebookEntry notebookEntry - where notebookEntry.pupil.id = :pupilId - or (notebookEntry.pupil is null and notebookEntry.classroom.id = :classroomId) + where (notebookEntry.pupil.id = :pupilId + or (notebookEntry.pupil is null and notebookEntry.classroom.id = :classroomId)) + and notebookEntry.stop.displayOrder <= :completedStops + 1 """ ) - fun findAllVisibleToPupil(pupilId: Long, classroomId: Long): List + fun findAllVisibleToPupil(pupilId: Long, classroomId: Long, completedStops: Int): List } diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt index 48f7f207..099de886 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt.backend.notebook import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom +import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver @@ -31,7 +32,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val classroom = classroom(7L) @@ -39,7 +46,8 @@ class NotebookServiceTests { Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(pupil) Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) - Mockito.`when`(notebookEntryRepository.findAllVisibleToPupil(42L, 7L)).thenReturn(entries) + Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(42L, 7L)).thenReturn(null) + Mockito.`when`(notebookEntryRepository.findAllVisibleToPupil(42L, 7L, 0)).thenReturn(entries) val result = service.getAllNotebookEntries(currentUser) @@ -51,7 +59,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val classroom = classroom(7L) @@ -86,15 +100,20 @@ class NotebookServiceTests { } @Test - fun `create notebook entry with game source persists null pupil`() { + fun `create notebook entry rejects game source`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val classroom = classroom(7L) - val stop = stop(9L) val command = CreateNotebookEntryCommand( currentUser = currentUser, classroomId = 7L, @@ -106,15 +125,12 @@ class NotebookServiceTests { Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(pupil) Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) - Mockito.`when`(mapStopsRepository.findById(9L)).thenReturn(Optional.of(stop)) - Mockito.`when`(notebookEntryRepository.save(Mockito.any(NotebookEntry::class.java))) - .thenAnswer { it.arguments[0] as NotebookEntry } - service.createNotebookEntry(command) - val captor = ArgumentCaptor.forClass(NotebookEntry::class.java) - Mockito.verify(notebookEntryRepository).save(captor.capture()) + val exception = assertThrows { + service.createNotebookEntry(command) + } - assertEquals(null, captor.value.pupil) + assertEquals(HttpStatus.FORBIDDEN, exception.statusCode) } @Test @@ -122,7 +138,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val classroom = classroom(7L) @@ -150,7 +172,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val classroom = classroom(7L) @@ -158,7 +186,7 @@ class NotebookServiceTests { currentUser = currentUser, classroomId = 7L, stopId = 9L, - sourceType = NotebookSourceType.GAME, + sourceType = NotebookSourceType.PUPIL, title = "Auto note", content = "Generated note", ) @@ -179,7 +207,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val existing = notebookEntry(15L, pupil).apply { @@ -209,7 +243,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val existing = notebookEntry(15L, pupilUser(99L)) @@ -235,7 +275,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val existing = notebookEntry(15L, pupil) @@ -257,7 +303,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val service = NotebookService(notebookEntryRepository, currentUserResolver, mapStopsRepository) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = NotebookService( + notebookEntryRepository, + currentUserResolver, + mapStopsRepository, + pupilGameProgressRepository, + ) val currentUser = currentUser("42") val pupil = pupilUser(42L) val existing = notebookEntry(15L, pupilUser(99L)) From 6f758e636608efe697db2a238134544e070ea326 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 10:46:24 +0200 Subject: [PATCH 7/8] Add 3 sample notes per stop Signed-off-by: sindrelm --- .../backend/bootstrap/DevDataBootstrap.kt | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt index b650ceee..b00a884a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt @@ -356,6 +356,10 @@ class DevDataBootstrap( ) { val activeStopsBySlug = stops.associateBy { requireNotNull(it.slug) { "Seeded map stop must have a slug" } } + stops.forEach { stop -> + seedGameNotebookEntriesForStop(classroom, stop) + } + activeStopsBySlug["nyhetskvartalet"]?.let { stop -> seedNotebookEntry( pupil = pupilAda, @@ -366,16 +370,6 @@ class DevDataBootstrap( content = "Ada la merke til at overskriften brukte sterke følelser og manglet tydelige kilder.", ) } - activeStopsBySlug["postkontoret"]?.let { stop -> - seedNotebookEntry( - pupil = null, - classroom = classroom, - stop = stop, - sourceType = NotebookSourceType.GAME, - title = "Phishing-tegn", - content = "Spillet markerte dårlig språk, hastverk og mistenkelig lenke som tydelige faresignaler.", - ) - } activeStopsBySlug["fotografen"]?.let { stop -> seedNotebookEntry( pupil = pupilNoah, @@ -388,6 +382,29 @@ class DevDataBootstrap( } } + private fun seedGameNotebookEntriesForStop( + classroom: Classroom, + stop: MapStop, + ) { + val stopTitle = requireNotNull(stop.title) { "Seeded map stop must have a title" } + val notebookEntries = listOf( + "Hva vi oppdaget" to "Spillet oppsummerte de viktigste sporene fra $stopTitle og viste hvilke detaljer som burde undersøkes nærmere.", + "Tegn å passe på" to "Notatet fra $stopTitle fremhevet faresignaler som uklar avsender, manglende kontekst eller forsøk på å skape stress.", + "Huskeliste" to "Spillet foreslo å dobbeltsjekke kilder, sammenligne informasjon og ta vare på bevis før man går videre fra $stopTitle.", + ) + + notebookEntries.forEach { (title, content) -> + seedNotebookEntry( + pupil = null, + classroom = classroom, + stop = stop, + sourceType = NotebookSourceType.GAME, + title = title, + content = content, + ) + } + } + private fun seedNotebookEntry( pupil: User?, classroom: Classroom, From fc0fff02bf43bceb4e425428e3b53d39db4e7c38 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 10:52:37 +0200 Subject: [PATCH 8/8] Add displayOrder to map stops in tests Signed-off-by: sindrelm --- .../notebook/NotebookEntryControllerIntegrationTests.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookEntryControllerIntegrationTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookEntryControllerIntegrationTests.kt index 423196d7..a0718eba 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookEntryControllerIntegrationTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookEntryControllerIntegrationTests.kt @@ -79,7 +79,7 @@ class NotebookEntryControllerIntegrationTests { Classroom(ownerTeacherId = requireNotNull(teacher.id), title = "Class A") ) val stop = mapStopsRepository.save( - MapStop(slug = "intro", title = "Intro") + MapStop(slug = "intro", title = "Intro", displayOrder = 1) ) val pupil = userRepository.save(activePupil("ada")) val otherPupil = userRepository.save(activePupil("grace")) @@ -127,7 +127,7 @@ class NotebookEntryControllerIntegrationTests { Classroom(ownerTeacherId = requireNotNull(teacher.id), title = "Class A") ) val stop = mapStopsRepository.save( - MapStop(slug = "intro", title = "Intro") + MapStop(slug = "intro", title = "Intro", displayOrder = 1) ) val pupil = userRepository.save(activePupil("ada")) pupilProfileRepository.save(PupilProfile(user = pupil, displayName = "Ada", currentClassroomId = classroom.id)) @@ -169,7 +169,7 @@ class NotebookEntryControllerIntegrationTests { Classroom(ownerTeacherId = requireNotNull(teacher.id), title = "Class A") ) val stop = mapStopsRepository.save( - MapStop(slug = "intro", title = "Intro") + MapStop(slug = "intro", title = "Intro", displayOrder = 1) ) val pupil = userRepository.save(activePupil("ada")) pupilProfileRepository.save(PupilProfile(user = pupil, displayName = "Ada", currentClassroomId = classroom.id)) @@ -208,7 +208,7 @@ class NotebookEntryControllerIntegrationTests { Classroom(ownerTeacherId = requireNotNull(teacher.id), title = "Class A") ) val stop = mapStopsRepository.save( - MapStop(slug = "intro", title = "Intro") + MapStop(slug = "intro", title = "Intro", displayOrder = 1) ) val pupil = userRepository.save(activePupil("ada")) val otherPupil = userRepository.save(activePupil("grace"))