All files / utils permissionsChangedPush.js

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 284x                             5x   7x 7x   1x           4x  
const { pushToUserConnections } = require('./webSocketPush')
 
/**
 * Tells members, over their open WebSocket connections, that their roles in a
 * flow changed (roles assigned, role deleted, removed from the flow), so their
 * UI reloads its permissions at once instead of keeping stale ones until the
 * next reload (`{ type: 'permissionsChanged', flowId }`).
 *
 * Best-effort, like every push: a failure is logged, never fails the request.
 * A no-op when the lambda has no WEBSOCKET_API_ENDPOINT (see webSocketPush.js).
 *
 * @param {string[]} userIds
 * @param {string} flowId
 */
async function notifyPermissionsChanged(userIds, flowId) {
  await Promise.all(
    [...new Set(userIds)].map(async (userId) => {
      try {
        await pushToUserConnections(userId, { type: 'permissionsChanged', flowId })
      } catch (error) {
        console.error('Failed to push permissionsChanged:', error)
      }
    })
  )
}
 
module.exports = { notifyPermissionsChanged }