public IActionResult SendInvitations(SendInvitationViewModel model)
        {
            Console.WriteLine("");

            try
            {
                var response = _service.GetAllTeamsFromQuiz(model.QuizId);
                if (response.DidError)
                {
                    return(BadRequest(response.Errors));
                }

                RestClient client = new RestClient();
                client.BaseUrl       = new Uri("https://api.mailgun.net/v3/sandbox3d7deafd31914a929d422e3699215cb8.mailgun.org/messages");
                client.Authenticator = new HttpBasicAuthenticator("api", "97cfda12723cac0322b1040bf1c133f3-ba042922-43f2ab8e");

                foreach (var team in response.DTO)
                {
                    var         pin     = Guid.NewGuid().ToString();
                    RestRequest request = new RestRequest();
                    request.AddParameter("domain", "sandbox3d7deafd31914a929d422e3699215cb8.mailgun.org", ParameterType.UrlSegment);
                    //request.Resource = "{domain}/messages";
                    request.AddParameter("from", "Excited User <*****@*****.**>");
                    request.AddParameter("to", team.Email);
                    //request.AddParameter("to", "*****@*****.**");
                    request.AddParameter("subject", "Invitation to play in a quiz!");
                    var message = model.Message + "\n \nHere is you pin to get access to the quiz: " + pin;
                    if (model.PaypalLink != "")
                    {
                        message += "\n \nHere is the link where you can pay before you can play the quiz: " + model.PaypalLink;
                    }
                    request.AddParameter("text", message);
                    request.Method = Method.POST;
                    var responseClient = client.Execute(request);

                    if (responseClient.IsSuccessful)
                    {
                        team.PIN    = pin;
                        team.QuizId = model.QuizId;


                        var teamResponse = _teamService.Update(team);
                        if (teamResponse.DidError)
                        {
                            return(BadRequest(teamResponse.Errors));
                        }
                        var teamReturn = TeamViewModelMapper.MapTeamDTOToTeamViewModelResponse(teamResponse.DTO);
                        return(Ok(teamReturn));
                    }
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Beispiel #2
0
        public IHttpActionResult SendInvitation(SendInvitationViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var group = this.groupService.GetGroup(viewModel.GroupName);

            if (group == null)
            {
                return(NotFound());
            }

            var currentUserId = User.Identity.GetUserId();
            var isAdmin       = group.CreatorId == currentUserId;

            if (!isAdmin)
            {
                return(this.Content(HttpStatusCode.Forbidden, "You don't have permissions to send invitation to this group"));
            }

            var user = this.userService.GetUser(viewModel.ToUsername);

            if (user == null)
            {
                return(NotFound());
            }

            var alreadyInvited = this.groupService.AlreadyInvited(group.GroupId, user.Id);

            if (alreadyInvited)
            {
                return(Conflict());
            }

            var invitation = new Invitation
            {
                GroupId   = group.GroupId,
                UserId    = user.Id,
                CreatedAt = DateTime.Now,
            };

            this.groupService.AddInvitation(invitation);
            var result = new SendInvitationResultModel
            {
                InvitationId = invitation.InvitationId,
                GroupId      = invitation.GroupId,
                CreatedAt    = invitation.CreatedAt,
                CreatedBy    = user.Username,
            };

            return(Created("", result)); // TODO: url
        }
Beispiel #3
0
        public async Task <IActionResult> SendInvitaion(Guid listId, [FromBody] SendInvitationViewModel invitation)
        {
            var senderAccountId = User.ReadClaimAsGuidValue("urn:codefliptodo:accountid");
            var senderEmail     = User.FindFirst(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value;

            var command = new SendInvitation()
            {
                SenderAccountId = senderAccountId,
                SenderEmail     = senderEmail,
                ListId          = listId,
                InviteeEmail    = invitation.Email
            };

            var response = await _mediator.Send(command);

            if (response == true)
            {
                return(Ok());
            }

            return(BadRequest());
        }
Beispiel #4
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));
        }