Add getCurrentStudent method

This commit is contained in:
Mikołaj Pich 2023-05-11 10:47:53 +02:00
parent d897ce49ae
commit f7716915d3
8 changed files with 58 additions and 27 deletions

View file

@ -17,6 +17,7 @@ public final class io/github/wulkanowy/sdk/scrapper/Scrapper {
public final fun getCompletedLessons (Ljava/time/LocalDate;Ljava/time/LocalDate;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getCompletedLessons$default (Lio/github/wulkanowy/sdk/scrapper/Scrapper;Ljava/time/LocalDate;Ljava/time/LocalDate;ILkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getConferences (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getCurrentStudent (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getDeletedMessages (Ljava/lang/String;IILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getDeletedMessages$default (Lio/github/wulkanowy/sdk/scrapper/Scrapper;Ljava/lang/String;IILkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getDiaryId ()I

View file

@ -26,6 +26,7 @@ import io.github.wulkanowy.sdk.scrapper.messages.Recipient
import io.github.wulkanowy.sdk.scrapper.mobile.Device
import io.github.wulkanowy.sdk.scrapper.mobile.TokenResponse
import io.github.wulkanowy.sdk.scrapper.notes.Note
import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent
import io.github.wulkanowy.sdk.scrapper.register.RegisterUser
import io.github.wulkanowy.sdk.scrapper.register.Semester
import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository
@ -263,6 +264,8 @@ class Scrapper {
suspend fun getSemesters(): List<Semester> = studentStart.getSemesters()
suspend fun getCurrentStudent(): RegisterStudent? = student.getStudent(studentId, unitId)
suspend fun getAttendance(startDate: LocalDate, endDate: LocalDate? = null): List<Attendance> {
if (diaryId == 0) return emptyList()

View file

@ -0,0 +1,30 @@
package io.github.wulkanowy.sdk.scrapper.register
import io.github.wulkanowy.sdk.scrapper.timetable.CacheResponse
internal fun getStudentsFromDiaries(
diaries: List<Diary>,
cache: CacheResponse?,
unitId: Int,
): List<RegisterStudent> = diaries
.filter { it.semesters.orEmpty().isNotEmpty() || it.kindergartenDiaryId != 0 }
.sortedByDescending { it.level }
.distinctBy { listOf(it.studentId, it.semesters?.firstOrNull()?.classId ?: 0) }
.map { diary ->
val classId = diary.semesters?.firstOrNull()?.classId ?: 0
RegisterStudent(
studentId = diary.studentId,
studentName = diary.studentName.trim(),
studentSecondName = diary.studentSecondName.orEmpty(),
studentSurname = diary.studentSurname,
className = diary.symbol.orEmpty(),
classId = classId,
isParent = cache?.isParent == true,
isAuthorized = diary.isAuthorized == true,
semesters = diaries.toSemesters(
studentId = diary.studentId,
classId = classId,
unitId = unitId,
),
)
}

View file

@ -21,6 +21,7 @@ import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent
import io.github.wulkanowy.sdk.scrapper.register.RegisterSymbol
import io.github.wulkanowy.sdk.scrapper.register.RegisterUnit
import io.github.wulkanowy.sdk.scrapper.register.RegisterUser
import io.github.wulkanowy.sdk.scrapper.register.getStudentsFromDiaries
import io.github.wulkanowy.sdk.scrapper.register.toSemesters
import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS
import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS_CARDS
@ -150,33 +151,6 @@ internal class RegisterRepository(
}
}
private fun getStudentsFromDiaries(
diaries: List<Diary>,
cache: CacheResponse?,
unitId: Int,
): List<RegisterStudent> = diaries
.filter { it.semesters.orEmpty().isNotEmpty() || it.kindergartenDiaryId != 0 }
.sortedByDescending { it.level }
.distinctBy { listOf(it.studentId, it.semesters?.firstOrNull()?.classId ?: 0) }
.map { diary ->
val classId = diary.semesters?.firstOrNull()?.classId ?: 0
RegisterStudent(
studentId = diary.studentId,
studentName = diary.studentName.trim(),
studentSecondName = diary.studentSecondName.orEmpty(),
studentSurname = diary.studentSurname,
className = diary.symbol.orEmpty(),
classId = classId,
isParent = cache?.isParent == true,
isAuthorized = diary.isAuthorized == true,
semesters = diaries.toSemesters(
studentId = diary.studentId,
classId = classId,
unitId = unitId,
),
)
}
private suspend fun getLoginType(symbol: String): Scrapper.LoginType {
val urlGenerator = url.also { it.symbol = symbol }
val page = register.getFormType(urlGenerator.generate(UrlGenerator.Site.LOGIN) + "Account/LogOn").page

View file

@ -40,6 +40,8 @@ import io.github.wulkanowy.sdk.scrapper.mobile.UnregisterDeviceRequest
import io.github.wulkanowy.sdk.scrapper.notes.Note
import io.github.wulkanowy.sdk.scrapper.register.AuthorizePermission
import io.github.wulkanowy.sdk.scrapper.register.AuthorizePermissionRequest
import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent
import io.github.wulkanowy.sdk.scrapper.register.getStudentsFromDiaries
import io.github.wulkanowy.sdk.scrapper.school.School
import io.github.wulkanowy.sdk.scrapper.school.Teacher
import io.github.wulkanowy.sdk.scrapper.school.mapToSchool
@ -84,6 +86,16 @@ internal class StudentRepository(private val api: StudentService) {
return api.authorizePermission(AuthorizePermissionRequest(AuthorizePermission(pesel))).data?.success ?: false
}
suspend fun getStudent(studentId: Int, unitId: Int): RegisterStudent? {
return getStudentsFromDiaries(
cache = getCache(),
diaries = api.getDiaries().data.orEmpty(),
unitId = unitId,
).find {
it.studentId == studentId
}
}
suspend fun getAttendance(startDate: LocalDate, endDate: LocalDate?): List<Attendance> {
return api.getAttendance(AttendanceRequest(startDate.atStartOfDay()))
.handleErrors()

View file

@ -79,6 +79,9 @@ class ScrapperRemoteTest : BaseTest() {
@Test
fun studentsTest() = runTest {
val res = api.getCurrentStudent()
assertEquals("Jan", res?.studentName)
val user = api.getUserSubjects()
val symbol = user.symbols[0]
val school = symbol.schools[0]

View file

@ -16,6 +16,7 @@ public final class io/github/wulkanowy/sdk/Sdk {
public final fun getCompletedLessons (Ljava/time/LocalDate;Ljava/time/LocalDate;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getCompletedLessons$default (Lio/github/wulkanowy/sdk/Sdk;Ljava/time/LocalDate;Ljava/time/LocalDate;ILkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getConferences (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getCurrentStudent (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getDeletedMessages (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getDeletedMessages$default (Lio/github/wulkanowy/sdk/Sdk;Ljava/lang/String;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getDiaryId ()I

View file

@ -323,6 +323,13 @@ class Sdk {
}
}
suspend fun getCurrentStudent(): RegisterStudent? = withContext(Dispatchers.IO) {
when(mode) {
Mode.SCRAPPER, Mode.HYBRID -> scrapper.getCurrentStudent()?.mapStudent()
Mode.HEBE -> throw NotImplementedError("Not available in HEBE mode")
}
}
suspend fun getSemesters(): List<Semester> = withContext(Dispatchers.IO) {
when (mode) {
Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSemesters().mapSemesters()