fix(sync): notify all co-users on inventory mutations (PATCH/DELETE/PUT)

This commit is contained in:
Jens Reinemann 2026-05-18 18:53:21 +02:00
parent 5a5e2548ac
commit 6d5066e26a
2 changed files with 18 additions and 0 deletions

View file

@ -93,6 +93,15 @@ internal class InventoryRepository {
Inventories.deleteWhere { Inventories.id eq inventoryId } Inventories.deleteWhere { Inventories.id eq inventoryId }
} }
/**
* Returns all userIds that are assigned to the given inventory.
*/
fun getUsersForInventory(inventoryId: String): List<String> = transaction {
Users.selectAll()
.where { Users.inventoryId eq inventoryId }
.map { it[Users.id] }
}
/** /**
* Returns all inventories visible to the given user, marking the user's active one. * Returns all inventories visible to the given user, marking the user's active one.
*/ */

View file

@ -112,6 +112,9 @@ internal fun Route.inventoryRoutes(
repository.saveInventory(inventoryId, inventory) repository.saveInventory(inventoryId, inventory)
val saved = repository.loadInventory(inventoryId) val saved = repository.loadInventory(inventoryId)
wsManager.notifyFullSyncRequired(userId) wsManager.notifyFullSyncRequired(userId)
repository.getUsersForInventory(inventoryId)
.filter { it != userId }
.forEach { wsManager.notifyFullSyncRequired(it) }
call.respond(HttpStatusCode.OK, saved) call.respond(HttpStatusCode.OK, saved)
} }
@ -136,6 +139,9 @@ internal fun Route.inventoryRoutes(
} else { } else {
val item = repository.loadItem(inventoryId, itemId)!! val item = repository.loadItem(inventoryId, itemId)!!
wsManager.notifyInventoryUpdated(userId, itemId) wsManager.notifyInventoryUpdated(userId, itemId)
repository.getUsersForInventory(inventoryId)
.filter { it != userId }
.forEach { wsManager.notifyInventoryUpdated(it, itemId) }
call.respond(HttpStatusCode.OK, item) call.respond(HttpStatusCode.OK, item)
} }
} }
@ -152,6 +158,9 @@ internal fun Route.inventoryRoutes(
call.respond(HttpStatusCode.NotFound, ErrorResponse(status = 404, message = "Item not found")) call.respond(HttpStatusCode.NotFound, ErrorResponse(status = 404, message = "Item not found"))
} else { } else {
wsManager.notifyInventoryUpdated(userId, itemId) wsManager.notifyInventoryUpdated(userId, itemId)
repository.getUsersForInventory(inventoryId)
.filter { it != userId }
.forEach { wsManager.notifyInventoryUpdated(it, itemId) }
call.respond(HttpStatusCode.NoContent) call.respond(HttpStatusCode.NoContent)
} }
} }