From 7ab3cc8de14471965f0ceb661a5ea7ca627e295d Mon Sep 17 00:00:00 2001 From: eilifhl Date: Mon, 27 Apr 2026 09:38:42 +0200 Subject: [PATCH] add hasCompletedOnboarding colymn to userprofile --- .../api/CurrentUserProfileApiMappings.kt | 2 + .../profile/api/CurrentUserProfileResponse.kt | 1 + .../profile/api/UserProfileController.kt | 6 +++ .../application/CurrentUserProfileService.kt | 27 ++++++++++++ .../users/profile/domain/PupilProfile.kt | 2 + ...completed_onboarding_to_pupil_profiles.sql | 2 + .../profile/CurrentUserProfileServiceTests.kt | 42 +++++++++++++++++++ .../UserProfileControllerIntegrationTests.kt | 29 +++++++++++++ 8 files changed, 111 insertions(+) create mode 100644 src/main/resources/db/migration/V22__add_has_completed_onboarding_to_pupil_profiles.sql 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 eac625c6..c1f5fbcd 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 @@ -47,6 +47,7 @@ class CurrentUserProfileService( currentClassroomId = it.currentClassroomId, totalXp = progress?.totalXp ?: 0, currentLevel = progress?.currentLevel ?: 1, + hasCompletedOnboarding = it.hasCompletedOnboarding, ) } .orElseThrow { @@ -105,6 +106,31 @@ class CurrentUserProfileService( currentClassroomId = savedProfile.currentClassroomId, 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.currentClassroomId) + pupilProfile.hasCompletedOnboarding = true + val savedProfile = pupilProfileRepository.save(pupilProfile) + + return PupilProfileDetails( + displayName = savedProfile.displayName, + currentClassroomId = savedProfile.currentClassroomId, + totalXp = progress?.totalXp ?: 0, + currentLevel = progress?.currentLevel ?: 1, + hasCompletedOnboarding = savedProfile.hasCompletedOnboarding, ) } @@ -151,6 +177,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 d52e74ed..6ea8c039 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 @@ -35,4 +35,6 @@ class PupilProfile( // this column references classrooms.id. @Column(name = "current_classroom_id") var currentClassroomId: Long? = 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 0be7d098..2b71c9c6 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 @@ -58,6 +58,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) } @@ -291,9 +292,50 @@ 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 pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) + val service = CurrentUserProfileService( + currentUserResolver, + pupilProfileRepository, + pupilGameProgressRepository, + teacherProfileRepository, + ) + val user = pupilUser(42L) + val pupilProfile = PupilProfile( + user = user, + displayName = "Ada", + currentClassroomId = 101L, + ).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`(pupilGameProgressRepository.findByPupilIdAndClassroomId(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 9532f630..2803b9cf 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")