diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileApiMappings.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileApiMappings.kt index d6854a51..56601375 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileApiMappings.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileApiMappings.kt @@ -21,6 +21,7 @@ fun CurrentUserProfile.toCurrentUserProfileResponse(): CurrentUserProfileRespons currentClassroomId = it.currentClassroomId, totalXp = it.totalXp, currentLevel = it.currentLevel, + hasCompletedOnboarding = it.hasCompletedOnboarding, ) }, teacherProfile = teacherProfile?.let { @@ -42,6 +43,7 @@ fun PupilProfileDetails.toPupilProfileResponse(): PupilProfileResponse = currentClassroomId = currentClassroomId, totalXp = totalXp, currentLevel = currentLevel, + hasCompletedOnboarding = hasCompletedOnboarding, ) /** diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileResponse.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileResponse.kt index 9d4f908c..2edda0b7 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileResponse.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/CurrentUserProfileResponse.kt @@ -34,6 +34,7 @@ data class PupilProfileResponse( val currentClassroomId: Long?, val totalXp: Int, val currentLevel: Int, + val hasCompletedOnboarding: Boolean, ) /** diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/UserProfileController.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/UserProfileController.kt index c38b6088..326d962a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/UserProfileController.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/api/UserProfileController.kt @@ -38,4 +38,10 @@ class UserProfileController( @Valid @RequestBody request: UpdateCurrentUserDisplayNameRequest, ): PupilProfileResponse = currentUserProfileService.updateCurrentUserDisplayName(currentUser.subject, request.toCommand()).toPupilProfileResponse() + + @PutMapping("/onboarding/completed") + fun completeCurrentUserOnboarding( + @AuthenticationPrincipal currentUser: CurrentUser, + ): PupilProfileResponse = + currentUserProfileService.completeCurrentUserOnboarding(currentUser.subject).toPupilProfileResponse() } diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt index 0a357b3f..25486fba 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt @@ -48,6 +48,7 @@ class CurrentUserProfileService( currentClassroomId = it.currentClassroom?.id, totalXp = progress?.totalXp ?: 0, currentLevel = progress?.currentLevel ?: 1, + hasCompletedOnboarding = it.hasCompletedOnboarding, ) } .orElseThrow { @@ -106,6 +107,31 @@ class CurrentUserProfileService( currentClassroomId = savedProfile.currentClassroom?.id, totalXp = progress?.totalXp ?: 0, currentLevel = progress?.currentLevel ?: 1, + hasCompletedOnboarding = savedProfile.hasCompletedOnboarding, + ) + } + + /** + * Marks the current authenticated pupil's onboarding flow as completed. + * + * @param authenticatedSubject JWT subject containing the persisted user id + * @return updated pupil profile projection + */ + @Transactional + fun completeCurrentUserOnboarding(authenticatedSubject: String): PupilProfileDetails { + val user = currentUserResolver.requirePupil(authenticatedSubject) + val userId = requireNotNull(user.id) { "Authenticated user is missing an id" } + val pupilProfile = findPupilProfile(userId) + val progress = getCurrentClassroomProgress(userId, pupilProfile.currentClassroom?.id) + pupilProfile.hasCompletedOnboarding = true + val savedProfile = pupilProfileRepository.save(pupilProfile) + + return PupilProfileDetails( + displayName = savedProfile.displayName, + currentClassroomId = savedProfile.currentClassroom?.id, + totalXp = progress?.totalXp ?: 0, + currentLevel = progress?.currentLevel ?: 1, + hasCompletedOnboarding = savedProfile.hasCompletedOnboarding, ) } @@ -164,6 +190,7 @@ data class PupilProfileDetails( val currentClassroomId: Long?, val totalXp: Int, val currentLevel: Int, + val hasCompletedOnboarding: Boolean, ) /** diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/domain/PupilProfile.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/domain/PupilProfile.kt index 5dfd6f9d..f8fbc7f2 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/domain/PupilProfile.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/domain/PupilProfile.kt @@ -36,4 +36,6 @@ class PupilProfile( @ManyToOne(fetch = FetchType.LAZY, optional = true) @JoinColumn(name = "current_classroom_id") var currentClassroom: Classroom? = null, + @Column(name = "has_completed_onboarding", nullable = false) + var hasCompletedOnboarding: Boolean = false, ) diff --git a/src/main/resources/db/migration/V22__add_has_completed_onboarding_to_pupil_profiles.sql b/src/main/resources/db/migration/V22__add_has_completed_onboarding_to_pupil_profiles.sql new file mode 100644 index 00000000..ac7da238 --- /dev/null +++ b/src/main/resources/db/migration/V22__add_has_completed_onboarding_to_pupil_profiles.sql @@ -0,0 +1,2 @@ +ALTER TABLE pupil_profiles + ADD COLUMN has_completed_onboarding BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt index 7a7e8f8d..8da9751f 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt @@ -59,6 +59,7 @@ class CurrentUserProfileServiceTests { assertEquals(101L, resolvedProfile.pupilProfile?.currentClassroomId) assertEquals(480, resolvedProfile.pupilProfile?.totalXp) assertEquals(4, resolvedProfile.pupilProfile?.currentLevel) + assertEquals(false, resolvedProfile.pupilProfile?.hasCompletedOnboarding) assertNull(resolvedProfile.teacherProfile) } @@ -294,9 +295,51 @@ class CurrentUserProfileServiceTests { assertEquals(101L, updatedProfile.currentClassroomId) assertEquals(480, updatedProfile.totalXp) assertEquals(4, updatedProfile.currentLevel) + assertEquals(false, updatedProfile.hasCompletedOnboarding) assertEquals("Ada Lovelace", pupilProfile.displayName) } + @Test + fun `complete current user onboarding persists submitted value for pupil`() { + val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) + val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) + val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) + val classroom = Classroom(id = 101L, ownerTeacherId = 7L, title = "Class A") + val service = CurrentUserProfileService( + currentUserResolver, + pupilProfileRepository, + pupilGameProgressService, + teacherProfileRepository, + ) + val user = pupilUser(42L) + val pupilProfile = PupilProfile( + user = user, + displayName = "Ada", + currentClassroom = classroom, + ).apply { + userId = 42L + } + + Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(user) + Mockito.`when`(pupilProfileRepository.findById(42L)).thenReturn(Optional.of(pupilProfile)) + Mockito.`when`(pupilProfileRepository.save(Mockito.any(PupilProfile::class.java))).thenAnswer { invocation -> + invocation.arguments[0] as PupilProfile + } + Mockito.`when`(pupilGameProgressService.getPupilProgress(42L, 101L)).thenReturn( + PupilGameProgress(totalXp = 480, currentLevel = 4), + ) + + val updatedProfile = service.completeCurrentUserOnboarding("42") + + assertEquals("Ada", updatedProfile.displayName) + assertEquals(101L, updatedProfile.currentClassroomId) + assertEquals(480, updatedProfile.totalXp) + assertEquals(4, updatedProfile.currentLevel) + assertEquals(true, updatedProfile.hasCompletedOnboarding) + assertEquals(true, pupilProfile.hasCompletedOnboarding) + } + @Test fun `update current user display name rejects teacher user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/UserProfileControllerIntegrationTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/UserProfileControllerIntegrationTests.kt index 1d850d5b..20f77271 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/UserProfileControllerIntegrationTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/UserProfileControllerIntegrationTests.kt @@ -113,6 +113,7 @@ class UserProfileControllerIntegrationTests { assertEquals(classroom.id, body.get("pupilProfile").get("currentClassroomId").asLong()) assertEquals(480, body.get("pupilProfile").get("totalXp").asInt()) assertEquals(4, body.get("pupilProfile").get("currentLevel").asInt()) + assertEquals(false, body.get("pupilProfile").get("hasCompletedOnboarding").asBoolean()) assertTrue(body.get("teacherProfile").isNull) } @@ -250,6 +251,34 @@ class UserProfileControllerIntegrationTests { } } + @Test + fun `authenticated pupil can complete onboarding`() { + val pupil = userRepository.save(activePupil()) + pupilProfileRepository.save( + PupilProfile( + user = pupil, + displayName = "Ada", + ), + ) + val accessToken = pupilAccessToken("ada", "secret123") + + val body = readJson( + mockMvc.put("/api/v1/users/me/onboarding/completed") { + with(csrf()) + header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken") + }.andExpect { + status { isOk() } + jsonPath("$.hasCompletedOnboarding") { value(true) } + }.andReturn().response.contentAsString, + ) + + assertEquals(true, body.get("hasCompletedOnboarding").asBoolean()) + assertEquals( + true, + pupilProfileRepository.findById(requireNotNull(pupil.id)).get().hasCompletedOnboarding, + ) + } + @Test fun `user profile endpoint requires authentication`() { mockMvc.get("/api/v1/users/me")