Exemplo n.º 1
0
        public async Task <ActionResult> SendInvitation(SendInvitationViewModel submitedModel)
        {
            if (ModelState.IsValid)
            {
                var user = UserManager.FindById(User.Identity.GetUserId());

                string orgName        = null;
                int    invitationCode = -1;

                using (ApplicationDbContext context = new ApplicationDbContext())
                {
                    var existingEmail = await UserManager.FindByEmailAsync(submitedModel.Email);

                    if (existingEmail == null)
                    {
                        var userOrg = from org in context.Orgs
                                      where org.OrgID == user.OrgID
                                      select org;
                        if (userOrg.Count() == 1)
                        {
                            Random random = new Random();
                            int    emailConfirmationCode;
                            bool   isUnique = false;

                            do
                            {
                                emailConfirmationCode = random.Next(10000, 1000000000);

                                var duplicitInvitationCode = from inv in context.Invitations
                                                             where inv.Code == emailConfirmationCode
                                                             select inv;
                                isUnique = !duplicitInvitationCode.Any();
                            }while (!isUnique);

                            var newInvitation = new Invitation()
                            {
                                Accepted = false,
                                Code     = emailConfirmationCode,
                                Email    = submitedModel.Email.Trim().ToLower(),
                                Org      = userOrg.FirstOrDefault()
                            };

                            context.Invitations.Add(newInvitation);
                            context.SaveChanges();


                            invitationCode = newInvitation.Code;
                            orgName        = newInvitation.Org.Name;
                        }
                        else
                        {
                            return(View("~/Views/Shared/Error.cshtml"));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", OrgResources.InvitationCantBeSentAlreadyExistEmail);
                        return(View(submitedModel));
                    }
                }

                var email = _userMailer.SendInvitation(submitedModel.Email.Trim(), user.UserName, user.Email, orgName, invitationCode);
                email.SendAsync();

                ViewBag.StatusMessage = OrgResources.InvitationSend;
                return(View(new SendInvitationViewModel()));
            }

            return(View(submitedModel));
        }