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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fun CurrentUserProfile.toCurrentUserProfileResponse(): CurrentUserProfileRespons
currentClassroomId = it.currentClassroomId,
totalXp = it.totalXp,
currentLevel = it.currentLevel,
hasCompletedOnboarding = it.hasCompletedOnboarding,
)
},
teacherProfile = teacherProfile?.let {
Expand All @@ -42,6 +43,7 @@ fun PupilProfileDetails.toPupilProfileResponse(): PupilProfileResponse =
currentClassroomId = currentClassroomId,
totalXp = totalXp,
currentLevel = currentLevel,
hasCompletedOnboarding = hasCompletedOnboarding,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ data class PupilProfileResponse(
val currentClassroomId: Long?,
val totalXp: Int,
val currentLevel: Int,
val hasCompletedOnboarding: Boolean,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CurrentUserProfileService(
currentClassroomId = it.currentClassroom?.id,
totalXp = progress?.totalXp ?: 0,
currentLevel = progress?.currentLevel ?: 1,
hasCompletedOnboarding = it.hasCompletedOnboarding,
)
}
.orElseThrow {
Expand Down Expand Up @@ -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,
)
}

Expand Down Expand Up @@ -164,6 +190,7 @@ data class PupilProfileDetails(
val currentClassroomId: Long?,
val totalXp: Int,
val currentLevel: Int,
val hasCompletedOnboarding: Boolean,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE pupil_profiles
ADD COLUMN has_completed_onboarding BOOLEAN NOT NULL DEFAULT FALSE;
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
Expand Down