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

408 lines
17 KiB
C#
Raw Normal View History

2020-04-16 19:41:37 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
2021-03-09 15:37:16 +00:00
using ASC.CRM.ApiModels;
2020-04-16 19:41:37 +00:00
using ASC.Common.Web;
using ASC.CRM.Core.Entities;
using ASC.Web.Api.Routing;
2021-03-02 16:29:07 +00:00
2020-04-16 19:41:37 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-03-10 15:38:56 +00:00
using ASC.Api.CRM;
using ASC.CRM.Core;
using ASC.Api.Core;
using ASC.CRM.Core.Dao;
using ASC.Web.CRM.Services.NotifyService;
using ASC.MessagingSystem;
using ASC.Web.Api.Models;
namespace ASC.CRM.Api
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public class TaskTemplateController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public TaskTemplateController(CRMSecurity cRMSecurity,
DaoFactory daoFactory,
EmployeeWraperHelper employeeWraperHelper)
: base(daoFactory, cRMSecurity)
{
EmployeeWraperHelper = employeeWraperHelper;
}
public EmployeeWraperHelper EmployeeWraperHelper { get; }
2020-04-16 19:41:37 +00:00
/// <summary>
/// Creates a new task template container with the type and title specified in the request
/// </summary>
/// <param name="entityType">Type</param>
/// <param name="title">Title</param>
/// <short>Create task template container</short>
/// <category>Task Templates</category>
/// <returns>
/// Task template container
/// </returns>
/// <exception cref="ArgumentException"></exception>
/// <visible>false</visible>
2021-03-02 16:29:07 +00:00
[Create(@"{entityType:regex(contact|person|company|opportunity|case)}/tasktemplatecontainer")]
2021-03-09 15:37:16 +00:00
public TaskTemplateContainerDto CreateTaskTemplateContainer(string entityType, string title)
2020-04-16 19:41:37 +00:00
{
if (string.IsNullOrEmpty(title)) throw new ArgumentException();
var taskTemplateContainer = new TaskTemplateContainer
2021-03-02 16:29:07 +00:00
{
EntityType = ToEntityType(entityType),
Title = title
};
2020-04-16 19:41:37 +00:00
taskTemplateContainer.ID = DaoFactory.GetTaskTemplateContainerDao().SaveOrUpdate(taskTemplateContainer);
2021-03-09 15:37:16 +00:00
return ToTaskTemplateContainerDto(taskTemplateContainer);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the complete list of all the task template containers available on the portal
/// </summary>
/// <param name="entityType">Type</param>
/// <short>Get task template container list</short>
/// <category>Task Templates</category>
/// <returns>
/// Task template container list
/// </returns>
/// <visible>false</visible>
2021-03-02 16:29:07 +00:00
[Read(@"{entityType:regex(contact|person|company|opportunity|case)}/tasktemplatecontainer")]
2021-03-09 15:37:16 +00:00
public IEnumerable<TaskTemplateContainerDto> GetTaskTemplateContainers(string entityType)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
return ToTaskListTemplateContainerDto(DaoFactory.GetTaskTemplateContainerDao().GetItems(ToEntityType(entityType)));
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the task template container with the ID specified in the request
/// </summary>
/// <param name="containerid">Task template container ID</param>
/// <short>Delete task template container</short>
/// <category>Task Templates</category>
/// <returns>
/// Deleted task template container
/// </returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Delete(@"tasktemplatecontainer/{containerid:int}")]
2021-03-09 15:37:16 +00:00
public TaskTemplateContainerDto DeleteTaskTemplateContainer(int containerid)
2020-04-16 19:41:37 +00:00
{
if (containerid <= 0) throw new ArgumentException();
2021-03-09 15:37:16 +00:00
var result = ToTaskTemplateContainerDto(DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid));
2020-04-16 19:41:37 +00:00
if (result == null) throw new ItemNotFoundException();
DaoFactory.GetTaskTemplateContainerDao().Delete(containerid);
return result;
}
/// <summary>
/// Updates the task template container with the ID specified in the request
/// </summary>
/// <param name="containerid">Task template container ID</param>
/// <param name="title">Title</param>
/// <short>Update task template container</short>
/// <category>Task Templates</category>
/// <returns>
/// Task template container
/// </returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Update(@"tasktemplatecontainer/{containerid:int}")]
2021-03-09 15:37:16 +00:00
public TaskTemplateContainerDto UpdateTaskTemplateContainer(int containerid, string title)
2020-04-16 19:41:37 +00:00
{
if (containerid <= 0 || string.IsNullOrEmpty(title)) throw new ArgumentException();
var result = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (result == null) throw new ItemNotFoundException();
result.Title = title;
DaoFactory.GetTaskTemplateContainerDao().SaveOrUpdate(result);
2021-03-09 15:37:16 +00:00
return ToTaskTemplateContainerDto(result);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the detailed information on the task template container with the ID specified in the request
/// </summary>
/// <param name="containerid">Task template container ID</param>
/// <short>Get task template container by ID</short>
/// <category>Task Templates</category>
/// <returns>
/// Task template container
/// </returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Read(@"tasktemplatecontainer/{containerid:int}")]
2021-03-09 15:37:16 +00:00
public TaskTemplateContainerDto GetTaskTemplateContainerByID(int containerid)
2020-04-16 19:41:37 +00:00
{
if (containerid <= 0) throw new ArgumentException();
var item = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (item == null) throw new ItemNotFoundException();
2021-03-09 15:37:16 +00:00
return ToTaskTemplateContainerDto(item);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of all tasks in the container with the ID specified in the request
/// </summary>
/// <param name="containerid">Task template container ID</param>
/// <short>Get task template list by contaier ID</short>
/// <category>Task Templates</category>
/// <returns>
/// Task template list
/// </returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Read(@"tasktemplatecontainer/{containerid:int}/tasktemplate")]
2021-03-09 15:37:16 +00:00
public IEnumerable<TaskTemplateDto> GetTaskTemplates(int containerid)
2020-04-16 19:41:37 +00:00
{
if (containerid <= 0) throw new ArgumentException();
var container = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (container == null) throw new ItemNotFoundException();
2021-03-09 15:37:16 +00:00
return DaoFactory.GetTaskTemplateDao().GetList(containerid).ConvertAll(ToTaskTemplateDto);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates a new task template with the parameters specified in the request in the container with the selected ID
/// </summary>
/// <param name="containerid">Task template container ID</param>
/// <param name="title">Title</param>
/// <param name="description">Description</param>
/// <param name="responsibleid">Responsible ID</param>
/// <param name="categoryid">Category ID</param>
/// <param name="isNotify">Responsible notification: notify or not</param>
/// <param name="offsetTicks">Ticks offset</param>
/// <param name="deadLineIsFixed"></param>
/// <short>Create task template</short>
/// <category>Task Templates</category>
/// <returns>Task template</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Create(@"tasktemplatecontainer/{containerid:int}/tasktemplate")]
2021-03-09 15:37:16 +00:00
public TaskTemplateDto CreateTaskTemplate(
2020-04-16 19:41:37 +00:00
int containerid,
string title,
string description,
Guid responsibleid,
int categoryid,
bool isNotify,
long offsetTicks,
bool deadLineIsFixed
)
{
if (containerid <= 0 || string.IsNullOrEmpty(title) || categoryid <= 0) throw new ArgumentException();
var container = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (container == null) throw new ItemNotFoundException();
var item = new TaskTemplate
2021-03-02 16:29:07 +00:00
{
CategoryID = categoryid,
ContainerID = containerid,
DeadLineIsFixed = deadLineIsFixed,
Description = description,
isNotify = isNotify,
ResponsibleID = responsibleid,
Title = title,
Offset = TimeSpan.FromTicks(offsetTicks)
};
2020-04-16 19:41:37 +00:00
item.ID = DaoFactory.GetTaskTemplateDao().SaveOrUpdate(item);
2021-03-09 15:37:16 +00:00
return ToTaskTemplateDto(item);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the selected task template with the parameters specified in the request in the container with the selected ID
/// </summary>
/// <param name="id">Task template ID</param>
/// <param name="containerid">Task template container ID</param>
/// <param name="title">Title</param>
/// <param name="description">Description</param>
/// <param name="responsibleid">Responsible ID</param>
/// <param name="categoryid">Category ID</param>
/// <param name="isNotify">Responsible notification: notify or not</param>
/// <param name="offsetTicks">Ticks offset</param>
/// <param name="deadLineIsFixed"></param>
/// <short>Update task template</short>
/// <category>Task Templates</category>
/// <returns>Task template</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Update(@"tasktemplatecontainer/{containerid:int}/tasktemplate")]
2021-03-09 15:37:16 +00:00
public TaskTemplateDto UpdateTaskTemplate(
2020-04-16 19:41:37 +00:00
int id,
int containerid,
string title,
string description,
Guid responsibleid,
int categoryid,
bool isNotify,
long offsetTicks,
bool deadLineIsFixed
)
{
if (containerid <= 0 || string.IsNullOrEmpty(title) || categoryid <= 0) throw new ArgumentException();
var updatingItem = DaoFactory.GetTaskTemplateDao().GetByID(id);
if (updatingItem == null) throw new ItemNotFoundException();
var container = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (container == null) throw new ItemNotFoundException();
var item = new TaskTemplate
2021-03-02 16:29:07 +00:00
{
CategoryID = categoryid,
ContainerID = containerid,
DeadLineIsFixed = deadLineIsFixed,
Description = description,
isNotify = isNotify,
ResponsibleID = responsibleid,
Title = title,
ID = id,
Offset = TimeSpan.FromTicks(offsetTicks)
};
2020-04-16 19:41:37 +00:00
item.ID = DaoFactory.GetTaskTemplateDao().SaveOrUpdate(item);
2021-03-09 15:37:16 +00:00
return ToTaskTemplateDto(item);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the task template with the ID specified in the request
/// </summary>
/// <param name="id">Task template ID</param>
/// <short>Delete task template</short>
/// <category>Task Templates</category>
/// <returns>Task template</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Delete(@"tasktemplatecontainer/tasktemplate/{id:int}")]
2021-03-09 15:37:16 +00:00
public TaskTemplateDto DeleteTaskTemplate(int id)
2020-04-16 19:41:37 +00:00
{
if (id <= 0) throw new ArgumentException();
var taskTemplate = DaoFactory.GetTaskTemplateDao().GetByID(id);
if (taskTemplate == null) throw new ItemNotFoundException();
2021-03-09 15:37:16 +00:00
var result = ToTaskTemplateDto(taskTemplate);
2020-04-16 19:41:37 +00:00
DaoFactory.GetTaskTemplateDao().Delete(id);
return result;
}
/// <summary>
/// Return the task template with the ID specified in the request
/// </summary>
/// <param name="id">Task template ID</param>
/// <short>Get task template by ID</short>
/// <category>Task Templates</category>
/// <returns>Task template</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <visible>false</visible>
[Read(@"tasktemplatecontainer/tasktemplate/{id:int}")]
2021-03-09 15:37:16 +00:00
public TaskTemplateDto GetTaskTemplateByID(int id)
2020-04-16 19:41:37 +00:00
{
if (id <= 0) throw new ArgumentException();
var taskTemplate = DaoFactory.GetTaskTemplateDao().GetByID(id);
if (taskTemplate == null) throw new ItemNotFoundException();
2021-03-09 15:37:16 +00:00
return ToTaskTemplateDto(taskTemplate);
2020-04-16 19:41:37 +00:00
}
2021-03-09 15:37:16 +00:00
protected TaskTemplateDto ToTaskTemplateDto(TaskTemplate taskTemplate)
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
// TODO: set task template category
2021-03-09 15:37:16 +00:00
return new TaskTemplateDto
2021-03-02 16:29:07 +00:00
{
2021-03-10 15:38:56 +00:00
// Category = GetTaskCategoryByID(taskTemplate.CategoryID),
2021-03-02 16:29:07 +00:00
ContainerID = taskTemplate.ContainerID,
DeadLineIsFixed = taskTemplate.DeadLineIsFixed,
Description = taskTemplate.Description,
Id = taskTemplate.ID,
isNotify = taskTemplate.isNotify,
Title = taskTemplate.Title,
OffsetTicks = taskTemplate.Offset.Ticks,
Responsible = EmployeeWraperHelper.Get(taskTemplate.ResponsibleID)
};
2020-04-16 19:41:37 +00:00
}
2021-03-09 15:37:16 +00:00
protected IEnumerable<TaskTemplateContainerDto> ToTaskListTemplateContainerDto(IEnumerable<TaskTemplateContainer> items)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
var result = new List<TaskTemplateContainerDto>();
2020-04-16 19:41:37 +00:00
var taskTemplateDictionary = DaoFactory.GetTaskTemplateDao().GetAll()
.GroupBy(item => item.ContainerID)
2021-03-09 15:37:16 +00:00
.ToDictionary(x => x.Key, y => y.Select(ToTaskTemplateDto));
2020-04-16 19:41:37 +00:00
foreach (var item in items)
{
2021-03-09 15:37:16 +00:00
var taskTemplateContainer = new TaskTemplateContainerDto
2021-03-02 16:29:07 +00:00
{
Title = item.Title,
EntityType = item.EntityType.ToString(),
Id = item.ID
};
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
if (taskTemplateDictionary.ContainsKey(taskTemplateContainer.Id))
2020-04-16 19:41:37 +00:00
{
2020-04-22 20:46:49 +00:00
taskTemplateContainer.Items = taskTemplateDictionary[taskTemplateContainer.Id];
2020-04-16 19:41:37 +00:00
}
result.Add(taskTemplateContainer);
}
return result;
}
2021-03-09 15:37:16 +00:00
protected TaskTemplateContainerDto ToTaskTemplateContainerDto(TaskTemplateContainer item)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
return ToTaskListTemplateContainerDto(new List<TaskTemplateContainer>
2020-04-16 19:41:37 +00:00
{
item
}).FirstOrDefault();
}
}
}