Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        private async Task <CaseResponse> HandleCaseResponse(ICaseClientProxy client, ProxySuccessResponse response, bool isError, MetricWatcher metric)
        {
            var caseResponse = await CreateResponse(response, client);

            if (!isError)
            {
                metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary <string, object>
                {
                    { "Id", caseResponse.Id },
                    { "CaseNumber", string.IsNullOrWhiteSpace(caseResponse.CaseNumber) ? null : caseResponse.CaseNumber }
                });
            }
            return(caseResponse);
        }