public int AddCreditProcessFlow(CreditProcessXCompany creditProcessFlow)
        {
            _databaseModel.CreditProcessesXCompanies.Add(creditProcessFlow);
            _databaseModel.SaveChanges();

            return creditProcessFlow.CreditProcessXCompanyId;
        }
        public void DeleteCreditProcessFlow(CreditProcessXCompany creditProcessFlow)
        {
            var entityToDelete = _databaseModel.CreditProcessesXCompanies
                .FirstOrDefault(cpc => cpc.CreditProcessXCompanyId.Equals(creditProcessFlow.CreditProcessXCompanyId));
            if (entityToDelete != null)
            {
                _databaseModel.CreditProcessesXCompanies.Remove(entityToDelete);
            }

            _databaseModel.SaveChanges();
        }
        private static CreditProcessXCompany Map(ProcessFlowViewModel viewModel)
        {
            var creditProcessXCompany = new CreditProcessXCompany
                                        {
                                            CompanyId = viewModel.CompanyId,
                                            CreditProcessId = viewModel.CreditProcessId,
                                            CreditProcessXCompanyId = viewModel.CreditProcessXCompanyId,
                                            CreditStatusId = viewModel.CreditStatusId,
                                            CreatedAt = viewModel.CreatedAt,
                                            UpdatedAt = viewModel.UpdatedAt,
                                        };

            return creditProcessXCompany;
        }
        /// <summary>
        /// To add an update event in the credit process logs
        /// </summary>
        /// <param name="creditProcessFlowFormed"></param>
        /// <returns></returns>
        private CreditProcessLog UpdateCreditProcessLog(CreditProcessXCompany creditProcessFlowFormed)
        {
            var currentProcess =
                _creditProcessesRepository.GetCreditProcessXCompanyFlow(creditProcessFlowFormed.CreditProcessXCompanyId);

            var creditProcessLog = new CreditProcessLog
                                       {
                                           ChangeDate = DateTime.Now,
                                           CreditProcessXCompanyId =
                                               creditProcessFlowFormed.CreditProcessXCompanyId,
                                           LastStatusId = currentProcess.CreditStatusId,
                                           NewStatusId = creditProcessFlowFormed.CreditStatusId,
                                       };

            return creditProcessLog;
        }
        /// <summary>
        /// To add a new event in the credit process logs
        /// </summary>
        /// <param name="creditProcessFlowFormed"></param>
        /// <returns></returns>
        private CreditProcessLog CreateNewCreditProcessLog(CreditProcessXCompany creditProcessFlowFormed)
        {
            var creditProcessLog = new CreditProcessLog
                                       {
                                           ChangeDate = DateTime.Now,
                                           CreditProcessXCompanyId =
                                               creditProcessFlowFormed.CreditProcessXCompanyId,
                                           LastStatusId = creditProcessFlowFormed.CreditStatusId,
                                           NewStatusId = creditProcessFlowFormed.CreditStatusId,
                                       };

            return creditProcessLog;
        }
        public void UpdateCreditProcessFlow(CreditProcessXCompany creditProcessFlow)
        {
            var entry = _databaseModel.Entry(creditProcessFlow);
            if (entry.State == EntityState.Detached)
            {
                var set = _databaseModel.Set<CreditProcessXCompany>();
                var attachedEntity = set.Find(creditProcessFlow.CreditProcessXCompanyId);  // You need to have access to key

                if (attachedEntity != null)
                {
                    var attachedEntry = _databaseModel.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(creditProcessFlow);
                }
                else
                {
                    entry.State = EntityState.Modified; // This should attach entity
                }
            }

            _databaseModel.SaveChanges();
        }
        private static ProcessFlowViewModel Map(CreditProcessXCompany creditProcessXCompany)
        {
            var viewModel = new ProcessFlowViewModel
                            {
                                CompanyId = creditProcessXCompany.CompanyId,
                                CreditProcessId = creditProcessXCompany.CreditProcessId,
                                CreditProcessXCompanyId = creditProcessXCompany.CreditProcessXCompanyId,
                                CreditStatusId = creditProcessXCompany.CreditStatusId,
                                IsNew = false,
                                CreatedAt = creditProcessXCompany.CreatedAt,
                                UpdatedAt = creditProcessXCompany.UpdatedAt,
                            };

            return viewModel;
        }