Exemple #1
0
 public OrganizationViewModel Get(int id)
 {
     using (var unitWork = new UnitOfWork(context))
     {
         var            organizationList = unitWork.OrganizationRepository.GetWithInclude(o => o.Id.Equals(id), new string[] { "OrganizationType" });
         EFOrganization organization     = null;
         foreach (var org in organizationList)
         {
             organization = org;
         }
         return(mapper.Map <OrganizationViewModel>(organization));
     }
 }
Exemple #2
0
        public async Task <ActionResponse> ImportEnvelopeData(List <ImportedEnvelopeData> envelopeList)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response                = new ActionResponse();
                var            organizations           = unitWork.OrganizationRepository.GetManyQueryable(o => o.Id != 0);
                var            defaultOrganizationType = unitWork.OrganizationTypesRepository.GetOne(o => o.TypeName == "Other");
                var            envelopeTypes           = unitWork.EnvelopeTypesRepository.GetManyQueryable(e => e.Id != 0);
                var            financialYears          = unitWork.FinancialYearRepository.GetManyQueryable(f => f.Id != 0);

                if (defaultOrganizationType == null)
                {
                    defaultOrganizationType = unitWork.OrganizationTypesRepository.Insert(new EFOrganizationTypes()
                    {
                        TypeName = "Other"
                    });
                    unitWork.Save();
                }

                var envelopeDevelopment = (from et in envelopeTypes
                                           where et.TypeName.Equals("Development", StringComparison.OrdinalIgnoreCase)
                                           select et).FirstOrDefault();
                var envelopeHumanitarian = (from et in envelopeTypes
                                            where et.TypeName.Equals("Humanitarian", StringComparison.OrdinalIgnoreCase)
                                            select et).FirstOrDefault();

                if (envelopeDevelopment == null)
                {
                    envelopeDevelopment = unitWork.EnvelopeTypesRepository.Insert(new EFEnvelopeTypes()
                    {
                        TypeName = "Development"
                    });
                    unitWork.Save();
                }

                if (envelopeHumanitarian == null)
                {
                    envelopeHumanitarian = unitWork.EnvelopeTypesRepository.Insert(new EFEnvelopeTypes()
                    {
                        TypeName = "Humanatarian"
                    });
                }

                var yearEighteen = (from fy in financialYears
                                    where fy.FinancialYear == 2018
                                    select fy).FirstOrDefault();

                var yearNineteen = (from fy in financialYears
                                    where fy.FinancialYear == 2019
                                    select fy).FirstOrDefault();

                var yearTwenty = (from fy in financialYears
                                  where fy.FinancialYear == 2020
                                  select fy).FirstOrDefault();

                if (yearEighteen == null)
                {
                    yearEighteen = unitWork.FinancialYearRepository.Insert(new EFFinancialYears()
                    {
                        FinancialYear = 2018
                    });
                    unitWork.Save();
                }

                if (yearNineteen == null)
                {
                    yearNineteen = unitWork.FinancialYearRepository.Insert(new EFFinancialYears()
                    {
                        FinancialYear = 2019
                    });
                    unitWork.Save();
                }

                if (yearTwenty == null)
                {
                    yearTwenty = unitWork.FinancialYearRepository.Insert(new EFFinancialYears()
                    {
                        FinancialYear = 2020
                    });
                    unitWork.Save();
                }

                try
                {
                    var strategy = context.Database.CreateExecutionStrategy();
                    await strategy.ExecuteAsync(async() =>
                    {
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            foreach (var envelope in envelopeList)
                            {
                                EFOrganization organization = null;
                                if (!string.IsNullOrEmpty(envelope.Organization))
                                {
                                    organization = (from o in organizations
                                                    where o.OrganizationName.Equals(envelope.Organization, StringComparison.OrdinalIgnoreCase)
                                                    select o).FirstOrDefault();

                                    if (organization == null)
                                    {
                                        if (defaultOrganizationType != null)
                                        {
                                            organization = unitWork.OrganizationRepository.Insert(new EFOrganization()
                                            {
                                                OrganizationType = defaultOrganizationType,
                                                OrganizationName = envelope.Organization
                                            });
                                            unitWork.Save();
                                        }
                                    }

                                    EFEnvelope newEnvelope = null;
                                    if (organization != null)
                                    {
                                        newEnvelope = unitWork.EnvelopeRepository.Insert(new EFEnvelope()
                                        {
                                            Funder       = organization,
                                            Currency     = envelope.Currency,
                                            ExchangeRate = envelope.ExchangeRate
                                        });
                                        unitWork.Save();
                                    }

                                    unitWork.EnvelopeYearlyBreakupRepository.Insert(new EFEnvelopeYearlyBreakup()
                                    {
                                        Envelope     = newEnvelope,
                                        Year         = yearEighteen,
                                        EnvelopeType = envelopeDevelopment,
                                        Amount       = envelope.DevelopmentEighteen
                                    });

                                    unitWork.EnvelopeYearlyBreakupRepository.Insert(new EFEnvelopeYearlyBreakup()
                                    {
                                        Envelope     = newEnvelope,
                                        Year         = yearNineteen,
                                        EnvelopeType = envelopeDevelopment,
                                        Amount       = envelope.DevelopmentNineteen
                                    });

                                    unitWork.EnvelopeYearlyBreakupRepository.Insert(new EFEnvelopeYearlyBreakup()
                                    {
                                        Envelope     = newEnvelope,
                                        Year         = yearTwenty,
                                        EnvelopeType = envelopeDevelopment,
                                        Amount       = envelope.DevelopmentTwenty
                                    });

                                    unitWork.EnvelopeYearlyBreakupRepository.Insert(new EFEnvelopeYearlyBreakup()
                                    {
                                        Envelope     = newEnvelope,
                                        Year         = yearEighteen,
                                        EnvelopeType = envelopeHumanitarian,
                                        Amount       = envelope.HumanitarianEighteen
                                    });

                                    unitWork.EnvelopeYearlyBreakupRepository.Insert(new EFEnvelopeYearlyBreakup()
                                    {
                                        Envelope     = newEnvelope,
                                        Year         = yearNineteen,
                                        EnvelopeType = envelopeHumanitarian,
                                        Amount       = envelope.HumanitarianNineteen
                                    });

                                    unitWork.EnvelopeYearlyBreakupRepository.Insert(new EFEnvelopeYearlyBreakup()
                                    {
                                        Envelope     = newEnvelope,
                                        Year         = yearTwenty,
                                        EnvelopeType = envelopeHumanitarian,
                                        Amount       = envelope.HumanitarianTwenty
                                    });

                                    await unitWork.SaveAsync();
                                }
                            }
                            transaction.Commit();
                        }
                    });
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
                return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
            }
        }
        public ActionResponse Add(UserModel model, SmtpClient smtp, string adminEmail)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                try
                {
                    EFOrganization  organization = null;
                    ISecurityHelper sHelper      = new SecurityHelper();
                    IMessageHelper  mHelper;

                    if (!model.IsNewOrganization)
                    {
                        organization = unitWork.OrganizationRepository.GetByID(model.OrganizationId);
                        if (organization == null)
                        {
                            mHelper          = new MessageHelper();
                            response.Success = false;
                            response.Message = mHelper.GetNotFound("Organization");
                            return(response);
                        }
                    }
                    else
                    {
                        EFOrganizationTypes organizationType = null;
                        if (model.IsNewOrganization)
                        {
                            organizationType = unitWork.OrganizationTypesRepository.Get(o => o.TypeName.Equals("Default"));
                            if (organizationType == null)
                            {
                                mHelper          = new MessageHelper();
                                response.Success = false;
                                response.Message = mHelper.GetNotFound("Organization Type");
                                return(response);
                            }

                            organization = new EFOrganization()
                            {
                                OrganizationName = model.OrganizationName,
                                OrganizationType = organizationType
                            };

                            unitWork.Save();
                            model.OrganizationId = organization.Id;
                        }
                    }

                    string passwordHash = sHelper.GetPasswordHash(model.Password);
                    //TODO: Set approved to false to make it approved through notification
                    var newUser = unitWork.UserRepository.Insert(new EFUser()
                    {
                        Name             = model.Name,
                        Email            = model.Email,
                        UserType         = UserTypes.Standard,
                        Organization     = organization,
                        Password         = passwordHash,
                        IsApproved       = true,
                        IsActive         = true,
                        RegistrationDate = DateTime.Now
                    });
                    unitWork.Save();
                    //Get emails for all the users
                    //TODO: To bind the email and notifications with user account creation

                    /*var users = unitWork.UserRepository.GetMany(u => u.OrganizationId.Equals(organization.Id) && u.IsApproved == true);
                     * List<EmailsModel> usersEmailList = new List<EmailsModel>();
                     * foreach (var user in users)
                     * {
                     *  usersEmailList.Add(new EmailsModel()
                     *  {
                     *      Email = user.Email,
                     *      UserName = user.Name,
                     *      UserType = user.UserType
                     *  });
                     * }
                     *
                     * if (usersEmailList.Count == 0)
                     * {
                     *  var managerUsers = unitWork.UserRepository.GetMany(u => u.UserType == UserTypes.Manager || u.UserType == UserTypes.SuperAdmin);
                     *  foreach (var user in managerUsers)
                     *  {
                     *      usersEmailList.Add(new EmailsModel()
                     *      {
                     *          Email = user.Email,
                     *          UserName = user.Name,
                     *          UserType = user.UserType
                     *      });
                     *  }
                     * }
                     *
                     * if (usersEmailList.Count > 0)
                     * {
                     *  //Send emails
                     *  IEmailHelper emailHelper = new EmailHelper(smtp, adminEmail);
                     *  emailHelper.SendNewRegistrationEmail(usersEmailList, organization.OrganizationName);
                     *  mHelper = new MessageHelper();
                     *  string notificationMessage = mHelper.NewUserForOrganization(organization.OrganizationName, model.Name);
                     *
                     *  //Add notification
                     *  unitWork.NotificationsRepository.Insert(new EFUserNotifications()
                     *  {
                     *      UserType = model.UserType,
                     *      Organization = organization,
                     *      Message = notificationMessage,
                     *      TreatmentId = newUser.Id,
                     *      Dated = DateTime.Now,
                     *      IsSeen = false,
                     *      NotificationType = NotificationTypes.NewUser
                     *  });
                     *  unitWork.Save();
                     * }*/
                    response.ReturnedId = newUser.Id;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
                return(response);
            }
        }