private void AddOrResendInvitation(string inviteeEmail, Invitation i, IQueryable<Invitation> prevSentInvitations) { if (prevSentInvitations.Any(o => o.StatusValue == (int) InvitationStatus.Blocked)) throw new Exception("This user blocked invitations from you."); //check already existing invitation to this project var prevSentToThisProject = prevSentInvitations.FirstOrDefault(o => o.ProjectId == i.ProjectId); if (prevSentToThisProject != null) { switch (prevSentToThisProject.Status) { case InvitationStatus.Sent: throw new Exception("You have already sent invitation to this user."); break; case InvitationStatus.Accepted: throw new Exception("This user is already in your team."); break; case InvitationStatus.Declined: prevSentToThisProject.LastSentDate = DateTime.UtcNow; //resend invitation email again MessageService.SendEmail(inviteeEmail, "Invitation to project", "InviteToProject", new EmailDTO<Invitation>(prevSentToThisProject)); Database.ObjectContext.ApplyCurrentValues("Invitations", prevSentToThisProject); Database.SaveChanges(); return; break; } } i.Project = Database.Projects.FirstOrDefault(p => p.Id == i.ProjectId); Database.Invitations.Add(i); Database.SaveChanges(); //sent email MessageService.SendEmail(inviteeEmail, "Invitation to project", "InviteToProject", new EmailDTO<Invitation>(i)); }
public void SendInvitation(string inviteeEmail, int senderId, int projectId) { var sender = Database.Users.First(u => u.Id == senderId); var i = new Invitation { Sender = sender, ProjectId = projectId }; if (string.Compare(sender.Email, inviteeEmail, true) == 0) throw new Exception("You can't invite yourself."); IQueryable<Invitation> prevSentInvitations; var possibleInvitee = Database.Users.FirstOrDefault(u => u.Email == inviteeEmail); if (possibleInvitee != null) { //we have registered user with this email i.Invitee = possibleInvitee; prevSentInvitations = Database.Invitations.Where(o => o.Sender.Id == senderId && o.Invitee != null && o.Invitee.Id == possibleInvitee.Id); } else { //we don't have registered user. Send him invitation with proposition of registration i.InviteeEmail = inviteeEmail; prevSentInvitations = Database.Invitations.Where(o => o.Sender.Id == senderId && o.InviteeEmail == inviteeEmail); } AddOrResendInvitation(inviteeEmail, i, prevSentInvitations); }
public void SendInvitation(int inviteeId, int senderId, int projectId) { if (inviteeId == senderId) throw new Exception("You can't invite yourself."); var sender = Database.Users.First(u => u.Id == senderId); var invitee = Database.Users.First(u => u.Id == inviteeId); var i = new Invitation { Invitee = invitee, Sender = sender, ProjectId = projectId }; var prevSentInvitations = Database.Invitations.Where(o => o.Sender.Id == senderId && o.Invitee != null && o.Invitee.Id == invitee.Id); AddOrResendInvitation(invitee.Email, i, prevSentInvitations); }
public string ProcessInvitation(Invitation invitation, string verb, int userId) { string result = "Error"; switch (verb) { case "accept": Database.TeamMates.Add(new TeamMate { ProjectId = invitation.ProjectId, UserId = userId, AccessLevel = invitation.AccessLevel }); invitation.Status = InvitationStatus.Accepted; result = "You have successfully accepted the invitation"; break; case "decline": invitation.Status = InvitationStatus.Declined; result = "You have declined the invitation"; break; case "block": invitation.Status = InvitationStatus.Blocked; result = "You will not receive more invitations from this user."; break; } Database.SaveChanges(); return result; }