<!--
Please see our guide for opening issues: https://rocket.chat/docs/contr…ibuting/reporting-issues
If you have questions or are looking for help/support please see: https://rocket.chat/docs/getting-support
If you are experiencing a bug please search our issues to be sure it is not already present: https://github.com/RocketChat/Rocket.Chat/issues
-->
### Description:
With routing LiveChat routing method set as Auto-Selection, sometimes when a new room is created, an agent is not assigned to it, leading it to be left there until someone opens the current chats tab and realizes that that chat has not been responded to yet.
### Steps to reproduce:
I was not able to reproduce it consistently, but every new room go through the same steps:
- Get the desired department [using the API](https://developer.rocket.chat/reference/api/rest-api/endpoints/omnichannel/omnichannel-endpoints/department/get-a-list-of-departments)
- Create a visitor [using the API](https://developer.rocket.chat/reference/api/rest-api/endpoints/omnichannel/livechat-endpoints/visitor/register-a-new-livechat-visitor)
- Create a Room for that visitor [using the API](https://developer.rocket.chat/reference/api/rest-api/endpoints/omnichannel/livechat-endpoints/livechat-room/livechat-room-info)
### Expected behavior:
When a new room is opened, an agent should be assigned to it automatically when the routing method is auto-selection.
### Actual behavior:
Sometimes, new rooms are opened without an agent assigned.
### Server Setup Information:
- Version of Rocket.Chat Server: 4.4.0
- Operating System: Ubuntu Debian
- Deployment Method: Docker
- Number of Running Instances: 1
- DB Replicaset Oplog: Enabled
- NodeJS Version: v14.18.2
- MongoDB Version: 4.2.18
### Client Setup Information
- Desktop App or Browser Version: Any
- Operating System: Any
### Additional context
What I was able to see based on the logs provided below is that the first inquiry delegation is for an undefined agent then the queue manager notices that this is invalid and get a new one, this new one returns a valid agentId, but when trying to give that inquiry to that agent it is reported to be offline. If the agent is offline the queue manager should not return it as the next agent.
### Relevant logs:
Here are the logs that I've obtained when a room with this issue was opened.
<details><summary>Logs</summary>
```
{"level":20,"time":"2022-02-23T16:24:06.328Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"New incoming conversation: id: undefined | token: 104f885b-dfad-445b-8412-030d0a31618e"}
{"level":20,"time":"2022-02-23T16:24:06.328Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Attempt to find a department with id/name sDwWMktnE39GQ2xgp"}
{"level":20,"time":"2022-02-23T16:24:06.339Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Assigning visitor 104f885b-dfad-445b-8412-030d0a31618e to department sDwWMktnE39GQ2xgp"}
{"level":20,"time":"2022-02-23T16:24:06.376Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"No matches found. Attempting to create new user with token 104f885b-dfad-445b-8412-030d0a31618e"}
{"level":35,"time":"2022-02-23T16:24:06.432Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"API","method":"POST","url":"/api/v1/livechat/visitor","userId":"sRFYPEx8dNJzMbPav","userAgent":"python-requests/2.25.1","length":"312","host":"----","remoteIP":"----","status":200,"responseTime":121}
{"level":20,"time":"2022-02-23T16:24:06.547Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-on-check-room-params-api for hook livechat.onCheckRoomApiParams"}
{"level":20,"time":"2022-02-23T16:24:06.573Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Attempting to find or create a room for visitor yqkQDkMPhd5exNtky"}
{"level":20,"time":"2022-02-23T16:24:06.576Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-check-default-agent-new-room for hook livechat.checkDefaultAgentOnNewRoom"}
{"level":20,"time":"2022-02-23T16:24:06.577Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Calling QueueManager to request a room for visitor yqkQDkMPhd5exNtky"}
{"level":20,"time":"2022-02-23T16:24:06.577Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"QueueManager","msg":"Requesting a room for guest yqkQDkMPhd5exNtky"}
{"level":20,"time":"2022-02-23T16:24:06.577Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Checking online agents for department sDwWMktnE39GQ2xgp"}
{"level":20,"time":"2022-02-23T16:24:06.582Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Are online agents for department sDwWMktnE39GQ2xgp?: true"}
{"level":20,"time":"2022-02-23T16:24:06.582Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-before-new-room for hook livechat.beforeRoom"}
{"level":20,"time":"2022-02-23T16:24:06.582Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatHelper","msg":"Creating livechat room for visitor yqkQDkMPhd5exNtky"}
{"level":20,"time":"2022-02-23T16:24:06.591Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-send-crm-start-room for hook livechat.newRoom"}
{"level":20,"time":"2022-02-23T16:24:06.592Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-save-last-chat for hook livechat.newRoom"}
{"level":20,"time":"2022-02-23T16:24:06.634Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id mentions for hook beforeSaveMessage"}
{"level":20,"time":"2022-02-23T16:24:06.636Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id filterATAllTag for hook beforeSaveMessage"}
{"level":20,"time":"2022-02-23T16:24:06.637Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id filterATHereTag for hook beforeSaveMessage"}
{"level":20,"time":"2022-02-23T16:24:06.638Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id ReplyEmail for hook beforeSaveMessage"}
{"level":20,"time":"2022-02-23T16:24:06.639Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id jumpToMessage for hook beforeSaveMessage"}
{"level":20,"time":"2022-02-23T16:24:06.639Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id spotify-save for hook beforeSaveMessage"}
{"level":20,"time":"2022-02-23T16:24:06.660Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Integrations","section":"Outgoing WebHook","msg":"Execute Trigger:","arg":"sendMessage"}
{"level":20,"time":"2022-02-23T16:24:06.663Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Integrations","section":"Outgoing WebHook","msg":"Got the event arguments for the event: sendMessage","argObject":{"event":"sendMessage","message":{"t":"livechat-started","msg":"","groupable":false,"ts":"2022-02-23T16:24:06.589Z","u":{"_id":"yqkQDkMPhd5exNtky","username":"guest-5602","name":"Lucas"},"rid":"TsPbZ2MuquDwuxKv3","_id":"mxFqZabdwrDDwXzr5","_updatedAt":"2022-02-23T16:24:06.645Z","urls":[],"mentions":[],"channels":[]},"room":{"_id":"TsPbZ2MuquDwuxKv3","msgs":0,"usersCount":1,"lm":"2022-02-23T16:24:06.582Z","fname":"Lucas","t":"l","ts":"2022-02-23T16:24:06.582Z","departmentId":"sDwWMktnE39GQ2xgp","v":{"_id":"yqkQDkMPhd5exNtky","username":"guest-5602","token":"104f885b-dfad-445b-8412-030d0a31618e","status":"online"},"cl":false,"open":true,"waitingResponse":true,"source":{"type":"api"},"queuedAt":"2022-02-23T16:24:06.582Z","_updatedAt":"2022-02-23T16:24:06.582Z"}}}
{"level":20,"time":"2022-02-23T16:24:06.664Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Integrations","section":"Outgoing WebHook","msg":"Starting search for triggers for the room: TsPbZ2MuquDwuxKv3"}
{"level":20,"time":"2022-02-23T16:24:06.664Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Integrations","section":"Outgoing WebHook","msg":"Found 0 to iterate over and see if the match the event."}
{"level":20,"time":"2022-02-23T16:24:06.681Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"QueueManager","msg":"Room for visitor yqkQDkMPhd5exNtky created with id TsPbZ2MuquDwuxKv3"}
{"level":20,"time":"2022-02-23T16:24:06.682Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-before-new-inquiry for hook livechat.beforeInquiry"}
{"level":20,"time":"2022-02-23T16:24:06.682Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatEnterprise","section":"Callbacks","msg":"Skipping callback. No search param provided"}
{"level":20,"time":"2022-02-23T16:24:06.683Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatHelper","msg":"Creating livechat inquiry for visitor yqkQDkMPhd5exNtky"}
{"level":20,"time":"2022-02-23T16:24:06.685Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id emojione-shortnameToUnicode for hook beforeSendMessageNotifications"}
{"level":20,"time":"2022-02-23T16:24:06.696Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatHelper","msg":"Inquiry YsmeRqc9SfHztaTwY created for visitor yqkQDkMPhd5exNtky"}
{"level":20,"time":"2022-02-23T16:24:06.703Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"QueueManager","msg":"Generated inquiry for visitor yqkQDkMPhd5exNtky with id YsmeRqc9SfHztaTwY [Not queued]"}
{"level":20,"time":"2022-02-23T16:24:06.717Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Delegating Inquiry YsmeRqc9SfHztaTwY"}
{"level":20,"time":"2022-02-23T16:24:06.717Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-before-delegate-agent for hook livechat.beforeDelegateAgent"}
{"level":20,"time":"2022-02-23T16:24:06.718Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Queueing inquiry YsmeRqc9SfHztaTwY"}
{"level":20,"time":"2022-02-23T16:24:06.718Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatHelper","msg":"Notifying agents of new inquiry YsmeRqc9SfHztaTwY queued"}
{"level":20,"time":"2022-02-23T16:24:06.720Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"QueueManager","msg":"Delegating inquiry with id YsmeRqc9SfHztaTwY to agent undefined"}
{"level":20,"time":"2022-02-23T16:24:06.720Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-before-routing-chat for hook livechat.beforeRouteChat"}
{"level":20,"time":"2022-02-23T16:24:06.720Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Checking online agents for department sDwWMktnE39GQ2xgp"}
{"level":20,"time":"2022-02-23T16:24:06.721Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-send-crm-room-queued for hook livechat.chatQueued"}
{"level":20,"time":"2022-02-23T16:24:06.724Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Are online agents for department sDwWMktnE39GQ2xgp?: true"}
{"level":20,"time":"2022-02-23T16:24:06.724Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatEnterprise","section":"Callbacks","msg":"Skipping callback. Waiting queue disabled by setting"}
{"level":20,"time":"2022-02-23T16:24:06.725Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"QueueManager","msg":"Inquiry with id YsmeRqc9SfHztaTwY is ready. Delegating to agent undefined"}
{"level":20,"time":"2022-02-23T16:24:06.725Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Attempting to delegate inquiry YsmeRqc9SfHztaTwY"}
{"level":20,"time":"2022-02-23T16:24:06.725Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Agent offline or invalid. Using routing method to get next agent for inquiry YsmeRqc9SfHztaTwY"}
{"level":20,"time":"2022-02-23T16:24:06.725Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Getting next available agent with method Auto_Selection"}
{"level":20,"time":"2022-02-23T16:24:06.730Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Routing method returned agent vTnEynAfAGJAFdhZg for inquiry YsmeRqc9SfHztaTwY"}
{"level":20,"time":"2022-02-23T16:24:06.730Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Inquiry YsmeRqc9SfHztaTwY will be taken by agent vTnEynAfAGJAFdhZg"}
{"level":20,"time":"2022-02-23T16:24:06.730Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Attempting to take Inquiry YsmeRqc9SfHztaTwY [Agent vTnEynAfAGJAFdhZg] "}
{"level":20,"time":"2022-02-23T16:24:06.735Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Callbacks","msg":"Executing callback with id livechat-before-take-inquiry for hook livechat.checkAgentBeforeTakeInquiry"}
{"level":20,"time":"2022-02-23T16:24:06.737Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"LivechatEnterprise","section":"Callbacks","msg":"Callback with error. provided agent is not online"}
{"level":20,"time":"2022-02-23T16:24:06.737Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"RoutingManager","msg":"Cannot take Inquiry YsmeRqc9SfHztaTwY: Precondition failed for agent"}
{"level":20,"time":"2022-02-23T16:24:06.737Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"QueueManager","msg":"Inquiry YsmeRqc9SfHztaTwY queued"}
{"level":20,"time":"2022-02-23T16:24:06.738Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"Livechat","msg":"Room obtained for visitor yqkQDkMPhd5exNtky -> TsPbZ2MuquDwuxKv3"}
{"level":35,"time":"2022-02-23T16:24:06.740Z","pid":1,"hostname":"rocketchat-c457ffdf-d8cm5","name":"","name":"API","method":"GET","url":"/api/v1/livechat/room?token=104f885b-dfad-445b-8412-030d0a31618e","userId":"sRFYPEx8dNJzMbPav","userAgent":"python-requests/2.25.1","host":"----","remoteIP":"----","status":200,"responseTime":193}
```
</details>