From a7bb026c1b35bd084eb10c9f2ee392b81f0e7db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 14 Jan 2021 18:37:02 +0100 Subject: [PATCH] Use senderId to differentiate saved recipients between accounts (#1075) --- .../wulkanowy/data/db/dao/RecipientDao.kt | 4 ++-- .../wulkanowy/data/db/entities/Recipient.kt | 2 +- .../data/db/entities/ReportingUnit.kt | 2 +- .../wulkanowy/data/mappers/RecipientMapper.kt | 5 ++--- .../data/mappers/ReportingUnitMapper.kt | 2 +- .../data/repositories/RecipientRepository.kt | 16 +++++++------- .../services/sync/works/RecipientWork.kt | 2 +- .../message/send/SendMessagePresenter.kt | 2 +- .../data/repositories/RecipientLocalTest.kt | 22 +++++++++---------- 9 files changed, 28 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt index abf3beac3..943f3f0cc 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt @@ -9,6 +9,6 @@ import javax.inject.Singleton @Dao interface RecipientDao : BaseDao { - @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 + @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 } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Recipient.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Recipient.kt index 3021da72d..b1f1f3530 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Recipient.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Recipient.kt @@ -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, diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/ReportingUnit.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/ReportingUnit.kt index 601d8aac7..0570a2ffd 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/ReportingUnit.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/ReportingUnit.kt @@ -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, diff --git a/app/src/main/java/io/github/wulkanowy/data/mappers/RecipientMapper.kt b/app/src/main/java/io/github/wulkanowy/data/mappers/RecipientMapper.kt index 71792afa1..9996f6800 100644 --- a/app/src/main/java/io/github/wulkanowy/data/mappers/RecipientMapper.kt +++ b/app/src/main/java/io/github/wulkanowy/data/mappers/RecipientMapper.kt @@ -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.mapToEntities(student: Student) = map { +fun List.mapToEntities(userLoginId: Int) = map { Recipient( - studentId = student.studentId, + userLoginId = userLoginId, realId = it.id, realName = it.name, name = it.shortName, diff --git a/app/src/main/java/io/github/wulkanowy/data/mappers/ReportingUnitMapper.kt b/app/src/main/java/io/github/wulkanowy/data/mappers/ReportingUnitMapper.kt index 95483e46a..71ea7099d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/mappers/ReportingUnitMapper.kt +++ b/app/src/main/java/io/github/wulkanowy/data/mappers/ReportingUnitMapper.kt @@ -7,7 +7,7 @@ import io.github.wulkanowy.sdk.pojo.ReportingUnit as SdkReportingUnit fun List.mapToEntities(student: Student) = map { ReportingUnit( studentId = student.studentId, - realId = it.id, + unitId = it.id, roles = it.roles, senderId = it.senderId, senderName = it.senderName, diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/RecipientRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/RecipientRepository.kt index 1fedb4792..24ab5f0c9 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/RecipientRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/RecipientRepository.kt @@ -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 { - return recipientDb.loadAll(student.studentId, role, unit.realId).ifEmpty { - refreshRecipients(student, role, unit) + suspend fun getRecipients(student: Student, unit: ReportingUnit, role: Int): List { + 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 { - return sdk.init(student).getMessageRecipients(message.messageId, message.senderId).mapToEntities(student) + return sdk.init(student).getMessageRecipients(message.messageId, message.senderId).mapToEntities(student.userLoginId) } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt index dc4cd2a7b..34ab3db04 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt @@ -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) } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt index 471feaae1..77bd0f5e6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt @@ -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) diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/RecipientLocalTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/RecipientLocalTest.kt index 1e645f305..43a361912 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/RecipientLocalTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/RecipientLocalTest.kt @@ -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() }) } }