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 ;
using System.Collections.Generic ;
using System.Linq ;
2021-05-28 15:18:55 +00:00
using System.Text.Json ;
2021-03-19 16:56:26 +00:00
using ASC.Api.CRM ;
2020-04-16 19:41:37 +00:00
using ASC.Common.Web ;
2021-03-19 16:56:26 +00:00
using ASC.CRM.ApiModels ;
2020-04-18 17:49:09 +00:00
using ASC.CRM.Classes ;
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 ;
2022-05-19 15:47:59 +00:00
using ASC.MessagingSystem.Core ;
using ASC.MessagingSystem.Models ;
2020-04-16 19:41:37 +00:00
using ASC.Web.Api.Routing ;
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-03-24 15:55:47 +00:00
using Microsoft.AspNetCore.Mvc ;
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public class ContactInfosController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
private readonly MessageService _messageService ;
private readonly MessageTarget _messageTarget ;
2021-05-05 14:09:05 +00:00
public ContactInfosController ( CrmSecurity crmSecurity ,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory ,
MessageTarget messageTarget ,
MessageService messageService ,
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-19 16:56:26 +00:00
_messageTarget = messageTarget ;
_messageService = messageService ;
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 available contact categories
/// </summary>
/// <param name="infoType">
/// Contact information type
/// </param>
/// <short>Get all categories</short>
/// <category>Contacts</category>
/// <returns>
/// List of all available contact categories
/// </returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"contact/data/{infoType}/category")]
2020-04-16 19:41:37 +00:00
public IEnumerable < string > GetContactInfoCategory ( ContactInfoType infoType )
{
2020-04-18 17:49:09 +00:00
return Enum . GetNames ( ContactInfo . GetCategory ( infoType ) ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of all available contact information types
/// </summary>
/// <short>Get all contact info types</short>
/// <category>Contacts</category>
/// <returns></returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"contact/data/infoType")]
2020-04-16 19:41:37 +00:00
public IEnumerable < string > GetContactInfoType ( )
{
2020-04-18 17:49:09 +00:00
return Enum . GetNames ( typeof ( ContactInfoType ) ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the detailed information for the contact
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <short>Get contact information</short>
/// <category>Contacts</category>
/// <returns>
/// Contact information
/// </returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"contact/{contactid:int}/data")]
2021-03-09 15:37:16 +00:00
public IEnumerable < ContactInfoDto > GetContactInfo ( int contactid )
2020-04-16 19:41:37 +00:00
{
if ( contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +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
2021-03-19 16:56:26 +00:00
var sourceData = _daoFactory . GetContactInfoDao ( ) . GetList ( contactid , null , null , null )
2020-04-16 19:41:37 +00:00
. OrderByDescending ( info = > info . ID )
2021-03-19 16:56:26 +00:00
. ToList ( ) ;
return _mapper . Map < List < ContactInfo > , List < ContactInfoDto > > ( sourceData ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the detailed list of all information available for the contact with the ID specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="id">Contact information ID</param>
/// <short>Get contact info</short>
/// <category>Contacts</category>
/// <returns>Contact information</returns>
///<exception cref="ArgumentException"></exception>
2022-05-31 14:01:58 +00:00
[HttpGet(@"contact/{contactid:int}/data/{id:int}")]
2021-03-09 15:37:16 +00:00
public ContactInfoDto GetContactInfoByID ( int contactid , int id )
2020-04-16 19:41:37 +00:00
{
if ( contactid < = 0 | | id < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +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
2021-03-19 16:56:26 +00:00
var contactInfo = _daoFactory . GetContactInfoDao ( ) . GetByID ( id ) ;
2020-04-16 19:41:37 +00:00
if ( contactInfo = = null | | contactInfo . ContactID ! = contactid ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
return _mapper . Map < ContactInfoDto > ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Adds the information with the parameters specified in the request to the contact with the selected ID
/// </summary>
///<param name="contactid">Contact ID</param>
///<param name="infoType">Contact information type</param>
///<param name="data">Data</param>
///<param name="isPrimary">Contact importance: primary or not</param>
///<param name="category">Category</param>
///<short> Add contact info</short>
///<category>Contacts</category>
/// <seealso cref="GetContactInfoType"/>
/// <seealso cref="GetContactInfoCategory"/>
/// <returns>
/// Contact information
/// </returns>
///<exception cref="ArgumentException"></exception>
2022-05-31 14:01:58 +00:00
[HttpPost(@"contact/{contactid:int}/data")]
2021-03-24 15:55:47 +00:00
public ContactInfoDto CreateContactInfo (
2021-05-28 15:18:55 +00:00
[FromRoute] int contactid ,
2021-06-25 13:58:42 +00:00
[FromBody] CreateContactInfoRequestDto inDto )
2020-04-16 19:41:37 +00:00
{
2021-06-25 13:58:42 +00:00
var data = inDto . Data ;
var infoType = inDto . InfoType ;
var category = inDto . Category ;
var isPrimary = inDto . IsPrimary ;
2020-04-16 19:41:37 +00:00
if ( string . IsNullOrEmpty ( data ) | | contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
2020-04-16 19:41:37 +00:00
if ( contact = = null ) throw new ItemNotFoundException ( ) ;
if ( infoType = = ContactInfoType . Twitter )
{
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanAccessTo ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
}
else
{
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanEdit ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
}
var categoryType = ContactInfo . GetCategory ( infoType ) ;
if ( ! Enum . IsDefined ( categoryType , category ) ) throw new ArgumentException ( ) ;
var contactInfo = new ContactInfo
2021-03-02 16:29:07 +00:00
{
Data = data ,
InfoType = infoType ,
ContactID = contactid ,
IsPrimary = isPrimary ,
Category = ( int ) Enum . Parse ( categoryType , category )
} ;
2020-04-16 19:41:37 +00:00
if ( contactInfo . InfoType = = ContactInfoType . Address )
{
Address res ;
if ( ! Address . TryParse ( contactInfo , out res ) )
throw new ArgumentException ( ) ;
}
2021-03-19 16:56:26 +00:00
var contactInfoID = _daoFactory . GetContactInfoDao ( ) . Save ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
var messageAction = contact is Company ? MessageAction . CompanyUpdatedPrincipalInfo : MessageAction . PersonUpdatedPrincipalInfo ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( contact . ID ) , contact . GetTitle ( ) ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var contactInfoDto = _mapper . Map < ContactInfoDto > ( contactInfo ) ;
2020-04-22 20:46:49 +00:00
2021-03-09 15:37:16 +00:00
contactInfoDto . Id = contactInfoID ;
2020-04-18 17:49:09 +00:00
2021-03-09 15:37:16 +00:00
return contactInfoDto ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Adds the address information to the contact with the selected ID
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="address">Address data</param>
/// <short>Add address info</short>
/// <category>Contacts</category>
/// <seealso cref="GetContactInfoType"/>
/// <seealso cref="GetContactInfoCategory"/>
/// <returns>
/// Contact information
/// </returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
2022-05-31 14:01:58 +00:00
[HttpPost(@"contact/{contactid:int}/addressdata")]
2021-03-24 15:55:47 +00:00
public ContactInfoDto CreateContactInfoAddress ( [ FromRoute ] int contactid , Address address )
2020-04-16 19:41:37 +00:00
{
if ( contactid < = 0 ) throw new ArgumentException ( "Invalid value" , "contactid" ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
if ( contact = = null | | ! _crmSecurity . CanEdit ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
if ( address = = null ) throw new ArgumentException ( "Value cannot be null" , "address" ) ;
if ( ! Enum . IsDefined ( typeof ( AddressCategory ) , address . Category ) ) throw new ArgumentException ( "Value does not fall within the expected range." , "address.Category" ) ;
address . CategoryName = ( ( AddressCategory ) address . Category ) . ToLocalizedString ( ) ;
2021-05-28 15:18:55 +00:00
var settings = new JsonSerializerOptions
2021-03-02 16:29:07 +00:00
{
2021-05-28 15:18:55 +00:00
PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
WriteIndented = true
2021-03-02 16:29:07 +00:00
} ;
2020-04-16 19:41:37 +00:00
var contactInfo = new ContactInfo
2021-03-02 16:29:07 +00:00
{
InfoType = ContactInfoType . Address ,
ContactID = contactid ,
IsPrimary = address . IsPrimary ,
Category = address . Category ,
2021-05-28 15:18:55 +00:00
Data = JsonSerializer . Serialize ( address , settings )
2021-03-02 16:29:07 +00:00
} ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
contactInfo . ID = _daoFactory . GetContactInfoDao ( ) . Save ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
var messageAction = contact is Company ? MessageAction . CompanyUpdatedPrincipalInfo : MessageAction . PersonUpdatedPrincipalInfo ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( contact . ID ) , contact . GetTitle ( ) ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
return _mapper . Map < ContactInfoDto > ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates contact information (add new information to the old list) with the parameters specified in the request for the contact with the selected ID
/// </summary>
///<short>Group contact info</short>
/// <param name="contactid">Contact ID</param>
/// <param name="items">Contact information</param>
/// <remarks>
/// <![CDATA[
/// items has format
/// [{infoType : 1, category : 1, categoryName : 'work', data : "myemail@email.com", isPrimary : true}, {infoType : 0, category : 0, categoryName : 'home', data : "+8999111999111", isPrimary : true}]
/// ]]>
/// </remarks>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Contact information
/// </returns>
/// <visible>false</visible>
2022-05-31 14:01:58 +00:00
[HttpPost(@"contact/{contactid:int}/batch")]
2021-06-23 19:32:56 +00:00
public IEnumerable < ContactInfoDto > CreateBatchContactInfo ( [ FromRoute ] int contactid , [ FromBody ] IEnumerable < ContactInfoDto > items )
2020-04-16 19:41:37 +00:00
{
if ( contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
if ( contact = = null | | ! _crmSecurity . CanEdit ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
var itemsList = items ! = null ? items . ToList ( ) : new List < ContactInfoDto > ( ) ;
var contactInfoList = itemsList . Select ( FromContactInfoDto ) . ToList ( ) ;
2020-04-16 19:41:37 +00:00
foreach ( var contactInfo in contactInfoList )
{
if ( contactInfo . InfoType = = ContactInfoType . Address )
{
Address res ;
2021-03-02 16:29:07 +00:00
if ( ! Address . TryParse ( contactInfo , out res ) )
2020-04-16 19:41:37 +00:00
throw new ArgumentException ( ) ;
}
contactInfo . ContactID = contactid ;
}
2021-03-19 16:56:26 +00:00
var ids = _daoFactory . GetContactInfoDao ( ) . SaveList ( contactInfoList , contact ) ;
2020-04-16 19:41:37 +00:00
for ( var index = 0 ; index < itemsList . Count ; index + + )
{
2021-03-09 15:37:16 +00:00
var infoDto = itemsList [ index ] ;
infoDto . Id = ids [ index ] ;
2020-04-16 19:41:37 +00:00
}
return itemsList ;
}
/// <summary>
/// Updates the information with the parameters specified in the request for the contact with the selected ID
/// </summary>
///<param name="id">Contact information record ID</param>
///<param name="contactid">Contact ID</param>
///<param optional="true" name="infoType">Contact information type</param>
///<param name="data">Data</param>
///<param optional="true" name="isPrimary">Contact importance: primary or not</param>
///<param optional="true" name="category">Contact information category</param>
///<short>Update contact info</short>
///<category>Contacts</category>
///<exception cref="ArgumentException"></exception>
/// <returns>
/// Contact information
/// </returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"contact/{contactid:int}/data/{id:int}")]
2021-03-19 16:56:26 +00:00
public ContactInfoDto UpdateContactInfo ( int id , int contactid , ContactInfoType ? infoType , string data , bool? isPrimary , string category )
2020-04-16 19:41:37 +00:00
{
if ( id < = 0 | | string . IsNullOrEmpty ( data ) | | contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
if ( contact = = null | | ! _crmSecurity . CanEdit ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var contactInfo = _daoFactory . GetContactInfoDao ( ) . GetByID ( id ) ;
2020-04-16 19:41:37 +00:00
if ( infoType ! = null )
{
var categoryType = ContactInfo . GetCategory ( infoType . Value ) ;
if ( ! string . IsNullOrEmpty ( category ) & & Enum . IsDefined ( categoryType , category ) )
{
contactInfo . Category = ( int ) Enum . Parse ( categoryType , category ) ;
}
contactInfo . InfoType = infoType . Value ;
}
contactInfo . ContactID = contactid ;
if ( isPrimary ! = null )
{
contactInfo . IsPrimary = isPrimary . Value ;
}
contactInfo . Data = data ;
if ( contactInfo . InfoType = = ContactInfoType . Address )
{
Address res ;
if ( ! Address . TryParse ( contactInfo , out res ) )
throw new ArgumentException ( ) ;
}
2021-03-19 16:56:26 +00:00
_daoFactory . GetContactInfoDao ( ) . Update ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
var messageAction = contact is Company ? MessageAction . CompanyUpdatedPrincipalInfo : MessageAction . PersonUpdatedPrincipalInfo ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( contact . ID ) , contact . GetTitle ( ) ) ;
var contactInfoDto = _mapper . Map < ContactInfoDto > ( contactInfo ) ;
2020-04-22 20:46:49 +00:00
2021-03-09 15:37:16 +00:00
return contactInfoDto ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the address information with the parameters specified in the request for the contact with the selected ID
/// </summary>
/// <param name="id">Contact information record ID</param>
/// <param name="contactid">Contact ID</param>
/// <param name="address">Address data</param>
/// <short>Update address info</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Contact information
/// </returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"contact/{contactid:int}/addressdata/{id:int}")]
2021-03-09 15:37:16 +00:00
public ContactInfoDto UpdateContactInfoAddress ( int id , int contactid , Address address )
2020-04-16 19:41:37 +00:00
{
if ( id < = 0 ) throw new ArgumentException ( "Invalid value" , "id" ) ;
2021-03-19 16:56:26 +00:00
var contactInfo = _daoFactory . GetContactInfoDao ( ) . GetByID ( id ) ;
2020-04-16 19:41:37 +00:00
if ( contactInfo = = null | | contactInfo . InfoType ! = ContactInfoType . Address ) throw new ItemNotFoundException ( ) ;
if ( contactid < = 0 ) throw new ArgumentException ( "Invalid value" , "contactid" ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
if ( contact = = null | | ! _crmSecurity . CanEdit ( contact ) | | contactInfo . ContactID ! = contactid ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
if ( address = = null ) throw new ArgumentException ( "Value cannot be null" , "address" ) ;
if ( ! Enum . IsDefined ( typeof ( AddressCategory ) , address . Category ) ) throw new ArgumentException ( "Value does not fall within the expected range." , "address.Category" ) ;
2021-03-02 16:29:07 +00:00
address . CategoryName = ( ( AddressCategory ) address . Category ) . ToLocalizedString ( ) ;
2020-04-16 19:41:37 +00:00
2021-05-28 15:18:55 +00:00
var settings = new JsonSerializerOptions
2020-04-16 19:41:37 +00:00
{
2021-05-28 15:18:55 +00:00
PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
WriteIndented = true
2020-04-16 19:41:37 +00:00
} ;
contactInfo . IsPrimary = address . IsPrimary ;
contactInfo . Category = address . Category ;
2021-05-28 15:18:55 +00:00
contactInfo . Data = JsonSerializer . Serialize ( address , settings ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetContactInfoDao ( ) . Update ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
var messageAction = contact is Company ? MessageAction . CompanyUpdatedPrincipalInfo : MessageAction . PersonUpdatedPrincipalInfo ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( contact . ID ) , contact . GetTitle ( ) ) ;
return _mapper . Map < ContactInfoDto > ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates contact information (delete old information and add new list) with the parameters specified in the request for the contact with the selected ID
/// </summary>
///<short>Group contact info update</short>
///<param name="contactid">Contact ID</param>
///<param name="items">Contact information</param>
/// <![CDATA[
/// items has format
/// [{infoType : 1, category : 1, categoryName : 'work', data : "myemail@email.com", isPrimary : true}, {infoType : 0, category : 0, categoryName : 'home', data : "+8999111999111", isPrimary : true}]
/// ]]>
///<category>Contacts</category>
///<exception cref="ArgumentException"></exception>
/// <returns>
/// Contact information
/// </returns>
/// <visible>false</visible>
2022-05-31 14:01:58 +00:00
[HttpPut(@"contact/{contactid:int}/batch")]
2021-03-09 15:37:16 +00:00
public IEnumerable < ContactInfoDto > UpdateBatchContactInfo ( int contactid , IEnumerable < ContactInfoDto > items )
2020-04-16 19:41:37 +00:00
{
if ( contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
if ( contact = = null | | ! _crmSecurity . CanEdit ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
var itemsList = items ! = null ? items . ToList ( ) : new List < ContactInfoDto > ( ) ;
var contactInfoList = itemsList . Select ( FromContactInfoDto ) . ToList ( ) ;
2020-04-16 19:41:37 +00:00
foreach ( var contactInfo in contactInfoList )
{
if ( contactInfo . InfoType = = ContactInfoType . Address )
{
Address res ;
if ( ! Address . TryParse ( contactInfo , out res ) )
throw new ArgumentException ( ) ;
}
contactInfo . ContactID = contactid ;
}
2021-03-19 16:56:26 +00:00
_daoFactory . GetContactInfoDao ( ) . DeleteByContact ( contactid ) ;
var ids = _daoFactory . GetContactInfoDao ( ) . SaveList ( contactInfoList , contact ) ;
2020-04-16 19:41:37 +00:00
for ( var index = 0 ; index < itemsList . Count ; index + + )
{
2021-03-09 15:37:16 +00:00
var infoDto = itemsList [ index ] ;
infoDto . Id = ids [ index ] ;
2020-04-16 19:41:37 +00:00
}
return itemsList ;
}
/// <summary>
/// Returns the detailed information for the contact with the selected ID by the information type specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="infoType">Contact information type</param>
/// <short>Get contact information by type</short>
/// <category>Contacts</category>
/// <returns>
/// Contact information
/// </returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"contact/{contactid:int}/data/{infoType}")]
2020-04-16 19:41:37 +00:00
public IEnumerable < string > GetContactInfo ( int contactid , ContactInfoType infoType )
{
if ( contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +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
2021-03-19 16:56:26 +00:00
return _daoFactory . GetContactInfoDao ( ) . GetListData ( contactid , infoType ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the contact information for the contact with the ID specified in the request
/// </summary>
/// <param name="contactid">Contact ID</param>
/// <param name="id">Contact information record ID</param>
/// <short>Delete contact info</short>
/// <category>Contacts</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Contact information
/// </returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"contact/{contactid:int}/data/{id:int}")]
2021-03-09 15:37:16 +00:00
public ContactInfoDto DeleteContactInfo ( int contactid , int id )
2020-04-16 19:41:37 +00:00
{
if ( id < = 0 | | contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
if ( contact = = null | | ! _crmSecurity . CanEdit ( contact ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var contactInfo = _daoFactory . GetContactInfoDao ( ) . GetByID ( id ) ;
2020-04-16 19:41:37 +00:00
if ( contactInfo = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
var wrapper = _mapper . Map < ContactInfoDto > ( contactInfo ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetContactInfoDao ( ) . Delete ( id ) ;
2020-04-16 19:41:37 +00:00
var messageAction = contact is Company ? MessageAction . CompanyUpdatedPrincipalInfo : MessageAction . PersonUpdatedPrincipalInfo ;
2021-03-02 16:29:07 +00:00
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( contact . ID ) , contact . GetTitle ( ) ) ;
2020-04-16 19:41:37 +00:00
return wrapper ;
}
2022-01-10 15:13:30 +00:00
private ContactInfo FromContactInfoDto ( ContactInfoDto contactInfoDto )
2020-04-16 19:41:37 +00:00
{
return new ContactInfo
2021-03-02 16:29:07 +00:00
{
2021-03-09 15:37:16 +00:00
ID = contactInfoDto . Id ,
Category = contactInfoDto . Category ,
Data = contactInfoDto . Data ,
InfoType = contactInfoDto . InfoType ,
IsPrimary = contactInfoDto . IsPrimary
2021-03-02 16:29:07 +00:00
} ;
2020-04-16 19:41:37 +00:00
}
}
}