mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2025-02-20 20:54:44 +01:00
Use senderId to differentiate saved recipients between accounts (#1075)
This commit is contained in:
parent
8ce59a3098
commit
a7bb026c1b
@ -9,6 +9,6 @@ import javax.inject.Singleton
|
||||
@Dao
|
||||
interface RecipientDao : BaseDao<Recipient> {
|
||||
|
||||
@Query("SELECT * FROM Recipients WHERE student_id = :studentId AND role = :role AND unit_id = :unitId")
|
||||
suspend fun loadAll(studentId: Int, role: Int, unitId: Int): List<Recipient>
|
||||
@Query("SELECT * FROM Recipients WHERE student_id = :userLoginId AND unit_id = :unitId AND role = :role")
|
||||
suspend fun loadAll(userLoginId: Int, unitId: Int, role: Int): List<Recipient>
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import java.io.Serializable
|
||||
data class Recipient(
|
||||
|
||||
@ColumnInfo(name = "student_id")
|
||||
val studentId: Int,
|
||||
val userLoginId: Int,
|
||||
|
||||
@ColumnInfo(name = "real_id")
|
||||
val realId: String,
|
||||
|
@ -12,7 +12,7 @@ data class ReportingUnit(
|
||||
val studentId: Int,
|
||||
|
||||
@ColumnInfo(name = "real_id")
|
||||
val realId: Int,
|
||||
val unitId: Int,
|
||||
|
||||
@ColumnInfo(name = "short")
|
||||
val shortName: String,
|
||||
|
@ -1,12 +1,11 @@
|
||||
package io.github.wulkanowy.data.mappers
|
||||
|
||||
import io.github.wulkanowy.data.db.entities.Recipient
|
||||
import io.github.wulkanowy.data.db.entities.Student
|
||||
import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient
|
||||
|
||||
fun List<SdkRecipient>.mapToEntities(student: Student) = map {
|
||||
fun List<SdkRecipient>.mapToEntities(userLoginId: Int) = map {
|
||||
Recipient(
|
||||
studentId = student.studentId,
|
||||
userLoginId = userLoginId,
|
||||
realId = it.id,
|
||||
realName = it.name,
|
||||
name = it.shortName,
|
||||
|
@ -7,7 +7,7 @@ import io.github.wulkanowy.sdk.pojo.ReportingUnit as SdkReportingUnit
|
||||
fun List<SdkReportingUnit>.mapToEntities(student: Student) = map {
|
||||
ReportingUnit(
|
||||
studentId = student.studentId,
|
||||
realId = it.id,
|
||||
unitId = it.id,
|
||||
roles = it.roles,
|
||||
senderId = it.senderId,
|
||||
senderName = it.senderName,
|
||||
|
@ -18,23 +18,23 @@ class RecipientRepository @Inject constructor(
|
||||
private val sdk: Sdk
|
||||
) {
|
||||
|
||||
suspend fun refreshRecipients(student: Student, role: Int, unit: ReportingUnit) {
|
||||
val new = sdk.init(student).getRecipients(unit.realId, role).mapToEntities(student)
|
||||
val old = recipientDb.loadAll(student.studentId, role, unit.realId)
|
||||
suspend fun refreshRecipients(student: Student, unit: ReportingUnit, role: Int) {
|
||||
val new = sdk.init(student).getRecipients(unit.unitId, role).mapToEntities(unit.senderId)
|
||||
val old = recipientDb.loadAll(unit.senderId, unit.unitId, role)
|
||||
|
||||
recipientDb.deleteAll(old uniqueSubtract new)
|
||||
recipientDb.insertAll(new uniqueSubtract old)
|
||||
}
|
||||
|
||||
suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List<Recipient> {
|
||||
return recipientDb.loadAll(student.studentId, role, unit.realId).ifEmpty {
|
||||
refreshRecipients(student, role, unit)
|
||||
suspend fun getRecipients(student: Student, unit: ReportingUnit, role: Int): List<Recipient> {
|
||||
return recipientDb.loadAll(unit.senderId, unit.unitId, role).ifEmpty {
|
||||
refreshRecipients(student, unit, role)
|
||||
|
||||
recipientDb.loadAll(student.studentId, role, unit.realId)
|
||||
recipientDb.loadAll(unit.senderId, unit.unitId, role)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMessageRecipients(student: Student, message: Message): List<Recipient> {
|
||||
return sdk.init(student).getMessageRecipients(message.messageId, message.senderId).mapToEntities(student)
|
||||
return sdk.init(student).getMessageRecipients(message.messageId, message.senderId).mapToEntities(student.userLoginId)
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ class RecipientWork @Inject constructor(
|
||||
|
||||
reportingUnitRepository.getReportingUnits(student).let { units ->
|
||||
units.map {
|
||||
recipientRepository.refreshRecipients(student, 2, it)
|
||||
recipientRepository.refreshRecipients(student, it, 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ class SendMessagePresenter @Inject constructor(
|
||||
|
||||
Timber.i("Loading recipients started")
|
||||
val recipients = when {
|
||||
unit != null -> recipientRepository.getRecipients(student, 2, unit)
|
||||
unit != null -> recipientRepository.getRecipients(student, unit, 2)
|
||||
else -> listOf()
|
||||
}.let { createChips(it) }
|
||||
Timber.i("Loading recipients result: Success, fetched %d recipients", recipients.size)
|
||||
|
@ -46,39 +46,39 @@ class RecipientLocalTest {
|
||||
@Test
|
||||
fun `load recipients when items already in database`() {
|
||||
// prepare
|
||||
coEvery { recipientDb.loadAll(1, 1, 1) } returnsMany listOf(
|
||||
remoteList.mapToEntities(student),
|
||||
remoteList.mapToEntities(student)
|
||||
coEvery { recipientDb.loadAll(4, 123, 7) } returnsMany listOf(
|
||||
remoteList.mapToEntities(4),
|
||||
remoteList.mapToEntities(4)
|
||||
)
|
||||
coEvery { recipientDb.insertAll(any()) } returns listOf(1, 2, 3)
|
||||
coEvery { recipientDb.deleteAll(any()) } just Runs
|
||||
|
||||
// execute
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, 1, ReportingUnit(1, 1, "", 1, "", listOf())) }
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(1, 123, "", 4, "", listOf()), 7) }
|
||||
|
||||
// verify
|
||||
assertEquals(3, res.size)
|
||||
coVerify { recipientDb.loadAll(1, 1, 1) }
|
||||
coVerify { recipientDb.loadAll(4, 123, 7) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `load recipients when database is empty`() {
|
||||
// prepare
|
||||
coEvery { sdk.getRecipients(1, 1) } returns remoteList
|
||||
coEvery { recipientDb.loadAll(1, 1, 1) } returnsMany listOf(
|
||||
coEvery { sdk.getRecipients(123, 7) } returns remoteList
|
||||
coEvery { recipientDb.loadAll(4, 123, 7) } returnsMany listOf(
|
||||
emptyList(),
|
||||
remoteList.mapToEntities(student)
|
||||
remoteList.mapToEntities(4)
|
||||
)
|
||||
coEvery { recipientDb.insertAll(any()) } returns listOf(1, 2, 3)
|
||||
coEvery { recipientDb.deleteAll(any()) } just Runs
|
||||
|
||||
// execute
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, 1, ReportingUnit(1, 1, "", 1, "", listOf())) }
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(1, 123, "", 4, "", listOf()), 7) }
|
||||
|
||||
// verify
|
||||
assertEquals(3, res.size)
|
||||
coVerify { sdk.getRecipients(1, 1) }
|
||||
coVerify { recipientDb.loadAll(1, 1, 1) }
|
||||
coVerify { sdk.getRecipients(123, 7) }
|
||||
coVerify { recipientDb.loadAll(4, 123, 7) }
|
||||
coVerify { recipientDb.insertAll(match { it.isEmpty() }) }
|
||||
coVerify { recipientDb.deleteAll(match { it.isEmpty() }) }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user