2021-03-05 20:06:49 +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 ;
2022-02-10 18:25:40 +00:00
using System.Threading.Tasks ;
2021-03-10 15:38:56 +00:00
2021-03-05 20:06:49 +00:00
using ASC.Api.Core ;
2021-03-10 15:38:56 +00:00
using ASC.Api.CRM ;
2021-03-05 20:06:49 +00:00
using ASC.Common.Web ;
using ASC.Core ;
using ASC.Core.Users ;
2021-03-10 15:38:56 +00:00
using ASC.CRM.ApiModels ;
2021-03-05 20:06:49 +00:00
using ASC.CRM.Core ;
2021-03-10 15:38:56 +00:00
using ASC.CRM.Core.Dao ;
2021-03-05 20:06:49 +00:00
using ASC.CRM.Core.Entities ;
using ASC.CRM.Core.Enums ;
using ASC.MessagingSystem ;
using ASC.Web.Api.Models ;
using ASC.Web.Api.Routing ;
2021-03-10 15:38:56 +00:00
using ASC.Web.Core.Users ;
2021-03-05 20:06:49 +00:00
using ASC.Web.CRM.Classes ;
2021-03-10 15:38:56 +00:00
using ASC.Web.CRM.Services.NotifyService ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
using AutoMapper ;
2021-03-15 11:36:22 +00:00
using Microsoft.AspNetCore.Mvc ;
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2021-03-05 20:06:49 +00:00
{
2021-03-10 15:38:56 +00:00
public class DealsController : BaseApiController
2021-03-05 20:06:49 +00:00
{
2021-03-23 15:41:56 +00:00
private readonly DisplayUserSettingsHelper _displayUserSettingsHelper ;
private readonly NotifyClient _notifyClient ;
private readonly ApiContext _apiContext ;
private readonly MessageService _messageService ;
private readonly MessageTarget _messageTarget ;
private readonly SecurityContext _securityContext ;
private readonly CurrencyProvider _currencyProvider ;
private readonly UserManager _userManager ;
private readonly EmployeeWraperHelper _employeeWraperHelper ;
2021-05-05 14:09:05 +00:00
public DealsController ( CrmSecurity crmSecurity ,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory ,
ApiContext apiContext ,
MessageTarget messageTarget ,
MessageService messageService ,
NotifyClient notifyClient ,
CurrencyProvider currencyProvider ,
UserManager userManager ,
EmployeeWraperHelper employeeWraperHelper ,
2021-03-19 16:56:26 +00:00
DisplayUserSettingsHelper displayUserSettingsHelper ,
2021-03-23 15:41:56 +00:00
SecurityContext securityContext ,
2021-03-19 16:56:26 +00:00
IMapper mapper )
2021-03-23 15:41:56 +00:00
: base ( daoFactory , crmSecurity , mapper )
2021-03-10 15:38:56 +00:00
{
2021-03-23 15:41:56 +00:00
_apiContext = apiContext ;
_messageTarget = messageTarget ;
_messageService = messageService ;
_notifyClient = notifyClient ;
_currencyProvider = currencyProvider ;
_userManager = userManager ;
_employeeWraperHelper = employeeWraperHelper ;
_displayUserSettingsHelper = displayUserSettingsHelper ;
_securityContext = securityContext ;
2021-03-10 15:38:56 +00:00
}
2021-03-05 20:06:49 +00:00
/// <summary>
/// Returns the detailed information about the opportunity with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <returns>
/// Opportunity
/// </returns>
/// <short>Get opportunity by ID</short>
/// <category>Opportunities</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
[Read(@"opportunity/{opportunityid:int}")]
2021-03-09 15:37:16 +00:00
public OpportunityDto GetDealByID ( int opportunityid )
2021-03-05 20:06:49 +00:00
{
if ( opportunityid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var deal = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
if ( deal = = null | | ! _crmSecurity . CanAccessTo ( deal ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
return _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Updates the selected opportunity to the stage with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <param name="stageid">Opportunity stage ID</param>
/// <returns>
/// Opportunity
/// </returns>
/// <short>Update opportunity stage</short>
/// <category>Opportunities</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
[Update(@"opportunity/{opportunityid:int}/stage/{id:int}")]
2021-03-09 15:37:16 +00:00
public OpportunityDto UpdateToDealMilestone ( int opportunityid , int stageid )
2021-03-05 20:06:49 +00:00
{
if ( opportunityid < = 0 | | stageid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var deal = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
if ( deal = = null | | ! _crmSecurity . CanEdit ( deal ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var stage = _daoFactory . GetDealMilestoneDao ( ) . GetByID ( stageid ) ;
2021-03-05 20:06:49 +00:00
if ( stage = = null ) throw new ItemNotFoundException ( ) ;
deal . DealMilestoneID = stageid ;
deal . DealMilestoneProbability = stage . Probability ;
deal . ActualCloseDate = stage . Status ! = DealMilestoneStatus . Open ? DateTime . UtcNow : DateTime . MinValue ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetDealDao ( ) . EditDeal ( deal ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . OpportunityUpdatedStage , _messageTarget . Create ( deal . ID ) , deal . Title ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
return _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Sets access rights for the selected opportunity with the parameters specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <param name="isPrivate">Opportunity privacy: private or not</param>
/// <param name="accessList">List of users with access</param>
/// <short>Set rights to opportunity</short>
/// <category>Opportunities</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Opportunity
/// </returns>
[Update(@"opportunity/{opportunityid:int}/access")]
2021-03-09 15:37:16 +00:00
public OpportunityDto SetAccessToDeal ( int opportunityid , bool isPrivate , IEnumerable < Guid > accessList )
2021-03-05 20:06:49 +00:00
{
if ( opportunityid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var deal = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
2021-03-05 20:06:49 +00:00
if ( deal = = null ) throw new ItemNotFoundException ( ) ;
2021-03-23 15:41:56 +00:00
if ( ! ( _crmSecurity . IsAdmin | | deal . CreateBy = = _securityContext . CurrentAccount . ID ) ) throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
return SetAccessToDeal ( deal , isPrivate , accessList , false , true ) ;
}
2021-03-09 15:37:16 +00:00
private OpportunityDto SetAccessToDeal ( Deal deal , bool isPrivate , IEnumerable < Guid > accessList , bool isNotify , bool isMessageServicSende )
2021-03-05 20:06:49 +00:00
{
var accessListLocal = accessList ! = null ? accessList . Distinct ( ) . ToList ( ) : new List < Guid > ( ) ;
if ( isPrivate & & accessListLocal . Count > 0 )
{
if ( isNotify )
{
2021-03-23 15:41:56 +00:00
accessListLocal = accessListLocal . Where ( u = > u ! = _securityContext . CurrentAccount . ID ) . ToList ( ) ;
_notifyClient . SendAboutSetAccess ( EntityType . Opportunity , deal . ID , _daoFactory , accessListLocal . ToArray ( ) ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-23 15:41:56 +00:00
if ( ! accessListLocal . Contains ( _securityContext . CurrentAccount . ID ) )
2021-03-05 20:06:49 +00:00
{
2021-03-23 15:41:56 +00:00
accessListLocal . Add ( _securityContext . CurrentAccount . ID ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
_crmSecurity . SetAccessTo ( deal , accessListLocal ) ;
2021-03-05 20:06:49 +00:00
if ( isMessageServicSende )
{
2021-03-23 15:41:56 +00:00
var users = _userManager . GetUsers ( ) . Where ( x = > accessListLocal . Contains ( x . ID ) ) ;
_messageService . Send ( MessageAction . OpportunityRestrictedAccess , _messageTarget . Create ( deal . ID ) , deal . Title , users . Select ( x = > x . DisplayUserName ( false , _displayUserSettingsHelper ) ) ) ;
2021-03-05 20:06:49 +00:00
}
}
else
{
2021-03-19 16:56:26 +00:00
_crmSecurity . MakePublic ( deal ) ;
2021-03-05 20:06:49 +00:00
if ( isMessageServicSende )
{
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . OpportunityOpenedAccess , _messageTarget . Create ( deal . ID ) , deal . Title ) ;
2021-03-05 20:06:49 +00:00
}
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Sets access rights for other users to the list of all opportunities matching the parameters specified in the request
/// </summary>
/// <param optional="true" name="responsibleid">Opportunity responsible</param>
/// <param optional="true" name="opportunityStagesid">Opportunity stage ID</param>
/// <param optional="true" name="tags">Tags</param>
/// <param optional="true" name="contactid">Contact ID</param>
/// <param optional="true" name="contactAlsoIsParticipant">Participation status: take into account opportunities where the contact is a participant or not</param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <param optional="true" name="stageType" remark="Allowed values: {Open, ClosedAndWon, ClosedAndLost}">Opportunity stage type</param>
/// <param name="isPrivate">Opportunity privacy: private or not</param>
/// <param name="accessList">List of users with access</param>
/// <short>Set opportunity access rights</short>
/// <category>Opportunities</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Opportunity list
/// </returns>
[Update(@"opportunity/filter/access")]
2021-06-23 19:32:56 +00:00
public IEnumerable < OpportunityDto > SetAccessToBatchDeal ( [ FromBody ] SetAccessToBatchDealByFilterRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
var responsibleid = inDto . Responsibleid ;
var opportunityStagesid = inDto . OpportunityStagesid ;
var contactid = inDto . Contactid ;
var tags = inDto . Tags ;
var stageType = inDto . StageType ;
var contactAlsoIsParticipant = inDto . ContactAlsoIsParticipant ;
var fromDate = inDto . FromDate ;
var toDate = inDto . ToDate ;
var isPrivate = inDto . isPrivate ;
var accessList = inDto . AccessList ;
var result = new List < Deal > ( ) ;
2021-03-10 15:38:56 +00:00
2021-03-19 16:56:26 +00:00
var deals = _daoFactory . GetDealDao ( )
2021-03-23 15:41:56 +00:00
. GetDeals ( _apiContext . FilterValue ,
2021-03-05 20:06:49 +00:00
responsibleid ,
opportunityStagesid ,
tags ,
contactid ,
stageType ,
contactAlsoIsParticipant ,
fromDate , toDate , 0 , 0 , null ) ;
2021-03-10 15:38:56 +00:00
2021-03-09 15:37:16 +00:00
if ( ! deals . Any ( ) ) return Enumerable . Empty < OpportunityDto > ( ) ;
2021-03-05 20:06:49 +00:00
foreach ( var deal in deals )
{
if ( deal = = null ) throw new ItemNotFoundException ( ) ;
2021-03-23 15:41:56 +00:00
if ( ! ( _crmSecurity . IsAdmin | | deal . CreateBy = = _securityContext . CurrentAccount . ID ) ) continue ;
2021-03-05 20:06:49 +00:00
SetAccessToDeal ( deal . ID , isPrivate , accessList ) ;
2021-03-10 15:38:56 +00:00
2021-03-05 20:06:49 +00:00
result . Add ( deal ) ;
2021-03-10 15:38:56 +00:00
2021-03-05 20:06:49 +00:00
}
2021-03-09 15:37:16 +00:00
return ToListOpportunityDto ( result ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Sets access rights for other users to the list of opportunities with the IDs specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID list</param>
/// <param name="isPrivate">Opportunity privacy: private or not</param>
/// <param name="accessList">List of users with access</param>
/// <short>Set opportunity access rights</short>
/// <category>Opportunities</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Opportunity list
/// </returns>
[Update(@"opportunity/access")]
2021-03-17 11:50:13 +00:00
public IEnumerable < OpportunityDto > SetAccessToBatchDeal ( SetAccessToBatchDealRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
var opportunityid = inDto . Opportunityid ;
var isPrivate = inDto . isPrivate ;
var accessList = inDto . AccessList ;
if ( opportunityid = = null ) throw new ArgumentException ( ) ;
var result = new List < Deal > ( ) ;
2021-03-19 16:56:26 +00:00
var deals = _daoFactory . GetDealDao ( ) . GetDeals ( opportunityid . ToArray ( ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
if ( ! deals . Any ( ) ) return new List < OpportunityDto > ( ) ;
2021-03-05 20:06:49 +00:00
foreach ( var d in deals )
{
if ( d = = null ) throw new ItemNotFoundException ( ) ;
2021-03-23 15:41:56 +00:00
if ( ! ( _crmSecurity . IsAdmin | | d . CreateBy = = _securityContext . CurrentAccount . ID ) ) continue ;
2021-03-05 20:06:49 +00:00
SetAccessToDeal ( d , isPrivate , accessList , false , true ) ;
result . Add ( d ) ;
}
2021-03-09 15:37:16 +00:00
return ToListOpportunityDto ( result ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Deletes the group of opportunities with the IDs specified in the request
/// </summary>
/// <param name="opportunityids">Opportunity ID list</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <short>Delete opportunity group</short>
/// <category>Opportunities</category>
/// <returns>
/// Opportunity list
/// </returns>
[Update(@"opportunity")]
2022-02-16 12:57:37 +00:00
public Task < IEnumerable < OpportunityDto > > DeleteBatchDealsAsync ( IEnumerable < int > opportunityids )
2021-03-05 20:06:49 +00:00
{
if ( opportunityids = = null | | ! opportunityids . Any ( ) ) throw new ArgumentException ( ) ;
2022-02-16 12:57:37 +00:00
return InternalDeleteBatchDealsAsync ( opportunityids ) ;
}
private async Task < IEnumerable < OpportunityDto > > InternalDeleteBatchDealsAsync ( IEnumerable < int > opportunityids )
{
2022-02-10 18:25:40 +00:00
var opportunities = await _daoFactory . GetDealDao ( ) . DeleteBatchDealsAsync ( opportunityids . ToArray ( ) ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . OpportunitiesDeleted , _messageTarget . Create ( opportunityids ) , opportunities . Select ( o = > o . Title ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
return ToListOpportunityDto ( opportunities ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Deletes the list of all opportunities matching the parameters specified in the request
/// </summary>
/// <param optional="true" name="responsibleid">Opportunity responsible</param>
/// <param optional="true" name="opportunityStagesid">Opportunity stage ID</param>
/// <param optional="true" name="tags">Tags</param>
/// <param optional="true" name="contactid">Contact ID</param>
/// <param optional="true" name="contactAlsoIsParticipant">Participation status: take into account opportunities where the contact is a participant or not</param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <param optional="true" name="stageType" remark="Allowed values: {Open, ClosedAndWon, ClosedAndLost}">Opportunity stage type</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <short>Delete opportunity group</short>
/// <category>Opportunities</category>
/// <returns>
/// Opportunity list
/// </returns>
[Delete(@"opportunity/filter")]
2022-02-16 12:57:37 +00:00
public Task < IEnumerable < OpportunityDto > > DeleteBatchDealsAsync (
2021-03-05 20:06:49 +00:00
Guid responsibleid ,
int opportunityStagesid ,
IEnumerable < string > tags ,
int contactid ,
DealMilestoneStatus ? stageType ,
bool? contactAlsoIsParticipant ,
ApiDateTime fromDate ,
ApiDateTime toDate )
{
2021-03-23 15:41:56 +00:00
var deals = _daoFactory . GetDealDao ( ) . GetDeals ( _apiContext . FilterValue ,
2021-03-05 20:06:49 +00:00
responsibleid ,
opportunityStagesid ,
tags ,
contactid ,
stageType ,
contactAlsoIsParticipant ,
fromDate , toDate , 0 , 0 , null ) ;
2022-02-16 12:57:37 +00:00
if ( deals . Count = = 0 ) return System . Threading . Tasks . Task . FromResult ( Enumerable . Empty < OpportunityDto > ( ) ) ;
return InternalDeleteBatchDealsAsync ( deals ) ;
}
2021-03-05 20:06:49 +00:00
2022-02-16 12:57:37 +00:00
private async Task < IEnumerable < OpportunityDto > > InternalDeleteBatchDealsAsync ( List < Deal > deals )
{
2022-02-10 18:25:40 +00:00
deals = await _daoFactory . GetDealDao ( ) . DeleteBatchDealsAsync ( deals ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . OpportunitiesDeleted , _messageTarget . Create ( deals . Select ( x = > x . ID ) ) , deals . Select ( d = > d . Title ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
return ToListOpportunityDto ( deals ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the list of all opportunities matching the parameters specified in the request
/// </summary>
/// <param optional="true" name="responsibleid">Opportunity responsible</param>
/// <param optional="true" name="opportunityStagesid">Opportunity stage ID</param>
/// <param optional="true" name="tags">Tags</param>
/// <param optional="true" name="contactid">Contact ID</param>
/// <param optional="true" name="contactAlsoIsParticipant">Participation status: take into account opportunities where the contact is a participant or not</param>
/// <param optional="true" name="fromDate">Start date</param>
/// <param optional="true" name="toDate">End date</param>
/// <param optional="true" name="stageType" remark="Allowed values: {Open, ClosedAndWon, ClosedAndLost}">Opportunity stage type</param>
/// <short>Get opportunity list</short>
/// <category>Opportunities</category>
/// <returns>
/// Opportunity list
/// </returns>
[Read(@"opportunity/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable < OpportunityDto > GetDeals (
2021-03-22 11:38:28 +00:00
[FromQuery] Guid responsibleid ,
[FromQuery] int opportunityStagesid ,
[FromQuery] IEnumerable < string > tags ,
[FromQuery] int contactid ,
[FromQuery] DealMilestoneStatus ? stageType ,
[FromQuery] bool? contactAlsoIsParticipant ,
[FromQuery] ApiDateTime fromDate ,
[FromQuery] ApiDateTime toDate )
2021-03-05 20:06:49 +00:00
{
DealSortedByType dealSortedByType ;
2021-03-09 15:37:16 +00:00
IEnumerable < OpportunityDto > result ;
2021-03-05 20:06:49 +00:00
2021-03-23 15:41:56 +00:00
var searchString = _apiContext . FilterValue ;
2021-03-05 20:06:49 +00:00
OrderBy dealsOrderBy ;
2021-03-23 15:41:56 +00:00
if ( ASC . CRM . Classes . EnumExtension . TryParse ( _apiContext . SortBy , true , out dealSortedByType ) )
2021-03-05 20:06:49 +00:00
{
2021-03-23 15:41:56 +00:00
dealsOrderBy = new OrderBy ( dealSortedByType , ! _apiContext . SortDescending ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-23 15:41:56 +00:00
else if ( string . IsNullOrEmpty ( _apiContext . SortBy ) )
2021-03-05 20:06:49 +00:00
{
dealsOrderBy = new OrderBy ( DealSortedByType . Stage , true ) ;
}
else
{
dealsOrderBy = null ;
}
2021-03-23 15:41:56 +00:00
var fromIndex = ( int ) _apiContext . StartIndex ;
var count = ( int ) _apiContext . Count ;
2021-03-05 20:06:49 +00:00
if ( dealsOrderBy ! = null )
{
2021-03-19 16:56:26 +00:00
result = ToListOpportunityDto ( _daoFactory . GetDealDao ( ) . GetDeals (
2021-03-05 20:06:49 +00:00
searchString ,
responsibleid ,
opportunityStagesid ,
tags ,
contactid ,
stageType ,
contactAlsoIsParticipant ,
fromDate ,
toDate ,
fromIndex ,
count ,
dealsOrderBy ) ) . ToList ( ) ;
2021-03-23 15:41:56 +00:00
_apiContext . SetDataPaginated ( ) ;
_apiContext . SetDataFiltered ( ) ;
_apiContext . SetDataSorted ( ) ;
2021-03-05 20:06:49 +00:00
}
else
{
2021-03-19 16:56:26 +00:00
result = ToListOpportunityDto ( _daoFactory . GetDealDao ( ) . GetDeals (
2021-03-05 20:06:49 +00:00
searchString ,
responsibleid ,
opportunityStagesid ,
tags ,
contactid ,
stageType ,
contactAlsoIsParticipant ,
fromDate ,
toDate ,
0 , 0 , null ) ) . ToList ( ) ;
}
int totalCount ;
if ( result . Count ( ) < count )
{
totalCount = fromIndex + result . Count ( ) ;
}
else
{
2021-03-19 16:56:26 +00:00
totalCount = _daoFactory
2021-03-05 20:06:49 +00:00
. GetDealDao ( )
. GetDealsCount ( searchString ,
responsibleid ,
opportunityStagesid ,
tags ,
contactid ,
stageType ,
contactAlsoIsParticipant ,
fromDate ,
toDate ) ;
}
2021-03-23 15:41:56 +00:00
_apiContext . SetTotalCount ( totalCount ) ;
2021-03-05 20:06:49 +00:00
return result ;
}
/// <summary>
/// Deletes the opportunity with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <short>Delete opportunity</short>
/// <category>Opportunities</category>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Opportunity
/// </returns>
[Delete(@"opportunity/{opportunityid:int}")]
2022-02-16 12:57:37 +00:00
public Task < OpportunityDto > DeleteDealAsync ( int opportunityid )
2021-03-05 20:06:49 +00:00
{
if ( opportunityid < = 0 ) throw new ArgumentException ( ) ;
2022-02-16 12:57:37 +00:00
return InternalDeleteDealAsync ( opportunityid ) ;
}
private async Task < OpportunityDto > InternalDeleteDealAsync ( int opportunityid )
{
2022-02-10 18:25:40 +00:00
var deal = await _daoFactory . GetDealDao ( ) . DeleteDealAsync ( opportunityid ) ;
2021-03-05 20:06:49 +00:00
if ( deal = = null ) throw new ItemNotFoundException ( ) ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( MessageAction . OpportunityDeleted , _messageTarget . Create ( deal . ID ) , deal . Title ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
return _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Creates the opportunity with the parameters specified in the request
/// </summary>
/// <short>Create opportunity</short>
/// <param name="contactid">Opportunity primary contact</param>
/// <param optional="true" name="members">Participants</param>
/// <param name="title">Opportunity title</param>
/// <param optional="true" name="description">Opportunity description</param>
/// <param name="responsibleid">Opportunity responsible</param>
/// <param name="bidType" remark="Allowed values: FixedBid, PerHour, PerDay,PerWeek, PerMonth, PerYear">Bid</param>
/// <param optional="true" name="bidValue">Amount of transaction</param>
/// <param name="bidCurrencyAbbr">Currency (Abbreviation)</param>
/// <param name="perPeriodValue">Period</param>
/// <param name="stageid">Stage ID</param>
/// <param optional="true" name="successProbability">Opportunity success probability</param>
/// <param optional="true" name="actualCloseDate">Actual opportunity closure date</param>
/// <param optional="true" name="expectedCloseDate">Expected opportunity closure date</param>
/// <param optional="true" name="customFieldList">User field list</param>
/// <param name="isPrivate">Opportunity privacy: private or not</param>
/// <param optional="true" name="accessList">List of users with access to the opportunity</param>
/// <param name="isNotify" optional="true">Notify users in accessList about the opportunity</param>
/// <category>Opportunities</category>
/// <returns>
/// Opportunity
/// </returns>
///<exception cref="ArgumentException"></exception>
[Create(@"opportunity")]
2021-03-17 11:50:13 +00:00
public OpportunityDto CreateDeal ( CreateOrUpdateDealRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
var title = inDto . Title ;
var description = inDto . Description ;
var responsibleid = inDto . Responsibleid ;
var bidType = inDto . BidType ;
var bidValue = inDto . BidValue ;
var perPeriodValue = inDto . PerPeriodValue ;
var stageid = inDto . Stageid ;
var successProbability = inDto . SuccessProbability ;
var contactid = inDto . Contactid ;
var actualCloseDate = inDto . ActualCloseDate ;
var expectedCloseDate = inDto . ExpectedCloseDate ;
var bidCurrencyAbbr = inDto . BidCurrencyAbbr ;
var isPrivate = inDto . isPrivate ;
var isNotify = inDto . isNotify ;
var accessList = inDto . AccessList ;
var customFieldList = inDto . CustomFieldList ;
var members = inDto . Members ;
var deal = new Deal
{
Title = title ,
Description = description ,
ResponsibleID = responsibleid ,
BidType = bidType ,
BidValue = bidValue ,
PerPeriodValue = perPeriodValue ,
DealMilestoneID = stageid ,
DealMilestoneProbability = successProbability < 0 ? 0 : ( successProbability > 100 ? 100 : successProbability ) ,
ContactID = contactid ,
ActualCloseDate = actualCloseDate ,
ExpectedCloseDate = expectedCloseDate ,
BidCurrency = ! String . IsNullOrEmpty ( bidCurrencyAbbr ) ? bidCurrencyAbbr . ToUpper ( ) : null ,
} ;
2021-03-19 16:56:26 +00:00
_crmSecurity . DemandCreateOrUpdate ( deal ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
deal . ID = _daoFactory . GetDealDao ( ) . CreateNewDeal ( deal ) ;
2021-03-05 20:06:49 +00:00
2021-03-23 15:41:56 +00:00
deal . CreateBy = _securityContext . CurrentAccount . ID ;
2021-03-05 20:06:49 +00:00
deal . CreateOn = DateTime . UtcNow ;
SetAccessToDeal ( deal , isPrivate , accessList , isNotify , false ) ;
var membersList = members ! = null ? members . ToList ( ) : new List < int > ( ) ;
if ( deal . ContactID > 0 )
membersList . Add ( deal . ContactID ) ;
if ( membersList . Any ( ) )
{
2021-03-19 16:56:26 +00:00
var contacts = _daoFactory . GetContactDao ( ) . GetContacts ( membersList . ToArray ( ) ) . Where ( _crmSecurity . CanAccessTo ) . ToList ( ) ;
2021-03-05 20:06:49 +00:00
membersList = contacts . Select ( m = > m . ID ) . ToList ( ) ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetDealDao ( ) . SetMembers ( deal . ID , membersList . ToArray ( ) ) ;
2021-03-05 20:06:49 +00:00
}
if ( customFieldList ! = null )
{
2021-03-19 16:56:26 +00:00
var existingCustomFieldList = _daoFactory . GetCustomFieldDao ( ) . GetFieldsDescription ( EntityType . Opportunity ) . Select ( fd = > fd . ID ) . ToList ( ) ;
2021-03-05 20:06:49 +00:00
foreach ( var field in customFieldList )
{
if ( string . IsNullOrEmpty ( field . Value ) | | ! existingCustomFieldList . Contains ( field . Key ) ) continue ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetCustomFieldDao ( ) . SetFieldValue ( EntityType . Opportunity , deal . ID , field . Key , field . Value ) ;
2021-03-05 20:06:49 +00:00
}
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Updates the selected opportunity with the parameters specified in the request
/// </summary>
/// <short>Update opportunity</short>
///<param name="opportunityid">Opportunity ID</param>
///<param name="contactid">Opportunity primary contact</param>
/// <param optional="true" name="members">Participants</param>
/// <param name="title">Opportunity title</param>
/// <param optional="true" name="description">Opportunity description</param>
/// <param name="responsibleid">Opportunity responsible</param>
/// <param name="bidType" remark="Allowed values: FixedBid, PerHour, PerDay,PerWeek, PerMonth, PerYear">Bid</param>
/// <param optional="true" name="bidValue">Amount of transaction</param>
/// <param optional="true" name="bidCurrencyAbbr">Currency (Abbreviation)</param>
/// <param name="perPeriodValue">Period</param>
/// <param name="stageid">Stage ID</param>
/// <param optional="true" name="successProbability">Opportunity success probability</param>
/// <param optional="true" name="actualCloseDate">Actual opportunity closure date</param>
/// <param optional="true" name="expectedCloseDate">Expected opportunity closure date</param>
/// <param optional="true" name="customFieldList">User field list</param>
/// <param name="isPrivate">Opportunity privacy: private or not</param>
/// <param optional="true" name="accessList">List of users with access to the opportunity</param>
/// <param name="isNotify" optional="true">Notify users in accessList about the opportunity</param>
/// <category>Opportunities</category>
/// <returns>
/// Opportunity
/// </returns>
///<exception cref="ArgumentException"></exception>
[Update(@"opportunity/{opportunityid:int}")]
2021-03-09 15:37:16 +00:00
public OpportunityDto UpdateDeal (
2021-03-17 11:50:13 +00:00
int opportunityid , CreateOrUpdateDealRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
var title = inDto . Title ;
var description = inDto . Description ;
var responsibleid = inDto . Responsibleid ;
var bidType = inDto . BidType ;
var bidValue = inDto . BidValue ;
var perPeriodValue = inDto . PerPeriodValue ;
var stageid = inDto . Stageid ;
var successProbability = inDto . SuccessProbability ;
var contactid = inDto . Contactid ;
var actualCloseDate = inDto . ActualCloseDate ;
var expectedCloseDate = inDto . ExpectedCloseDate ;
var bidCurrencyAbbr = inDto . BidCurrencyAbbr ;
var isPrivate = inDto . isPrivate ;
var isNotify = inDto . isNotify ;
var accessList = inDto . AccessList ;
var customFieldList = inDto . CustomFieldList ;
var members = inDto . Members ;
2021-03-10 15:38:56 +00:00
2021-03-19 16:56:26 +00:00
var deal = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
2021-03-05 20:06:49 +00:00
if ( deal = = null ) throw new ItemNotFoundException ( ) ;
deal . Title = title ;
deal . Description = description ;
deal . ResponsibleID = responsibleid ;
deal . BidType = bidType ;
deal . BidValue = bidValue ;
deal . PerPeriodValue = perPeriodValue ;
deal . DealMilestoneID = stageid ;
deal . DealMilestoneProbability = successProbability < 0 ? 0 : ( successProbability > 100 ? 100 : successProbability ) ;
deal . ContactID = contactid ;
deal . ActualCloseDate = actualCloseDate ;
deal . ExpectedCloseDate = expectedCloseDate ;
deal . BidCurrency = ! String . IsNullOrEmpty ( bidCurrencyAbbr ) ? bidCurrencyAbbr . ToUpper ( ) : null ;
2021-03-19 16:56:26 +00:00
_crmSecurity . DemandCreateOrUpdate ( deal ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetDealDao ( ) . EditDeal ( deal ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
deal = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
2021-03-05 20:06:49 +00:00
var membersList = members ! = null ? members . ToList ( ) : new List < int > ( ) ;
if ( membersList . Any ( ) )
{
2021-03-19 16:56:26 +00:00
var contacts = _daoFactory . GetContactDao ( ) . GetContacts ( membersList . ToArray ( ) ) . Where ( _crmSecurity . CanAccessTo ) . ToList ( ) ;
2021-03-05 20:06:49 +00:00
membersList = contacts . Select ( m = > m . ID ) . ToList ( ) ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetDealDao ( ) . SetMembers ( deal . ID , membersList . ToArray ( ) ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-23 15:41:56 +00:00
if ( _crmSecurity . IsAdmin | | deal . CreateBy = = _securityContext . CurrentAccount . ID )
2021-03-05 20:06:49 +00:00
{
SetAccessToDeal ( deal , isPrivate , accessList , isNotify , false ) ;
}
if ( customFieldList ! = null )
{
2021-03-19 16:56:26 +00:00
var existingCustomFieldList = _daoFactory . GetCustomFieldDao ( ) . GetFieldsDescription ( EntityType . Opportunity ) . Select ( fd = > fd . ID ) . ToList ( ) ;
2021-03-05 20:06:49 +00:00
foreach ( var field in customFieldList )
{
if ( string . IsNullOrEmpty ( field . Value ) | | ! existingCustomFieldList . Contains ( field . Key ) ) continue ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetCustomFieldDao ( ) . SetFieldValue ( EntityType . Opportunity , deal . ID , field . Key , field . Value ) ;
2021-03-05 20:06:49 +00:00
}
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the list of all contacts associated with the opportunity with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <short>Get all opportunity contacts</short>
/// <category>Opportunities</category>
/// <returns>Contact list</returns>
///<exception cref="ArgumentException"></exception>
///<exception cref="ItemNotFoundException"></exception>
[Read(@"opportunity/{opportunityid:int}/contact")]
2021-03-09 15:37:16 +00:00
public IEnumerable < ContactDto > GetDealMembers ( int opportunityid )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
var opportunity = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
if ( opportunity = = null | | ! _crmSecurity . CanAccessTo ( opportunity ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var contactIDs = _daoFactory . GetDealDao ( ) . GetMembers ( opportunityid ) ;
2021-03-05 20:06:49 +00:00
2021-03-10 15:38:56 +00:00
if ( contactIDs = = null ) return new List < ContactDto > ( ) ;
2021-03-23 15:41:56 +00:00
var contacts = _daoFactory . GetContactDao ( ) . GetContacts ( contactIDs ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var result = _mapper . Map < List < Core . Entities . Contact > , List < ContactDto > > ( contacts ) ;
2021-03-05 20:06:49 +00:00
result . ForEach ( item = > { if ( item . Id = = opportunity . ContactID ) item . CanEdit = false ; } ) ;
return result ;
}
/// <summary>
/// Adds the selected contact to the opportunity with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <param name="contactid">Contact ID</param>
/// <short>Add opportunity contact</short>
/// <category>Opportunities</category>
/// <exception cref="ArgumentException"></exception>
/// <returns>
/// Participant
/// </returns>
[Create(@"opportunity/{opportunityid:int}/contact/{contactid:int}")]
2021-03-24 15:55:47 +00:00
public ContactDto AddMemberToDeal ( [ FromRoute ] int opportunityid , [ FromRoute ] int contactid )
2021-03-05 20:06:49 +00:00
{
if ( opportunityid < = 0 | | contactid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var opportunity = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
if ( opportunity = = null | | ! _crmSecurity . CanAccessTo ( opportunity ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
if ( contact = = null | | ! _crmSecurity . CanAccessTo ( contact ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var result = _mapper . Map < ContactDto > ( contact ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetDealDao ( ) . AddMember ( opportunityid , contactid ) ;
2021-03-05 20:06:49 +00:00
var messageAction = contact is Company ? MessageAction . OpportunityLinkedCompany : MessageAction . OpportunityLinkedPerson ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( opportunity . ID ) , opportunity . Title , contact . GetTitle ( ) ) ;
2021-03-05 20:06:49 +00:00
return result ;
}
/// <summary>
/// Deletes the selected contact from the opportunity with the ID specified in the request
/// </summary>
/// <param name="opportunityid">Opportunity ID</param>
/// <param name="contactid">Contact ID</param>
/// <short>Delete opportunity contact</short>
/// <category>Opportunities</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// Participant
/// </returns>
[Delete(@"opportunity/{opportunityid:int}/contact/{contactid:int}")]
2021-03-09 15:37:16 +00:00
public ContactDto DeleteMemberFromDeal ( int opportunityid , int contactid )
2021-03-05 20:06:49 +00:00
{
if ( ( opportunityid < = 0 ) | | ( contactid < = 0 ) ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var opportunity = _daoFactory . GetDealDao ( ) . GetByID ( opportunityid ) ;
if ( opportunity = = null | | ! _crmSecurity . CanAccessTo ( opportunity ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var contact = _daoFactory . GetContactDao ( ) . GetByID ( contactid ) ;
if ( contact = = null | | ! _crmSecurity . CanAccessTo ( contact ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var result = _mapper . Map < ContactDto > ( contact ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetDealDao ( ) . RemoveMember ( opportunityid , contactid ) ;
2021-03-05 20:06:49 +00:00
var messageAction = contact is Company ? MessageAction . OpportunityUnlinkedCompany : MessageAction . OpportunityUnlinkedPerson ;
2021-03-23 15:41:56 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( opportunity . ID ) , opportunity . Title , contact . GetTitle ( ) ) ;
2021-03-05 20:06:49 +00:00
return result ;
}
/// <summary>
/// Returns the list of 30 opportunities in the CRM module with prefix
/// </summary>
/// <param optional="true" name="prefix"></param>
/// <param optional="true" name="contactID"></param>
/// <param optional="true" name="internalSearch"></param>
/// <category>Opportunities</category>
/// <returns>
/// Opportunities list
/// </returns>
/// <visible>false</visible>
[Read(@"opportunity/byprefix")]
2021-03-09 15:37:16 +00:00
public IEnumerable < OpportunityDto > GetDealsByPrefix ( string prefix , int contactID , bool internalSearch = true )
2021-03-05 20:06:49 +00:00
{
2021-03-09 15:37:16 +00:00
var result = new List < OpportunityDto > ( ) ;
2021-03-05 20:06:49 +00:00
if ( contactID > 0 & & internalSearch )
{
2021-03-19 16:56:26 +00:00
var findedDeals = _daoFactory . GetDealDao ( ) . GetDealsByContactID ( contactID ) ;
2021-03-05 20:06:49 +00:00
foreach ( var item in findedDeals )
{
if ( item . Title . IndexOf ( prefix , StringComparison . Ordinal ) ! = - 1 )
{
2021-03-19 16:56:26 +00:00
result . Add ( _mapper . Map < OpportunityDto > ( item ) ) ;
2021-03-05 20:06:49 +00:00
}
}
2021-03-23 15:41:56 +00:00
_apiContext . SetTotalCount ( result . Count ) ;
2021-03-05 20:06:49 +00:00
}
else
{
const int maxItemCount = 30 ;
2021-03-19 16:56:26 +00:00
var findedDeals = _daoFactory . GetDealDao ( ) . GetDealsByPrefix ( prefix , 0 , maxItemCount , contactID , internalSearch ) ;
2021-03-05 20:06:49 +00:00
foreach ( var item in findedDeals )
{
2021-03-19 16:56:26 +00:00
result . Add ( _mapper . Map < OpportunityDto > ( item ) ) ;
2021-03-05 20:06:49 +00:00
}
}
return result ;
}
/// <summary>
/// Returns the list of all contact opportunities
/// </summary>
/// <param optional="true" name="contactid">Contact ID</param>
/// <short>Get opportunity list</short>
/// <category>Opportunities</category>
/// <returns>
/// Opportunity list
/// </returns>
[Read(@"opportunity/bycontact/{contactid:int}")]
2021-03-09 15:37:16 +00:00
public IEnumerable < OpportunityDto > GetDeals ( int contactid )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
var deals = _daoFactory . GetDealDao ( ) . GetDealsByContactID ( contactid ) ;
2021-03-09 15:37:16 +00:00
return ToListOpportunityDto ( deals ) ;
2021-03-05 20:06:49 +00:00
}
/// <visible>false</visible>
[Update(@"opportunity/{opportunityid:int}/creationdate")]
public void SetDealCreationDate ( int opportunityid , ApiDateTime creationDate )
{
2021-03-19 16:56:26 +00:00
var dao = _daoFactory . GetDealDao ( ) ;
2021-03-05 20:06:49 +00:00
var opportunity = dao . GetByID ( opportunityid ) ;
2021-03-19 16:56:26 +00:00
if ( opportunity = = null | | ! _crmSecurity . CanAccessTo ( opportunity ) )
2021-03-05 20:06:49 +00:00
throw new ItemNotFoundException ( ) ;
dao . SetDealCreationDate ( opportunityid , creationDate ) ;
}
/// <visible>false</visible>
[Update(@"opportunity/{opportunityid:int}/lastmodifeddate")]
public void SetDealLastModifedDate ( int opportunityid , ApiDateTime lastModifedDate )
{
2021-03-19 16:56:26 +00:00
var dao = _daoFactory . GetDealDao ( ) ;
2021-03-05 20:06:49 +00:00
var opportunity = dao . GetByID ( opportunityid ) ;
2021-03-19 16:56:26 +00:00
if ( opportunity = = null | | ! _crmSecurity . CanAccessTo ( opportunity ) )
2021-03-05 20:06:49 +00:00
throw new ItemNotFoundException ( ) ;
dao . SetDealLastModifedDate ( opportunityid , lastModifedDate ) ;
}
2021-03-09 15:37:16 +00:00
private IEnumerable < OpportunityDto > ToListOpportunityDto ( ICollection < Deal > deals )
2021-03-05 20:06:49 +00:00
{
2021-03-09 15:37:16 +00:00
if ( deals = = null | | deals . Count = = 0 ) return new List < OpportunityDto > ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
var result = new List < OpportunityDto > ( ) ;
2021-03-05 20:06:49 +00:00
var contactIDs = new List < int > ( ) ;
var dealIDs = new List < int > ( ) ;
var dealMilestoneIDs = new List < int > ( ) ;
foreach ( var deal in deals )
{
contactIDs . Add ( deal . ContactID ) ;
dealIDs . Add ( deal . ID ) ;
dealMilestoneIDs . Add ( deal . DealMilestoneID ) ;
}
dealMilestoneIDs = dealMilestoneIDs . Distinct ( ) . ToList ( ) ;
2021-03-09 15:37:16 +00:00
var contacts = new Dictionary < int , ContactBaseDto > ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var customFields = _daoFactory . GetCustomFieldDao ( ) . GetEnityFields ( EntityType . Opportunity , dealIDs . ToArray ( ) )
2021-03-05 20:06:49 +00:00
. GroupBy ( item = > item . EntityID )
2021-03-22 12:42:03 +00:00
. ToDictionary ( item = > item . Key , item = > item . Select ( x = > _mapper . Map < CustomFieldBaseDto > ( x ) ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var dealMilestones = _daoFactory . GetDealMilestoneDao ( ) . GetAll ( dealMilestoneIDs . ToArray ( ) )
2021-03-09 15:37:16 +00:00
. ToDictionary ( item = > item . ID , item = > new DealMilestoneBaseDto ( item ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var dealMembers = _daoFactory . GetDealDao ( ) . GetMembers ( dealIDs . ToArray ( ) ) ;
2021-03-05 20:06:49 +00:00
foreach ( var value in dealMembers . Values )
{
contactIDs . AddRange ( value ) ;
}
contactIDs = contactIDs . Distinct ( ) . ToList ( ) ;
if ( contactIDs . Count > 0 )
{
2021-03-19 16:56:26 +00:00
_daoFactory . GetContactDao ( ) . GetContacts ( contactIDs . ToArray ( ) ) . ForEach ( item = >
2021-03-05 20:06:49 +00:00
{
if ( item = = null ) return ;
2021-03-19 16:56:26 +00:00
contacts . Add ( item . ID , _mapper . Map < ContactBaseDto > ( item ) ) ;
2021-03-05 20:06:49 +00:00
} ) ;
}
foreach ( var deal in deals )
{
2021-03-19 16:56:26 +00:00
var dealDto = _mapper . Map < OpportunityDto > ( deal ) ;
2021-03-05 20:06:49 +00:00
if ( contacts . ContainsKey ( deal . ContactID ) )
{
2021-03-09 15:37:16 +00:00
dealDto . Contact = contacts [ deal . ContactID ] ;
2021-03-05 20:06:49 +00:00
}
2021-03-09 15:37:16 +00:00
dealDto . CustomFields = customFields . ContainsKey ( deal . ID )
2021-03-05 20:06:49 +00:00
? customFields [ deal . ID ]
2021-03-09 15:37:16 +00:00
: new List < CustomFieldBaseDto > ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
dealDto . Members = dealMembers . ContainsKey ( dealDto . Id )
? dealMembers [ dealDto . Id ] . Where ( contacts . ContainsKey ) . Select ( item = > contacts [ item ] )
: new List < ContactBaseDto > ( ) ;
2021-03-05 20:06:49 +00:00
if ( dealMilestones . ContainsKey ( deal . DealMilestoneID ) )
{
2021-03-09 15:37:16 +00:00
dealDto . Stage = dealMilestones [ deal . DealMilestoneID ] ;
2021-03-05 20:06:49 +00:00
}
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
dealDto . IsPrivate = _crmSecurity . IsPrivate ( deal ) ;
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
if ( dealDto . IsPrivate )
2021-03-05 20:06:49 +00:00
{
2021-03-23 15:41:56 +00:00
dealDto . AccessList = _crmSecurity . GetAccessSubjectTo ( deal ) . Select ( item = > _employeeWraperHelper . Get ( item . Key ) ) ;
2021-03-05 20:06:49 +00:00
}
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
if ( ! string . IsNullOrEmpty ( deal . BidCurrency ) )
{
2021-03-23 15:41:56 +00:00
dealDto . BidCurrency = _mapper . Map < CurrencyInfoDto > ( _currencyProvider . Get ( deal . BidCurrency ) ) ;
2021-03-05 20:06:49 +00:00
}
2020-04-16 19:41:37 +00:00
2021-03-09 15:37:16 +00:00
result . Add ( dealDto ) ;
2021-03-05 20:06:49 +00:00
}
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
return result ;
}
}
}