Attach proper school name to RegisterUnit inside register repo

This commit is contained in:
Mikołaj Pich 2024-04-21 23:12:36 +02:00
parent 617b4eb62c
commit d766d243be
No known key found for this signature in database
6 changed files with 111 additions and 51 deletions

View file

@ -1546,27 +1546,33 @@ public final class io/github/wulkanowy/sdk/scrapper/register/RegisterEmployee :
}
public final class io/github/wulkanowy/sdk/scrapper/register/RegisterStudent : io/github/wulkanowy/sdk/scrapper/register/RegisterSubject {
public fun <init> (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IZLjava/util/List;ZZ)V
public fun <init> (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIZLjava/util/List;ZZ)V
public final fun component1 ()I
public final fun component10 ()Z
public final fun component11 ()Ljava/util/List;
public final fun component12 ()Z
public final fun component13 ()Z
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Ljava/lang/String;
public final fun component4 ()Ljava/lang/String;
public final fun component5 ()Ljava/lang/String;
public final fun component6 ()I
public final fun component7 ()Z
public final fun component8 ()Ljava/util/List;
public final fun component9 ()Z
public final fun copy (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IZLjava/util/List;ZZ)Lio/github/wulkanowy/sdk/scrapper/register/RegisterStudent;
public static synthetic fun copy$default (Lio/github/wulkanowy/sdk/scrapper/register/RegisterStudent;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IZLjava/util/List;ZZILjava/lang/Object;)Lio/github/wulkanowy/sdk/scrapper/register/RegisterStudent;
public final fun component6 ()Ljava/lang/String;
public final fun component7 ()Ljava/lang/String;
public final fun component8 ()I
public final fun component9 ()I
public final fun copy (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIZLjava/util/List;ZZ)Lio/github/wulkanowy/sdk/scrapper/register/RegisterStudent;
public static synthetic fun copy$default (Lio/github/wulkanowy/sdk/scrapper/register/RegisterStudent;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIZLjava/util/List;ZZILjava/lang/Object;)Lio/github/wulkanowy/sdk/scrapper/register/RegisterStudent;
public fun equals (Ljava/lang/Object;)Z
public final fun getClassId ()I
public final fun getClassName ()Ljava/lang/String;
public final fun getSchoolName ()Ljava/lang/String;
public final fun getSchoolNameShort ()Ljava/lang/String;
public final fun getSemesters ()Ljava/util/List;
public final fun getStudentId ()I
public final fun getStudentName ()Ljava/lang/String;
public final fun getStudentSecondName ()Ljava/lang/String;
public final fun getStudentSurname ()Ljava/lang/String;
public final fun getUnitId ()I
public fun hashCode ()I
public final fun isAuthorized ()Z
public final fun isEduOne ()Z

View file

@ -39,6 +39,9 @@ internal fun ContextStudent.mapToRegisterStudent(semesters: List<GradeSemester>)
studentSurname = studentName.substringAfterLast(" "),
className = className,
isParent = opiekunUcznia,
unitId = key.unitId,
schoolName = schoolName,
schoolNameShort = null,
semesters = semesters.mapToSemester(this),
isAuthorized = !isAuthorizationRequired,
isEduOne = true, // we already in eduOne context here

View file

@ -20,6 +20,9 @@ internal fun List<Diary>.getStudentsFromDiaries(
isParent = isParent == true,
isAuthorized = diary.isAuthorized == true,
isEduOne = isEduOne,
unitId = unitId,
schoolName = "",
schoolNameShort = null,
semesters = toSemesters(
studentId = diary.studentId,
classId = classId,

View file

@ -44,8 +44,11 @@ data class RegisterStudent(
val studentName: String,
val studentSecondName: String,
val studentSurname: String,
val schoolName: String,
val schoolNameShort: String?,
val className: String,
val classId: Int,
val unitId: Int,
val isParent: Boolean,
val semesters: List<Semester>,
val isAuthorized: Boolean,

View file

@ -160,7 +160,7 @@ internal class RegisterRepository(
val version = getScriptParam("appVersion", homeResponse?.document.toString()).substringBefore("|")
logger.warn("Can't find permissions on homepage version $version")
return studentModules.map { (name, url) ->
return studentModules.flatMap { (name, url) ->
getRegisterUnit(
originalName = name,
studentModuleUrl = url,
@ -185,7 +185,7 @@ internal class RegisterRepository(
originalName: String,
studentModuleUrl: String,
studentModuleUrls: List<String>,
): RegisterUnit {
): List<RegisterUnit> {
val extractedSchoolId = studentModuleUrl.toHttpUrl().pathSegments[1]
url.schoolId = extractedSchoolId
val isEduOne = isCurrentLoginHasEduOne(studentModuleUrls, url)
@ -193,42 +193,60 @@ internal class RegisterRepository(
val originalSchoolShortName = when {
isEduOne -> originalName.takeIf { it != "Uczeń Plus" }
else -> originalName.takeIf { it != "Uczeń" }
} ?: "Nieznana nazwa szkoły"
}
val loginResult = runCatching {
val site = when {
isEduOne -> UrlGenerator.Site.STUDENT_PLUS
else -> UrlGenerator.Site.STUDENT
}
loginModule(site)
}
val registerStudents = runCatching {
when {
isEduOne -> {
// todo: fetch school name from cache
getEduOneDiaries()
}
else -> {
val (_, startPage) = loginModule(UrlGenerator.Site.STUDENT)
// todo: get school name
val isParent = isStudentFromParentAccount(startPage)
val diaries = getStudentDiaries()
diaries.getStudentsFromDiaries(
isParent = isParent,
isEduOne = false,
unitId = diaries.firstOrNull()?.componentUnitId
?: error("Can't find componentUnitId in student diaries"),
)
val (baseStudentPlus, _) = loginResult.getOrThrow()
getEduOneDiaries(baseStudentPlus)
}
else -> getStudentsFromOldModule(
loginResult = loginResult.getOrThrow(),
unitId = null, // will be extracted from diary
)
}
}
val firstStudentFromUnit = registerStudents.getOrNull()?.firstOrNull()
val students = registerStudents.getOrNull().orEmpty()
return RegisterUnit(
userLoginId = -1,
schoolId = extractedSchoolId,
schoolName = firstStudentFromUnit?.className.orEmpty(), // todo: schoolName
schoolShortName = firstStudentFromUnit?.className // todo: schoolStartName
?: originalSchoolShortName,
error = registerStudents.exceptionOrNull(),
employeeIds = emptyList(),
studentIds = emptyList(),
parentIds = emptyList(),
subjects = registerStudents.getOrDefault(emptyList()),
)
return students.groupBy { it.unitId }.map { (_, students) ->
val firstStudentFromUnit = students.firstOrNull()
RegisterUnit(
userLoginId = -1,
schoolId = extractedSchoolId,
schoolName = firstStudentFromUnit?.schoolName ?: "Nieznana pełna nazwa szkoły",
schoolShortName = firstStudentFromUnit?.schoolNameShort ?: originalSchoolShortName.orEmpty(),
error = registerStudents.exceptionOrNull(),
employeeIds = emptyList(),
studentIds = emptyList(),
parentIds = emptyList(),
subjects = students,
)
}.ifEmpty {
listOf(
RegisterUnit(
userLoginId = -1,
schoolId = extractedSchoolId,
schoolName = "Nieznana pełna nazwa szkoły",
schoolShortName = originalSchoolShortName.orEmpty(),
error = registerStudents.exceptionOrNull(),
employeeIds = emptyList(),
studentIds = emptyList(),
parentIds = emptyList(),
subjects = registerStudents.getOrDefault(emptyList()),
),
)
}
}
private suspend fun getRegisterUnit(
@ -241,6 +259,14 @@ internal class RegisterRepository(
val isEduOne = isCurrentLoginHasEduOne(studentModuleUrls, url)
val loginResult = runCatching {
val site = when {
isEduOne -> UrlGenerator.Site.STUDENT_PLUS
else -> UrlGenerator.Site.STUDENT
}
loginModule(site)
}
val registerStudents = runCatching {
when {
authInfo?.parentIds.isNullOrEmpty() && authInfo?.studentIds.isNullOrEmpty() -> {
@ -248,17 +274,15 @@ internal class RegisterRepository(
}
else -> when {
isEduOne -> getEduOneDiaries()
else -> {
val (_, startPage) = loginModule(UrlGenerator.Site.STUDENT)
val isParent = isStudentFromParentAccount(startPage)
getStudentDiaries()
.getStudentsFromDiaries(
isParent = isParent,
isEduOne = false,
unitId = unit.id,
)
isEduOne -> {
val (baseStudentPlus, _) = loginResult.getOrThrow()
getEduOneDiaries(baseStudentPlus)
}
else -> getStudentsFromOldModule(
loginResult = loginResult.getOrThrow(),
unitId = unit.id,
)
}
}
}
@ -283,6 +307,26 @@ internal class RegisterRepository(
)
}
private suspend fun getStudentsFromOldModule(
loginResult: Pair<String, String>,
unitId: Int?,
): List<RegisterStudent> {
val (_, startPage) = loginResult
val isParent = isStudentFromParentAccount(startPage)
val diaries = getStudentDiaries()
return diaries.getStudentsFromDiaries(
isParent = isParent,
isEduOne = false,
unitId = unitId
?: diaries.firstOrNull()?.componentUnitId
?: error("Can't find componentUnitId in student diaries"),
).map {
it.copy(
schoolName = getScriptParam("organizationName", startPage),
)
}
}
private suspend fun getStudentDiaries(): List<Diary> = student
.getSchoolInfo(url.generate(UrlGenerator.Site.STUDENT) + "UczenDziennik.mvc/Get")
.handleErrors()
@ -377,9 +421,7 @@ internal class RegisterRepository(
return userCache?.isParent
}
private suspend fun getEduOneDiaries(): List<RegisterStudent> {
val (baseStudentPlus) = loginModule(UrlGenerator.Site.STUDENT_PLUS)
private suspend fun getEduOneDiaries(baseStudentPlus: String): List<RegisterStudent> {
return studentPlus
.getContext(url = baseStudentPlus + "api/Context").students
.map { contextStudent ->

View file

@ -106,7 +106,10 @@ internal class StudentPlusRepository(
isAuthorized = !contextStudent.isAuthorizationRequired,
isEduOne = true, // we already in eduOne context here
studentSecondName = "", //
classId = 0, //
classId = 0,
schoolName = "",
schoolNameShort = null,
unitId = getDecodedKey(contextStudent.key).unitId,
)
}