Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: persisting team members connection state (RC) [WPB-5338] #2191

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +28,7 @@ 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
Expand Down Expand Up @@ -762,6 +763,68 @@ 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()
assertTrue {
result != null &&
result.name == updatedTeamMemberUser.name &&
result.handle == updatedTeamMemberUser.handle &&
result.email == updatedTeamMemberUser.email &&
result.phone == updatedTeamMemberUser.phone &&
result.accentId == updatedTeamMemberUser.accentId &&
result.team == updatedTeamMemberUser.team &&
result.connectionStatus == updatedTeamMemberUser.connectionStatus &&
result.previewAssetId == updatedTeamMemberUser.previewAssetId &&
result.completeAssetId == updatedTeamMemberUser.completeAssetId &&
result.availabilityStatus != updatedTeamMemberUser.availabilityStatus && // should not be updated
result.userType != updatedTeamMemberUser.userType && // should not be updated
result.botService == updatedTeamMemberUser.botService &&
result.deleted != updatedTeamMemberUser.deleted && // should not be updated
result.hasIncompleteMetadata != updatedTeamMemberUser.hasIncompleteMetadata && // should not be updated
result.expiresAt != updatedTeamMemberUser.expiresAt && // should not be updated
result.defederated != updatedTeamMemberUser.defederated // should not be updated
}
saleniuk marked this conversation as resolved.
Show resolved Hide resolved
}

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