(socket: AuthenticatedSocket, roomManager: IRoomManager)
| 21 | const logger = createLogger('OperationsHandlers') |
| 22 | |
| 23 | export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager: IRoomManager) { |
| 24 | socket.on('workflow-operation', async (data) => { |
| 25 | const emitOperationError = ( |
| 26 | forbidden: { type: string; message: string; operation?: string; target?: string }, |
| 27 | failed?: { error: string; retryable?: boolean } |
| 28 | ) => { |
| 29 | socket.emit('operation-forbidden', forbidden) |
| 30 | if (failed && data?.operationId) { |
| 31 | socket.emit('operation-failed', { operationId: data.operationId, ...failed }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if (!roomManager.isReady()) { |
| 36 | emitOperationError( |
| 37 | { type: 'ROOM_MANAGER_UNAVAILABLE', message: 'Realtime unavailable' }, |
| 38 | { error: 'Realtime unavailable', retryable: true } |
| 39 | ) |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | let workflowId: string | null = null |
| 44 | let session: UserSession | null = null |
| 45 | |
| 46 | try { |
| 47 | workflowId = await roomManager.getWorkflowIdForSocket(socket.id) |
| 48 | session = await roomManager.getUserSession(socket.id) |
| 49 | } catch (error) { |
| 50 | logger.error('Error loading session for workflow operation:', error) |
| 51 | emitOperationError( |
| 52 | { type: 'ROOM_MANAGER_UNAVAILABLE', message: 'Realtime unavailable' }, |
| 53 | { error: 'Realtime unavailable', retryable: true } |
| 54 | ) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | if (!workflowId || !session) { |
| 59 | emitOperationError( |
| 60 | { type: 'SESSION_ERROR', message: 'Session expired, please rejoin workflow' }, |
| 61 | { error: 'Session expired' } |
| 62 | ) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | let hasRoom = false |
| 67 | try { |
| 68 | hasRoom = await roomManager.hasWorkflowRoom(workflowId) |
| 69 | } catch (error) { |
| 70 | logger.error('Error checking workflow room:', error) |
| 71 | emitOperationError( |
| 72 | { type: 'ROOM_MANAGER_UNAVAILABLE', message: 'Realtime unavailable' }, |
| 73 | { error: 'Realtime unavailable', retryable: true } |
| 74 | ) |
| 75 | return |
| 76 | } |
| 77 | if (!hasRoom) { |
| 78 | emitOperationError( |
| 79 | { type: 'ROOM_NOT_FOUND', message: 'Workflow room not found' }, |
| 80 | { error: 'Workflow room not found' } |
no test coverage detected