fix(sync): notify all co-users on inventory mutations (PATCH/DELETE/PUT)
This commit is contained in:
parent
5a5e2548ac
commit
6d5066e26a
2 changed files with 18 additions and 0 deletions
|
|
@ -93,6 +93,15 @@ internal class InventoryRepository {
|
|||
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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -112,6 +112,9 @@ internal fun Route.inventoryRoutes(
|
|||
repository.saveInventory(inventoryId, inventory)
|
||||
val saved = repository.loadInventory(inventoryId)
|
||||
wsManager.notifyFullSyncRequired(userId)
|
||||
repository.getUsersForInventory(inventoryId)
|
||||
.filter { it != userId }
|
||||
.forEach { wsManager.notifyFullSyncRequired(it) }
|
||||
call.respond(HttpStatusCode.OK, saved)
|
||||
}
|
||||
|
||||
|
|
@ -136,6 +139,9 @@ internal fun Route.inventoryRoutes(
|
|||
} else {
|
||||
val item = repository.loadItem(inventoryId, itemId)!!
|
||||
wsManager.notifyInventoryUpdated(userId, itemId)
|
||||
repository.getUsersForInventory(inventoryId)
|
||||
.filter { it != userId }
|
||||
.forEach { wsManager.notifyInventoryUpdated(it, itemId) }
|
||||
call.respond(HttpStatusCode.OK, item)
|
||||
}
|
||||
}
|
||||
|
|
@ -152,6 +158,9 @@ internal fun Route.inventoryRoutes(
|
|||
call.respond(HttpStatusCode.NotFound, ErrorResponse(status = 404, message = "Item not found"))
|
||||
} else {
|
||||
wsManager.notifyInventoryUpdated(userId, itemId)
|
||||
repository.getUsersForInventory(inventoryId)
|
||||
.filter { it != userId }
|
||||
.forEach { wsManager.notifyInventoryUpdated(it, itemId) }
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue