Don't allow null in getCurrentStudent()
This commit is contained in:
parent
5459ac7fc4
commit
71654e3186
6 changed files with 47 additions and 42 deletions
|
@ -331,7 +331,7 @@ class Scrapper {
|
|||
|
||||
// AUTHORIZED - student
|
||||
|
||||
suspend fun getCurrentStudent(): RegisterStudent? {
|
||||
suspend fun getCurrentStudent(): RegisterStudent {
|
||||
val loginResult = serviceManager.userLogin()
|
||||
return when (loginResult.isStudentSchoolUseEduOne) {
|
||||
true -> studentPlus.getStudent(studentId)
|
||||
|
|
|
@ -78,36 +78,36 @@ internal class StudentPlusRepository(
|
|||
return true
|
||||
}
|
||||
|
||||
suspend fun getStudent(studentId: Int): RegisterStudent? {
|
||||
return api.getContext().students.find { contextStudent ->
|
||||
suspend fun getStudent(studentId: Int): RegisterStudent {
|
||||
val contextStudent = api.getContext().students.find { contextStudent ->
|
||||
val key = getDecodedKey(contextStudent.key)
|
||||
key.studentId == studentId
|
||||
}?.let { contextStudent ->
|
||||
val semesters = runCatching {
|
||||
when {
|
||||
contextStudent.isAuthorizationRequired -> emptyList()
|
||||
else -> api.getSemesters(
|
||||
key = contextStudent.key,
|
||||
diaryId = contextStudent.registerId,
|
||||
)
|
||||
}
|
||||
}.onFailure {
|
||||
logger.error("Can't fetch semesters", it)
|
||||
}.getOrNull().orEmpty()
|
||||
} ?: throw NoSuchElementException()
|
||||
|
||||
RegisterStudent(
|
||||
studentId = studentId,
|
||||
studentName = contextStudent.studentName.substringBefore(" "),
|
||||
studentSurname = contextStudent.studentName.substringAfterLast(" "),
|
||||
className = contextStudent.className,
|
||||
isParent = contextStudent.opiekunUcznia,
|
||||
semesters = semesters.mapToSemester(contextStudent),
|
||||
isAuthorized = !contextStudent.isAuthorizationRequired,
|
||||
isEduOne = true,
|
||||
studentSecondName = "", //
|
||||
classId = 0, //
|
||||
)
|
||||
}
|
||||
val semesters = runCatching {
|
||||
when {
|
||||
contextStudent.isAuthorizationRequired -> emptyList()
|
||||
else -> api.getSemesters(
|
||||
key = contextStudent.key,
|
||||
diaryId = contextStudent.registerId,
|
||||
)
|
||||
}
|
||||
}.onFailure {
|
||||
logger.error("Can't fetch semesters", it)
|
||||
}.getOrNull().orEmpty()
|
||||
|
||||
return RegisterStudent(
|
||||
studentId = studentId,
|
||||
studentName = contextStudent.studentName.substringBefore(" "),
|
||||
studentSurname = contextStudent.studentName.substringAfterLast(" "),
|
||||
className = contextStudent.className,
|
||||
isParent = contextStudent.opiekunUcznia,
|
||||
semesters = semesters.mapToSemester(contextStudent),
|
||||
isAuthorized = !contextStudent.isAuthorizationRequired,
|
||||
isEduOne = true, // we already in eduOne context here
|
||||
studentSecondName = "", //
|
||||
classId = 0, //
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun getSemesters(studentId: Int, diaryId: Int): List<Semester> {
|
||||
|
|
|
@ -41,15 +41,14 @@ internal class StudentStartRepository(
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun getStudent(studentId: Int, unitId: Int): RegisterStudent? {
|
||||
suspend fun getStudent(studentId: Int, unitId: Int): RegisterStudent {
|
||||
return getStudentsFromDiaries(
|
||||
isParent = getCache().isParent,
|
||||
diaries = api.getDiaries().handleErrors().data.orEmpty(),
|
||||
unitId = unitId,
|
||||
isEduOne = false,
|
||||
).find {
|
||||
it.studentId == studentId
|
||||
}
|
||||
).find { it.studentId == studentId }
|
||||
?: throw NoSuchElementException()
|
||||
}
|
||||
|
||||
private suspend fun getCache(): CacheResponse {
|
||||
|
|
|
@ -79,7 +79,7 @@ class ScrapperRemoteTest : BaseTest() {
|
|||
@Test
|
||||
fun studentsTest() = runTest {
|
||||
val res = api.getCurrentStudent()
|
||||
assertEquals("Jan", res?.studentName)
|
||||
assertEquals("Jan", res.studentName)
|
||||
|
||||
val user = api.getUserSubjects()
|
||||
val symbol = user.symbols[0]
|
||||
|
|
|
@ -9,6 +9,14 @@ import org.junit.Test
|
|||
|
||||
class StudentPlusTest : BaseLocalTest() {
|
||||
|
||||
@Test(expected = NoSuchElementException::class)
|
||||
fun `get current student when there is no matching student`() = runTest {
|
||||
val repo = getStudentPlusRepo {
|
||||
it.enqueue("Context-all-enabled.json")
|
||||
}
|
||||
repo.getStudent(2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get current student info without error but with empty real semesters`() = runTest {
|
||||
val repo = getStudentPlusRepo {
|
||||
|
@ -16,7 +24,7 @@ class StudentPlusTest : BaseLocalTest() {
|
|||
it.enqueueContent("[]")
|
||||
}
|
||||
val student = repo.getStudent(1)
|
||||
with(requireNotNull(student)) {
|
||||
with(student) {
|
||||
assertEquals(1, studentId)
|
||||
assertEquals("Jan", studentName)
|
||||
assertEquals("", studentSecondName)
|
||||
|
@ -24,12 +32,10 @@ class StudentPlusTest : BaseLocalTest() {
|
|||
assertEquals("7a", className)
|
||||
assertEquals(0, classId)
|
||||
assertFalse(isParent)
|
||||
assertEquals(1, semesters.size)
|
||||
assertTrue(isAuthorized)
|
||||
|
||||
with(semesters[0]) {
|
||||
assertEquals(-1, semesterId)
|
||||
}
|
||||
assertEquals(1, semesters.size)
|
||||
assertEquals(-1, semesters[0].semesterId)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,7 +46,7 @@ class StudentPlusTest : BaseLocalTest() {
|
|||
it.enqueue("OkresyKlasyfikacyjne.json", RegisterTest::class.java)
|
||||
}
|
||||
val student = repo.getStudent(1)
|
||||
with(requireNotNull(student)) {
|
||||
with(student) {
|
||||
assertEquals(1, studentId)
|
||||
assertEquals("Jan", studentName)
|
||||
assertEquals("", studentSecondName)
|
||||
|
@ -48,9 +54,9 @@ class StudentPlusTest : BaseLocalTest() {
|
|||
assertEquals("7a", className)
|
||||
assertEquals(0, classId)
|
||||
assertFalse(isParent)
|
||||
assertEquals(2, semesters.size)
|
||||
assertTrue(isAuthorized)
|
||||
|
||||
assertEquals(2, semesters.size)
|
||||
assertEquals(12, semesters[0].semesterId)
|
||||
assertEquals(13, semesters[1].semesterId)
|
||||
}
|
||||
|
|
|
@ -378,9 +378,9 @@ class Sdk {
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun getCurrentStudent(): RegisterStudent? = withContext(Dispatchers.IO) {
|
||||
suspend fun getCurrentStudent(): RegisterStudent = withContext(Dispatchers.IO) {
|
||||
when (mode) {
|
||||
Mode.SCRAPPER, Mode.HYBRID -> scrapper.getCurrentStudent()?.mapStudent()
|
||||
Mode.SCRAPPER, Mode.HYBRID -> scrapper.getCurrentStudent().mapStudent()
|
||||
Mode.HEBE -> throw NotImplementedError("Not available in HEBE mode")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue