Exemplo n.º 1
0
        public async Task <Result <TicketTask> > AddTaskAsync(TicketTask task)
        {
            await using var context = Context;

            Ticket ticket = context.Tickets
                            .Include(t => t.CorrespondingProject)
                            .FirstOrDefault(t => t.TicketId == task.TicketId);

            if (ticket != null)
            {
                User notifier = await context.Users.FindAsync(UserId);

                IReadOnlyCollection <User> usersToNotify = await context.Members
                                                           .Where(member => member.ProjectId == ticket.ProjectId)
                                                           .Include(member => member.CorrespondingUser)
                                                           .Select(member => member.CorrespondingUser)
                                                           .ToListAsync();

                var notificationBuilder = new NotificationBuilder(notifier, usersToNotify);
                IReadOnlyCollection <Notification> notifications =
                    notificationBuilder.BuildTaskCreateNotifications(ticket.CorrespondingProject, ticket, task);

                await context.Notifications.AddRangeAsync(notifications);
            }

            await context.Tasks.AddAsync(task);

            await context.SaveChangesAsync();

            return(new Result <TicketTask> {
                Payload = task, ResultType = ResultType.Ok
            });
        }
Exemplo n.º 2
0
        public async Task <Result <Ticket> > UpdateOrCreateTicketAsync(Ticket ticket)
        {
            await using var context = Context;

            Project project = await context.Projects.FindAsync(ticket.ProjectId);

            if (project == null)
            {
                return(new Result <Ticket> {
                    ResultType = ResultType.NotFound, Message = "Corresponding project not found"
                });
            }

            bool userHasAccess = context.Members.Any(member => member.ProjectId == project.ProjectId && member.UserId == UserId);

            if (!userHasAccess)
            {
                return(new Result <Ticket>
                {
                    ResultType = ResultType.Forbidden,
                    Message = "Sorry, but you don't have permission to update this ticket",
                });
            }

            User user = await context.Users.FindAsync(UserId);

            IReadOnlyCollection <User> members =
                await context.Members
                .Where(member => member.ProjectId == project.ProjectId)
                .Include(member => member.CorrespondingUser)
                .Select(member => member.CorrespondingUser)
                .ToListAsync();

            var notificationsBuilder = new NotificationBuilder(user, members);

            if (ticket.TicketId != 0 && context.Tickets.Any(t => t.TicketId == ticket.TicketId))
            {
                if (ticket.AssigneeId == 0)
                {
                    ticket.AssigneeId = null;
                }
                context.Tickets.Update(ticket);
                await context.Notifications.AddRangeAsync(notificationsBuilder.BuildTicketUpdateNotifications(project, ticket));
            }
            else
            {
                await context.Tickets.AddAsync(ticket);

                await context.Notifications.AddRangeAsync(notificationsBuilder.BuildTicketCreateNotifications(project));
            }

            await context.SaveChangesAsync();

            return(new Result <Ticket> {
                ResultType = ResultType.Ok, Payload = ticket
            });
        }
Exemplo n.º 3
0
        public async Task <Result <string> > UpdateRole(int memberId, int role)
        {
            await using var context = Context;

            Member member = await context.Members.FindAsync(memberId);

            if (member == null)
            {
                return(new Result <string> {
                    ResultType = ResultType.NotFound, Message = "Member not found."
                });
            }

            Project project = await context.Projects.FindAsync(member.ProjectId);

            if (project == null)
            {
                return(new Result <string> {
                    ResultType = ResultType.NotFound, Message = "Corresponding project not found."
                });
            }

            bool userIsOwner = context.Members
                               .Any(m => m.ProjectId == project.ProjectId && m.Role == MemberRole.Owner && m.UserId == _userId);

            if (!userIsOwner)
            {
                return(new Result <string> {
                    ResultType = ResultType.Forbidden, Message = "You don't have permission for this."
                });
            }

            User notifier = await context.Users.FindAsync(_userId);

            IReadOnlyCollection <User> userToNotify = await context.Users.Where(u => u.UserId == member.UserId).ToListAsync();

            var notificationBuilder = new NotificationBuilder(notifier, userToNotify);
            IReadOnlyCollection <Notification> notifications = notificationBuilder.BuildChangeRoleNotifications(project, role);

            member.Role = role;
            context.Members.Update(member);
            await context.Notifications.AddRangeAsync(notifications);

            await context.SaveChangesAsync();

            return(new Result <string> {
                ResultType = ResultType.Ok, Message = "Changed role"
            });
        }
Exemplo n.º 4
0
        public async Task <Result <string> > DeleteTaskAsync(int taskId)
        {
            await using var context = Context;

            TicketTask task = await context.Tasks.FindAsync(taskId);

            Ticket ticket = context.Tickets
                            .Include(t => t.CorrespondingProject)
                            .FirstOrDefault(t => t.TicketId == task.TicketId);

            User notifier = await context.Users.FindAsync(UserId);

            IReadOnlyCollection <User> usersToNotify = await context.Members
                                                       .Where(member => member.ProjectId == ticket.ProjectId)
                                                       .Include(member => member.CorrespondingUser)
                                                       .Select(member => member.CorrespondingUser)
                                                       .ToListAsync();

            var notificationBuilder = new NotificationBuilder(notifier, usersToNotify);
            IReadOnlyCollection <Notification> notifications =
                notificationBuilder.BuildTaskDeleteNotifications(ticket.CorrespondingProject, ticket, task);

            TicketTask correspondingTask = await context.Tasks.FindAsync(taskId);

            if (correspondingTask == null)
            {
                return(new Result <string> {
                    ResultType = ResultType.NotFound, Message = "No task to delete."
                });
            }

            await context.Notifications.AddRangeAsync(notifications);

            context.Tasks.Remove(correspondingTask);
            await context.SaveChangesAsync();

            return(new Result <string> {
                ResultType = ResultType.Ok, Message = "Deleted"
            });
        }
Exemplo n.º 5
0
        public async Task <Result <TicketTask> > ModifyTaskAsync(TicketTask task)
        {
            await using var context = Context;

            if (!context.Tasks.Any(t => t.TaskId == task.TaskId))
            {
                return(new Result <TicketTask> {
                    ResultType = ResultType.NotFound, Message = "No task to update."
                });
            }

            Ticket ticket = context.Tickets
                            .Include(t => t.CorrespondingProject)
                            .FirstOrDefault(t => t.TicketId == task.TicketId);

            if (task.Done && ticket != null)
            {
                User notifier = await context.Users.FindAsync(UserId);

                IReadOnlyCollection <User> usersToNotify = await context.Members
                                                           .Where(member => member.ProjectId == ticket.ProjectId)
                                                           .Include(member => member.CorrespondingUser)
                                                           .Select(member => member.CorrespondingUser)
                                                           .ToListAsync();

                var notificationBuilder = new NotificationBuilder(notifier, usersToNotify);
                IReadOnlyCollection <Notification> notifications =
                    notificationBuilder.BuildTaskModifyNotifications(ticket.CorrespondingProject, ticket, task);

                await context.Notifications.AddRangeAsync(notifications);
            }

            context.Tasks.Update(task);
            await context.SaveChangesAsync();

            return(new Result <TicketTask> {
                ResultType = ResultType.Ok, Payload = task
            });
        }
Exemplo n.º 6
0
        public async Task <Result <Member> > CreateMemberAsync(string email, int projectId)
        {
            await using var context = Context;

            User user = context.Users.FirstOrDefault(u => u.EmailAddress == email);

            if (user == null)
            {
                return(new Result <Member> {
                    ResultType = ResultType.NotFound, Message = "User not found"
                });
            }

            Project project = await context.Projects.FindAsync(projectId);

            if (project == null)
            {
                return(new Result <Member> {
                    ResultType = ResultType.NotFound, Message = "Corresponding project not found."
                });
            }

            bool userHavePermission = context.Members
                                      .Any(m => m.UserId == _userId && m.ProjectId == projectId && (m.Role == MemberRole.Admin || m.Role == MemberRole.Owner));

            if (!userHavePermission)
            {
                return(new Result <Member> {
                    ResultType = ResultType.Forbidden, Message = "You don't have permission."
                });
            }

            var member = new Member {
                ProjectId = projectId, UserId = user.UserId, Role = MemberRole.Member
            };

            if (context.Members.Any(m => m.ProjectId == projectId && m.UserId == user.UserId))
            {
                return(new Result <Member> {
                    ResultType = ResultType.Bad, Message = "Already a member."
                });
            }

            User notifier = await context.Users.FindAsync(_userId);

            var usersToNotify = new List <User> {
                user
            };
            var notificationBuilder = new NotificationBuilder(notifier, usersToNotify);
            IReadOnlyCollection <Notification> notifications = notificationBuilder.BuildInvitations(project);

            await context.Notifications.AddRangeAsync(notifications);

            await context.Members.AddAsync(member);

            await context.SaveChangesAsync();

            return(new Result <Member> {
                ResultType = ResultType.Created, Payload = member
            });
        }