Ejemplo n.º 1
0
        public async Task ExecuteAsync(CaseEx request, CaseActionEnum action)
        {
            if (action == CaseActionEnum.Update && string.IsNullOrEmpty(request.Id))
            {
                request.Id = await CaseRepository.GetCaseId(request.CaseNumber);

                if (string.IsNullOrEmpty(request.Id))
                {
                    throw new NotFoundException("Cannot find the case with case number.",
                                                new LogObject("GetDataFromExternalTask_UpdateExecuteAsync", null));
                }
            }
            Task <string> ownerId = null;

            if (!string.IsNullOrEmpty(request.GroupName))
            {
                ownerId = CaseRepository.GetOwnerId(request.GroupName);
            }
            else if (!string.IsNullOrEmpty(request.CaseOwner))
            {
                ownerId = CaseRepository.GetOwnerIdByUserName(request.CaseOwner);
            }
            var personAccountInput = new PersonAccountInput
            {
                FirstName         = request.First_Name__c,
                LastName          = request.Last_Name__c,
                AccountIdentifier = request.AccountIdentifier,
                Brand             = request.Brand_OFAC__c,
                Product           = "Wave"
            };
            //await GetCustomerInfo(personAccountInput);//Get Customer info from CardAccount service

            var recordTypeId  = CaseRepository.GetRecordTypeId(request.RecordType, ObjectTypeEnum.Case);
            var personAccount = CaseRepository.GetPersonAccount(personAccountInput);//Get or Create person Account

            string ownerIdResult = null;

            if (ownerId != null)
            {
                ownerIdResult = await ownerId;
            }

            var recordTypeIdResult  = await recordTypeId;
            var personAccountResult = await personAccount;

            if (!string.IsNullOrWhiteSpace(ownerIdResult))
            {
                request.OwnerId = ownerIdResult;
            }
            if (action == CaseActionEnum.Update)
            {
                if (!string.IsNullOrEmpty(request.CaseOwner) && string.IsNullOrEmpty(request.OwnerId))
                {
                    throw new NotFoundException("Salesforce case owner does not exist.",
                                                new LogObject("GetDataFromExternalTask_UpdateExecuteAsync", null));
                }
            }
            if (!string.IsNullOrWhiteSpace(recordTypeIdResult))
            {
                request.RecordTypeId = recordTypeIdResult;
            }
            if (personAccountResult != null)
            {
                request.AccountId = personAccountResult.Id;
                request.ContactId = personAccountResult.PersonContactId;
            }
        }
Ejemplo n.º 2
0
        //protected virtual async Task<string> GetCustomerId(string accountKey)
        //{
        //    ProxySuccessResponse response = null;
        //    var isError = false;
        //    var metric = new MetricWatcher(SalesforceEventTypeEnum.CreateCustomer.ToString(),
        //        new MetricWatcherOption
        //        {
        //            ManualStartStop = true,
        //            LoggingOption = LogOptionEnum.FullLog,
        //            LogMessage = new Dictionary<string, object>
        //            {
        //                {"AccountKey", accountKey},
        //                {"EventType", SalesforceEventTypeEnum.CreateCustomer}
        //            }
        //        });
        //    try
        //    {
        //        metric.Start();

        //        if (string.IsNullOrWhiteSpace(accountKey))
        //        {
        //            return "";
        //        }
        //        var customer =
        //            await QueryObjectAsync<Customer__c>(new Dictionary<string, object> {{ "AccountKey__c", accountKey}}, "Id");

        //        if (customer != null && customer.Records.Count > 0)
        //        {
        //            return customer.Records[0].Id;
        //        }
        //        var customerNew = new Customer__c()
        //        {
        //            Name = accountKey,
        //            AccountKey__c = accountKey
        //        };
        //        var client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default);
        //        response = await client.CreateAsync("Customer__c", customerNew);
        //        if (response != null && response.Success)
        //        {
        //            return response.Id;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        isError = true;
        //        if (ex is ForceException)
        //        {
        //            throw new ExternalErrorException(ex.GetExceptionMessages(),
        //                new LogObject(SalesforceEventTypeEnum.CreateCustomer.ToString(),
        //                    new Dictionary<string, object>
        //                    {
        //                        {"AccountKey", accountKey},
        //                        {"EventType", SalesforceEventTypeEnum.CreateCustomer}
        //                    }));
        //        }
        //    }
        //    finally
        //    {
        //        if (response != null
        //            && !isError)
        //        {
        //            metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary<string, object>
        //            {
        //                {"Id", response.Id},
        //                {"EventType", SalesforceEventTypeEnum.CreateCustomer},
        //                {"Error", response.Errors}
        //            });
        //        }
        //        metric.Stop(isError);
        //    }
        //    return "";
        //}

        #endregion

        #region Get / Create Customer Object

        public async Task <Account> GetPersonAccount(PersonAccountInput input, ICaseClientProxy client = null)
        {
            ProxySuccessResponse response = null;
            var isError = false;
            var logData = new Dictionary <string, object>
            {
                { "AccountIdentifier", input.AccountIdentifier },
                { "EventType", SalesforceEventTypeEnum.CreatePersonAccount }
            };
            var metric = new MetricWatcher(SalesforceEventTypeEnum.CreatePersonAccount.ToString(),
                                           new MetricWatcherOption
            {
                ManualStartStop = true,
                LoggingOption   = LogOptionEnum.FullLog,
                LogMessage      = logData
            });

            try
            {
                metric.Start();
                if (client == null)
                {
                    client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default);
                }
                if (string.IsNullOrWhiteSpace(input.AccountIdentifier))
                {
                    return(null);
                }
                var account = await QueryObjectAsync <Account>(
                    new Dictionary <string, object> {
                    { "Account_Identifier__c", input.AccountIdentifier }
                }, "Id",
                    "PersonContactId");

                if (account != null && account.Records.Count > 0)
                {
                    return(account.Records[0]);
                }
                if (string.IsNullOrEmpty(input.LastName))
                {
                    return(null);
                }
                var waveRecordTypeId = await GetRecordTypeId("WaveAccount");

                var personAccount = new Account
                {
                    LastName              = input.LastName,
                    FirstName             = input.FirstName,
                    RecordTypeId          = waveRecordTypeId,
                    Account_Identifier__c = input.AccountIdentifier,
                    Product__c            = input.Product,
                    Brand__c              = input.Brand
                };
                response = await client.CreateAsync("Account", personAccount);

                if (response != null && response.Success)
                {
                    var newAccount = await QueryObjectAsync <Account>(new Dictionary <string, object> {
                        { "ID", response.Id }
                    },
                                                                      "PersonContactId");

                    var contactId = string.Empty;
                    if (newAccount != null && newAccount.Records.Count > 0)
                    {
                        contactId = newAccount.Records[0].PersonContactId;
                    }
                    return(new Account {
                        Id = response.Id, PersonContactId = contactId
                    });
                }
            }
            catch (Exception ex)
            {
                isError = true;
                var log = new LogObject(SalesforceEventTypeEnum.CreatePersonAccount.ToString(), logData);
                if (ex is ForceException)
                {
                    throw new ExternalErrorException(ex.GetExceptionMessages(), log, ex);
                }
                throw new GdErrorException("Unable to get Person Account data", log, ErrorCodeEnum.ExternalError, ex);
            }
            finally
            {
                if (response != null && isError)
                {
                    metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary <string, object>
                    {
                        { "Id", response.Id },
                        { "EventType", SalesforceEventTypeEnum.CreatePersonAccount },
                        { "Error", response.Errors }
                    });
                }
                metric.Stop(isError);
            }
            return(null);
        }