Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine filter #254

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,50 +278,63 @@ private void UpdateAgentAllFields(Agent agent)
public List<Agent> GetAgents(AgentFilter filter)
{
var agents = new List<Agent>();
IQueryable<AgentDocument> query = _dc.Agents.AsQueryable();
var builder = Builders<AgentDocument>.Filter;
var filters = new List<FilterDefinition<AgentDocument>>() { builder.Empty };

if (!string.IsNullOrEmpty(filter.AgentName))
{
query = query.Where(x => x.Name.ToLower() == filter.AgentName.ToLower());
filters.Add(builder.Eq(x => x.Name, filter.AgentName));
}

if (filter.Disabled.HasValue)
{
query = query.Where(x => x.Disabled == filter.Disabled);
filters.Add(builder.Eq(x => x.Disabled, filter.Disabled.Value));
}

if (filter.AllowRouting.HasValue)
{
query = query.Where(x => x.AllowRouting == filter.AllowRouting);
filters.Add(builder.Eq(x => x.AllowRouting, filter.AllowRouting.Value));
}

if (filter.IsPublic.HasValue)
{
query = query.Where(x => x.IsPublic == filter.IsPublic);
filters.Add(builder.Eq(x => x.IsPublic, filter.IsPublic.Value));
}

if (filter.IsRouter.HasValue)
{
var route = _services.GetRequiredService<RoutingSettings>();
query = filter.IsRouter.Value ?
query.Where(x => route.AgentIds.Contains(x.Id)) :
query.Where(x => !route.AgentIds.Contains(x.Id));
if (filter.IsRouter.Value)
{
filters.Add(builder.In(x => x.Id, route.AgentIds));
}
else
{
filters.Add(builder.Nin(x => x.Id, route.AgentIds));
}
}

if (filter.IsEvaluator.HasValue)
{
var evaluate = _services.GetRequiredService<EvaluatorSetting>();
query = filter.IsEvaluator.Value ?
query.Where(x => x.Id == evaluate.AgentId) :
query.Where(x => x.Id != evaluate.AgentId);
if (filter.IsEvaluator.Value)
{
filters.Add(builder.Eq(x => x.Id, evaluate.AgentId));
}
else
{
filters.Add(builder.Ne(x => x.Id, evaluate.AgentId));
}
}

if (filter.AgentIds != null)
{
query = query.Where(x => filter.AgentIds.Contains(x.Id));
filters.Add(builder.In(x => x.Id, filter.AgentIds));
}

return query.ToList().Select(x => new Agent
var agentDocs = _dc.Agents.Find(builder.And(filters)).ToList();

return agentDocs.Select(x => new Agent
{
Id = x.Id,
Name = x.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,21 @@ public Conversation GetConversation(string conversationId)

public List<Conversation> GetConversations(ConversationFilter filter)
{
var records = new List<Conversation>();
var conversations = new List<Conversation>();
var builder = Builders<ConversationDocument>.Filter;
var filters = new List<FilterDefinition<ConversationDocument>>();
var filters = new List<FilterDefinition<ConversationDocument>>() { builder.Empty };

if (!string.IsNullOrEmpty(filter.AgentId)) filters.Add(builder.Eq(x => x.AgentId, filter.AgentId));
if (!string.IsNullOrEmpty(filter.Status)) filters.Add(builder.Eq(x => x.Status, filter.Status));
if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel));
if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId));

var conversations = _dc.Conversations.Find(builder.And(filters)).ToList();
var conversationDocs = _dc.Conversations.Find(builder.And(filters)).ToList();

foreach (var conv in conversations)
foreach (var conv in conversationDocs)
{
var convId = conv.Id.ToString();
records.Add(new Conversation
conversations.Add(new Conversation
{
Id = convId,
AgentId = conv.AgentId.ToString(),
Expand All @@ -233,7 +233,7 @@ public List<Conversation> GetConversations(ConversationFilter filter)
});
}

return records;
return conversations;
}

public List<Conversation> GetLastConversations()
Expand Down