Example #1
0
        public async Task <GenericServiceResponse> UpdateLeadFromUpdateEvent(PayloadParent payloadResponse, LogCommand logCommand)
        {
            const string method           = "UpdateLeadFromUpdateEvent";
            var          updateEventState = string.Empty;

            try
            {
                logCommand.LogMessage = $"{Service}.{method} Starting input parameter PayloadResponse = {payloadResponse}";
                _logHandler.HandleLog(logCommand);


                var leadPayload = new LeadPayload(payloadResponse);

                logCommand.LogMessage = $"{Service}.{method} Starting input parameter LeadPayload = {leadPayload}";
                _logHandler.HandleLog(logCommand);
                var recordsAffected = 0;

                var existingLeadEnumerable = await _leadRepository.GetLeadByCustomerGuidAsync(leadPayload._PayloadHeader.CmcCustomerGuidC, logCommand);

                var existingLeadList = existingLeadEnumerable.ToList();
                if (!existingLeadList.Any())
                {
                    throw new Exception($"Attempting to update Lead where CustomerGuid =  {leadPayload._PayloadHeader.CmcCustomerGuidC} but lead was not found ");
                }
                var existingLead = existingLeadList[0];

                Guid programGuid;
                Guid subprogramGuid;
                Guid lkpLeadStatusGuid;
                Guid lkpAuditTypeGuid;
                Guid accountTypeGuid;

                //Customer Guid Check
                updateEventState = "Attempting to check CustomerGuid" + Environment.NewLine;
                if (leadPayload._PayloadHeader.CmcCustomerGuidC == Guid.Empty)
                {
                    throw new Exception($"Error: {Service}.{method} -- Missing Customer Guid ");
                }

                //ProgramGuid
                updateEventState = "Attempting To Get Program Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(leadPayload._PayloadHeader.SalesforceProgramIDC))
                {
                    var program = await _programRepository.GetProgramFromSFID(leadPayload._PayloadHeader.SalesforceProgramIDC, logCommand);

                    programGuid = program.ProgramGuid;
                }
                else
                {
                    throw new Exception($"Error: {Service}.{method} -- Missing SalesforceProgramID ");
                }

                //SubprogramGuid
                updateEventState = "Attempting To Get SubProgram Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(leadPayload._PayloadHeader.SalesforceSubProgramIDC))
                {
                    var subprogram = await _programRepository.GetSubProgramSFID(leadPayload._PayloadHeader.SalesforceSubProgramIDC, logCommand);

                    subprogramGuid = subprogram.SubProgramGuid;
                }
                else
                {
                    subprogramGuid = Guid.Parse("FB02657F-3985-4F06-84C3-1669BE2F2991");
                }

                //LKPLeadStatusGuid
                updateEventState = "Attempting To Get LKPLeadStatus Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(leadPayload._PayloadBody.Status))
                {
                    var lkPleadStatus = await _leadRepository.GetLkpLeadStatusFromName(leadPayload._PayloadBody.Status, logCommand);

                    lkpLeadStatusGuid = lkPleadStatus.LeadStatusGuid;
                }
                else
                {
                    throw new Exception($"Error: {Service}.{method} -- Missing LKPLeadStatus ");
                }

                //LKPAuditTypeGuid
                updateEventState = "Attempting To Get LKPAuditType Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(leadPayload._PayloadBody.AuditTypeC))
                {
                    var lkpAuditType = await _leadRepository.GetLkpLeadAuditTypeFromName(leadPayload._PayloadBody.AuditTypeC, logCommand);

                    lkpAuditTypeGuid = lkpAuditType.AuditTypeGuid;
                }
                else
                {
                    throw new Exception($"Error: {Service}.{method} -- Missing LKPAuditType ");
                }

                updateEventState = "Attempting To Instantiate customer" + Environment.NewLine;
                var customer = new Customer(leadPayload, programGuid, subprogramGuid, accountTypeGuid);

                //Get Primary Contact Guid
                updateEventState = "Attempting To Instantiate primaryContact" + Environment.NewLine;
                var primaryContact = new Contact(leadPayload, Guid.Parse("BEF01936-5D80-471D-AF77-4CC2AE5161B4"));
                //primaryContact.CustomerGuid = Guid.Parse("A30FD8B1-9BE2-47FB-8D9E-AC8EFCEFE369");

                updateEventState = "Attempting To Get Primary Contact" + Environment.NewLine;
                var primaryContactData = await _contactRepository.GetPrimaryContactFromCustomerGuid(leadPayload._PayloadHeader.CmcCustomerGuidC, logCommand);


                //Customer
                updateEventState = "Attempting To Execute on Customer" + Environment.NewLine;
                recordsAffected += await _customerRepository.UpdateCustomer(customer, logCommand);

                ServiceResponse = await InsertAuditRecordAsync(logCommand, payloadResponse, customer.GetType().FullName, leadPayload._PayloadBody.LastModifiedById);

                if (!ServiceResponse.Success)
                {
                    return(ServiceResponse);
                }

                //Lead
                updateEventState = "Attempting To Instantiate lead" + Environment.NewLine;
                var lead = new Lead(leadPayload, lkpLeadStatusGuid, lkpAuditTypeGuid);

                //Update Lead Status Change Date timestamp if lead status changed
                if (existingLead.LeadStatusGuid == lead.LeadStatusGuid)
                {
                    if (lead.LeadStatusChangeDate == null)
                    {
                        lead.LeadStatusChangeDate = Convert.ToDateTime("1900-01-01");
                    }
                }
                else
                {
                    lead.LeadStatusChangeDate = DateTime.Now.Date;
                }

                //Set ReserveDate to Today.23:59.59 if reservation is done by SF.  See GetPrioLeads which sets
                //Reservation for leads to now + 15 minutes as a way to send different sets of leads to CSR's
                //that are looking for leads with similar criteria
                if (lead.ReservedDate == DateTime.Today)
                {
                    lead.ReservedDate = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);
                }

                updateEventState = "Attempting To Execute on Lead" + Environment.NewLine;
                recordsAffected += await _leadRepository.UpdateLeadAsync(lead, logCommand);

                ServiceResponse = await InsertAuditRecordAsync(logCommand, payloadResponse, lead.GetType().FullName, leadPayload._PayloadBody.LastModifiedById);

                if (!ServiceResponse.Success)
                {
                    return(ServiceResponse);
                }

                //Update Primary Contact
                updateEventState = "Attempting To Execute on Primary Contact" + Environment.NewLine;
                recordsAffected += await _contactRepository.UpdatePrimaryContact(primaryContact, logCommand);

                ServiceResponse = await InsertAuditRecordAsync(logCommand, payloadResponse, primaryContact.GetType().FullName, leadPayload._PayloadBody.LastModifiedById);

                if (!ServiceResponse.Success)
                {
                    return(ServiceResponse);
                }

                //Phone and Email
                updateEventState = "Attempting To Instantiate Phone and Email Objects" + Environment.NewLine;
                var phone = new PhoneNumbers(leadPayload, primaryContactData.ContactGuid);
                var email = new Email(leadPayload, primaryContactData.ContactGuid);

                updateEventState = "Attempting To Execute on Phone" + Environment.NewLine;
                recordsAffected += await _contactRepository.UpdatePhone(phone, logCommand);

                ServiceResponse = await InsertAuditRecordAsync(logCommand, payloadResponse, phone.GetType().FullName, leadPayload._PayloadBody.LastModifiedById);

                if (!ServiceResponse.Success)
                {
                    return(ServiceResponse);
                }

                updateEventState = "Attempting To Execute on Email" + Environment.NewLine;
                recordsAffected += await _contactRepository.UpdateEmail(email, logCommand);

                ServiceResponse = await InsertAuditRecordAsync(logCommand, payloadResponse, email.GetType().FullName, leadPayload._PayloadBody.LastModifiedById);

                if (!ServiceResponse.Success)
                {
                    return(ServiceResponse);
                }

                updateEventState = "Executions Complete" + Environment.NewLine;


                ServiceResponse = new GenericServiceResponse
                {
                    Entity             = $"Update Lead Related Tables Successful, rows added/updated = {recordsAffected}",
                    Success            = true,
                    RestResponseStatus = GenericServiceResponse.RestStatus.Success
                };

                logCommand.LogMessage = string.Format($"{Service}.{method} completed");
                _logHandler.HandleLog(logCommand);

                return(ServiceResponse);
            }
            catch (Exception ex)
            {
                ex.Data.Add("Point of Failure", updateEventState);
                AppLogger.LogException(_loggingInstance, ex.Message, ex);
                return(ServiceHelper.SetErrorGenericServiceResponse(ex));
            }
        }