/* * * (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. * */ using System; using System.Collections.Generic; using System.Linq; using ASC.Api.CRM; using ASC.Common.Web; using ASC.CRM.ApiModels; using ASC.CRM.Core; using ASC.CRM.Core.Dao; using ASC.CRM.Core.Entities; using ASC.Web.Api.Models; using ASC.Web.Api.Routing; using AutoMapper; using Microsoft.AspNetCore.Mvc; namespace ASC.CRM.Api { public class TaskTemplateController : BaseApiController { private readonly EmployeeWraperHelper _employeeWraperHelper; public TaskTemplateController(CrmSecurity crmSecurity, DaoFactory daoFactory, EmployeeWraperHelper employeeWraperHelper, IMapper mapper) : base(daoFactory, crmSecurity, mapper) { _employeeWraperHelper = employeeWraperHelper; } /// /// Creates a new task template container with the type and title specified in the request /// /// Type /// Title /// Create task template container /// Task Templates /// /// Task template container /// /// /// false [Create(@"{entityType:regex(contact|person|company|opportunity|case)}/tasktemplatecontainer")] public TaskTemplateContainerDto CreateTaskTemplateContainer([FromRoute] string entityType, [FromBody] string title) { if (string.IsNullOrEmpty(title)) throw new ArgumentException(); var taskTemplateContainer = new TaskTemplateContainer { EntityType = ToEntityType(entityType), Title = title }; taskTemplateContainer.ID = _daoFactory.GetTaskTemplateContainerDao().SaveOrUpdate(taskTemplateContainer); return ToTaskTemplateContainerDto(taskTemplateContainer); } /// /// Returns the complete list of all the task template containers available on the portal /// /// Type /// Get task template container list /// Task Templates /// /// Task template container list /// /// false [Read(@"{entityType:regex(contact|person|company|opportunity|case)}/tasktemplatecontainer")] public IEnumerable GetTaskTemplateContainers(string entityType) { return ToTaskListTemplateContainerDto(_daoFactory.GetTaskTemplateContainerDao().GetItems(ToEntityType(entityType))); } /// /// Deletes the task template container with the ID specified in the request /// /// Task template container ID /// Delete task template container /// Task Templates /// /// Deleted task template container /// /// /// /// false [Delete(@"tasktemplatecontainer/{containerid:int}")] public TaskTemplateContainerDto DeleteTaskTemplateContainer(int containerid) { if (containerid <= 0) throw new ArgumentException(); var result = ToTaskTemplateContainerDto(_daoFactory.GetTaskTemplateContainerDao().GetByID(containerid)); if (result == null) throw new ItemNotFoundException(); _daoFactory.GetTaskTemplateContainerDao().Delete(containerid); return result; } /// /// Updates the task template container with the ID specified in the request /// /// Task template container ID /// Title /// Update task template container /// Task Templates /// /// Task template container /// /// /// /// false [Update(@"tasktemplatecontainer/{containerid:int}")] public TaskTemplateContainerDto UpdateTaskTemplateContainer(int containerid, string title) { 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); return ToTaskTemplateContainerDto(result); } /// /// Returns the detailed information on the task template container with the ID specified in the request /// /// Task template container ID /// Get task template container by ID /// Task Templates /// /// Task template container /// /// /// /// false [Read(@"tasktemplatecontainer/{containerid:int}")] public TaskTemplateContainerDto GetTaskTemplateContainerByID(int containerid) { if (containerid <= 0) throw new ArgumentException(); var item = _daoFactory.GetTaskTemplateContainerDao().GetByID(containerid); if (item == null) throw new ItemNotFoundException(); return ToTaskTemplateContainerDto(item); } /// /// Returns the list of all tasks in the container with the ID specified in the request /// /// Task template container ID /// Get task template list by contaier ID /// Task Templates /// /// Task template list /// /// /// /// false [Read(@"tasktemplatecontainer/{containerid:int}/tasktemplate")] public IEnumerable GetTaskTemplates(int containerid) { if (containerid <= 0) throw new ArgumentException(); var container = _daoFactory.GetTaskTemplateContainerDao().GetByID(containerid); if (container == null) throw new ItemNotFoundException(); return _daoFactory.GetTaskTemplateDao().GetList(containerid).ConvertAll(ToTaskTemplateDto); } /// /// Creates a new task template with the parameters specified in the request in the container with the selected ID /// /// Task template container ID /// Title /// Description /// Responsible ID /// Category ID /// Responsible notification: notify or not /// Ticks offset /// /// Create task template /// Task Templates /// Task template /// /// /// false [Create(@"tasktemplatecontainer/{containerid:int}/tasktemplate")] public TaskTemplateDto CreateTaskTemplate( [FromRoute] int containerid, [FromBody] CreateOrUpdateTaskTemplateRequestDto inDto) { var title = inDto.Title; var categoryid = inDto.Categoryid; var deadLineIsFixed = inDto.DeadLineIsFixed; var description = inDto.Description; var isNotify = inDto.isNotify; var responsibleid = inDto.Responsibleid; var offsetTicks = inDto.OffsetTicks; 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 { CategoryID = categoryid, ContainerID = containerid, DeadLineIsFixed = deadLineIsFixed, Description = description, isNotify = isNotify, ResponsibleID = responsibleid, Title = title, Offset = TimeSpan.FromTicks(offsetTicks) }; item.ID = _daoFactory.GetTaskTemplateDao().SaveOrUpdate(item); return ToTaskTemplateDto(item); } /// /// Updates the selected task template with the parameters specified in the request in the container with the selected ID /// /// Task template ID /// Task template container ID /// Title /// Description /// Responsible ID /// Category ID /// Responsible notification: notify or not /// Ticks offset /// /// Update task template /// Task Templates /// Task template /// /// /// false [Update(@"tasktemplatecontainer/{containerid:int}/tasktemplate/{id:int}")] public TaskTemplateDto UpdateTaskTemplate( [FromRoute] int id, [FromRoute] int containerid, [FromBody] CreateOrUpdateTaskTemplateRequestDto inDto) { var title = inDto.Title; var categoryid = inDto.Categoryid; var deadLineIsFixed = inDto.DeadLineIsFixed; var description = inDto.Description; var isNotify = inDto.isNotify; var responsibleid = inDto.Responsibleid; var offsetTicks = inDto.OffsetTicks; 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 { CategoryID = categoryid, ContainerID = containerid, DeadLineIsFixed = deadLineIsFixed, Description = description, isNotify = isNotify, ResponsibleID = responsibleid, Title = title, ID = id, Offset = TimeSpan.FromTicks(offsetTicks) }; item.ID = _daoFactory.GetTaskTemplateDao().SaveOrUpdate(item); return ToTaskTemplateDto(item); } /// /// Deletes the task template with the ID specified in the request /// /// Task template ID /// Delete task template /// Task Templates /// Task template /// /// /// false [Delete(@"tasktemplatecontainer/tasktemplate/{id:int}")] public TaskTemplateDto DeleteTaskTemplate(int id) { if (id <= 0) throw new ArgumentException(); var taskTemplate = _daoFactory.GetTaskTemplateDao().GetByID(id); if (taskTemplate == null) throw new ItemNotFoundException(); var result = ToTaskTemplateDto(taskTemplate); _daoFactory.GetTaskTemplateDao().Delete(id); return result; } /// /// Return the task template with the ID specified in the request /// /// Task template ID /// Get task template by ID /// Task Templates /// Task template /// /// /// false [Read(@"tasktemplatecontainer/tasktemplate/{id:int}")] public TaskTemplateDto GetTaskTemplateByID(int id) { if (id <= 0) throw new ArgumentException(); var taskTemplate = _daoFactory.GetTaskTemplateDao().GetByID(id); if (taskTemplate == null) throw new ItemNotFoundException(); return ToTaskTemplateDto(taskTemplate); } protected TaskTemplateDto ToTaskTemplateDto(TaskTemplate taskTemplate) { // TODO: set task template category return new TaskTemplateDto { // Category = GetTaskCategoryByID(taskTemplate.CategoryID), 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) }; } protected IEnumerable ToTaskListTemplateContainerDto(IEnumerable items) { var result = new List(); var taskTemplateDictionary = _daoFactory.GetTaskTemplateDao().GetAll() .GroupBy(item => item.ContainerID) .ToDictionary(x => x.Key, y => y.Select(ToTaskTemplateDto)); foreach (var item in items) { var taskTemplateContainer = new TaskTemplateContainerDto { Title = item.Title, EntityType = item.EntityType.ToString(), Id = item.ID }; if (taskTemplateDictionary.ContainsKey(taskTemplateContainer.Id)) { taskTemplateContainer.Items = taskTemplateDictionary[taskTemplateContainer.Id]; } result.Add(taskTemplateContainer); } return result; } protected TaskTemplateContainerDto ToTaskTemplateContainerDto(TaskTemplateContainer item) { return ToTaskListTemplateContainerDto(new List { item }).FirstOrDefault(); } } }