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-19 16:56:26 +00:00
using System ;
2021-04-06 17:33:28 +00:00
using System.Collections ;
2021-03-19 16:56:26 +00:00
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
2021-04-06 17:33:28 +00:00
using System.Net.Mime ;
2021-03-19 16:56:26 +00:00
using System.Text ;
using System.Text.RegularExpressions ;
2020-04-18 17:49:09 +00:00
using ASC.Api.Core ;
2021-03-19 16:56:26 +00:00
using ASC.Api.CRM ;
2020-04-18 17:49:09 +00:00
using ASC.Api.Documents ;
2020-04-16 19:41:37 +00:00
using ASC.Common.Web ;
2021-04-06 17:33:28 +00:00
using ASC.Core ;
2021-03-19 16:56:26 +00:00
using ASC.CRM.ApiModels ;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core ;
2021-03-19 16:56:26 +00:00
using ASC.CRM.Core.Dao ;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core.Entities ;
using ASC.CRM.Core.Enums ;
using ASC.MessagingSystem ;
using ASC.Web.Api.Routing ;
2021-04-06 17:33:28 +00:00
using ASC.Web.CRM.Services.NotifyService ;
using ASC.Web.Files.Classes ;
2020-04-16 19:41:37 +00:00
using ASC.Web.Files.Utils ;
2021-03-02 16:29:07 +00:00
2021-03-19 16:56:26 +00:00
using AutoMapper ;
2021-03-02 16:29:07 +00:00
2021-04-06 17:33:28 +00:00
using Microsoft.AspNetCore.Http ;
2021-03-24 15:55:47 +00:00
using Microsoft.AspNetCore.Mvc ;
2020-04-16 19:41:37 +00:00
using OrderBy = ASC . CRM . Core . Entities . OrderBy ;
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public class RelationshipEventsController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
private readonly FileUploader _fileUploader ;
private readonly ASC . Files . Core . Data . DaoFactory _filesDaoFactory ;
private readonly FileWrapperHelper _fileWrapperHelper ;
2021-04-06 17:33:28 +00:00
private readonly FilesSettingsHelper _filesSettingsHelper ;
2021-03-23 15:41:56 +00:00
private readonly ApiContext _apiContext ;
private readonly MessageService _messageService ;
private readonly MessageTarget _messageTarget ;
2021-04-06 17:33:28 +00:00
private readonly SecurityContext _securityContext ;
private readonly NotifyClient _notifyClient ;
2021-03-23 15:41:56 +00:00
2021-04-06 17:33:28 +00:00
public RelationshipEventsController (
2021-05-05 14:09:05 +00:00
CrmSecurity crmSecurity ,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory ,
ApiContext apiContext ,
MessageTarget messageTarget ,
MessageService messageService ,
FileWrapperHelper fileWrapperHelper ,
ASC . Files . Core . Data . DaoFactory filesDaoFactory ,
FileUploader fileUploader ,
2021-04-06 17:33:28 +00:00
SecurityContext securityContext ,
NotifyClient notifyClient ,
FilesSettingsHelper filesSettingsHelper ,
2021-03-19 16:56:26 +00:00
IMapper mapper )
2021-04-06 17:33:28 +00:00
: base ( daoFactory , crmSecurity , mapper )
2021-03-10 15:38:56 +00:00
{
2021-03-23 15:41:56 +00:00
_apiContext = apiContext ;
_messageTarget = messageTarget ;
_messageService = messageService ;
_fileWrapperHelper = fileWrapperHelper ;
_filesDaoFactory = filesDaoFactory ;
_fileUploader = fileUploader ;
2021-04-06 17:33:28 +00:00
_securityContext = securityContext ;
_notifyClient = notifyClient ;
_filesSettingsHelper = filesSettingsHelper ;
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 list of all events matching the parameters specified in the request
/// </summary>
/// <short>
/// Get event list
/// </short>
/// <category>History</category>
/// <param optional="true" name="entityType" remark="Allowed values: opportunity, contact or case">Related entity type</param>
/// <param optional="true" name="entityId">Related entity ID</param>
/// <param optional="true" name="categoryId">Task category ID</param>
/// <param optional="true" name="createBy">Event author</param>
/// <param optional="true" name="fromDate">Earliest task due date</param>
/// <param optional="true" name="toDate">Latest task due date</param>
/// <returns>
/// Event list
/// </returns>
[Read(@"history/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable < RelationshipEventDto > GetHistory (
2020-04-16 19:41:37 +00:00
string entityType ,
int entityId ,
int categoryId ,
Guid createBy ,
ApiDateTime fromDate ,
ApiDateTime toDate )
{
var entityTypeObj = ToEntityType ( entityType ) ;
2021-03-02 16:29:07 +00:00
switch ( entityTypeObj )
{
2020-04-16 19:41:37 +00:00
case EntityType . Contact :
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( entityId ) ;
if ( contact = = null | | ! _crmSecurity . CanAccessTo ( contact ) )
2020-04-16 19:41:37 +00:00
throw new ItemNotFoundException ( ) ;
break ;
case EntityType . Case :
2021-03-19 16:56:26 +00:00
var cases = _daoFactory . GetCasesDao ( ) . GetByID ( entityId ) ;
if ( cases = = null | | ! _crmSecurity . CanAccessTo ( cases ) )
2020-04-16 19:41:37 +00:00
throw new ItemNotFoundException ( ) ;
break ;
case EntityType . Opportunity :
2021-03-19 16:56:26 +00:00
var deal = _daoFactory . GetDealDao ( ) . GetByID ( entityId ) ;
if ( deal = = null | | ! _crmSecurity . CanAccessTo ( deal ) )
2020-04-16 19:41:37 +00:00
throw new ItemNotFoundException ( ) ;
break ;
default :
if ( entityId ! = 0 )
{
throw new ArgumentException ( ) ;
}
break ;
}
RelationshipEventByType eventByType ;
2021-03-09 15:37:16 +00:00
IEnumerable < RelationshipEventDto > result ;
2020-04-16 19:41:37 +00:00
OrderBy eventOrderBy ;
2021-03-23 15:41:56 +00:00
if ( ASC . CRM . Classes . EnumExtension . TryParse ( _apiContext . SortBy , true , out eventByType ) )
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
eventOrderBy = new OrderBy ( eventByType , ! _apiContext . SortDescending ) ;
2020-04-16 19:41:37 +00:00
}
2021-03-23 15:41:56 +00:00
else if ( string . IsNullOrEmpty ( _apiContext . SortBy ) )
2020-04-16 19:41:37 +00:00
{
eventOrderBy = new OrderBy ( RelationshipEventByType . Created , false ) ;
}
else
{
eventOrderBy = null ;
}
if ( eventOrderBy ! = null )
{
2021-03-19 16:56:26 +00:00
result = ToListRelationshipEventDto ( _daoFactory . GetRelationshipEventDao ( ) . GetItems (
2021-03-23 15:41:56 +00:00
_apiContext . FilterValue ,
2020-04-16 19:41:37 +00:00
entityTypeObj ,
entityId ,
createBy ,
categoryId ,
fromDate ,
toDate ,
2021-03-23 15:41:56 +00:00
( int ) _apiContext . StartIndex ,
( int ) _apiContext . Count ,
2020-04-16 19:41:37 +00:00
eventOrderBy ) ) ;
2021-03-23 15:41:56 +00:00
_apiContext . SetDataPaginated ( ) ;
_apiContext . SetDataFiltered ( ) ;
_apiContext . SetDataSorted ( ) ;
2020-04-16 19:41:37 +00:00
}
else
{
2021-03-19 16:56:26 +00:00
result = ToListRelationshipEventDto ( _daoFactory . GetRelationshipEventDao ( ) . GetItems (
2021-03-23 15:41:56 +00:00
_apiContext . FilterValue ,
2020-04-16 19:41:37 +00:00
entityTypeObj ,
entityId ,
createBy ,
categoryId ,
fromDate ,
toDate ,
0 ,
0 ,
null ) ) ;
}
2020-04-18 17:49:09 +00:00
return result ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the event with the ID specified in the request and all the files associated with this event
/// </summary>
/// <short>
/// Delete event and related files
/// </short>
/// <category>History</category>
/// <param name="id">Event ID</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Event
/// </returns>
[Delete(@"history/{id:int}")]
2021-03-09 15:37:16 +00:00
public RelationshipEventDto DeleteHistory ( int id )
2020-04-16 19:41:37 +00:00
{
if ( id < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var item = _daoFactory . GetRelationshipEventDao ( ) . GetByID ( id ) ;
2020-04-16 19:41:37 +00:00
if ( item = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
var wrapper = _mapper . Map < RelationshipEventDto > ( item ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetRelationshipEventDao ( ) . DeleteItem ( id ) ;
2020-04-16 19:41:37 +00:00
var messageAction = GetHistoryDeletedAction ( item . EntityType , item . ContactID ) ;
var entityTitle = wrapper . Contact = = null ? wrapper . Entity . EntityTitle : wrapper . Contact . DisplayName ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( item . ID ) , entityTitle , wrapper . Category . Title ) ;
2020-04-16 19:41:37 +00:00
return wrapper ;
}
/// <summary>
/// Creates a text (.txt) file in the selected folder with the title and contents sent in the request
/// </summary>
/// <short>Create txt</short>
/// <category>Files</category>
/// <param name="entityType">Entity type</param>
/// <param name="entityid">Entity ID</param>
/// <param name="title">File title</param>
/// <param name="content">File contents</param>
/// <returns>
/// File info
/// </returns>
2021-03-02 16:29:07 +00:00
[Create(@"{entityType:regex(contact|opportunity|case)}/{entityid:int}/files/text")]
2021-03-24 15:55:47 +00:00
public FileWrapper < int > CreateTextFile (
[FromRoute] string entityType ,
[FromRoute] int entityid ,
2021-06-28 09:42:42 +00:00
[FromBody] RelationshipEventCreateTextFileRequestDto inDto )
2020-04-16 19:41:37 +00:00
{
2021-06-28 09:42:42 +00:00
var title = inDto . Title ;
var content = inDto . Content ;
2020-04-16 19:41:37 +00:00
if ( title = = null ) throw new ArgumentNullException ( "title" ) ;
if ( content = = null ) throw new ArgumentNullException ( "content" ) ;
var folderid = GetRootFolderID ( ) ;
2020-04-18 17:49:09 +00:00
FileWrapper < int > result ;
2020-04-16 19:41:37 +00:00
var extension = ".txt" ;
if ( ! string . IsNullOrEmpty ( content ) )
{
if ( Regex . IsMatch ( content , @"<([^\s>]*)(\s[^<]*)>" ) )
{
extension = ".html" ;
}
}
using ( var memStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( content ) ) )
{
title = title . EndsWith ( extension , StringComparison . OrdinalIgnoreCase ) ? title : ( title + extension ) ;
result = SaveFile ( folderid , memStream , title ) ;
}
2021-03-02 16:29:07 +00:00
AttachFiles ( entityType , entityid , new List < int > { ( int ) result . Id } ) ;
2020-04-16 19:41:37 +00:00
return result ;
}
2020-04-22 20:46:49 +00:00
/// <summary>
/// Upload file
/// </summary>
/// <short>Upload file</short>
/// <category>Files</category>
/// <remarks>
/// <![CDATA[
/// Upload can be done in 2 different ways:
/// <ol>
/// <li>Single file upload. You should set Content-Type & Content-Disposition header to specify filename and content type, and send file in request body</li>
/// <li>Using standart multipart/form-data method</li>
/// </ol>]]>
/// </remarks>
/// <param name="entityType">Entity type</param>
/// <param name="entityid">Entity ID</param>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="contentType" visible="false">Content-Type Header</param>
/// <param name="contentDisposition" visible="false">Content-Disposition Header</param>
/// <param name="files" visible="false">List of files when posted as multipart/form-data</param>
/// <param name="storeOriginalFileFlag" visible="false">If True, upload documents in original formats as well</param>
/// <returns>
/// File info
/// </returns>
2021-04-06 17:33:28 +00:00
[Create(@"{entityType:regex(contact|opportunity|case)}/{entityid:int}/files/upload")]
2021-06-23 19:32:56 +00:00
public FileWrapper < int > UploadFileInCRM ( [ FromBody ] UploadFileInCRMRequestDto inDto )
2021-04-06 17:33:28 +00:00
{
string entityType = inDto . EntityType ;
int entityid = inDto . Entityid ;
Stream file = inDto . File ;
ContentType contentType = inDto . ContentType ;
ContentDisposition contentDisposition = inDto . ContentDisposition ;
IEnumerable < IFormFile > files = inDto . Files ;
bool storeOriginalFileFlag = inDto . StoreOriginalFileFlag ;
_filesSettingsHelper . StoreOriginalFiles = storeOriginalFileFlag ;
var folderid = GetRootFolderID ( ) ;
var fileNames = new List < string > ( ) ;
FileWrapper < int > uploadedFile = null ;
if ( files ! = null & & files . Any ( ) )
{
//For case with multiple files
foreach ( var postedFile in files )
{
using var fileStream = postedFile . OpenReadStream ( ) ;
uploadedFile = SaveFile ( folderid , fileStream , postedFile . FileName ) ;
fileNames . Add ( uploadedFile . Title ) ;
}
}
else if ( file ! = null )
{
uploadedFile = SaveFile ( folderid , file , contentDisposition . FileName ) ;
fileNames . Add ( uploadedFile . Title ) ;
}
return uploadedFile ;
}
2020-04-18 17:49:09 +00:00
private FileWrapper < int > SaveFile ( int folderid , Stream file , string fileName )
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
var resultFile = _fileUploader . Exec < int > ( folderid , fileName , file . Length , file ) ;
2020-04-16 19:41:37 +00:00
2021-03-23 15:41:56 +00:00
return _fileWrapperHelper . Get < int > ( resultFile ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates the event with the parameters specified in the request
/// </summary>
/// <short>
/// Create event
/// </short>
/// <category>History</category>
/// <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>
/// <remarks>
/// <![CDATA[
/// You should obligatorily set the value for 'contactId' if 'entityId' is not set or the value for parameters 'entityId' and 'entityType' if 'contactId' is not set.
/// ]]>
/// </remarks>
/// <param optional="false" name="content">Contents</param>
/// <param optional="false" name="categoryId">Category ID</param>
/// <param optional="true" name="created">Event creation date</param>
/// <param optional="true" name="fileId">List of IDs of the files associated with the event</param>
/// <param optional="true" name="notifyUserList">User field list</param>
/// <returns>
/// Created event
/// </returns>
2021-04-06 17:33:28 +00:00
[Create(@"history")]
2021-06-23 19:32:56 +00:00
public RelationshipEventDto AddHistoryTo ( [ FromBody ] AddHistoryToRequestDto inDto )
2021-04-06 17:33:28 +00:00
{
string entityType = inDto . EntityType ;
int entityId = inDto . EntityId ;
int contactId = inDto . ContactId ;
string content = inDto . Content ;
int categoryId = inDto . CategoryId ;
ApiDateTime created = inDto . Created ;
IEnumerable < int > fileId = inDto . FileId ;
IEnumerable < Guid > notifyUserList = inDto . NotifyUserList ;
if ( ! string . IsNullOrEmpty ( entityType ) & &
! (
string . Compare ( entityType , "opportunity" , StringComparison . OrdinalIgnoreCase ) = = 0 | |
string . Compare ( entityType , "case" , StringComparison . OrdinalIgnoreCase ) = = 0 )
)
throw new ArgumentException ( ) ;
var entityTypeObj = ToEntityType ( entityType ) ;
var entityTitle = "" ;
if ( contactId > 0 )
{
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
if ( contact = = null | | ! _crmSecurity . CanAccessTo ( contact ) )
throw new ArgumentException ( ) ;
entityTitle = contact . GetTitle ( ) ;
}
if ( entityTypeObj = = EntityType . Case )
{
var cases = _daoFactory . GetCasesDao ( ) . GetByID ( entityId ) ;
if ( cases = = null | | ! _crmSecurity . CanAccessTo ( cases ) )
throw new ArgumentException ( ) ;
if ( contactId < = 0 )
{
entityTitle = cases . Title ;
}
}
if ( entityTypeObj = = EntityType . Opportunity )
{
var deal = _daoFactory . GetDealDao ( ) . GetByID ( entityId ) ;
if ( deal = = null | | ! _crmSecurity . CanAccessTo ( deal ) )
throw new ArgumentException ( ) ;
if ( contactId < = 0 )
{
entityTitle = deal . Title ;
}
}
var relationshipEvent = new RelationshipEvent
{
CategoryID = categoryId ,
EntityType = entityTypeObj ,
EntityID = entityId ,
Content = content ,
ContactID = contactId ,
CreateOn = created ,
CreateBy = _securityContext . CurrentAccount . ID
} ;
var category = _daoFactory . GetListItemDao ( ) . GetByID ( categoryId ) ;
if ( category = = null ) throw new ArgumentException ( ) ;
var item = _daoFactory . GetRelationshipEventDao ( ) . CreateItem ( relationshipEvent ) ;
notifyUserList = notifyUserList ! = null ? notifyUserList . ToList ( ) : new List < Guid > ( ) ;
var needNotify = notifyUserList . Any ( ) ;
var fileListInfoHashtable = new Hashtable ( ) ;
if ( fileId ! = null )
{
var fileIds = fileId . ToList ( ) ;
var files = _filesDaoFactory . GetFileDao < int > ( ) . GetFiles ( fileIds . ToArray ( ) ) ;
if ( needNotify )
{
foreach ( var file in files )
{
var extension = Path . GetExtension ( file . Title ) ;
if ( extension = = null ) continue ;
var fileInfo = string . Format ( "{0} ({1})" , file . Title , extension . ToUpper ( ) ) ;
if ( ! fileListInfoHashtable . ContainsKey ( fileInfo ) )
{
fileListInfoHashtable . Add ( fileInfo , file . DownloadUrl ) ;
}
else
{
fileInfo = string . Format ( "{0} ({1}, {2})" , file . Title , extension . ToUpper ( ) , file . UniqID ) ;
fileListInfoHashtable . Add ( fileInfo , file . DownloadUrl ) ;
}
}
}
_daoFactory . GetRelationshipEventDao ( ) . AttachFiles ( item . ID , fileIds . ToArray ( ) ) ;
if ( files . Any ( ) )
{
var fileAttachAction = GetFilesAttachAction ( entityTypeObj , contactId ) ;
2021-06-04 14:38:42 +00:00
2021-04-06 17:33:28 +00:00
_messageService . Send ( fileAttachAction , _messageTarget . Create ( item . ID ) , entityTitle , files . Select ( x = > x . Title ) ) ;
}
}
if ( needNotify )
{
_notifyClient . SendAboutAddRelationshipEventAdd ( item , fileListInfoHashtable , _daoFactory , notifyUserList . ToArray ( ) ) ;
}
2021-06-04 14:38:42 +00:00
2021-04-06 17:33:28 +00:00
var historyCreatedAction = GetHistoryCreatedAction ( entityTypeObj , contactId ) ;
_messageService . Send ( historyCreatedAction , _messageTarget . Create ( item . ID ) , entityTitle , category . Title ) ;
return _mapper . Map < RelationshipEventDto > ( item ) ;
}
2020-04-16 19:41:37 +00:00
/// <summary>
/// Associates the selected file(s) with the entity with the ID or type specified in the request
/// </summary>
/// <short>
/// Associate file with entity
/// </short>
/// <param name="entityType">Entity type</param>
/// <param name="entityid">Entity ID</param>
/// <param name="fileids">List of IDs of the files</param>
/// <category>Files</category>
/// <returns>Entity with the file attached</returns>
2021-03-02 16:29:07 +00:00
[Create(@"{entityType:regex(contact|opportunity|case)}/{entityid:int}/files")]
2021-03-24 15:55:47 +00:00
public RelationshipEventDto AttachFiles (
2021-06-04 14:38:42 +00:00
[FromRoute] string entityType ,
2021-03-24 15:55:47 +00:00
[FromRoute] int entityid ,
2021-06-23 19:32:56 +00:00
[FromBody] IEnumerable < int > fileids )
2020-04-16 19:41:37 +00:00
{
if ( entityid < = 0 | | fileids = = null ) throw new ArgumentException ( ) ;
2021-03-23 15:41:56 +00:00
var files = _filesDaoFactory . GetFileDao < int > ( ) . GetFiles ( fileids . ToArray ( ) ) ;
2020-04-16 19:41:37 +00:00
var folderid = GetRootFolderID ( ) ;
if ( files . Exists ( file = > file . FolderID . ToString ( ) ! = folderid . ToString ( ) ) )
throw new ArgumentException ( "invalid file folder" ) ;
var entityTypeObj = ToEntityType ( entityType ) ;
DomainObject entityObj ;
var entityTitle = GetEntityTitle ( entityTypeObj , entityid , true , out entityObj ) ;
switch ( entityTypeObj )
{
case EntityType . Contact :
2021-03-19 16:56:26 +00:00
var relationshipEvent1 = _daoFactory . GetRelationshipEventDao ( ) . AttachFiles ( entityid , EntityType . Any , 0 , fileids . ToArray ( ) ) ;
2020-04-16 19:41:37 +00:00
var messageAction = entityObj is Company ? MessageAction . CompanyAttachedFiles : MessageAction . PersonAttachedFiles ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( entityid ) , entityTitle , files . Select ( x = > x . Title ) ) ;
2021-03-19 16:56:26 +00:00
return _mapper . Map < RelationshipEventDto > ( relationshipEvent1 ) ;
2020-04-16 19:41:37 +00:00
case EntityType . Opportunity :
2021-03-19 16:56:26 +00:00
var relationshipEvent2 = _daoFactory . GetRelationshipEventDao ( ) . AttachFiles ( 0 , entityTypeObj , entityid , fileids . ToArray ( ) ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . OpportunityAttachedFiles , _messageTarget . Create ( entityid ) , entityTitle , files . Select ( x = > x . Title ) ) ;
2021-03-19 16:56:26 +00:00
return _mapper . Map < RelationshipEventDto > ( relationshipEvent2 ) ;
2020-04-16 19:41:37 +00:00
case EntityType . Case :
2021-03-19 16:56:26 +00:00
var relationshipEvent3 = _daoFactory . GetRelationshipEventDao ( ) . AttachFiles ( 0 , entityTypeObj , entityid , fileids . ToArray ( ) ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . CaseAttachedFiles , _messageTarget . Create ( entityid ) , entityTitle , files . Select ( x = > x . Title ) ) ;
2021-03-19 16:56:26 +00:00
return _mapper . Map < RelationshipEventDto > ( relationshipEvent3 ) ;
2020-04-16 19:41:37 +00:00
default :
throw new ArgumentException ( ) ;
}
}
/// <summary>
/// Returns the ID for the root folder used to store the files for the CRM module
/// </summary>
/// <short>Get root folder ID</short>
/// <category>Files</category>
/// <returns>
/// Root folder ID
/// </returns>
[Read(@"files/root")]
2020-04-18 17:49:09 +00:00
public int GetRootFolderID ( )
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
return _daoFactory . GetFileDao ( ) . GetRoot ( ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of all files for the entity with the ID or type specified in the request
/// </summary>
/// <param name="entityType">Entity type</param>
/// <param name="entityid">Entity ID</param>
/// <short>Get file list</short>
/// <category>Files</category>
/// <returns>
/// File list
/// </returns>
2021-03-02 16:29:07 +00:00
[Read(@"{entityType:regex(contact|opportunity|case)}/{entityid:int}/files")]
2020-04-18 17:49:09 +00:00
public IEnumerable < FileWrapper < int > > GetFiles ( string entityType , int entityid )
2020-04-16 19:41:37 +00:00
{
if ( entityid < = 0 ) throw new ArgumentException ( ) ;
var entityTypeObj = ToEntityType ( entityType ) ;
switch ( entityTypeObj )
{
case EntityType . Contact :
2021-03-23 15:41:56 +00:00
return _daoFactory . GetRelationshipEventDao ( ) . GetAllFiles ( new [ ] { entityid } , EntityType . Any , 0 ) . ConvertAll ( file = > _fileWrapperHelper . Get < int > ( file ) ) ;
2020-04-16 19:41:37 +00:00
case EntityType . Opportunity :
case EntityType . Case :
2021-03-23 15:41:56 +00:00
return _daoFactory . GetRelationshipEventDao ( ) . GetAllFiles ( null , entityTypeObj , entityid ) . ConvertAll ( file = > _fileWrapperHelper . Get < int > ( file ) ) ;
2020-04-16 19:41:37 +00:00
default :
throw new ArgumentException ( ) ;
}
}
/// <summary>
/// Deletes the file with the ID specified in the request
/// </summary>
/// <short>Delete file</short>
/// <category>Files</category>
/// <param name="fileid">File ID</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// File Info
/// </returns>
[Delete(@"files/{fileid:int}")]
2020-04-18 17:49:09 +00:00
public FileWrapper < int > DeleteCRMFile ( int fileid )
2020-04-16 19:41:37 +00:00
{
if ( fileid < 0 ) throw new ArgumentException ( ) ;
2021-03-23 15:41:56 +00:00
var file = _filesDaoFactory . GetFileDao < int > ( ) . GetFile ( fileid ) ;
2020-04-16 19:41:37 +00:00
if ( file = = null ) throw new ItemNotFoundException ( ) ;
2021-03-23 15:41:56 +00:00
var result = _fileWrapperHelper . Get < int > ( file ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var _eventsDao = _daoFactory . GetRelationshipEventDao ( ) ;
2020-04-16 19:41:37 +00:00
var eventIDs = _eventsDao . RemoveFile ( file ) ;
var events = new List < RelationshipEvent > ( ) ;
eventIDs . ForEach ( id = > events . Add ( _eventsDao . GetByID ( id ) ) ) ;
foreach ( var evt in events )
{
DomainObject entityObj ;
var entityTitle = evt . ContactID > 0
? GetEntityTitle ( EntityType . Contact , evt . ContactID , false , out entityObj )
: GetEntityTitle ( evt . EntityType , evt . EntityID , false , out entityObj ) ;
var messageAction = GetFilesDetachAction ( evt . EntityType , evt . ContactID ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( file . ID ) , entityTitle , file . Title ) ;
2020-04-16 19:41:37 +00:00
}
return result ;
}
2021-03-09 15:37:16 +00:00
private IEnumerable < RelationshipEventDto > ToListRelationshipEventDto ( List < RelationshipEvent > itemList )
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
if ( itemList . Count = = 0 ) return new List < RelationshipEventDto > ( ) ;
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
var result = new List < RelationshipEventDto > ( ) ;
2020-04-16 19:41:37 +00:00
var contactIDs = new List < int > ( ) ;
var eventIDs = 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 )
{
eventIDs . 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 ) 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
2021-03-02 16:29:07 +00:00
{
EntityId = item . ID ,
EntityTitle = item . Title ,
EntityType = "opportunity"
} ) ;
2020-04-16 19:41:37 +00:00
} ) ;
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
2021-03-02 16:29:07 +00:00
{
EntityId = item . ID ,
EntityTitle = item . Title ,
EntityType = "case"
} ) ;
2020-04-16 19:41:37 +00:00
} ) ;
break ;
default :
throw new ArgumentException ( ) ;
}
}
2021-03-23 15:41:56 +00:00
var categories = _daoFactory . GetListItemDao ( ) . GetItems ( categoryIDs . ToArray ( ) ) . ToDictionary ( x = > x . ID , x = > _mapper . Map < HistoryCategoryDto > ( x ) ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var files = _daoFactory . GetRelationshipEventDao ( ) . GetFiles ( eventIDs . ToArray ( ) ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var contacts = _daoFactory . GetContactDao ( ) . GetContacts ( contactIDs . ToArray ( ) ) . ToDictionary ( item = > item . ID , x = > _mapper . Map < ContactBaseDto > ( x ) ) ;
2020-04-16 19:41:37 +00:00
foreach ( var item in itemList )
{
2021-03-19 16:56:26 +00:00
var eventObjWrap = _mapper . Map < RelationshipEventDto > ( item ) ;
2020-04-16 19:41:37 +00:00
if ( contacts . ContainsKey ( item . ContactID ) )
{
eventObjWrap . Contact = contacts [ item . ContactID ] ;
}
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
eventObjWrap . Entity = entityDtos [ entityStrKey ] ;
2020-04-16 19:41:37 +00:00
}
}
2020-04-18 17:49:09 +00:00
eventObjWrap . Files = files . ContainsKey ( item . ID ) ? files [ item . ID ] . ConvertAll ( file = >
2021-03-23 15:41:56 +00:00
_fileWrapperHelper . Get < int > ( file ) ) : new List < FileWrapper < int > > ( ) ;
2020-04-16 19:41:37 +00:00
if ( categories . ContainsKey ( item . CategoryID ) )
{
eventObjWrap . Category = categories [ item . CategoryID ] ;
}
result . Add ( eventObjWrap ) ;
}
return result ;
}
2021-03-19 16:56:26 +00:00
2020-04-16 19:41:37 +00:00
private MessageAction GetHistoryCreatedAction ( EntityType entityType , int contactId )
{
if ( contactId > 0 )
{
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyCreatedHistoryEvent : MessageAction . PersonCreatedHistoryEvent ;
}
switch ( entityType )
{
case EntityType . Opportunity :
return MessageAction . OpportunityCreatedHistoryEvent ;
case EntityType . Case :
return MessageAction . CaseCreatedHistoryEvent ;
case EntityType . Any :
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyCreatedHistoryEvent : MessageAction . PersonCreatedHistoryEvent ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
private MessageAction GetHistoryDeletedAction ( EntityType entityType , int contactId )
{
if ( contactId > 0 )
{
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyDeletedHistoryEvent : MessageAction . PersonDeletedHistoryEvent ;
}
switch ( entityType )
{
case EntityType . Opportunity :
return MessageAction . OpportunityDeletedHistoryEvent ;
case EntityType . Case :
return MessageAction . CaseDeletedHistoryEvent ;
case EntityType . Any :
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyDeletedHistoryEvent : MessageAction . PersonDeletedHistoryEvent ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
private MessageAction GetFilesAttachAction ( EntityType entityType , int contactId )
{
if ( contactId > 0 )
{
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyAttachedFiles : MessageAction . PersonAttachedFiles ;
}
switch ( entityType )
{
case EntityType . Opportunity :
return MessageAction . OpportunityAttachedFiles ;
case EntityType . Case :
return MessageAction . CaseAttachedFiles ;
case EntityType . Any :
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyAttachedFiles : MessageAction . PersonAttachedFiles ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
private MessageAction GetFilesDetachAction ( EntityType entityType , int contactId )
{
if ( contactId > 0 )
{
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyDetachedFile : MessageAction . PersonDetachedFile ;
}
switch ( entityType )
{
case EntityType . Opportunity :
return MessageAction . OpportunityDetachedFile ;
case EntityType . Case :
return MessageAction . CaseDetachedFile ;
case EntityType . Any :
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactId ) ;
2020-04-16 19:41:37 +00:00
return contact is Company ? MessageAction . CompanyDetachedFile : MessageAction . PersonAttachedFiles ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
}
}