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

2150 lines
89 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-10 15:38:56 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-04-16 19:41:37 +00:00
using ASC.Api.Collections;
using ASC.Api.Core;
2021-03-10 15:38:56 +00:00
using ASC.Api.CRM;
2021-03-13 18:00:08 +00:00
using ASC.Common.Threading.Progress;
2020-04-16 19:41:37 +00:00
using ASC.Common.Web;
2021-03-10 15:38:56 +00:00
using ASC.Core;
using ASC.Core.Users;
using ASC.CRM.ApiModels;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core;
2021-03-10 15:38:56 +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.CRM.Resources;
using ASC.MessagingSystem;
2020-04-18 17:49:09 +00:00
using ASC.Web.Api.Models;
2020-04-16 19:41:37 +00:00
using ASC.Web.Api.Routing;
2020-04-18 17:49:09 +00:00
using ASC.Web.CRM.Classes;
2021-03-10 15:38:56 +00:00
using ASC.Web.CRM.Services.NotifyService;
2020-04-16 19:41:37 +00:00
using ASC.Web.Studio.Core;
2020-04-16 19:41:37 +00:00
using Autofac;
2020-04-22 20:46:49 +00:00
using Microsoft.AspNetCore.Http;
2021-03-05 20:06:49 +00:00
using Microsoft.AspNetCore.Mvc;
2020-04-16 19:41:37 +00:00
using Contact = ASC.CRM.Core.Entities.Contact;
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 ContactsController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public ContactsController(CRMSecurity cRMSecurity,
DaoFactory daoFactory,
ApiContext apiContext,
MessageTarget messageTarget,
MessageService messageService,
NotifyClient notifyClient,
TaskDtoHelper taskDtoHelper,
ContactDtoHelper contactBaseDtoHelper,
TaskCategoryDtoHelper taskCategoryDtoHelper,
SecurityContext securityContext,
OpportunityDtoHelper opportunityDtoHelper,
SetupInfo setupInfo,
UserFormatter userFormatter,
EmployeeWraperHelper employeeWraperHelper,
ContactPhotoManager contactPhotoManager,
FileSizeComment fileSizeComment,
2021-03-13 18:00:08 +00:00
ContactInfoDtoHelper contactInfoDtoHelper,
MailSender mailSender)
2021-03-10 15:38:56 +00:00
: base(daoFactory, cRMSecurity)
{
ApiContext = apiContext;
MessageTarget = messageTarget;
MessageService = messageService;
NotifyClient = notifyClient;
TaskDtoHelper = taskDtoHelper;
ContactDtoHelper = contactBaseDtoHelper;
TaskCategoryDtoHelper = taskCategoryDtoHelper;
SecurityContext = securityContext;
OpportunityDtoHelper = opportunityDtoHelper;
SetupInfo = setupInfo;
UserFormatter = userFormatter;
EmployeeWraperHelper = employeeWraperHelper;
ContactPhotoManager = contactPhotoManager;
FileSizeComment = fileSizeComment;
ContactInfoDtoHelper = contactInfoDtoHelper;
2021-03-13 18:00:08 +00:00
MailSender = mailSender;
2021-03-10 15:38:56 +00:00
}
2021-03-13 18:00:08 +00:00
public MailSender MailSender { get; }
2021-03-10 15:38:56 +00:00
public ContactInfoDtoHelper ContactInfoDtoHelper { get; }
public FileSizeComment FileSizeComment { get; }
public ContactPhotoManager ContactPhotoManager { get; }
public EmployeeWraperHelper EmployeeWraperHelper { get; }
public UserFormatter UserFormatter { get; }
public SetupInfo SetupInfo { get; }
public SecurityContext SecurityContext { get; }
public TaskCategoryDtoHelper TaskCategoryDtoHelper { get; }
public ContactDtoHelper ContactDtoHelper { get; }
public TaskDtoHelper TaskDtoHelper { get; }
public NotifyClient NotifyClient { get; }
private ApiContext ApiContext { get; }
public MessageService MessageService { get; }
public MessageTarget MessageTarget { get; }
public OpportunityDtoHelper OpportunityDtoHelper { get; }
2020-04-16 19:41:37 +00:00
/// <summary>
/// Returns the detailed information about the contact with the ID specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <returns>Contact</returns>
/// <short>Get contact by ID</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
2021-03-02 16:29:07 +00:00
[Read(@"contact/{contactid:int}")]
2021-03-09 15:37:16 +00:00
public ContactDto GetContactByID(int contactid)
2020-04-16 19:41:37 +00:00
{
if (contactid <= 0) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
2021-03-09 15:37:16 +00:00
return ContactDtoHelper.GetContactDto(contact);
2020-04-16 19:41:37 +00:00
}
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> GetContactsByID(IEnumerable<int> contactid)
2020-04-16 19:41:37 +00:00
{
var contacts = DaoFactory.GetContactDao().GetContacts(contactid.ToArray()).Where(r => r != null && CRMSecurity.CanAccessTo(r));
2021-03-10 15:38:56 +00:00
return ContactDtoHelper.ToListContactDto(contacts.ToList());
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the contact list for the project with the ID specified in the request
/// </summary>
/// <short>
/// Get contacts by project ID
/// </short>
/// <param name="projectid">Project ID</param>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
///<exception cref="ArgumentException"></exception>
[Read(@"contact/project/{projectid:int}")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> GetContactsByProjectID(int projectid)
2020-04-16 19:41:37 +00:00
{
if (projectid <= 0) throw new ArgumentException();
var contacts = DaoFactory.GetContactDao().GetContactsByProjectID(projectid);
2021-03-10 15:38:56 +00:00
return ContactDtoHelper.ToListContactDto(contacts.ToList());
2020-04-16 19:41:37 +00:00
}
2020-04-18 17:49:09 +00:00
///// <summary>
///// Links the selected contact to the project with the ID specified in the request
///// </summary>
///// <param name="contactid">Contact ID</param>
///// <param name="projectid">Project ID</param>
///// <category>Contacts</category>
///// <short>Link contact with project</short>
///// <exception cref="ArgumentException"></exception>
///// <exception cref="ItemNotFoundException"></exception>
///// <returns>Contact Info</returns>
//[Create(@"contact/{contactid:int}/project/{projectid:int}")]
2021-03-09 15:37:16 +00:00
//public ContactDto SetRelativeContactToProject(int contactid, int projectid)
2020-04-18 17:49:09 +00:00
//{
// if (contactid <= 0 || projectid <= 0) throw new ArgumentException();
// var contact = DaoFactory.GetContactDao().GetByID(contactid);
// if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
// var project = ProjectsDaoFactory.ProjectDao.GetById(projectid);
// if (project == null) throw new ItemNotFoundException();
// using (var scope = DIHelper.Resolve())
// {
// if (!scope.Resolve<ProjectSecurity>().CanLinkContact(project)) throw CRMSecurity.CreateSecurityException();
// }
2021-03-02 16:29:07 +00:00
// DaoFactory.GetContactDao().SetRelativeContactProject(new List<int> { contactid }, projectid);
2020-04-18 17:49:09 +00:00
// var messageAction = contact is Company ? MessageAction.ProjectLinkedCompany : MessageAction.ProjectLinkedPerson;
2021-03-02 16:29:07 +00:00
// MessageService.Send(messageAction, MessageTarget.Create(contact.ID), project.Title, contact.GetTitle());
2020-04-18 17:49:09 +00:00
2021-03-09 15:37:16 +00:00
// return ToContactDto(contact);
2020-04-18 17:49:09 +00:00
//}
///// <summary>
///// Links the selected contacts to the project with the ID specified in the request
///// </summary>
///// <param name="contactid">Contact IDs array</param>
///// <param name="projectid">Project ID</param>
///// <category>Contacts</category>
///// <short>Link contact list with project</short>
///// <exception cref="ArgumentException"></exception>
///// <exception cref="ItemNotFoundException"></exception>
///// <returns>
///// Contact list
///// </returns>
//[Create(@"contact/project/{projectid:int}")]
2021-03-09 15:37:16 +00:00
//public IEnumerable<ContactDto> SetRelativeContactListToProject(IEnumerable<int> contactid, int projectid)
2020-04-18 17:49:09 +00:00
//{
// if (contactid == null) throw new ArgumentException();
2020-04-18 17:49:09 +00:00
// var contactIds = contactid.ToList();
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// if (!contactIds.Any() || projectid <= 0) throw new ArgumentException();
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// var project = ProjectsDaoFactory.ProjectDao.GetById(projectid);
// if (project == null) throw new ItemNotFoundException();
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// using (var scope = DIHelper.Resolve())
// {
// if (!scope.Resolve<ProjectSecurity>().CanLinkContact(project))
// throw CRMSecurity.CreateSecurityException();
// }
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// var contacts = DaoFactory.GetContactDao().GetContacts(contactIds.ToArray()).Where(CRMSecurity.CanAccessTo).ToList();
// contactIds = contacts.Select(c => c.ID).ToList();
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// DaoFactory.GetContactDao().SetRelativeContactProject(contactIds, projectid);
2020-04-16 19:41:37 +00:00
2021-03-02 16:29:07 +00:00
// MessageService.Send(MessageAction.ProjectLinkedContacts, MessageTarget.Create(contactIds), project.Title, contacts.Select(x => x.GetTitle()));
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
// return contacts.ConvertAll(ToContactDto);
2020-04-18 17:49:09 +00:00
//}
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
///// <summary>
///// Removes the link with the selected project from the contact with the ID specified in the request
///// </summary>
///// <param name="contactid">Contact ID</param>
///// <param name="projectid">Project ID</param>
///// <category>Contacts</category>
///// <short>Remove contact from project</short>
///// <returns>
///// Contact info
///// </returns>
//[Delete(@"contact/{contactid:int}/project/{projectid:int}")]
2021-03-09 15:37:16 +00:00
//public ContactBaseDto RemoveRelativeContactToProject(int contactid, int projectid)
2020-04-18 17:49:09 +00:00
//{
// if (contactid <= 0 || projectid <= 0) throw new ArgumentException();
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// var contact = DaoFactory.GetContactDao().GetByID(contactid);
// if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// var project = ProjectsDaoFactory.ProjectDao.GetById(projectid);
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// using (var scope = DIHelper.Resolve())
// {
// if (project == null || !scope.Resolve<ProjectSecurity>().CanLinkContact(project)) throw new ItemNotFoundException();
// }
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// DaoFactory.GetContactDao().RemoveRelativeContactProject(contactid, projectid);
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
// var action = contact is Company ? MessageAction.ProjectUnlinkedCompany : MessageAction.ProjectUnlinkedPerson;
2021-03-02 16:29:07 +00:00
// MessageService.Send(action, MessageTarget.Create(contact.ID), project.Title, contact.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
// return ToContactBaseDto(contact);
2020-04-18 17:49:09 +00:00
//}
2020-04-16 19:41:37 +00:00
/// <summary>
/// Adds the selected opportunity to the contact with the ID specified in the request. The same as AddMemberToDeal
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <param name="contactid">Contact ID</param>
/// <short>Add contact opportunity</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Opportunity
/// </returns>
[Create(@"contact/{contactid:int}/opportunity/{opportunityid:int}")]
2021-03-09 15:37:16 +00:00
public OpportunityDto AddDealToContact(int contactid, int opportunityid)
2020-04-16 19:41:37 +00:00
{
if ((opportunityid <= 0) || (contactid <= 0)) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
var opportunity = DaoFactory.GetDealDao().GetByID(opportunityid);
if (opportunity == null || !CRMSecurity.CanAccessTo(opportunity)) throw new ItemNotFoundException();
DaoFactory.GetDealDao().AddMember(opportunityid, contactid);
var messageAction = contact is Company ? MessageAction.OpportunityLinkedCompany : MessageAction.OpportunityLinkedPerson;
MessageService.Send(messageAction, MessageTarget.Create(contact.ID), opportunity.Title, contact.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return OpportunityDtoHelper.Get(opportunity);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the selected opportunity from the contact with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <param name="contactid">Contact ID</param>
/// <short>Delete contact opportunity</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Opportunity
/// </returns>
[Delete(@"contact/{contactid:int}/opportunity/{opportunityid:int}")]
2021-03-09 15:37:16 +00:00
public OpportunityDto DeleteDealFromContact(int contactid, int opportunityid)
2020-04-16 19:41:37 +00:00
{
if ((opportunityid <= 0) || (contactid <= 0)) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
var opportunity = DaoFactory.GetDealDao().GetByID(opportunityid);
if (opportunity == null || !CRMSecurity.CanAccessTo(opportunity)) throw new ItemNotFoundException();
DaoFactory.GetDealDao().RemoveMember(opportunityid, contactid);
2021-03-09 15:37:16 +00:00
return OpportunityDtoHelper.Get(opportunity);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of all contacts in the CRM module matching the parameters specified in the request
/// </summary>
/// <param optional="true" name="tags">Tag</param>
/// <param optional="true" name="contactStage">Contact stage ID (warmth)</param>
/// <param optional="true" name="contactType">Contact type ID</param>
/// <param optional="true" name="contactListView" remark="Allowed values: Company, Person, WithOpportunity"></param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <param optional="true" name="responsibleid">Responsible ID</param>
/// <param optional="true" name="isShared">Responsible ID</param>
/// <short>Get contact list</short>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
[Read(@"contact/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> GetContacts(
2020-04-16 19:41:37 +00:00
IEnumerable<String> tags,
int? contactStage,
int? contactType,
ContactListViewType contactListView,
Guid? responsibleid,
bool? isShared,
ApiDateTime fromDate,
ApiDateTime toDate)
{
2021-03-09 15:37:16 +00:00
IEnumerable<ContactDto> result;
2020-04-16 19:41:37 +00:00
OrderBy contactsOrderBy;
ContactSortedByType sortBy;
var searchString = ApiContext.FilterValue;
2020-04-18 17:49:09 +00:00
if (ASC.CRM.Classes.EnumExtension.TryParse(ApiContext.SortBy, true, out sortBy))
2020-04-16 19:41:37 +00:00
{
contactsOrderBy = new OrderBy(sortBy, !ApiContext.SortDescending);
}
else if (String.IsNullOrEmpty(ApiContext.SortBy))
{
contactsOrderBy = new OrderBy(ContactSortedByType.Created, false);
}
else
{
contactsOrderBy = null;
}
var fromIndex = (int)ApiContext.StartIndex;
var count = (int)ApiContext.Count;
var contactStageInt = contactStage.HasValue ? contactStage.Value : -1;
var contactTypeInt = contactType.HasValue ? contactType.Value : -1;
if (contactsOrderBy != null)
{
2021-03-10 15:38:56 +00:00
result = ContactDtoHelper.ToListContactDto(DaoFactory.GetContactDao().GetContacts(
2020-04-16 19:41:37 +00:00
searchString,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
fromIndex,
count,
contactsOrderBy,
responsibleid,
isShared));
ApiContext.SetDataPaginated();
ApiContext.SetDataFiltered();
ApiContext.SetDataSorted();
}
else
{
2021-03-10 15:38:56 +00:00
result = ContactDtoHelper.ToListContactDto(DaoFactory.GetContactDao().GetContacts(
2020-04-16 19:41:37 +00:00
searchString,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
0,
0,
null,
responsibleid,
isShared));
}
int totalCount;
if (result.Count() < count)
{
totalCount = fromIndex + result.Count();
}
else
{
totalCount = DaoFactory.GetContactDao().GetContactsCount(
searchString,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
responsibleid,
isShared);
}
ApiContext.SetTotalCount(totalCount);
return result;
}
/// <summary>
/// Returns the list of the contacts for auto complete feature.
/// </summary>
/// <param name="term">String part of contact name, lastname or email.</param>
/// <param name="maxCount">Max result count</param>
/// <short>Search contact list</short>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
/// <visible>false</visible>
[Read(@"contact/simple/byEmail")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactWithTaskDto> SearchContactsByEmail(string term, int maxCount)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
var result = ToSimpleListContactDto(DaoFactory.GetContactDao().SearchContactsByEmail(
2020-04-16 19:41:37 +00:00
term,
maxCount));
return result;
}
/// <summary>
/// Returns the list of all contacts in the CRM module matching the parameters specified in the request
/// </summary>
/// <param optional="true" name="tags">Tag</param>
/// <param optional="true" name="contactStage">Contact stage ID (warmth)</param>
/// <param optional="true" name="contactType">Contact type ID</param>
/// <param optional="true" name="contactListView" remark="Allowed values: Company, Person, WithOpportunity"></param>
/// <param optional="true" name="responsibleid">Responsible ID</param>
/// <param optional="true" name="isShared">Responsible ID</param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <short>Get contact list</short>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
/// <visible>false</visible>
[Read(@"contact/simple/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactWithTaskDto> GetSimpleContacts(
2020-04-16 19:41:37 +00:00
IEnumerable<string> tags,
int? contactStage,
int? contactType,
ContactListViewType contactListView,
Guid? responsibleid,
bool? isShared,
ApiDateTime fromDate,
ApiDateTime toDate)
{
2021-03-09 15:37:16 +00:00
IEnumerable<ContactWithTaskDto> result;
2020-04-16 19:41:37 +00:00
OrderBy contactsOrderBy;
ContactSortedByType sortBy;
var searchString = ApiContext.FilterValue;
2020-04-18 17:49:09 +00:00
if (ASC.CRM.Classes.EnumExtension.TryParse(ApiContext.SortBy, true, out sortBy))
2020-04-16 19:41:37 +00:00
{
contactsOrderBy = new OrderBy(sortBy, !ApiContext.SortDescending);
}
else if (String.IsNullOrEmpty(ApiContext.SortBy))
{
contactsOrderBy = new OrderBy(ContactSortedByType.DisplayName, true);
}
else
{
contactsOrderBy = null;
}
var fromIndex = (int)ApiContext.StartIndex;
var count = (int)ApiContext.Count;
var contactStageInt = contactStage.HasValue ? contactStage.Value : -1;
var contactTypeInt = contactType.HasValue ? contactType.Value : -1;
if (contactsOrderBy != null)
{
2021-03-09 15:37:16 +00:00
result = ToSimpleListContactDto(DaoFactory.GetContactDao().GetContacts(
2020-04-16 19:41:37 +00:00
searchString,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
fromIndex,
count,
contactsOrderBy,
responsibleid,
isShared));
ApiContext.SetDataPaginated();
ApiContext.SetDataFiltered();
ApiContext.SetDataSorted();
}
else
{
2021-03-09 15:37:16 +00:00
result = ToSimpleListContactDto(DaoFactory.GetContactDao().GetContacts(
2020-04-16 19:41:37 +00:00
searchString,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
0,
0,
null,
responsibleid,
isShared));
}
int totalCount;
if (result.Count() < count)
{
totalCount = fromIndex + result.Count();
}
else
{
totalCount = DaoFactory.GetContactDao().GetContactsCount(
searchString,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
responsibleid,
isShared);
}
ApiContext.SetTotalCount(totalCount);
return result;
}
/// <summary>
/// Get the group of contacts with the IDs specified in the request
/// </summary>
/// <param name="contactids">Contact ID list</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <short>Get contact group</short>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
/// <visible>false</visible>
[Read(@"contact/mail")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactBaseWithEmailDto> GetContactsForMail(IEnumerable<int> contactids)
2020-04-16 19:41:37 +00:00
{
if (contactids == null) throw new ArgumentException();
var contacts = DaoFactory.GetContactDao().GetContacts(contactids.ToArray());
2021-03-09 15:37:16 +00:00
var result = contacts.Select(x => ContactDtoHelper.GetContactBaseWithEmailDto(x));
2020-04-16 19:41:37 +00:00
return result;
}
/// <summary>
/// Deletes the list of all contacts in the CRM module matching the parameters specified in the request
/// </summary>
/// <param optional="true" name="tags">Tag</param>
/// <param optional="true" name="contactStage">Contact stage ID (warmth)</param>
/// <param optional="true" name="contactType">Contact type ID</param>
/// <param optional="true" name="contactListView" remark="Allowed values: Company, Person, WithOpportunity"></param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <short>Delete the list of all contacts </short>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
[Delete(@"contact/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactBaseDto> DeleteBatchContacts(
2020-04-16 19:41:37 +00:00
IEnumerable<String> tags,
int? contactStage,
int? contactType,
ContactListViewType contactListView,
ApiDateTime fromDate,
ApiDateTime toDate)
{
int contactStageInt = contactStage.HasValue ? contactStage.Value : -1;
int contactTypeInt = contactType.HasValue ? contactType.Value : -1;
var contacts = DaoFactory.GetContactDao().GetContacts(
ApiContext.FilterValue,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate,
toDate,
0,
0,
null);
contacts = DaoFactory.GetContactDao().DeleteBatchContact(contacts);
MessageService.Send(MessageAction.ContactsDeleted, MessageTarget.Create(contacts.Select(c => c.ID)), contacts.Select(c => c.GetTitle()));
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return contacts.Select(x => ContactDtoHelper.GetContactBaseDto(x));
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of all the persons linked to the company with the ID specified in the request
/// </summary>
/// <param name="companyid">Company ID</param>
/// <exception cref="ArgumentException"></exception>
/// <short>Get company linked persons list</short>
/// <category>Contacts</category>
/// <returns>
/// Linked persons
/// </returns>
[Read(@"contact/company/{companyid:int}/person")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> GetPeopleFromCompany(int companyid)
2020-04-16 19:41:37 +00:00
{
if (companyid <= 0) throw new ArgumentException();
var company = DaoFactory.GetContactDao().GetByID(companyid);
if (company == null || !CRMSecurity.CanAccessTo(company)) throw new ItemNotFoundException();
2021-03-10 15:38:56 +00:00
return ContactDtoHelper.ToListContactDto(DaoFactory.GetContactDao().GetMembers(companyid).Where(CRMSecurity.CanAccessTo).ToList());
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Adds the selected person to the company with the ID specified in the request
/// </summary>
/// <param optional="true" name="companyid">Company ID</param>
/// <param optional="true" name="personid">Person ID</param>
/// <short>Add person to company</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Person
/// </returns>
[Create(@"contact/company/{companyid:int}/person")]
2021-03-09 15:37:16 +00:00
public PersonDto AddPeopleToCompany(int companyid, int personid)
2020-04-16 19:41:37 +00:00
{
if ((companyid <= 0) || (personid <= 0)) throw new ArgumentException();
var company = DaoFactory.GetContactDao().GetByID(companyid);
var person = DaoFactory.GetContactDao().GetByID(personid);
if (person == null || company == null || !CRMSecurity.CanAccessTo(person) || !CRMSecurity.CanAccessTo(company)) throw new ItemNotFoundException();
DaoFactory.GetContactDao().AddMember(personid, companyid);
MessageService.Send(MessageAction.CompanyLinkedPerson, MessageTarget.Create(new[] { company.ID, person.ID }), company.GetTitle(), person.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return (PersonDto)ContactDtoHelper.GetContactDto(person);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the selected person from the company with the ID specified in the request
/// </summary>
/// <param optional="true" name="companyid">Company ID</param>
/// <param optional="true" name="personid">Person ID</param>
/// <short>Delete person from company</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Person
/// </returns>
[Delete(@"contact/company/{companyid:int}/person")]
2021-03-09 15:37:16 +00:00
public PersonDto DeletePeopleFromCompany(int companyid, int personid)
2020-04-16 19:41:37 +00:00
{
if ((companyid <= 0) || (personid <= 0)) throw new ArgumentException();
var company = DaoFactory.GetContactDao().GetByID(companyid);
var person = DaoFactory.GetContactDao().GetByID(personid);
if (person == null || company == null || !CRMSecurity.CanAccessTo(person) || !CRMSecurity.CanAccessTo(company)) throw new ItemNotFoundException();
DaoFactory.GetContactDao().RemoveMember(personid);
MessageService.Send(MessageAction.CompanyUnlinkedPerson, MessageTarget.Create(new[] { company.ID, person.ID }), company.GetTitle(), person.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return (PersonDto)ContactDtoHelper.GetContactDto(person);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates the person with the parameters (first name, last name, description, etc.) specified in the request
/// </summary>
/// <param name="firstName">First name</param>
/// <param name="lastName">Last name</param>
/// <param optional="true" name="jobTitle">Post</param>
/// <param optional="true" name="companyId">Company ID</param>
/// <param optional="true" name="about">Person description text</param>
/// <param name="shareType">Person privacy: 0 - not shared, 1 - shared for read/write, 2 - shared for read only</param>
/// <param optional="true" name="managerList">List of managers for the person</param>
/// <param optional="true" name="customFieldList">User field list</param>
/// <param optional="true" name="photo">Contact photo (upload using multipart/form-data)</param>
/// <short>Create person</short>
/// <category>Contacts</category>
/// <returns>Person</returns>
/// <exception cref="ArgumentException"></exception>
[Create(@"contact/person")]
2021-03-09 15:37:16 +00:00
public PersonDto CreatePerson([FromForm] CreateOrUpdatePersonInDto intDto)
2020-04-16 19:41:37 +00:00
{
2021-03-05 20:06:49 +00:00
string firstName = intDto.FirstName;
string lastName = intDto.LastName;
string jobTitle = intDto.JobTitle;
int companyId = intDto.CompanyId;
string about = intDto.About;
ShareType shareType = intDto.ShareType;
IEnumerable<Guid> managerList = intDto.ManagerList;
IEnumerable<ItemKeyValuePair<int, string>> customFieldList = intDto.CustomFieldList;
IEnumerable<IFormFile> photo = intDto.Photos;
if (companyId > 0)
{
2020-04-16 19:41:37 +00:00
var company = DaoFactory.GetContactDao().GetByID(companyId);
if (company == null || !CRMSecurity.CanAccessTo(company)) throw new ItemNotFoundException();
}
var peopleInst = new Person
{
FirstName = firstName,
LastName = lastName,
JobTitle = jobTitle,
CompanyID = companyId,
About = about,
ShareType = shareType
};
2020-04-16 19:41:37 +00:00
peopleInst.ID = DaoFactory.GetContactDao().SaveContact(peopleInst);
2020-04-18 17:49:09 +00:00
peopleInst.CreateBy = SecurityContext.CurrentAccount.ID;
2020-04-16 19:41:37 +00:00
peopleInst.CreateOn = DateTime.UtcNow;
var managerListLocal = managerList != null ? managerList.ToList() : new List<Guid>();
if (managerListLocal.Any())
{
CRMSecurity.SetAccessTo(peopleInst, managerListLocal);
}
if (customFieldList != null)
{
foreach (var field in customFieldList)
{
if (string.IsNullOrEmpty(field.Value)) continue;
DaoFactory.GetCustomFieldDao().SetFieldValue(EntityType.Person, peopleInst.ID, field.Key, field.Value);
}
}
2021-03-09 15:37:16 +00:00
var outDto = (PersonDto)ContactDtoHelper.GetContactDto(peopleInst);
2020-04-22 20:46:49 +00:00
var photoList = photo != null ? photo.ToList() : new List<IFormFile>();
2020-04-16 19:41:37 +00:00
if (photoList.Any())
{
2021-03-05 20:06:49 +00:00
outDto.SmallFotoUrl = ChangeContactPhoto(peopleInst.ID, photoList);
2020-04-16 19:41:37 +00:00
}
MessageService.Send(MessageAction.PersonCreated, MessageTarget.Create(peopleInst.ID), peopleInst.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
return outDto;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Changes the photo for the contact with the ID specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="photo">Contact photo (upload using multipart/form-data)</param>
/// <short> Change contact photo</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Path to contact photo
/// </returns>
[Update(@"contact/{contactid:int}/changephoto")]
2020-04-22 20:46:49 +00:00
public string ChangeContactPhoto(int contactid, IEnumerable<IFormFile> photo)
2020-04-16 19:41:37 +00:00
{
if (contactid <= 0)
throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
2020-04-16 19:41:37 +00:00
if (contact == null || !CRMSecurity.CanAccessTo(contact))
throw new ItemNotFoundException();
var firstPhoto = photo != null ? photo.FirstOrDefault() : null;
2020-04-22 20:46:49 +00:00
2020-04-16 19:41:37 +00:00
if (firstPhoto == null)
throw new ArgumentException();
2020-04-22 20:46:49 +00:00
var fileStream = firstPhoto.OpenReadStream();
2020-04-22 20:46:49 +00:00
if (firstPhoto.Length == 0 ||
!firstPhoto.ContentType.StartsWith("image/") ||
2020-04-22 20:46:49 +00:00
!fileStream.CanRead)
2020-04-16 19:41:37 +00:00
throw new InvalidOperationException(CRMErrorsResource.InvalidFile);
if (SetupInfo.MaxImageUploadSize > 0 &&
2020-04-22 20:46:49 +00:00
SetupInfo.MaxImageUploadSize < firstPhoto.Length)
2020-04-16 19:41:37 +00:00
throw new Exception(FileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false));
2020-04-22 20:46:49 +00:00
return ContactPhotoManager.UploadPhoto(fileStream, contactid, false).Url;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Changes the photo for the contact with the ID specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="photourl">contact photo url</param>
/// <short> Change contact photo</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Path to contact photo
/// </returns>
[Update(@"contact/{contactid:int}/changephotobyurl")]
public string ChangeContactPhoto(int contactid, string photourl)
{
if (contactid <= 0 || string.IsNullOrEmpty(photourl)) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
return ContactPhotoManager.UploadPhoto(photourl, contactid, false).Url;
}
/// <summary>
/// Merge two selected contacts
/// </summary>
/// <param name="fromcontactid">the first contact ID for merge</param>
/// <param name="tocontactid">the second contact ID for merge</param>
/// <short>Merge contacts</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <exception cref="SecurityException"></exception>
/// <returns>
/// Contact
/// </returns>
[Update(@"contact/merge")]
2021-03-09 15:37:16 +00:00
public ContactDto MergeContacts(int fromcontactid, int tocontactid)
2020-04-16 19:41:37 +00:00
{
if (fromcontactid <= 0 || tocontactid <= 0) throw new ArgumentException();
var fromContact = DaoFactory.GetContactDao().GetByID(fromcontactid);
var toContact = DaoFactory.GetContactDao().GetByID(tocontactid);
if (fromContact == null || toContact == null) throw new ItemNotFoundException();
if (!CRMSecurity.CanEdit(fromContact) || !CRMSecurity.CanEdit(toContact)) throw CRMSecurity.CreateSecurityException();
DaoFactory.GetContactDao().MergeDublicate(fromcontactid, tocontactid);
var resultContact = DaoFactory.GetContactDao().GetByID(tocontactid);
var messageAction = resultContact is Person ? MessageAction.PersonsMerged : MessageAction.CompaniesMerged;
MessageService.Send(messageAction, MessageTarget.Create(new[] { fromContact.ID, toContact.ID }), fromContact.GetTitle(), toContact.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return ContactDtoHelper.GetContactDto(resultContact);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the selected person with the parameters (first name, last name, description, etc.) specified in the request
/// </summary>
/// <param name="personid">Person ID</param>
/// <param name="firstName">First name</param>
/// <param name="lastName">Last name</param>
/// <param optional="true" name="jobTitle">Post</param>
/// <param optional="true" name="companyId">Company ID</param>
/// <param optional="true" name="about">Person description text</param>
/// <param name="shareType">Person privacy: 0 - not shared, 1 - shared for read/write, 2 - shared for read only</param>
/// <param optional="true" name="managerList">List of persons managers</param>
/// <param optional="true" name="customFieldList">User field list</param>
/// <param optional="true" name="photo">Contact photo (upload using multipart/form-data)</param>
/// <short>Update person</short>
/// <category>Contacts</category>
/// <returns>Person</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
[Update(@"contact/person/{personid:int}")]
2021-03-09 15:37:16 +00:00
public PersonDto UpdatePerson([FromQuery] int personid, [FromForm] CreateOrUpdatePersonInDto inDto)
2020-04-16 19:41:37 +00:00
{
2021-03-05 20:06:49 +00:00
string firstName = inDto.FirstName;
string lastName = inDto.LastName;
string jobTitle = inDto.JobTitle;
int companyId = inDto.CompanyId;
string about = inDto.About;
ShareType shareType = inDto.ShareType;
IEnumerable<Guid> managerList = inDto.ManagerList;
IEnumerable<ItemKeyValuePair<int, string>> customFieldList = inDto.CustomFieldList;
IEnumerable<IFormFile> photo = inDto.Photos;
2020-04-16 19:41:37 +00:00
if (personid <= 0 || string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName)) throw new ArgumentException();
var peopleInst = new Person
{
ID = personid,
FirstName = firstName,
LastName = lastName,
JobTitle = jobTitle,
CompanyID = companyId,
About = about,
ShareType = shareType
};
2020-04-16 19:41:37 +00:00
DaoFactory.GetContactDao().UpdateContact(peopleInst);
peopleInst = (Person)DaoFactory.GetContactDao().GetByID(peopleInst.ID);
var managerListLocal = managerList != null ? managerList.ToList() : new List<Guid>();
if (managerListLocal.Any())
{
CRMSecurity.SetAccessTo(peopleInst, managerListLocal);
}
if (customFieldList != null)
{
var existingCustomFieldList = DaoFactory.GetCustomFieldDao().GetFieldsDescription(EntityType.Person).Select(fd => fd.ID).ToList();
foreach (var field in customFieldList)
{
if (string.IsNullOrEmpty(field.Value) || !existingCustomFieldList.Contains(field.Key)) continue;
DaoFactory.GetCustomFieldDao().SetFieldValue(EntityType.Person, peopleInst.ID, field.Key, field.Value);
}
}
2021-03-09 15:37:16 +00:00
var outDto = (PersonDto)ContactDtoHelper.GetContactDto(peopleInst);
2020-04-22 20:46:49 +00:00
var photoList = photo != null ? photo.ToList() : new List<IFormFile>();
2020-04-16 19:41:37 +00:00
if (photoList.Any())
{
2021-03-05 20:06:49 +00:00
outDto.SmallFotoUrl = ChangeContactPhoto(peopleInst.ID, photoList);
2020-04-16 19:41:37 +00:00
}
MessageService.Send(MessageAction.PersonUpdated, MessageTarget.Create(peopleInst.ID), peopleInst.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
return outDto;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates the company with the parameters specified in the request
/// </summary>
/// <param name="companyName">Company name</param>
/// <param optional="true" name="about">Company description text</param>
/// <param optional="true" name="personList">Linked person list</param>
/// <param name="shareType">Company privacy: 0 - not shared, 1 - shared for read/write, 2 - shared for read only</param>
/// <param optional="true" name="managerList">List of managers for the company</param>
/// <param optional="true" name="customFieldList">User field list</param>
/// <param optional="true" name="photo">Contact photo (upload using multipart/form-data)</param>
/// <short>Create company</short>
/// <category>Contacts</category>
/// <returns>Company</returns>
/// <exception cref="ArgumentException"></exception>
[Create(@"contact/company")]
2021-03-09 15:37:16 +00:00
public CompanyDto CreateCompany(
2021-03-05 20:06:49 +00:00
[FromForm] CreateOrUpdateCompanyInDto inDto)
2020-04-16 19:41:37 +00:00
{
2021-03-05 20:06:49 +00:00
var personList = inDto.PersonList;
string companyName = inDto.CompanyName;
string about = inDto.About;
ShareType shareType = inDto.ShareType;
IEnumerable<Guid> managerList = inDto.ManagerList;
IEnumerable<ItemKeyValuePair<int, string>> customFieldList = inDto.CustomFieldList;
IEnumerable<IFormFile> photo = inDto.Photos;
2020-04-16 19:41:37 +00:00
var companyInst = new Company
{
CompanyName = companyName,
About = about,
ShareType = shareType
};
2020-04-16 19:41:37 +00:00
companyInst.ID = DaoFactory.GetContactDao().SaveContact(companyInst);
2020-04-18 17:49:09 +00:00
companyInst.CreateBy = SecurityContext.CurrentAccount.ID;
2020-04-16 19:41:37 +00:00
companyInst.CreateOn = DateTime.UtcNow;
if (personList != null)
{
foreach (var personID in personList)
{
var person = DaoFactory.GetContactDao().GetByID(personID);
if (person == null || !CRMSecurity.CanAccessTo(person)) continue;
AddPeopleToCompany(companyInst.ID, personID);
}
}
var managerListLocal = managerList != null ? managerList.ToList() : new List<Guid>();
if (managerListLocal.Any())
{
CRMSecurity.SetAccessTo(companyInst, managerListLocal);
}
if (customFieldList != null)
{
var existingCustomFieldList = DaoFactory.GetCustomFieldDao().GetFieldsDescription(EntityType.Company).Select(fd => fd.ID).ToList();
foreach (var field in customFieldList)
{
if (string.IsNullOrEmpty(field.Value) || !existingCustomFieldList.Contains(field.Key)) continue;
DaoFactory.GetCustomFieldDao().SetFieldValue(EntityType.Company, companyInst.ID, field.Key, field.Value);
}
}
2021-03-09 15:37:16 +00:00
var wrapper = (CompanyDto)ContactDtoHelper.GetContactDto(companyInst);
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
var photoList = photo != null ? photo.ToList() : new List<IFormFile>();
2020-04-16 19:41:37 +00:00
if (photoList.Any())
{
wrapper.SmallFotoUrl = ChangeContactPhoto(companyInst.ID, photoList);
}
MessageService.Send(MessageAction.CompanyCreated, MessageTarget.Create(companyInst.ID), companyInst.GetTitle());
2020-04-16 19:41:37 +00:00
return wrapper;
}
/// <summary>
/// Quickly creates the list of companies
/// </summary>
/// <short>
/// Quick company list creation
/// </short>
/// <param name="companyName">Company name</param>
/// <category>Contacts</category>
/// <returns>Contact list</returns>
/// <exception cref="ArgumentException"></exception>
[Create(@"contact/company/quick")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactBaseDto> CreateCompany(IEnumerable<string> companyName)
2020-04-16 19:41:37 +00:00
{
if (companyName == null) throw new ArgumentException();
var contacts = new List<Contact>();
var recordIndex = 0;
foreach (var item in companyName)
{
if (string.IsNullOrEmpty(item)) continue;
contacts.Add(new Company
{
ID = recordIndex++,
CompanyName = item,
ShareType = ShareType.None
});
2020-04-16 19:41:37 +00:00
}
if (contacts.Count == 0) return null;
DaoFactory.GetContactDao().SaveContactList(contacts);
var selectedManagers = new List<Guid> { SecurityContext.CurrentAccount.ID };
2020-04-16 19:41:37 +00:00
foreach (var ct in contacts)
{
CRMSecurity.SetAccessTo(ct, selectedManagers);
}
2021-03-09 15:37:16 +00:00
return contacts.ConvertAll(x => ContactDtoHelper.GetContactBaseDto(x));
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Quickly creates the list of persons with the first and last names specified in the request
/// </summary>
/// <short>
/// Quick person list creation
/// </short>
/// <param name="data">Pairs: user first name, user last name</param>
/// <remarks>
/// <![CDATA[
/// data has format
/// [{key: 'First name 1', value: 'Last name 1'},{key: 'First name 2', value: 'Last name 2'}]
/// ]]>
/// </remarks>
/// <category>Contacts</category>
/// <returns>Contact list</returns>
/// <exception cref="ArgumentException"></exception>
[Create(@"contact/person/quick")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactBaseDto> CreatePerson(IEnumerable<ItemKeyValuePair<string, string>> data)
2020-04-16 19:41:37 +00:00
{
if (data == null) return null;
var contacts = new List<Contact>();
var recordIndex = 0;
foreach (var item in data)
{
if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value)) continue;
contacts.Add(new Person
{
ID = recordIndex++,
FirstName = item.Key,
LastName = item.Value,
ShareType = ShareType.None
});
2020-04-16 19:41:37 +00:00
}
if (contacts.Count == 0) return null;
DaoFactory.GetContactDao().SaveContactList(contacts);
var selectedManagers = new List<Guid> { SecurityContext.CurrentAccount.ID };
2020-04-16 19:41:37 +00:00
foreach (var ct in contacts)
{
CRMSecurity.SetAccessTo(ct, selectedManagers);
}
MessageService.Send(MessageAction.PersonsCreated, MessageTarget.Create(contacts.Select(x => x.ID)), contacts.Select(x => x.GetTitle()));
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return contacts.ConvertAll(x => ContactDtoHelper.GetContactBaseDto(x));
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the selected company with the parameters specified in the request
/// </summary>
/// <param name="companyid">Company ID</param>
/// <param name="companyName">Company name</param>
/// <param optional="true" name="about">Company description text</param>
/// <param name="shareType">Company privacy: 0 - not shared, 1 - shared for read/write, 2 - shared for read only</param>
/// <param optional="true" name="managerList">List of company managers</param>
/// <param optional="true" name="customFieldList">User field list</param>
/// <short>Update company</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Company
/// </returns>
[Update(@"contact/company/{companyid:int}")]
2021-03-09 15:37:16 +00:00
public CompanyDto UpdateCompany(
2021-03-13 18:00:08 +00:00
[FromQuery] int companyid,
2021-03-05 20:06:49 +00:00
[FromForm] CreateOrUpdateCompanyInDto intDto)
2020-04-16 19:41:37 +00:00
{
2021-03-05 20:06:49 +00:00
string companyName = intDto.CompanyName;
string about = intDto.About;
ShareType shareType = intDto.ShareType;
IEnumerable<Guid> managerList = intDto.ManagerList;
IEnumerable<ItemKeyValuePair<int, string>> customFieldList = intDto.CustomFieldList;
IEnumerable<IFormFile> photo = intDto.Photos;
2020-04-16 19:41:37 +00:00
var companyInst = new Company
{
ID = companyid,
CompanyName = companyName,
About = about,
ShareType = shareType
};
2020-04-16 19:41:37 +00:00
DaoFactory.GetContactDao().UpdateContact(companyInst);
companyInst = (Company)DaoFactory.GetContactDao().GetByID(companyInst.ID);
var managerListLocal = managerList != null ? managerList.ToList() : new List<Guid>();
2020-04-16 19:41:37 +00:00
if (managerListLocal.Any())
{
CRMSecurity.SetAccessTo(companyInst, managerListLocal);
}
if (customFieldList != null)
{
var existingCustomFieldList = DaoFactory.GetCustomFieldDao().GetFieldsDescription(EntityType.Company).Select(fd => fd.ID).ToList();
foreach (var field in customFieldList)
{
if (string.IsNullOrEmpty(field.Value) || !existingCustomFieldList.Contains(field.Key)) continue;
DaoFactory.GetCustomFieldDao().SetFieldValue(EntityType.Company, companyInst.ID, field.Key, field.Value);
}
}
MessageService.Send(MessageAction.CompanyUpdated, MessageTarget.Create(companyInst.ID), companyInst.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return (CompanyDto)ContactDtoHelper.GetContactDto(companyInst);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the selected contact status
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="contactStatusid">Contact status ID</param>
/// <short>Update status in contact by id</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Company
/// </returns>
[Update(@"contact/{contactid:int}/status")]
2021-03-09 15:37:16 +00:00
public ContactDto UpdateContactStatus(int contactid, int contactStatusid)
2020-04-16 19:41:37 +00:00
{
if (contactid <= 0 || contactStatusid < 0) throw new ArgumentException();
var dao = DaoFactory.GetContactDao();
if (contactStatusid > 0)
{
var curListItem = DaoFactory.GetListItemDao().GetByID(contactStatusid);
if (curListItem == null) throw new ItemNotFoundException();
}
var companyInst = dao.GetByID(contactid);
if (companyInst == null || !CRMSecurity.CanAccessTo(companyInst)) throw new ItemNotFoundException();
if (!CRMSecurity.CanEdit(companyInst)) throw CRMSecurity.CreateSecurityException();
dao.UpdateContactStatus(new List<int> { companyInst.ID }, contactStatusid);
2020-04-16 19:41:37 +00:00
companyInst.StatusID = contactStatusid;
var messageAction = companyInst is Company ? MessageAction.CompanyUpdatedTemperatureLevel : MessageAction.PersonUpdatedTemperatureLevel;
MessageService.Send(messageAction, MessageTarget.Create(companyInst.ID), companyInst.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return ContactDtoHelper.GetContactDto(companyInst);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates status of the selected company and all its participants
/// </summary>
/// <param name="companyid">Company ID</param>
/// <param name="contactStatusid">Contact status ID</param>
/// <short>Update company and participants status</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Company
/// </returns>
[Update(@"contact/company/{companyid:int}/status")]
2021-03-09 15:37:16 +00:00
public ContactDto UpdateCompanyAndParticipantsStatus(int companyid, int contactStatusid)
2020-04-16 19:41:37 +00:00
{
if (companyid <= 0 || contactStatusid < 0) throw new ArgumentException();
var dao = DaoFactory.GetContactDao();
if (contactStatusid > 0)
{
var curListItem = DaoFactory.GetListItemDao().GetByID(contactStatusid);
if (curListItem == null) throw new ItemNotFoundException();
}
var companyInst = dao.GetByID(companyid);
if (companyInst == null || !CRMSecurity.CanAccessTo(companyInst)) throw new ItemNotFoundException();
if (companyInst is Person) throw new Exception(CRMErrorsResource.ContactIsNotCompany);
var forUpdateStatus = new List<int>();
forUpdateStatus.Add(companyInst.ID);
var members = dao.GetMembersIDsAndShareType(companyInst.ID);
foreach (var m in members)
{
if (CRMSecurity.CanAccessTo(m.Key, EntityType.Person, m.Value, 0))
{
forUpdateStatus.Add(m.Key);
}
}
dao.UpdateContactStatus(forUpdateStatus, contactStatusid);
2020-04-22 20:46:49 +00:00
MessageService.Send(MessageAction.CompanyUpdatedTemperatureLevel, MessageTarget.Create(companyInst.ID), companyInst.GetTitle());
MessageService.Send(MessageAction.CompanyUpdatedPersonsTemperatureLevel, MessageTarget.Create(companyInst.ID), companyInst.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return ContactDtoHelper.GetContactDto(companyInst);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates status of the selected person, related company and all its participants
/// </summary>
/// <param name="personid">Person ID</param>
/// <param name="contactStatusid">Contact status ID</param>
/// <short>Update person, related company and participants status</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Person
/// </returns>
[Update(@"contact/person/{personid:int}/status")]
2021-03-09 15:37:16 +00:00
public ContactDto UpdatePersonAndItsCompanyStatus(int personid, int contactStatusid)
2020-04-16 19:41:37 +00:00
{
if (personid <= 0 || contactStatusid < 0) throw new ArgumentException();
if (contactStatusid > 0)
{
var curListItem = DaoFactory.GetListItemDao().GetByID(contactStatusid);
if (curListItem == null) throw new ItemNotFoundException();
}
var dao = DaoFactory.GetContactDao();
var personInst = dao.GetByID(personid);
if (personInst == null || !CRMSecurity.CanAccessTo(personInst)) throw new ItemNotFoundException();
if (personInst is Company) throw new Exception(CRMErrorsResource.ContactIsNotPerson);
var forUpdateStatus = new List<int>();
var companyID = ((Person)personInst).CompanyID;
if (companyID != 0)
{
var companyInst = dao.GetByID(companyID);
if (companyInst == null) throw new ItemNotFoundException();
if (!CRMSecurity.CanAccessTo(companyInst))
{
forUpdateStatus.Add(personInst.ID);
dao.UpdateContactStatus(forUpdateStatus, contactStatusid);
}
else
{
forUpdateStatus.Add(companyInst.ID);
var members = dao.GetMembersIDsAndShareType(companyInst.ID);
foreach (var m in members)
{
if (CRMSecurity.CanAccessTo(m.Key, EntityType.Person, m.Value, 0))
{
forUpdateStatus.Add(m.Key);
}
}
dao.UpdateContactStatus(forUpdateStatus, contactStatusid);
}
}
else
{
forUpdateStatus.Add(personInst.ID);
dao.UpdateContactStatus(forUpdateStatus, contactStatusid);
}
MessageService.Send(MessageAction.PersonUpdatedTemperatureLevel, MessageTarget.Create(personInst.ID), personInst.GetTitle());
MessageService.Send(MessageAction.PersonUpdatedCompanyTemperatureLevel, MessageTarget.Create(personInst.ID), personInst.GetTitle());
2020-04-16 19:41:37 +00:00
personInst = dao.GetByID(personInst.ID);
2021-03-09 15:37:16 +00:00
return ContactDtoHelper.GetContactDto(personInst);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Get access rights to the contact with the ID specified in the request
/// </summary>
/// <short>Get contact access rights</short>
/// <category>Contacts</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
///<exception cref="SecurityException"></exception>
/// <returns>User list</returns>
[Read(@"contact/{contactid:int}/access")]
public IEnumerable<EmployeeWraper> GetContactAccessList(int contactid)
{
if (contactid <= 0) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
if (contact == null) throw new ItemNotFoundException();
if (!CRMSecurity.CanAccessTo(contact)) throw CRMSecurity.CreateSecurityException();
return CRMSecurity.IsPrivate(contact)
? CRMSecurity.GetAccessSubjectTo(contact)
2020-04-18 17:49:09 +00:00
.Select(item => EmployeeWraperHelper.Get(item.Key))
2020-04-16 19:41:37 +00:00
: new List<EmployeeWraper>();
}
/// <summary>
/// Sets access rights for other users to the contact with the ID specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="isShared">Contact privacy: private or not</param>
/// <param name="managerList">List of managers</param>
/// <short>Set contact access rights</short>
/// <category>Contacts</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="SecurityException"></exception>
///<exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Contact
/// </returns>
[Update(@"contact/{contactid:int}/access")]
2021-03-09 15:37:16 +00:00
public ContactDto SetAccessToContact(int contactid, bool isShared, IEnumerable<Guid> managerList)
2020-04-16 19:41:37 +00:00
{
if (contactid <= 0) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().GetByID(contactid);
if (contact == null) throw new ItemNotFoundException();
if (!CRMSecurity.CanEdit(contact)) throw CRMSecurity.CreateSecurityException();
SetAccessToContact(contact, isShared, managerList, false);
2021-03-09 15:37:16 +00:00
var wrapper = ContactDtoHelper.GetContactDto(contact);
2020-04-22 20:46:49 +00:00
2020-04-16 19:41:37 +00:00
return wrapper;
}
private void SetAccessToContact(Contact contact, bool isShared, IEnumerable<Guid> managerList, bool isNotify)
{
var managerListLocal = managerList != null ? managerList.Distinct().ToList() : new List<Guid>();
if (managerListLocal.Any())
{
if (isNotify)
{
2020-04-18 17:49:09 +00:00
var notifyUsers = managerListLocal.Where(n => n != SecurityContext.CurrentAccount.ID).ToArray();
2020-04-16 19:41:37 +00:00
if (contact is Person)
NotifyClient.SendAboutSetAccess(EntityType.Person, contact.ID, DaoFactory, notifyUsers);
2020-04-16 19:41:37 +00:00
else
NotifyClient.SendAboutSetAccess(EntityType.Company, contact.ID, DaoFactory, notifyUsers);
2020-04-16 19:41:37 +00:00
}
CRMSecurity.SetAccessTo(contact, managerListLocal);
}
else
{
CRMSecurity.MakePublic(contact);
}
DaoFactory.GetContactDao().MakePublic(contact.ID, isShared);
}
/// <summary>
/// Sets access rights for other users to the list of contacts with the IDs specified in the request
/// </summary>
/// <param name="contactid">Contact ID list</param>
/// <param name="isShared">Company privacy: shared or not</param>
/// <param name="managerList">List of managers</param>
/// <short>Set contact access rights</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Contact list
/// </returns>
[Update(@"contact/access")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> SetAccessToBatchContact(
2021-03-13 18:00:08 +00:00
[FromBody] SetAccessToBatchContactInDto inDto)
2020-04-16 19:41:37 +00:00
{
2021-03-05 20:06:49 +00:00
var contactid = inDto.ContactID;
var isShared = inDto.isShared;
var managerList = inDto.ManagerList;
2020-04-16 19:41:37 +00:00
if (contactid == null) throw new ArgumentException();
2021-03-09 15:37:16 +00:00
var result = new List<ContactDto>();
2020-04-16 19:41:37 +00:00
foreach (var id in contactid)
{
2021-03-09 15:37:16 +00:00
var contactDto = SetAccessToContact(id, isShared, managerList);
result.Add(contactDto);
2020-04-16 19:41:37 +00:00
}
return result;
}
/// <summary>
/// Sets access rights for the selected user to the list of contacts with the parameters specified in the request
/// </summary>
/// <param name="isPrivate">Contact privacy: private or not</param>
/// <param name="managerList">List of managers</param>
/// <param optional="true" name="tags">Tag</param>
/// <param optional="true" name="contactStage">Contact stage ID (warmth)</param>
/// <param optional="true" name="contactType">Contact type ID</param>
/// <param optional="true" name="contactListView" remark="Allowed values: Company, Person, WithOpportunity"></param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <short>Set contact access rights</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Contact list
/// </returns>
[Update(@"contact/filter/access")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> SetAccessToBatchContact(
2021-03-05 20:06:49 +00:00
[FromForm] SetAccessToBatchContactByFilterInDto inDto)
2020-04-16 19:41:37 +00:00
{
2021-03-05 20:06:49 +00:00
IEnumerable<String> tags = inDto.Tags;
int? contactStage = inDto.ContactStage;
int? contactType = inDto.ContactType;
ContactListViewType contactListView = inDto.ContactListView;
ApiDateTime fromDate = inDto.FromDate;
ApiDateTime toDate = inDto.ToDate;
bool isPrivate = inDto.isPrivate;
IEnumerable<Guid> managerList = inDto.ManagerList;
2020-04-16 19:41:37 +00:00
int contactStageInt = contactStage.HasValue ? contactStage.Value : -1;
int contactTypeInt = contactType.HasValue ? contactType.Value : -1;
var result = new List<Contact>();
var contacts = DaoFactory.GetContactDao().GetContacts(
ApiContext.FilterValue,
tags,
contactStageInt,
contactTypeInt,
contactListView,
fromDate, toDate,
0, 0, null);
if (!contacts.Any())
2021-03-09 15:37:16 +00:00
return Enumerable.Empty<ContactDto>();
2020-04-16 19:41:37 +00:00
foreach (var contact in contacts)
{
if (contact == null)
throw new ItemNotFoundException();
if (!CRMSecurity.CanEdit(contact)) continue;
SetAccessToContact(contact, isPrivate, managerList, false);
result.Add(contact);
}
2021-03-10 15:38:56 +00:00
return ContactDtoHelper.ToListContactDto(result);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the contact with the ID specified in the request from the portal
/// </summary>
/// <short>Delete contact</short>
/// <category>Contacts</category>
/// <param name="contactid">Contact ID</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Contact
/// </returns>
[Delete(@"contact/{contactid:int}")]
2021-03-09 15:37:16 +00:00
public ContactDto DeleteContact(int contactid)
2020-04-16 19:41:37 +00:00
{
if (contactid <= 0) throw new ArgumentException();
var contact = DaoFactory.GetContactDao().DeleteContact(contactid);
if (contact == null) throw new ItemNotFoundException();
var messageAction = contact is Person ? MessageAction.PersonDeleted : MessageAction.CompanyDeleted;
MessageService.Send(messageAction, MessageTarget.Create(contact.ID), contact.GetTitle());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return ContactDtoHelper.GetContactDto(contact);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the group of contacts with the IDs specified in the request
/// </summary>
/// <param name="contactids">Contact ID list</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <short>Delete contact group</short>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
[Update(@"contact")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactBaseDto> DeleteBatchContacts(IEnumerable<int> contactids)
2020-04-16 19:41:37 +00:00
{
if (contactids == null) throw new ArgumentException();
var contacts = DaoFactory.GetContactDao().DeleteBatchContact(contactids.ToArray());
MessageService.Send(MessageAction.ContactsDeleted, MessageTarget.Create(contactids), contacts.Select(c => c.GetTitle()));
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
return contacts.Select(x => ContactDtoHelper.GetContactBaseDto(x));
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of 30 contacts in the CRM module with prefix
/// </summary>
/// <param optional="true" name="prefix"></param>
/// <param optional="false" name="searchType" remark="Allowed values: -1 (Any), 0 (Company), 1 (Persons), 2 (PersonsWithoutCompany), 3 (CompaniesAndPersonsWithoutCompany)">searchType</param>
/// <param optional="true" name="entityType"></param>
/// <param optional="true" name="entityID"></param>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
/// <visible>false</visible>
[Read(@"contact/byprefix")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactBaseWithPhoneDto> GetContactsByPrefix(string prefix, int searchType, EntityType entityType, int entityID)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
var result = new List<ContactBaseWithPhoneDto>();
2020-04-16 19:41:37 +00:00
var allContacts = new List<Contact>();
if (entityID > 0)
{
var findedContacts = new List<Contact>();
switch (entityType)
{
case EntityType.Opportunity:
allContacts = DaoFactory.GetContactDao().GetContacts(DaoFactory.GetDealDao().GetMembers(entityID));
break;
case EntityType.Case:
allContacts = DaoFactory.GetContactDao().GetContacts(DaoFactory.GetCasesDao().GetMembers(entityID));
break;
}
foreach (var c in allContacts)
{
var person = c as Person;
if (person != null)
{
var people = person;
2020-04-18 17:49:09 +00:00
2021-03-01 17:36:18 +00:00
if (UserFormatter.GetUserName(people.FirstName, people.LastName).IndexOf(prefix, StringComparison.Ordinal) != -1)
2020-04-16 19:41:37 +00:00
{
findedContacts.Add(person);
}
}
else
{
var company = (Company)c;
if (company.CompanyName.IndexOf(prefix, StringComparison.Ordinal) != -1)
{
findedContacts.Add(c);
}
}
}
2021-03-09 15:37:16 +00:00
result.AddRange(findedContacts.Select(x => ContactDtoHelper.GetContactBaseWithPhoneDto(x)));
2020-04-22 20:46:49 +00:00
2020-04-16 19:41:37 +00:00
ApiContext.SetTotalCount(findedContacts.Count);
2020-04-16 19:41:37 +00:00
}
else
{
const int maxItemCount = 30;
if (searchType < -1 || searchType > 3) throw new ArgumentException();
allContacts = DaoFactory.GetContactDao().GetContactsByPrefix(prefix, searchType, 0, maxItemCount);
2021-03-09 15:37:16 +00:00
result.AddRange(allContacts.Select(x => ContactDtoHelper.GetContactBaseWithPhoneDto(x)));
2020-04-16 19:41:37 +00:00
}
return result;
}
/// <summary>
/// Returns the list contacts in the CRM module with contact information
/// </summary>
/// <param optional="false" name="infoType">Contact information type</param>
/// <param optional="false" name="data">Data</param>
/// <param optional="true" name="category">Category</param>
/// <param optional="true" name="isPrimary">Contact importance: primary or not</param>
/// <category>Contacts</category>
/// <returns>
/// Contact list
/// </returns>
[Read(@"contact/bycontactinfo")]
2021-03-09 15:37:16 +00:00
public IEnumerable<ContactDto> GetContactsByContactInfo(ContactInfoType? infoType, String data, int? category, bool? isPrimary)
2020-04-16 19:41:37 +00:00
{
if (!infoType.HasValue) throw new ArgumentException();
var ids = DaoFactory.GetContactDao().GetContactIDsByContactInfo(infoType.Value, data, category, isPrimary);
2021-03-09 15:37:16 +00:00
var result = DaoFactory.GetContactDao().GetContacts(ids.ToArray()).ConvertAll(x => ContactDtoHelper.GetContactDto(x));
2020-04-16 19:41:37 +00:00
return result;
}
2020-04-22 20:46:49 +00:00
///// <summary>
/////
///// </summary>
///// <param name="contactid"></param>
///// <param name="count"></param>
///// <category>Contacts</category>
///// <returns></returns>
//[Read(@"contact/{contactid:int}/tweets")]
//public List<Message> GetUserTweets(int contactid, int count)
//{
// var MessageCount = 10;
// var twitterAccounts = DaoFactory.GetContactInfoDao().GetList(contactid, ContactInfoType.Twitter, null, null);
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// if (twitterAccounts.Count == 0)
// throw new ResourceNotFoundException(
// Newtonsoft.Json.JsonConvert.SerializeObject(
// new
// {
// message = "",
// description = CRMSocialMediaResource.SocialMediaAccountNotFoundTwitter
// }
// ));
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// var apiInfo = TwitterApiHelper.GetTwitterApiInfoForCurrentUser();
// TwitterDataProvider twitterProvider = new TwitterDataProvider(apiInfo);
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// List<Message> messages = new List<Message>();
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// foreach (var twitterAccount in twitterAccounts)
// {
// try
// {
// messages.AddRange(twitterProvider.GetUserTweets(twitterAccount.ID, twitterAccount.Data, MessageCount));
// }
// catch (ResourceNotFoundException ex)
// {
// throw new ResourceNotFoundException(
// Newtonsoft.Json.JsonConvert.SerializeObject(
// new
// {
// message = ex.Message,
// description = String.Format("{0}: {1}", CRMSocialMediaResource.ErrorUnknownTwitterAccount, twitterAccount.Data)
// }
// ));
// }
// catch (Exception ex)
// {
// throw new Exception(
// Newtonsoft.Json.JsonConvert.SerializeObject(
// new
// {
// message = ex.Message,
// description = String.Format("{0}: {1}", CRMSocialMediaResource.ErrorUnknownTwitterAccount, twitterAccount.Data)
// }
// ));
// }
// }
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// return messages.OrderByDescending(m => m.PostedOn).Take(MessageCount).ToList();
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
//}
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
///// <summary>
/////
///// </summary>
///// <param name="searchText"></param>
///// <category>Contacts</category>
///// <returns></returns>
//[Read(@"contact/twitterprofile")]
//public List<TwitterUserInfo> FindTwitterProfiles(string searchText)
//{
// try
// {
// TwitterApiInfo apiInfo = TwitterApiHelper.GetTwitterApiInfoForCurrentUser();
// if (apiInfo == null)
// throw new SocialMediaAccountNotFound(CRMSocialMediaResource.SocialMediaAccountNotFoundTwitter);
// TwitterDataProvider provider = new TwitterDataProvider(apiInfo);
// List<TwitterUserInfo> users = provider.FindUsers(searchText);
// /*List<TwitterUserInfo> users = new List<TwitterUserInfo>();
// users.Add(new TwitterUserInfo { Description = "I'm a cool user", SmallImageUrl = "http://localhost/TeamLab/products/crm/data/0/photos/00/00/10/contact_10_50_50.jpg", UserName = "User", ScreenName = "user", UserID = 1 });
// users.Add(new TwitterUserInfo { Description = "I'm a cool user", SmallImageUrl = "http://localhost/TeamLab/products/crm/data/0/photos/00/00/10/contact_10_50_50.jpg", UserName = "User", ScreenName = "user", UserID = 1 });
// users.Add(new TwitterUserInfo { Description = "I'm a cool user", SmallImageUrl = "http://localhost/TeamLab/products/crm/data/0/photos/00/00/10/contact_10_50_50.jpg", UserName = "User", ScreenName = "user", UserID = 1 });*/
// return users;
// }
// catch (Exception ex) {
2021-03-10 15:38:56 +00:00
// throw new SocialMediaUI(DaoFactory).ProcessError(ex, "ASC.CRM.Api.CRMApi.FindTwitterProfiles");
2020-04-22 20:46:49 +00:00
// }
//}
2020-04-16 19:41:37 +00:00
/// <summary>
///
/// </summary>
/// <param name="contactId"></param>
/// <param name="contactType"></param>
/// <param name="uploadOnly"></param>
/// <category>Contacts</category>
/// <returns></returns>
[Delete(@"contact/{contactid:int}/avatar")]
public string DeleteContactAvatar(int contactId, string contactType, bool uploadOnly)
{
bool isCompany;
if (contactId != 0)
{
var contact = DaoFactory.GetContactDao().GetByID(contactId);
if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
if (!CRMSecurity.CanEdit(contact)) throw CRMSecurity.CreateSecurityException();
isCompany = contact is Company;
}
else
{
isCompany = contactType != "people";
}
if (!uploadOnly)
{
ContactPhotoManager.DeletePhoto(contactId);
return ContactPhotoManager.GetBigSizePhoto(0, isCompany);
}
return "";
}
2020-04-22 20:46:49 +00:00
///// <summary>
/////
///// </summary>
///// <param name="contactId"></param>
///// <category>Contacts</category>
///// <returns></returns>
//[Read(@"contact/{contactid:int}/socialmediaavatar")]
//public List<SocialMediaImageDescription> GetContactSMImages(int contactId)
//{
// return new SocialMediaUI(DaoFactory).GetContactSMImages(contactId);
//}
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
///// <summary>
/////
///// </summary>
///// <param name="socialNetworks"></param>
///// <category>Contacts</category>
///// <returns></returns>
//[Create(@"contact/socialmediaavatar")]
2021-03-09 15:37:16 +00:00
//public List<SocialMediaImageDescription> GetContactSMImagesByNetworks(List<ContactInfoDto> socialNetworks)
2020-04-22 20:46:49 +00:00
//{
// if (socialNetworks == null || socialNetworks.Count == 0)
// {
// return new List<SocialMediaImageDescription>();
// }
// var twitter = new List<String>();
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// foreach (var sn in socialNetworks)
// {
// if (sn.InfoType == ContactInfoType.Twitter) twitter.Add(sn.Data);
// }
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// return new SocialMediaUI(DaoFactory).GetContactSMImages(twitter);
//}
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
///// <summary>
/////
///// </summary>
///// <param name="contactId"></param>
///// <param name="socialNetwork"></param>
///// <param name="userIdentity"></param>
///// <param name="uploadOnly"></param>
///// <param name="tmpDirName" visible="false"></param>
///// <category>Contacts</category>
///// <returns></returns>
//[Update(@"contact/{contactid:int}/avatar")]
//public ContactPhotoManager.PhotoData UploadUserAvatarFromSocialNetwork(int contactId, SocialNetworks socialNetwork, string userIdentity, bool uploadOnly, string tmpDirName)
//{
// if (socialNetwork != SocialNetworks.Twitter)
// throw new ArgumentException();
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// if (contactId != 0)
// {
// var contact = DaoFactory.GetContactDao().GetByID(contactId);
// if (contact == null || !CRMSecurity.CanAccessTo(contact)) throw new ItemNotFoundException();
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// if (!CRMSecurity.CanEdit(contact)) throw CRMSecurity.CreateSecurityException();
// }
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// if (socialNetwork == SocialNetworks.Twitter)
// {
// var provider = new TwitterDataProvider(TwitterApiHelper.GetTwitterApiInfoForCurrentUser());
// var imageUrl = provider.GetUrlOfUserImage(userIdentity, TwitterDataProvider.ImageSize.Original);
// return UploadAvatar(contactId, imageUrl, uploadOnly, tmpDirName, false);
// }
2020-04-16 19:41:37 +00:00
2020-04-22 20:46:49 +00:00
// return null;
//}
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
/// <visible>false</visible>
[Create(@"contact/mailsmtp/send")]
public IProgressItem SendMailSMTPToContacts(
SendMailSMTPToContactsInDto inDto)
{
List<int> fileIDs = inDto.FileIDs;
List<int> contactIds = inDto.ContactIds;
String subject = inDto.Subject;
String body = inDto.body;
bool storeInHistory = inDto.StoreInHistory;
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
if (contactIds == null || contactIds.Count == 0 || String.IsNullOrEmpty(body)) throw new ArgumentException();
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
var contacts = DaoFactory.GetContactDao().GetContacts(contactIds.ToArray());
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
MessageService.Send(MessageAction.CrmSmtpMailSent, MessageTarget.Create(contactIds), contacts.Select(c => c.GetTitle()));
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
return MailSender.Start(fileIDs, contactIds, subject, body, storeInHistory);
}
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
/// <visible>false</visible>
[Create(@"contact/mailsmtp/preview")]
public string GetMailSMTPToContactsPreview(string template, int contactId)
{
if (contactId == 0 || String.IsNullOrEmpty(template)) throw new ArgumentException();
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
var manager = new MailTemplateManager(DaoFactory);
2020-04-16 19:41:37 +00:00
2021-03-13 18:00:08 +00:00
return manager.Apply(template, contactId);
}
/// <visible>false</visible>
[Read(@"contact/mailsmtp/status")]
public IProgressItem GetMailSMTPToContactsStatus()
{
return MailSender.GetStatus();
}
/// <visible>false</visible>
[Update(@"contact/mailsmtp/cancel")]
public IProgressItem CancelMailSMTPToContacts()
{
var progressItem = MailSender.GetStatus();
MailSender.Cancel();
return progressItem;
}
2020-04-16 19:41:37 +00:00
/// <visible>false</visible>
[Update(@"contact/{contactid:int}/creationdate")]
public void SetContactCreationDate(int contactId, ApiDateTime creationDate)
{
var dao = DaoFactory.GetContactDao();
var contact = dao.GetByID(contactId);
if (contact == null || !CRMSecurity.CanAccessTo(contact))
throw new ItemNotFoundException();
dao.SetContactCreationDate(contactId, creationDate);
}
/// <visible>false</visible>
[Update(@"contact/{contactid:int}/lastmodifeddate")]
public void SetContactLastModifedDate(int contactId, ApiDateTime lastModifedDate)
{
var dao = DaoFactory.GetContactDao();
var contact = dao.GetByID(contactId);
if (contact == null || !CRMSecurity.CanAccessTo(contact))
throw new ItemNotFoundException();
dao.SetContactLastModifedDate(contactId, lastModifedDate);
}
2020-04-16 19:41:37 +00:00
2020-04-18 17:49:09 +00:00
private ContactPhotoManager.PhotoData UploadAvatar(int contactID, string imageUrl, bool uploadOnly, string tmpDirName, bool checkFormat = true)
2020-04-16 19:41:37 +00:00
{
if (contactID != 0)
{
return ContactPhotoManager.UploadPhoto(imageUrl, contactID, uploadOnly, checkFormat);
}
if (string.IsNullOrEmpty(tmpDirName) || tmpDirName == "null") tmpDirName = null;
return ContactPhotoManager.UploadPhotoToTemp(imageUrl, tmpDirName, checkFormat);
}
2021-03-09 15:37:16 +00:00
private IEnumerable<ContactWithTaskDto> ToSimpleListContactDto(IReadOnlyList<Contact> itemList)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
if (itemList.Count == 0) return new List<ContactWithTaskDto>();
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
var result = new List<ContactWithTaskDto>();
2020-04-16 19:41:37 +00:00
var personsIDs = new List<int>();
var companyIDs = new List<int>();
var contactIDs = new int[itemList.Count];
var peopleCompanyIDs = new List<int>();
2021-03-09 15:37:16 +00:00
var peopleCompanyList = new Dictionary<int, ContactBaseDto>();
2020-04-16 19:41:37 +00:00
var contactDao = DaoFactory.GetContactDao();
for (var index = 0; index < itemList.Count; index++)
{
var contact = itemList[index];
if (contact is Company)
{
companyIDs.Add(contact.ID);
}
else
{
var person = contact as Person;
if (person != null)
{
personsIDs.Add(person.ID);
if (person.CompanyID > 0)
{
peopleCompanyIDs.Add(person.CompanyID);
}
}
}
contactIDs[index] = itemList[index].ID;
}
if (peopleCompanyIDs.Count > 0)
{
2021-03-09 15:37:16 +00:00
var tmpList = contactDao.GetContacts(peopleCompanyIDs.ToArray()).ConvertAll(item => ContactDtoHelper.GetContactBaseDtoQuick(item));
2020-04-18 17:49:09 +00:00
var tmpListCanDelete = contactDao.CanDelete(tmpList.Select(item => item.Id).ToArray());
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
foreach (var contactBaseDtoQuick in tmpList)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
contactBaseDtoQuick.CanDelete = contactBaseDtoQuick.CanEdit && tmpListCanDelete[contactBaseDtoQuick.Id];
peopleCompanyList.Add(contactBaseDtoQuick.Id, contactBaseDtoQuick);
2020-04-16 19:41:37 +00:00
}
}
2021-03-09 15:37:16 +00:00
var contactInfos = new Dictionary<int, List<ContactInfoDto>>();
2020-04-16 19:41:37 +00:00
var addresses = new Dictionary<int, List<Address>>();
DaoFactory.GetContactInfoDao().GetAll(contactIDs).ForEach(
item =>
{
if (item.InfoType == ContactInfoType.Address)
{
if (!addresses.ContainsKey(item.ContactID))
{
addresses.Add(item.ContactID, new List<Address>
{
new Address(item)
});
}
else
{
addresses[item.ContactID].Add(new Address(item));
}
}
else
{
if (!contactInfos.ContainsKey(item.ContactID))
{
2021-03-09 15:37:16 +00:00
contactInfos.Add(item.ContactID, new List<ContactInfoDto> { ContactInfoDtoHelper.Get(item) });
2020-04-16 19:41:37 +00:00
}
else
{
2021-03-09 15:37:16 +00:00
contactInfos[item.ContactID].Add(ContactInfoDtoHelper.Get(item));
2020-04-16 19:41:37 +00:00
}
}
}
);
var nearestTasks = DaoFactory.GetTaskDao().GetNearestTask(contactIDs.ToArray());
2021-03-09 15:37:16 +00:00
IEnumerable<TaskCategoryBaseDto> taskCategories = new List<TaskCategoryBaseDto>();
2020-04-16 19:41:37 +00:00
if (nearestTasks.Any())
{
2021-03-09 15:37:16 +00:00
taskCategories = DaoFactory.GetListItemDao().GetItems(ListType.TaskCategory).ConvertAll(item => TaskCategoryDtoHelper.Get(item));
2020-04-16 19:41:37 +00:00
}
foreach (var contact in itemList)
{
2021-03-09 15:37:16 +00:00
ContactDto contactDto;
2020-04-16 19:41:37 +00:00
var person = contact as Person;
if (person != null)
{
var people = person;
2021-03-09 15:37:16 +00:00
var peopleDto = ContactDtoHelper.GetPersonDtoQuick(people);
2020-04-16 19:41:37 +00:00
if (people.CompanyID > 0 && peopleCompanyList.ContainsKey(people.CompanyID))
{
2021-03-09 15:37:16 +00:00
peopleDto.Company = peopleCompanyList[people.CompanyID];
2020-04-16 19:41:37 +00:00
}
2021-03-09 15:37:16 +00:00
contactDto = peopleDto;
2020-04-16 19:41:37 +00:00
}
else
{
var company = contact as Company;
if (company != null)
{
2021-03-09 15:37:16 +00:00
contactDto = ContactDtoHelper.GetCompanyDtoQuick(company);
2020-04-16 19:41:37 +00:00
}
else
{
throw new ArgumentException();
}
}
2021-03-09 15:37:16 +00:00
contactDto.CommonData = contactInfos.ContainsKey(contact.ID) ? contactInfos[contact.ID] : new List<ContactInfoDto>();
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
TaskBaseDto taskDto = null;
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
if (nearestTasks.ContainsKey(contactDto.Id))
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
var task = nearestTasks[contactDto.Id];
2020-04-22 20:46:49 +00:00
2021-03-09 15:37:16 +00:00
taskDto = TaskDtoHelper.GetTaskBaseDto(task);
2020-04-16 19:41:37 +00:00
if (task.CategoryID > 0)
{
2021-03-09 15:37:16 +00:00
taskDto.Category = taskCategories.First(x => x.Id == task.CategoryID);
2020-04-16 19:41:37 +00:00
}
}
2021-03-09 15:37:16 +00:00
result.Add(new ContactWithTaskDto
{
2021-03-09 15:37:16 +00:00
Contact = contactDto,
Task = taskDto
});
2020-04-16 19:41:37 +00:00
}
#region CanDelete for main contacts
if (result.Count > 0)
{
2020-04-18 17:49:09 +00:00
var resultListCanDelete = contactDao.CanDelete(result.Select(item => item.Contact.Id).ToArray());
2021-03-09 15:37:16 +00:00
foreach (var contactBaseDtoQuick in result)
2020-04-16 19:41:37 +00:00
{
2021-03-09 15:37:16 +00:00
contactBaseDtoQuick.Contact.CanDelete = contactBaseDtoQuick.Contact.CanEdit && resultListCanDelete[contactBaseDtoQuick.Contact.Id];
2020-04-16 19:41:37 +00:00
}
}
#endregion
return result;
}
2021-03-13 18:00:08 +00:00
}
2020-04-16 19:41:37 +00:00
}