DocSpace-client/products/ASC.CRM/Server/Api/TasksController.cs

715 lines
27 KiB
C#
Raw Normal View History

2021-03-10 15:38:56 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using ASC.Api.Core;
using ASC.Api.CRM;
2020-04-16 19:41:37 +00:00
using ASC.Common.Web;
2021-03-10 15:38:56 +00:00
using ASC.CRM.ApiModels;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core;
using ASC.CRM.Core.Dao;
using ASC.CRM.Core.Entities;
using ASC.CRM.Core.Enums;
using ASC.CRM.Resources;
using ASC.MessagingSystem;
using ASC.Web.Api.Routing;
2021-06-02 15:24:49 +00:00
using ASC.Web.CRM.Core.Search;
2020-04-16 19:41:37 +00:00
using ASC.Web.CRM.Services.NotifyService;
2021-03-02 16:29:07 +00:00
2021-03-17 11:50:13 +00:00
using AutoMapper;
2021-03-24 15:55:47 +00:00
using Microsoft.AspNetCore.Mvc;
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
public class TasksController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-17 11:50:13 +00:00
private readonly NotifyClient _notifyClient;
private readonly ApiContext _apiContext;
private readonly MessageService _messageService;
private readonly MessageTarget _messageTarget;
2021-05-05 14:09:05 +00:00
public TasksController(CrmSecurity crmSecurity,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory,
ApiContext apiContext,
MessageTarget messageTarget,
MessageService messageService,
NotifyClient notifyClient,
2021-06-02 15:24:49 +00:00
IMapper mapper,
FactoryIndexerCase factoryIndexerCase)
2021-04-06 17:33:28 +00:00
: base(daoFactory, crmSecurity, mapper)
2021-03-10 15:38:56 +00:00
{
2021-03-17 11:50:13 +00:00
_apiContext = apiContext;
_messageTarget = messageTarget;
_messageService = messageService;
_notifyClient = notifyClient;
_mapper = mapper;
2021-03-10 15:38:56 +00:00
}
2021-03-19 16:56:26 +00:00
2020-04-16 19:41:37 +00:00
/// <summary>
/// Returns the detailed information about the task with the ID specified in the request
/// </summary>
/// <param name="taskid">Task ID</param>
/// <returns>Task</returns>
/// <short>Get task by ID</short>
/// <category>Tasks</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
[Read(@"task/{taskid:int}")]
2021-03-09 15:37:16 +00:00
public TaskDto GetTaskByID(int taskid)
2020-04-16 19:41:37 +00:00
{
if (taskid <= 0) throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var task = _daoFactory.GetTaskDao().GetByID(taskid);
2021-06-02 15:24:49 +00:00
2020-04-16 19:41:37 +00:00
if (task == null) throw new ItemNotFoundException();
2021-03-19 16:56:26 +00:00
if (!_crmSecurity.CanAccessTo(task))
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity.CreateSecurityException();
2020-04-16 19:41:37 +00:00
}
2021-03-17 11:50:13 +00:00
return _mapper.Map<TaskDto>(task);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of tasks matching the creteria specified in the request
/// </summary>
/// <param optional="true" name="responsibleid">Task responsible</param>
/// <param optional="true" name="categoryid">Task category ID</param>
/// <param optional="true" name="isClosed">Show open or closed tasks only</param>
/// <param optional="true" name="fromDate">Earliest task due date</param>
/// <param optional="true" name="toDate">Latest task due date</param>
/// <param name="entityType" remark="Allowed values: opportunity, contact or case">Related entity type</param>
/// <param name="entityid">Related entity ID</param>
/// <exception cref="ArgumentException"></exception>
/// <short>Get task list</short>
/// <category>Tasks</category>
/// <returns>
/// Task list
/// </returns>
[Read(@"task/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable<TaskDto> GetAllTasks(
2020-04-16 19:41:37 +00:00
Guid responsibleid,
int categoryid,
bool? isClosed,
ApiDateTime fromDate,
ApiDateTime toDate,
string entityType,
int entityid)
{
TaskSortedByType taskSortedByType;
if (!string.IsNullOrEmpty(entityType) &&
!(
string.Compare(entityType, "contact", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(entityType, "opportunity", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(entityType, "case", StringComparison.OrdinalIgnoreCase) == 0)
)
throw new ArgumentException();
2021-03-17 11:50:13 +00:00
var searchText = _apiContext.FilterValue;
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
IEnumerable<TaskDto> result;
2020-04-16 19:41:37 +00:00
OrderBy taskOrderBy;
2021-03-17 11:50:13 +00:00
if (ASC.CRM.Classes.EnumExtension.TryParse(_apiContext.SortBy, true, out taskSortedByType))
2020-04-16 19:41:37 +00:00
{
2021-03-17 11:50:13 +00:00
taskOrderBy = new OrderBy(taskSortedByType, !_apiContext.SortDescending);
2020-04-16 19:41:37 +00:00
}
2021-03-17 11:50:13 +00:00
else if (string.IsNullOrEmpty(_apiContext.SortBy))
2020-04-16 19:41:37 +00:00
{
taskOrderBy = new OrderBy(TaskSortedByType.DeadLine, true);
}
else
{
taskOrderBy = null;
}
2021-03-17 11:50:13 +00:00
var fromIndex = (int)_apiContext.StartIndex;
var count = (int)_apiContext.Count;
2020-04-16 19:41:37 +00:00
if (taskOrderBy != null)
{
2021-03-09 15:37:16 +00:00
result = ToTaskListDto(
2021-03-19 16:56:26 +00:00
_daoFactory.GetTaskDao()
2020-04-16 19:41:37 +00:00
.GetTasks(
searchText,
responsibleid,
categoryid,
isClosed,
fromDate,
toDate,
ToEntityType(entityType),
entityid,
fromIndex,
count,
taskOrderBy)).ToList();
2021-03-17 11:50:13 +00:00
_apiContext.SetDataPaginated();
_apiContext.SetDataFiltered();
_apiContext.SetDataSorted();
2020-04-16 19:41:37 +00:00
}
else
2021-03-09 15:37:16 +00:00
result = ToTaskListDto(
2021-03-19 16:56:26 +00:00
_daoFactory
2020-04-16 19:41:37 +00:00
.GetTaskDao()
.GetTasks(
searchText,
responsibleid,
categoryid,
isClosed,
fromDate,
toDate,
ToEntityType(entityType),
entityid,
0,
0, null)).ToList();
int totalCount;
if (result.Count() < count)
{
totalCount = fromIndex + result.Count();
}
else
{
2021-03-19 16:56:26 +00:00
totalCount = _daoFactory
2020-04-16 19:41:37 +00:00
.GetTaskDao()
.GetTasksCount(
searchText,
responsibleid,
categoryid,
isClosed,
fromDate,
toDate,
ToEntityType(entityType),
entityid);
}
2021-03-17 11:50:13 +00:00
_apiContext.SetTotalCount(totalCount);
2020-04-16 19:41:37 +00:00
return result;
}
/// <summary>
/// Open anew the task with the ID specified in the request
/// </summary>
/// <short>Resume task</short>
/// <category>Tasks</category>
/// <param name="taskid">Task ID</param>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Task
/// </returns>
[Update(@"task/{taskid:int}/reopen")]
2021-03-09 15:37:16 +00:00
public TaskDto ReOpenTask(int taskid)
2020-04-16 19:41:37 +00:00
{
if (taskid <= 0) throw new ArgumentException();
2021-03-19 16:56:26 +00:00
_daoFactory.GetTaskDao().OpenTask(taskid);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var task = _daoFactory.GetTaskDao().GetByID(taskid);
2020-04-16 19:41:37 +00:00
2021-03-17 11:50:13 +00:00
_messageService.Send(MessageAction.CrmTaskOpened, _messageTarget.Create(task.ID), task.Title);
2020-04-16 19:41:37 +00:00
2021-03-17 11:50:13 +00:00
return _mapper.Map<TaskDto>(task);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Close the task with the ID specified in the request
/// </summary>
/// <short>Close task</short>
/// <category>Tasks</category>
/// <param name="taskid">Task ID</param>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Task
/// </returns>
[Update(@"task/{taskid:int}/close")]
2021-03-09 15:37:16 +00:00
public TaskDto CloseTask(int taskid)
2020-04-16 19:41:37 +00:00
{
if (taskid <= 0) throw new ArgumentException();
2021-03-19 16:56:26 +00:00
_daoFactory.GetTaskDao().CloseTask(taskid);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var task = _daoFactory.GetTaskDao().GetByID(taskid);
2021-03-17 11:50:13 +00:00
_messageService.Send(MessageAction.CrmTaskClosed, _messageTarget.Create(task.ID), task.Title);
2020-04-16 19:41:37 +00:00
2021-03-17 11:50:13 +00:00
return _mapper.Map<TaskDto>(task);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Delete the task with the ID specified in the request
/// </summary>
/// <short>Delete task</short>
/// <category>Tasks</category>
/// <param name="taskid">Task ID</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Deleted task
/// </returns>
[Delete(@"task/{taskid:int}")]
2021-03-09 15:37:16 +00:00
public TaskDto DeleteTask(int taskid)
2020-04-16 19:41:37 +00:00
{
if (taskid <= 0) throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var task = _daoFactory.GetTaskDao().GetByID(taskid);
2020-04-16 19:41:37 +00:00
if (task == null) throw new ItemNotFoundException();
2021-03-19 16:56:26 +00:00
_daoFactory.GetTaskDao().DeleteTask(taskid);
2021-03-17 11:50:13 +00:00
_messageService.Send(MessageAction.CrmTaskDeleted, _messageTarget.Create(task.ID), task.Title);
2020-04-16 19:41:37 +00:00
2021-03-17 11:50:13 +00:00
return _mapper.Map<TaskDto>(task);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates the task with the parameters (title, description, due date, etc.) specified in the request
/// </summary>
/// <param name="title">Task title</param>
/// <param optional="true" name="description">Task description</param>
/// <param name="deadline">Task due date</param>
/// <param name="responsibleId">Task responsible ID</param>
/// <param name="categoryId">Task category ID</param>
/// <param optional="true" name="contactId">Contact ID</param>
/// <param optional="true" name="entityType" remark="Allowed values: opportunity or case">Related entity type</param>
/// <param optional="true" name="entityId">Related entity ID</param>
/// <param optional="true" name="isNotify">Notify the responsible about the task</param>
/// <param optional="true" name="alertValue">Time period in minutes for reminder to the responsible about the task</param>
/// <exception cref="ArgumentException"></exception>
/// <short>Create task</short>
/// <category>Tasks</category>
/// <returns>Task</returns>
[Create(@"task")]
2021-03-09 15:37:16 +00:00
public TaskDto CreateTask(
2021-03-24 15:55:47 +00:00
[FromForm] string title,
[FromForm] string description,
[FromForm] ApiDateTime deadline,
[FromForm] Guid responsibleId,
[FromForm] int categoryId,
[FromForm] int contactId,
[FromForm] string entityType,
[FromForm] int entityId,
[FromForm] bool isNotify,
[FromForm] int alertValue
2020-04-16 19:41:37 +00:00
)
{
if (!string.IsNullOrEmpty(entityType) &&
!(
string.Compare(entityType, "opportunity", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(entityType, "case", StringComparison.OrdinalIgnoreCase) == 0
)
|| categoryId <= 0)
throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var listItem = _daoFactory.GetListItemDao().GetByID(categoryId);
2020-04-16 19:41:37 +00:00
if (listItem == null) throw new ItemNotFoundException(CRMErrorsResource.TaskCategoryNotFound);
var task = new Task
{
Title = title,
Description = description,
ResponsibleID = responsibleId,
CategoryID = categoryId,
DeadLine = deadline,
ContactID = contactId,
EntityType = ToEntityType(entityType),
EntityID = entityId,
IsClosed = false,
AlertValue = alertValue
};
2021-03-19 16:56:26 +00:00
task = _daoFactory.GetTaskDao().SaveOrUpdateTask(task);
2020-04-16 19:41:37 +00:00
if (isNotify)
{
Contact taskContact = null;
Cases taskCase = null;
Deal taskDeal = null;
if (task.ContactID > 0)
{
2021-03-19 16:56:26 +00:00
taskContact = _daoFactory.GetContactDao().GetByID(task.ContactID);
2020-04-16 19:41:37 +00:00
}
if (task.EntityID > 0)
{
switch (task.EntityType)
{
case EntityType.Case:
2021-03-19 16:56:26 +00:00
taskCase = _daoFactory.GetCasesDao().GetByID(task.EntityID);
2020-04-16 19:41:37 +00:00
break;
case EntityType.Opportunity:
2021-03-19 16:56:26 +00:00
taskDeal = _daoFactory.GetDealDao().GetByID(task.EntityID);
2020-04-16 19:41:37 +00:00
break;
}
}
2021-03-17 11:50:13 +00:00
_notifyClient.SendAboutResponsibleByTask(task, listItem.Title, taskContact, taskCase, taskDeal, null);
2020-04-16 19:41:37 +00:00
}
2021-03-17 11:50:13 +00:00
_messageService.Send(MessageAction.CrmTaskCreated, _messageTarget.Create(task.ID), task.Title);
2020-04-16 19:41:37 +00:00
2021-03-17 11:50:13 +00:00
return _mapper.Map<TaskDto>(task);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates the group of the same task with the parameters (title, description, due date, etc.) specified in the request for several contacts
/// </summary>
/// <param name="title">Task title</param>
/// <param optional="true" name="description">Task description</param>
/// <param name="deadline">Task due date</param>
/// <param name="responsibleId">Task responsible ID</param>
/// <param name="categoryId">Task category ID</param>
/// <param name="contactId">contact ID list</param>
/// <param optional="true" name="entityType" remark="Allowed values: opportunity or case">Related entity type</param>
/// <param optional="true" name="entityId">Related entity ID</param>
/// <param optional="true" name="isNotify">Notify the responsible about the task</param>
/// <param optional="true" name="alertValue">Time period in minutes for reminder to the responsible about the task</param>
/// <exception cref="ArgumentException"></exception>
/// <short>Create task list</short>
/// <category>Tasks</category>
/// <returns>Tasks</returns>
/// <visible>false</visible>
[Create(@"contact/task/group")]
2021-03-09 15:37:16 +00:00
public IEnumerable<TaskDto> CreateTaskGroup(
2021-03-24 15:55:47 +00:00
[FromForm] string title,
[FromForm] string description,
[FromForm] ApiDateTime deadline,
[FromForm] Guid responsibleId,
[FromForm] int categoryId,
[FromForm] int[] contactId,
[FromForm] string entityType,
[FromForm] int entityId,
[FromForm] bool isNotify,
[FromForm] int alertValue)
2020-04-16 19:41:37 +00:00
{
var tasks = new List<Task>();
if (
!string.IsNullOrEmpty(entityType) &&
!(string.Compare(entityType, "opportunity", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(entityType, "case", StringComparison.OrdinalIgnoreCase) == 0)
)
throw new ArgumentException();
foreach (var cid in contactId)
{
tasks.Add(new Task
{
Title = title,
Description = description,
ResponsibleID = responsibleId,
CategoryID = categoryId,
DeadLine = deadline,
ContactID = cid,
EntityType = ToEntityType(entityType),
EntityID = entityId,
IsClosed = false,
AlertValue = alertValue
});
}
2021-03-19 16:56:26 +00:00
tasks = _daoFactory.GetTaskDao().SaveOrUpdateTaskList(tasks).ToList();
2020-04-16 19:41:37 +00:00
string taskCategory = null;
if (isNotify)
{
if (categoryId > 0)
{
2021-03-19 16:56:26 +00:00
var listItem = _daoFactory.GetListItemDao().GetByID(categoryId);
2020-04-16 19:41:37 +00:00
if (listItem == null) throw new ItemNotFoundException();
taskCategory = listItem.Title;
}
}
for (var i = 0; i < tasks.Count; i++)
{
if (!isNotify) continue;
Contact taskContact = null;
Cases taskCase = null;
Deal taskDeal = null;
if (tasks[i].ContactID > 0)
{
2021-03-19 16:56:26 +00:00
taskContact = _daoFactory.GetContactDao().GetByID(tasks[i].ContactID);
2020-04-16 19:41:37 +00:00
}
if (tasks[i].EntityID > 0)
{
switch (tasks[i].EntityType)
{
case EntityType.Case:
2021-03-19 16:56:26 +00:00
taskCase = _daoFactory.GetCasesDao().GetByID(tasks[i].EntityID);
2020-04-16 19:41:37 +00:00
break;
case EntityType.Opportunity:
2021-03-19 16:56:26 +00:00
taskDeal = _daoFactory.GetDealDao().GetByID(tasks[i].EntityID);
2020-04-16 19:41:37 +00:00
break;
}
}
2021-03-17 11:50:13 +00:00
_notifyClient.SendAboutResponsibleByTask(tasks[i], taskCategory, taskContact, taskCase, taskDeal, null);
2020-04-16 19:41:37 +00:00
}
if (tasks.Any())
{
2021-03-19 16:56:26 +00:00
var contacts = _daoFactory.GetContactDao().GetContacts(contactId);
2020-04-16 19:41:37 +00:00
var task = tasks.First();
2021-03-17 11:50:13 +00:00
_messageService.Send(MessageAction.ContactsCreatedCrmTasks, _messageTarget.Create(tasks.Select(x => x.ID)), contacts.Select(x => x.GetTitle()), task.Title);
2020-04-16 19:41:37 +00:00
}
2021-03-09 15:37:16 +00:00
return ToTaskListDto(tasks);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the selected task with the parameters (title, description, due date, etc.) specified in the request
/// </summary>
/// <param name="taskid">Task ID</param>
/// <param name="title">Task title</param>
/// <param name="description">Task description</param>
/// <param name="deadline">Task due date</param>
/// <param name="responsibleid">Task responsible ID</param>
/// <param name="categoryid">Task category ID</param>
/// <param name="contactid">Contact ID</param>
/// <param name="entityType" remark="Allowed values: opportunity or case">Related entity type</param>
/// <param name="entityid">Related entity ID</param>
/// <param name="isNotify">Notify or not</param>
/// <param optional="true" name="alertValue">Time period in minutes for reminder to the responsible about the task</param>
/// <short> Update task</short>
/// <category>Tasks</category>
/// <returns>Task</returns>
[Update(@"task/{taskid:int}")]
2021-03-09 15:37:16 +00:00
public TaskDto UpdateTask(
2020-04-16 19:41:37 +00:00
int taskid,
string title,
string description,
ApiDateTime deadline,
Guid responsibleid,
int categoryid,
int contactid,
string entityType,
int entityid,
bool isNotify,
int alertValue)
{
if (!string.IsNullOrEmpty(entityType) &&
!(string.Compare(entityType, "opportunity", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(entityType, "case", StringComparison.OrdinalIgnoreCase) == 0
) || categoryid <= 0)
throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var listItem = _daoFactory.GetListItemDao().GetByID(categoryid);
2020-04-16 19:41:37 +00:00
if (listItem == null) throw new ItemNotFoundException(CRMErrorsResource.TaskCategoryNotFound);
var task = new Task
{
ID = taskid,
Title = title,
Description = description,
DeadLine = deadline,
AlertValue = alertValue,
ResponsibleID = responsibleid,
CategoryID = categoryid,
ContactID = contactid,
EntityID = entityid,
EntityType = ToEntityType(entityType)
};
2021-03-19 16:56:26 +00:00
task = _daoFactory.GetTaskDao().SaveOrUpdateTask(task);
2020-04-16 19:41:37 +00:00
if (isNotify)
{
Contact taskContact = null;
Cases taskCase = null;
Deal taskDeal = null;
if (task.ContactID > 0)
{
2021-03-19 16:56:26 +00:00
taskContact = _daoFactory.GetContactDao().GetByID(task.ContactID);
2020-04-16 19:41:37 +00:00
}
if (task.EntityID > 0)
{
switch (task.EntityType)
{
case EntityType.Case:
2021-03-19 16:56:26 +00:00
taskCase = _daoFactory.GetCasesDao().GetByID(task.EntityID);
2020-04-16 19:41:37 +00:00
break;
case EntityType.Opportunity:
2021-03-19 16:56:26 +00:00
taskDeal = _daoFactory.GetDealDao().GetByID(task.EntityID);
2020-04-16 19:41:37 +00:00
break;
}
}
2021-03-17 11:50:13 +00:00
_notifyClient.SendAboutResponsibleByTask(task, listItem.Title, taskContact, taskCase, taskDeal, null);
2020-04-16 19:41:37 +00:00
}
2021-03-17 11:50:13 +00:00
_messageService.Send(MessageAction.CrmTaskUpdated, _messageTarget.Create(task.ID), task.Title);
2020-04-16 19:41:37 +00:00
2021-03-17 11:50:13 +00:00
return _mapper.Map<TaskDto>(task);
2020-04-16 19:41:37 +00:00
}
/// <visible>false</visible>
[Update(@"task/{taskid:int}/creationdate")]
public void SetTaskCreationDate(int taskId, ApiDateTime creationDate)
{
2021-03-19 16:56:26 +00:00
var dao = _daoFactory.GetTaskDao();
2020-04-16 19:41:37 +00:00
var task = dao.GetByID(taskId);
2021-03-19 16:56:26 +00:00
if (task == null || !_crmSecurity.CanAccessTo(task))
2020-04-16 19:41:37 +00:00
throw new ItemNotFoundException();
dao.SetTaskCreationDate(taskId, creationDate);
}
/// <visible>false</visible>
[Update(@"task/{taskid:int}/lastmodifeddate")]
public void SetTaskLastModifedDate(int taskId, ApiDateTime lastModifedDate)
{
2021-03-19 16:56:26 +00:00
var dao = _daoFactory.GetTaskDao();
2020-04-16 19:41:37 +00:00
var task = dao.GetByID(taskId);
2021-03-19 16:56:26 +00:00
if (task == null || !_crmSecurity.CanAccessTo(task))
2020-04-16 19:41:37 +00:00
throw new ItemNotFoundException();
dao.SetTaskLastModifedDate(taskId, lastModifedDate);
}
2021-03-09 15:37:16 +00:00
private IEnumerable<TaskDto> ToTaskListDto(IEnumerable<Task> itemList)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
var result = new List<TaskDto>();
2020-04-16 19:41:37 +00:00
var contactIDs = new List<int>();
var taskIDs = new List<int>();
var categoryIDs = new List<int>();
2021-03-09 15:37:16 +00:00
var entityDtosIDs = new Dictionary<EntityType, List<int>>();
2020-04-16 19:41:37 +00:00
foreach (var item in itemList)
{
taskIDs.Add(item.ID);
if (!categoryIDs.Contains(item.CategoryID))
{
categoryIDs.Add(item.CategoryID);
}
if (item.ContactID > 0 && !contactIDs.Contains(item.ContactID))
{
contactIDs.Add(item.ContactID);
}
if (item.EntityID > 0)
{
if (item.EntityType != EntityType.Opportunity && item.EntityType != EntityType.Case) continue;
2021-03-09 15:37:16 +00:00
if (!entityDtosIDs.ContainsKey(item.EntityType))
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
entityDtosIDs.Add(item.EntityType, new List<int>
2020-04-16 19:41:37 +00:00
{
item.EntityID
});
}
2021-03-09 15:37:16 +00:00
else if (!entityDtosIDs[item.EntityType].Contains(item.EntityID))
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
entityDtosIDs[item.EntityType].Add(item.EntityID);
2020-04-16 19:41:37 +00:00
}
}
}
2021-03-09 15:37:16 +00:00
var entityDtos = new Dictionary<string, EntityDto>();
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
foreach (var entityType in entityDtosIDs.Keys)
2020-04-16 19:41:37 +00:00
{
switch (entityType)
{
case EntityType.Opportunity:
2021-03-19 16:56:26 +00:00
_daoFactory.GetDealDao().GetDeals(entityDtosIDs[entityType].Distinct().ToArray())
2020-04-16 19:41:37 +00:00
.ForEach(item =>
{
if (item == null) return;
2021-03-09 15:37:16 +00:00
entityDtos.Add(
2020-04-16 19:41:37 +00:00
string.Format("{0}_{1}", (int)entityType, item.ID),
2021-03-09 15:37:16 +00:00
new EntityDto
2020-04-16 19:41:37 +00:00
{
EntityId = item.ID,
EntityTitle = item.Title,
EntityType = "opportunity"
});
});
break;
case EntityType.Case:
2021-03-19 16:56:26 +00:00
_daoFactory.GetCasesDao().GetByID(entityDtosIDs[entityType].ToArray())
2020-04-16 19:41:37 +00:00
.ForEach(item =>
{
if (item == null) return;
2021-03-09 15:37:16 +00:00
entityDtos.Add(
2020-04-16 19:41:37 +00:00
string.Format("{0}_{1}", (int)entityType, item.ID),
2021-03-09 15:37:16 +00:00
new EntityDto
2020-04-16 19:41:37 +00:00
{
EntityId = item.ID,
EntityTitle = item.Title,
EntityType = "case"
});
});
break;
}
}
2021-03-19 16:56:26 +00:00
var categories = _daoFactory.GetListItemDao().GetItems(categoryIDs.ToArray()).ToDictionary(x => x.ID, x => _mapper.Map<TaskCategoryDto>(x));
var contacts = _daoFactory.GetContactDao().GetContacts(contactIDs.ToArray()).ToDictionary(item => item.ID, x => _mapper.Map<ContactBaseWithEmailDto>(x));
var restrictedContacts = _daoFactory.GetContactDao().GetRestrictedContacts(contactIDs.ToArray()).ToDictionary(item => item.ID, x => _mapper.Map<ContactBaseWithEmailDto>(x));
2020-04-16 19:41:37 +00:00
foreach (var item in itemList)
{
2021-03-19 16:56:26 +00:00
var taskDto = _mapper.Map<TaskDto>(item);
2021-03-17 11:50:13 +00:00
2021-03-19 16:56:26 +00:00
taskDto.CanEdit = _crmSecurity.CanEdit(item);
2020-04-16 19:41:37 +00:00
if (contacts.ContainsKey(item.ContactID))
{
2021-03-09 15:37:16 +00:00
taskDto.Contact = contacts[item.ContactID];
2020-04-16 19:41:37 +00:00
}
2021-03-01 17:36:18 +00:00
2020-04-16 19:41:37 +00:00
if (restrictedContacts.ContainsKey(item.ContactID))
{
2021-03-09 15:37:16 +00:00
taskDto.Contact = restrictedContacts[item.ContactID];
2020-04-16 19:41:37 +00:00
/*Hide some fields. Should be refactored! */
2021-03-09 15:37:16 +00:00
taskDto.Contact.Currency = null;
taskDto.Contact.Email = null;
taskDto.Contact.AccessList = null;
2020-04-16 19:41:37 +00:00
}
if (item.EntityID > 0)
{
var entityStrKey = string.Format("{0}_{1}", (int)item.EntityType, item.EntityID);
2021-03-09 15:37:16 +00:00
if (entityDtos.ContainsKey(entityStrKey))
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
taskDto.Entity = entityDtos[entityStrKey];
2020-04-16 19:41:37 +00:00
}
}
if (categories.ContainsKey(item.CategoryID))
{
2021-03-09 15:37:16 +00:00
taskDto.Category = categories[item.CategoryID];
2020-04-16 19:41:37 +00:00
}
2021-03-09 15:37:16 +00:00
result.Add(taskDto);
2020-04-16 19:41:37 +00:00
}
return result;
}
}
}