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