Beispiel #1
0
 public Task<List<Lead>> GetLeads()
 {
     return _leadRepository.GetLeads();
 }
        public async Task <GenericServiceResponse> UpsertLeadFromInsertEvent(PayloadParent cdcResponseObject, LogCommand logCommand)
        {
            const string method           = "UpsertLeadFromInsertEvent";
            string       upsertEventState = string.Empty;

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

                var checkIfExists = await _leadRepository.GetLeads(cdcResponseObject.CmcCustomerGuidC, logCommand);

                var leadPayload = new LeadPayload(cdcResponseObject);

                var    recordsAffected = 0;
                string entityResponse;

                ServiceResponse = new GenericServiceResponse
                {
                    Success            = true,
                    RestResponseStatus = GenericServiceResponse.RestStatus.Empty
                };

                if (checkIfExists.Any())
                {
                    recordsAffected = await _leadRepository.LeadCreatedSetRecordId(
                        leadPayload._PayloadHeader.CmcCustomerGuidC, leadPayload._PayloadHeader.RecordIdC, logCommand);

                    entityResponse = $"Lead Exists, Update SF-Record-ID, rows added/updated = {recordsAffected}" + Environment.NewLine +
                                     $"Records Expected: 1 ";
                }
                else
                {
                    Guid programGuid;
                    Guid subprogramGuid;
                    Guid lkpLeadStatusGuid;
                    Guid lkpAuditTypeGuid;
                    Guid accountTypeGuid;

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

                    //ProgramGuid
                    upsertEventState = "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
                    upsertEventState = "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
                    upsertEventState = "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
                    {
                        logCommand.LogMessage = string.Format($"Error: {Service}.{method} -- Missing LKPLeadStatus ");
                        _logHandler.HandleLog(logCommand);

                        return(ServiceResponse);
                    }

                    //LKPAuditTypeGuid
                    upsertEventState = "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
                    {
                        var lkpAuditType = await _leadRepository.GetLkpLeadAuditTypeFromName("Not Qualified", logCommand);

                        lkpAuditTypeGuid = lkpAuditType.AuditTypeGuid;
                    }

                    //LKPAccountTypeGuid
                    //Todo This is hard coded!!! NotQualify, needs to be fixed.
                    upsertEventState = "Attempting To Get LkpAccountType Guid" + Environment.NewLine;
                    if (!string.IsNullOrEmpty("Commercial"))
                    {
                        var lkpAccountType = await _customerRepository.GetCustomerAccountTypeFromName("Commercial", logCommand);

                        accountTypeGuid = lkpAccountType.AccountTypeGuid;
                    }
                    else
                    {
                        logCommand.LogMessage = string.Format($"Error: {Service}.{method} -- Missing AccountTypeGuid ");
                        _logHandler.HandleLog(logCommand);


                        return(ServiceResponse);
                    }

                    upsertEventState = "Attempting To Instantiate Customer" + Environment.NewLine;
                    var customer = new Customer(leadPayload, programGuid, subprogramGuid, accountTypeGuid);
                    upsertEventState = "Attempting To Instantiate Lead" + Environment.NewLine;
                    var lead = new Lead(leadPayload, lkpLeadStatusGuid, lkpAuditTypeGuid)
                    {
                        LeadStatusChangeDate = DateTime.Now
                    };

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

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

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

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

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

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

                    upsertEventState = "Attempting To Instantiate Contact" + Environment.NewLine;
                    var primaryContact = new Contact(leadPayload, Guid.Parse("BEF01936-5D80-471D-AF77-4CC2AE5161B4"));
                    recordsAffected += await _contactRepository.InsertContact(primaryContact, logCommand);

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

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

                    upsertEventState = "Attempting To Get Primary Contact" + Environment.NewLine;
                    var primaryContactData = await _contactRepository.GetPrimaryContactFromCustomerGuid(primaryContact.CustomerGuid, logCommand);

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

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

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

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

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

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

                    if (!ServiceResponse.Success)
                    {
                        return(ServiceResponse);
                    }
                    upsertEventState = "Executions Complete" + Environment.NewLine;

                    entityResponse = $"Lead Does Not Exist, Insert Skeleton " + Environment.NewLine +
                                     $"Records Inserted: {recordsAffected} " + Environment.NewLine +
                                     $"Records Expected: 5 ";
                }

                if (recordsAffected == 0)
                {
                    return(ServiceHelper.SetGenericServiceResponseForEntity($"Insert for ObjectName: {cdcResponseObject.ObjectNameC} and RecordId: {cdcResponseObject.RecordIdC} failed."));
                }
                ServiceResponse.Entity             = entityResponse;
                ServiceResponse.Success            = true;
                ServiceResponse.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", upsertEventState);
                AppLogger.LogException(_loggingInstance, ex.Message, ex);
                return(ServiceHelper.SetErrorGenericServiceResponse(ex));
            }
        }