Skip to content

Commit

Permalink
fix: persisting team members connection state (RC) [WPB-5338] (#2191)
Browse files Browse the repository at this point in the history
* fix: persisting team members connection state [WPB-5338]

* format code

* add test for upsertTeamMembers and update doc

* change name

* upgrade test asserts
  • Loading branch information
saleniuk authored Nov 6, 2023
1 parent 5bee6d7 commit d77445c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,57 @@ VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

updateUser:
UPDATE User
SET name = ?, handle = ?, email = ?, phone = ?, accent_id = ?, team = ?, preview_asset_id = ?, complete_asset_id = ?, user_type = ?, bot_service = ?, incomplete_metadata = ?, expires_at = ?
SET name = ?,
handle = ?,
email = ?,
phone = ?,
accent_id = ?,
team = ?,
preview_asset_id = ?,
complete_asset_id = ?,
user_type = ?,
bot_service = ?,
incomplete_metadata = ?,
expires_at = ?
WHERE qualified_id = ?;

updateTeamMemberUser:
UPDATE User
SET name = ?, handle = ?, email = ?, phone = ?, accent_id = ?, team = ?, preview_asset_id = ?, complete_asset_id = ?, bot_service = ?, incomplete_metadata = 0
SET name = ?,
handle = ?,
email = ?,
phone = ?,
accent_id = ?,
team = ?,
connection_status = ?,
preview_asset_id = ?,
complete_asset_id = ?,
bot_service = ?,
incomplete_metadata = 0
WHERE qualified_id = ?;

updateTeamMemberType:
UPDATE User
SET team = ?, connection_status = ?, user_type = ?
SET team = ?,
connection_status = ?,
user_type = ?
WHERE qualified_id = ?;

markUserAsDeleted:
UPDATE User
SET team = NULL , preview_asset_id = NULL, complete_asset_id = NULL, user_type = ?, deleted = 1
SET team = NULL ,
preview_asset_id = NULL,
complete_asset_id = NULL,
user_type = ?,
deleted = 1
WHERE qualified_id = ?;

markUserAsDefederated:
UPDATE User
SET team = NULL , preview_asset_id = NULL, complete_asset_id = NULL, defederated = 1
SET team = NULL,
preview_asset_id = NULL,
complete_asset_id = NULL,
defederated = 1
WHERE qualified_id = ?;

insertOrIgnoreUserId:
Expand All @@ -87,7 +117,12 @@ VALUES(?, 1);

updateSelfUser:
UPDATE User
SET name = ?, handle = ?, email = ?, accent_id = ?, preview_asset_id = ?, complete_asset_id = ?
SET name = ?,
handle = ?,
email = ?,
accent_id = ?,
preview_asset_id = ?,
complete_asset_id = ?
WHERE qualified_id = ?;

insertOrIgnoreUserIdWithConnectionStatus:
Expand Down Expand Up @@ -136,7 +171,10 @@ updateUserhandle:
UPDATE User SET handle = ? WHERE qualified_id = ?;

updateUserAsset:
UPDATE User SET complete_asset_id = ?, preview_asset_id = ? WHERE complete_asset_id = ?;
UPDATE User
SET complete_asset_id = ?,
preview_asset_id = ?
WHERE complete_asset_id = ?;

selectChanges:
SELECT changes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ interface UserDAO {
suspend fun upsertTeamMembersTypes(users: List<UserEntity>)

/**
* This will update all columns, except [UserEntity.userType] or insert a new record with default values
* This will update all columns, except:
* - [UserEntity.availabilityStatus]
* - [UserEntity.userType]
* - [UserEntity.deleted]
* - [UserEntity.hasIncompleteMetadata]
* - [UserEntity.expiresAt]
* - [UserEntity.defederated]
* or insert a new record with default values
* An upsert operation is a one that tries to update a record and if fails (not rows affected by change) inserts instead.
* In this case as the transaction can be executed many times, we need to take care for not deleting old data.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class UserDAOImpl internal constructor(
phone = user.phone,
accent_id = user.accentId,
team = user.team,
connection_status = user.connectionStatus,
preview_asset_id = user.previewAssetId,
complete_asset_id = user.completeAssetId,
bot_service = user.botService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Clock
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -762,6 +764,66 @@ class UserDAOTest : BaseDatabaseTest() {
assertEquals(false, result.defederated)
}

@Test
fun givenExistingTeamMemberUser_whenUpsertingIt_thenAllImportantFieldsAreProperlyUpdated() = runTest(dispatcher) {
val user = user1.copy(
name = "Name",
handle = "Handle",
email = "Email",
phone = "Phone",
accentId = 1,
team = "Team",
connectionStatus = ConnectionEntity.State.ACCEPTED,
previewAssetId = UserAssetIdEntity("PreviewAssetId", "PreviewAssetDomain"),
completeAssetId = UserAssetIdEntity("CompleteAssetId", "CompleteAssetDomain"),
availabilityStatus = UserAvailabilityStatusEntity.AVAILABLE,
userType = UserTypeEntity.STANDARD,
botService = BotIdEntity("BotService", "BotServiceDomain"),
deleted = false,
hasIncompleteMetadata = false,
expiresAt = null,
defederated = false,
)
db.userDAO.insertUser(user)
val updatedTeamMemberUser = user1.copy(
name = "newName",
handle = "newHandle",
email = "newEmail",
phone = "newPhone",
accentId = 2,
team = "newTeam",
connectionStatus = ConnectionEntity.State.PENDING,
previewAssetId = UserAssetIdEntity("newPreviewAssetId", "newPreviewAssetDomain"),
completeAssetId = UserAssetIdEntity("newCompleteAssetId", "newCompleteAssetDomain"),
availabilityStatus = UserAvailabilityStatusEntity.BUSY,
userType = UserTypeEntity.EXTERNAL,
botService = BotIdEntity("newBotService", "newBotServiceDomain"),
deleted = true,
hasIncompleteMetadata = true,
expiresAt = Clock.System.now(),
defederated = true,
)
db.userDAO.upsertTeamMembers(listOf(updatedTeamMemberUser))
val result = db.userDAO.getUserByQualifiedID(user1.id).first()
assertNotNull(result)
assertEquals(updatedTeamMemberUser.name, result.name)
assertEquals(updatedTeamMemberUser.handle, result.handle)
assertEquals(updatedTeamMemberUser.email, result.email)
assertEquals(updatedTeamMemberUser.phone, result.phone)
assertEquals(updatedTeamMemberUser.accentId, result.accentId)
assertEquals(updatedTeamMemberUser.team, result.team)
assertEquals(updatedTeamMemberUser.connectionStatus, result.connectionStatus)
assertEquals(updatedTeamMemberUser.previewAssetId, result.previewAssetId)
assertEquals(updatedTeamMemberUser.completeAssetId, result.completeAssetId)
assertEquals(updatedTeamMemberUser.botService, result.botService)
assertNotEquals(updatedTeamMemberUser.availabilityStatus, result.availabilityStatus)
assertNotEquals(updatedTeamMemberUser.userType, result.userType)
assertNotEquals(updatedTeamMemberUser.deleted, result.deleted)
assertNotEquals(updatedTeamMemberUser.hasIncompleteMetadata, result.hasIncompleteMetadata)
assertNotEquals(updatedTeamMemberUser.expiresAt, result.expiresAt)
assertNotEquals(updatedTeamMemberUser.defederated, result.defederated)
}

private companion object {
val USER_ENTITY_1 = newUserEntity(QualifiedIDEntity("1", "wire.com"))
val USER_ENTITY_2 = newUserEntity(QualifiedIDEntity("2", "wire.com"))
Expand Down

0 comments on commit d77445c

Please sign in to comment.