public async Task <PlannerModel> Handle(CreatePlannerCommand request, CancellationToken cancellationToken)
        {
            var tags = new List <Tag>();

            if (request.Tags != null)
            {
                foreach (var tag in request.Tags)
                {
                    tags.Add(new Tag()
                    {
                        TagName = tag.TagName
                    });
                }
            }
            Planner planner = new Planner()
            {
                Id         = request.Id,
                Title      = request.Title,
                Purpose    = request.Purpose,
                Tags       = tags,
                TeamId     = request.TeamId,
                Milestones = new List <Milestone>()
            };

            _context.Planners.Add(planner);

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <PlannerModel>(planner));
        }
Beispiel #2
0
        public async Task <Unit> Handle(UpdateTeamCommand request, CancellationToken cancellationToken)
        {
            var team = await _context.Teams.FindAsync(Guid.Parse(request.Id));

            if (team == null)
            {
                throw new NotFoundException(nameof(Team), request.Id);
            }
            var loginUser = team.Users.First(u => u.UserOid == _currentUserService.UserId);

            if (loginUser.TeamRole != "Leader")
            {
                throw new NotOwnedException(nameof(Team), team.Id);
            }
            var users = new List <User>();

            foreach (UserModel userModel in request.Users)
            {
                users.Add(new User()
                {
                    DisplayName       = userModel.DisplayName,
                    Email             = userModel.Email,
                    Surname           = userModel.Surname,
                    GivenName         = userModel.GivenName,
                    UserPrincipalName = userModel.UserPrincipalName,
                    UserOid           = userModel.UserId.ToString(),
                    TeamRole          = userModel.TeamRole
                });
            }

            var deletedUsers = team.Users.Where(oldUser => !users.Any(newUser => newUser.UserOid == oldUser.UserOid)).ToList();

            // get value from request to update
            team.Name        = request.Name;
            team.Description = request.Description;
            team.Users       = users;

            // find all public channel in team
            var lstChannels = await _context.Conversations
                              .Where(con => con.TeamId == team.Id.ToString())
                              .ToListAsync(cancellationToken);

            // modify members of channel
            foreach (var channel in lstChannels)
            {
                if (channel.isPublic)
                {
                    channel.Members = users;
                }
                else
                {
                    channel.Members = channel.Members.Where(member =>
                                                            !deletedUsers.Any(deleteUser => deleteUser.UserOid.Equals(member.UserOid))).ToList();
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #3
0
        public async Task <Unit> Handle(DeleteChannelConversationCommand request, CancellationToken cancellationToken)
        {
            var conversation = await _context.Conversations.FindAsync(new Guid(request.ConversationId));

            if (conversation == null || string.IsNullOrEmpty(conversation.TeamId))
            {
                throw new NotFoundException(nameof(Conversation), request.ConversationId);
            }
            var team = await _context.Teams.FindAsync(new Guid(conversation.TeamId));

            if (team == null)
            {
                throw new NotFoundException(nameof(Team), conversation.TeamId);
            }
            if (!conversation.CreatedBy.Equals(_currentUserService.UserId) &&
                !_currentUserService.UserId.Equals(team.Users.First(u => u.TeamRole == "Leader").UserOid))
            {
                throw new NotOwnedException(nameof(Conversation), request.ConversationId);
            }
            var messageChats = await _context.MessageChats.Where(m => m.ConversationId == conversation.Id).ToListAsync();

            _context.MessageChats.RemoveRange(messageChats);
            _context.Conversations.Remove(conversation);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #4
0
        public async Task <ConversationModel> Handle(CreateConversationCommand request, CancellationToken cancellationToken)
        {
            var validConversationType = new List <string> {
                "pair", "group", "channel"
            };

            // check conversation type is valid
            if (!validConversationType.Contains(request.Type))
            {
                throw new NotTypeException(request.Type, validConversationType);
            }

            // copy prop from request
            var conversation = new Conversation
            {
                Name     = request.Name,
                Type     = request.Type,
                Members  = _mapper.Map <List <User> >(request.Members),
                isPublic = request.IsPublic,
                TeamId   = request.TeamId
            };

            // register new conversation
            _context.Conversations.Add(conversation);
            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <ConversationModel>(conversation));
        }
        public async Task <Unit> Handle(PinAttachFileCommand request, CancellationToken cancellationToken)
        {
            // get Message
            var messageChat = await _context.MessageChats.FindAsync(request.MessageId);

            if (messageChat == null)
            {
                throw new NotFoundException(nameof(MessageChat), request.MessageId);
            }

            // get attachfile
            var attachFile = messageChat.AttachFileList?
                             .Where(file => file.BlobStorageUrl == request.BlobStorageUrl)
                             .FirstOrDefault();

            if (attachFile == null)
            {
                throw new NotFoundException(nameof(AttachFile), request.BlobStorageUrl);
            }

            // update attach file
            attachFile.IsPin = request.IsPinFile;
            // update lai pinned date cua message
            messageChat.PinnedDate = DateTime.UtcNow;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <string> Handle(UpdateMessageChatCommand request, CancellationToken cancellationToken)
        {
            var messageChat = await _context.MessageChats.FindAsync(request.MessageId);

            var result = "";

            if (messageChat == null)
            {
                throw new NotFoundException(nameof(MessageChat), request.MessageId);
            }

            if (messageChat.ReactionList == null)
            {
                messageChat.ReactionList = new List <Reaction>();
            }
            if (request.ReactionType != null)
            {
                var reaction = messageChat.ReactionList.FirstOrDefault(r => r.ReactorId == _currentUserService.UserId);
                if (reaction == null)
                {
                    messageChat.ReactionList.Add(new Reaction
                    {
                        ReactorId    = _currentUserService.UserId,
                        ReactorName  = _currentUserService.UserName,
                        ReactionType = request.ReactionType
                    });
                    result = "addReaction";
                }
                else
                {
                    messageChat.ReactionList.Remove(reaction);
                    result = "deleteReaction";
                    if (!reaction.ReactionType.Equals(request.ReactionType))
                    {
                        messageChat.ReactionList.Add(new Reaction
                        {
                            ReactorId    = _currentUserService.UserId,
                            ReactorName  = _currentUserService.UserName,
                            ReactionType = request.ReactionType
                        });
                        result = "updateReaction";
                    }
                }
            }
            if (request.IsPin.HasValue)
            {
                messageChat.IsPin      = request.IsPin.Value;
                messageChat.PinnedDate = DateTime.UtcNow;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(result);
        }
        public async Task <ConversationModel> Handle(CreateChannelConversationCommand request, CancellationToken cancellationToken)
        {
            Team team = await _context.Teams.FindAsync(Guid.Parse(request.TeamId));

            if (team == null)
            {
                throw new NotFoundException(nameof(Team), request.TeamId);
            }

            var conversation = new Conversation();
            var allChannel   = _context.Conversations
                               .Where(conversation => conversation.TeamId == request.TeamId && conversation.Type == "channel").ToList();

            foreach (var temp in allChannel)
            {
                if (temp.Name == request.Name)
                {
                    throw new ArgumentException(String.Format("This name had exsisted: {0}", request.Name));
                }
            }

            if (request.IsPublic == true)
            {
                // copy prop from request
                conversation = new Conversation
                {
                    Name = request.Name,
                    ChannelDescription = request.ChannelDescription,
                    Type     = "channel",
                    Members  = team.Users,
                    isPublic = request.IsPublic,
                    TeamId   = request.TeamId
                };
            }
            else
            {
                conversation = new Conversation
                {
                    Name = request.Name,
                    ChannelDescription = request.ChannelDescription,
                    Type     = "channel",
                    Members  = _mapper.Map <List <User> >(request.Members),
                    isPublic = request.IsPublic,
                    TeamId   = request.TeamId
                };
            }

            // register new conversation
            _context.Conversations.Add(conversation);
            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <ConversationModel>(conversation));
        }
Beispiel #8
0
        public async Task <Unit> Handle(DeletePlannerCommand request, CancellationToken cancellationToken)
        {
            var planner = await _context.Planners.FindAsync(request.Id);

            if (planner == null)
            {
                throw new NotFoundException(nameof(Planner), request.Id);
            }
            _context.Planners.Remove(planner);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #9
0
        public async Task <Unit> Handle(UnReadNotificationCommand request, CancellationToken cancellationToken)
        {
            Notification notification = await _context.Notifications.FindAsync(request.NotificationId);

            if (notification == null)
            {
                throw new NotFoundException(nameof(Notification), request.NotificationId);
            }

            notification.isRead = false;
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #10
0
        public async Task <TaskItemModel> Handle(PinTaskItemCommand request, CancellationToken cancellationToken)
        {
            // lay thong tin task item
            var taskItem = _context.TaskItems.Find(request.Id);

            if (taskItem == null)
            {
                throw new NotFoundException(nameof(TaskItem), request.Id);
            }

            // cap nhat lai task item
            taskItem.IsPin      = request.IsPin;
            taskItem.PinnedDate = DateTime.UtcNow;

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <TaskItemModel>(taskItem));
        }
        public async Task <Guid> Handle(CreateTeamCommand request, CancellationToken cancellationToken)
        {
            // create team
            var users = new List <User>();

            foreach (UserModel userModel in request.Users)
            {
                users.Add(new User
                {
                    UserOid           = userModel.UserId.ToString(),
                    DisplayName       = userModel.DisplayName,
                    Email             = userModel.Email,
                    Surname           = userModel.Surname,
                    GivenName         = userModel.GivenName,
                    UserPrincipalName = userModel.UserPrincipalName,
                    TeamRole          = userModel.TeamRole
                });
            }
            var team = new Team()
            {
                Name        = request.Name,
                Users       = users,
                Description = request.Description,
            };

            _context.Teams.Add(team);

            // create general channel in team
            var conversation = new Conversation
            {
                Name     = "General",
                Type     = "channel",
                Members  = users,
                isPublic = true,
                TeamId   = team.Id.ToString()
            };

            // register new conversation
            _context.Conversations.Add(conversation);
            await _context.SaveChangesAsync(cancellationToken);

            return(team.Id);
        }
Beispiel #12
0
        public async Task <Unit> Handle(UpdateConversationNameCommand request, CancellationToken cancellationToken)
        {
            var conversation = await _context.Conversations.FindAsync(request.Id);

            if (conversation == null)
            {
                throw new NotFoundException(nameof(Conversation), request.Id);
            }

            conversation.Name = request.Name;

            if (request.Members?.Count > 0)
            {
                conversation.Members = _mapper.Map <List <User> >(request.Members);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteTeamCommand request, CancellationToken cancellationToken)
        {
            var team = await _context.Teams.FindAsync(Guid.Parse(request.Id));

            if (team == null)
            {
                throw new NotFoundException(nameof(Team), request.Id);
            }
            var loginUser = team.Users.First(u => u.UserOid == _currentUserService.UserId);

            if (loginUser.TeamRole != "Leader")
            {
                throw new NotOwnedException(nameof(Team), team.Id);
            }

            var taskItems = await _context.TaskItems.Where(t => t.TeamId == team.Id).ToListAsync();

            // find all channel when delete team
            var channels = await _context.Conversations.Where(t => t.TeamId == team.Id.ToString()).ToListAsync();

            // find all message chat
            var messageChats = new List <MessageChat>();

            foreach (var channel in channels)
            {
                // find MessageChat async (tim cac MessageChat cung mot luc)
                messageChats.AddRange(await _context.MessageChats.Where(t => t.ConversationId == channel.Id).ToListAsync());
            }

            _context.MessageChats.RemoveRange(messageChats);

            _context.TaskItems.RemoveRange(taskItems);

            _context.Conversations.RemoveRange(channels);

            _context.Teams.Remove(team);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(ReadConversationCommand request, CancellationToken cancellationToken)
        {
            Conversation conversation = await _context.Conversations.FindAsync(Guid.Parse(request.ConversationId));

            if (conversation == null)
            {
                throw new NotFoundException(nameof(Conversation), request.ConversationId);
            }

            foreach (User user in conversation.Members)
            {
                if (user.UserOid == _currentUserService.UserId)
                {
                    user.LastSeenTime = DateTime.UtcNow;
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #15
0
        public async Task <ConversationModel> Handle(GetConversationQuery request, CancellationToken cancellationToken)
        {
            Conversation conversation;

            if (!string.IsNullOrEmpty(request.ConversationId))
            {
                conversation = await _context.Conversations.FindAsync(new Guid(request.ConversationId));

                if (conversation == null)
                {
                    throw new NotFoundException(nameof(Conversation), request.ConversationId);
                }
            }
            else
            {
                var listMember = request.MembersId.Select(id => "AND ARRAY_CONTAINS(c.Members, {'UserOid': '" + id + "'}, true)").ToList();
                var queryStr   = $"SELECT c.id, c.Name, c.Type, c.Members, c.isHidden, c.TeamId, c.isPublic " +
                                 $"FROM c " +
                                 $"WHERE ARRAY_LENGTH(c.Members) = {listMember.Count} " +
                                 $"{string.Join(' ', listMember)} " +
                                 $"AND c.Type != 'channel' ";
                conversation = await GetConversationAsync(queryStr);
            }
            if (conversation != null)
            {
                Conversation tempConversation = await _context.Conversations.FindAsync(conversation.Id);

                foreach (User user in tempConversation.Members)
                {
                    if (user.isHidden && user.UserOid == request.LoginUserId)
                    {
                        user.isHidden = false;
                    }
                }
                await _context.SaveChangesAsync(cancellationToken);
            }


            return(_mapper.Map <Conversation, ConversationModel>(conversation));
        }
        /**
         * メンバーがタスク削除/Member Delete Task
         * リーダーがすべてメンバーのタスク削除/Leader Delete Task of All Members
         */
        public async Task <TaskItemModel> Handle(DeleteTaskItemCommand request, CancellationToken cancellationToken)
        {
            if (request.Id == null)
            {
                throw new ArgumentNullException("Task item id can not be null!");
            }

            var task = await _context.TaskItems.FindAsync(Guid.Parse(request.Id));

            if (task == null)
            {
                throw new NotFoundException(nameof(TaskItem), request.Id);
            }

            // check login user own task
            if (_currentUserService.UserId != task.CreatedBy.UserOid && _currentUserService.UserId != task.Assignee.UserOid)
            {
                var team = await _context.Teams.FindAsync(task.TeamId);

                if (team == null)
                {
                    throw new NotFoundException(nameof(team), task.TeamId);
                }
                else
                {
                    // search leader in team
                    var loginUser = team.Users.First(u => u.UserOid == _currentUserService.UserId);
                    if (loginUser.TeamRole != "Leader")
                    {
                        throw new NotOwnedException(nameof(TaskItem), task.Id);
                    }
                }
            }

            _context.TaskItems.Remove(task);

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <TaskItem, TaskItemModel>(task));
        }
        public async Task <Guid> Handle(CreateTaskItemCommand request, CancellationToken cancellationToken)
        {
            var team = await _context.Teams.FindAsync(request.TeamId);

            if (team == null)
            {
                throw new NotFoundException(nameof(Team), request.TeamId);
            }
            List <AttachFile> attachFiles = new List <AttachFile>();

            if (request.AttachFiles != null)
            {
                foreach (AttachFileModel file in request.AttachFiles)
                {
                    attachFiles.Add(new AttachFile()
                    {
                        FileName        = file.FileName,
                        FileStorageName = file.FileStorageName,
                        BlobStorageUrl  = file.BlobStorageUrl,
                        FileSize        = file.FileSize,
                        ThumbnailImage  = file.ThumbnailImage
                    });
                }
            }
            List <Tag> tags = new List <Tag>();

            if (request.Tags != null)
            {
                foreach (TagModel tag in request.Tags)
                {
                    tags.Add(new Tag()
                    {
                        TagName = tag.TagName
                    });
                }
            }

            // related Object
            List <RelatedObject> listRelatedObject = new List <RelatedObject>();

            if (request.Relations != null)
            {
                foreach (RelatedObjectModel relatedObject in request.Relations)
                {
                    listRelatedObject.Add(new RelatedObject()
                    {
                        ObjectId = relatedObject.ObjectId,
                        Title    = relatedObject.Title,
                        Type     = relatedObject.Type,
                        Url      = relatedObject.Url
                    });
                }
            }

            var taskItem = new TaskItem()
            {
                Name        = request.Name,
                Description = request.Description,
                Status      = request.Status,
                Assignee    = new User {
                    UserOid = request.Assignee.UserId.ToString(), DisplayName = request.Assignee.DisplayName
                },
                AttachFiles = attachFiles,
                CreatedBy   = new User {
                    UserOid = request.CreatedBy.UserId.ToString(), DisplayName = request.CreatedBy.DisplayName
                },
                TeamId            = request.TeamId,
                Deadline          = request.Deadline?.ToUniversalTime(),
                Tags              = tags,
                RelatedMessagesId = request.RelatedMessagesId,
                Relations         = listRelatedObject
            };

            // history update
            var historiesList = new List <History>();

            historiesList.Add(new History()
            {
                Title    = _currentUserService.UserName + " created a new task",
                Type     = "Task",
                Content  = "Create",
                UserId   = _currentUserService.UserId,
                UserName = _currentUserService.UserName,
                Date     = DateTime.UtcNow
            });

            taskItem.Histories = historiesList;

            _context.TaskItems.Add(taskItem);

            // login user la assignee thi khong tao feed
            if (_currentUserService.UserId != taskItem.Assignee.UserOid)
            {
                // tao feed khi tao moi 1 task
                var notification = new Notification
                {
                    isRead      = false,
                    TaskItemId  = taskItem.Id.ToString(),
                    TeamId      = taskItem.TeamId.ToString(),
                    ToUser      = taskItem.Assignee,
                    Type        = "AddTask",
                    RegUserName = taskItem.CreatedBy.DisplayName,
                    RegUserId   = taskItem.CreatedBy.UserOid,
                    Title       = taskItem.CreatedBy.DisplayName + " created task " + taskItem.Name
                };

                _context.Notifications.Add(notification);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(taskItem.Id);
        }
Beispiel #18
0
        public async Task <Unit> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
        {
            if (request.ToUser == null)
            {
                request.ToUser = new List <UserModel>();
            }
            var validNotificationType = new List <string> {
                "Mention", "AddGroup", "Reaction", "..."
            };

            if (!validNotificationType.Contains(request.Type))
            {
                throw new NotTypeException(request.Type, validNotificationType);
            }

            var notificationList = new List <Notification>()
            {
            };

            if (request.Type == "Mention")
            {
                if (string.IsNullOrEmpty(request.TeamId))
                {
                    foreach (var user in request.ToUser)
                    {
                        var notification = getNewNotification(request);
                        notification.Title  = request.RegUserName + " mentioned you in a message";
                        notification.ToUser = new User()
                        {
                            DisplayName = user.DisplayName, UserOid = user.UserId.ToString()
                        };
                        notification.MessageContent = request.MessageContent;
                        notification.MessageId      = request.MessageId;
                        notificationList.Add(notification);
                    }
                }
                else
                {
                    foreach (var user in request.ToUser)
                    {
                        var notification = getNewNotification(request);
                        notification.Title  = request.RegUserName + " mentioned you in a channel";
                        notification.ToUser = new User()
                        {
                            DisplayName = user.DisplayName, UserOid = user.UserId.ToString()
                        };
                        notification.MessageContent = request.MessageContent;
                        notification.MessageId      = request.MessageId;
                        notificationList.Add(notification);
                    }
                }
            }
            else if (request.Type == "AddGroup")
            {
                foreach (var user in request.ToUser)
                {
                    var notification = getNewNotification(request);
                    notification.Title          = request.RegUserName + " added you into a group";
                    notification.MessageContent = "";
                    notification.MessageId      = "";
                    notification.ToUser         = new User()
                    {
                        DisplayName = user.DisplayName, UserOid = user.UserId.ToString()
                    };
                    notificationList.Add(notification);
                }
            }
            else if (request.Type == "Reaction")
            {
                foreach (var user in request.ToUser)
                {
                    var notification = getNewNotification(request);
                    notification.Title          = request.RegUserName + " added a reaction";
                    notification.MessageContent = request.MessageContent;
                    notification.MessageId      = request.MessageId;
                    notification.ToUser         = new User()
                    {
                        DisplayName = user.DisplayName, UserOid = user.UserId.ToString()
                    };
                    notificationList.Add(notification);
                }
            }
            _context.Notifications.AddRange(notificationList);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #19
0
        public async Task <Unit> Handle(UpdateTaskItemCommand request, CancellationToken cancellationToken)
        {
            var task = await _context.TaskItems.FindAsync(request.Id);

            if (task == null)
            {
                throw new NotFoundException(nameof(TaskItem), request.Id);
            }

            var team = await _context.Teams.FindAsync(request.TeamId);

            if (team == null)
            {
                throw new NotFoundException(nameof(Team), request.TeamId);
            }
            // check login user own task
            if (_currentUserService.UserId != task.CreatedBy.UserOid &&
                _currentUserService.UserId != task.Assignee.UserOid)
            {
                var loginUser = team.Users.First(u => u.UserOid == _currentUserService.UserId);
                if (loginUser.TeamRole != "Leader")
                {
                    throw new NotOwnedException(nameof(TaskItem), task.Id);
                }
            }
            List <AttachFile> attachFiles = new List <AttachFile>();

            if (request.AttachFiles != null)
            {
                foreach (AttachFileModel file in request.AttachFiles)
                {
                    attachFiles.Add(new AttachFile()
                    {
                        FileName        = file.FileName,
                        FileStorageName = file.FileStorageName,
                        BlobStorageUrl  = file.BlobStorageUrl,
                        FileSize        = file.FileSize,
                        ThumbnailImage  = file.ThumbnailImage
                    });
                }
            }
            List <Tag> tags = new List <Tag>();

            if (request.Tags != null)
            {
                foreach (TagModel tag in request.Tags)
                {
                    tags.Add(new Tag()
                    {
                        TagName = tag.TagName
                    });
                }
            }

            // related Object
            List <RelatedObject> listRelatedObject = new List <RelatedObject>();

            if (request.Relations != null)
            {
                foreach (RelatedObjectModel relatedObject in request.Relations)
                {
                    listRelatedObject.Add(new RelatedObject()
                    {
                        ObjectId = relatedObject.ObjectId,
                        Title    = relatedObject.Title,
                        Type     = relatedObject.Type,
                        Url      = relatedObject.Url
                    });
                }
            }

            var history = createHistory(request, task);

            task.Name        = request.Name;
            task.Status      = request.Status;
            task.TeamId      = request.TeamId;
            task.Description = request.Description;
            task.Assignee    = new User {
                UserOid = request.Assignee?.UserId.ToString(), DisplayName = request.Assignee?.DisplayName
            };
            task.AttachFiles = attachFiles;
            task.CreatedBy   = new User {
                UserOid = request.CreatedBy?.UserId.ToString(), DisplayName = request.CreatedBy?.DisplayName
            };
            task.Tags      = tags;
            task.Deadline  = request.Deadline?.ToUniversalTime();
            task.Relations = listRelatedObject;

            if (!String.IsNullOrEmpty(history?.Title))
            {
                if (task.Histories == null || task.Histories?.Count == 0)
                {
                    List <History> histories = new List <History>();
                    if (history == null)
                    {
                    }
                    else
                    {
                        histories.Add(history);
                        task.Histories = histories;
                    }
                }
                else
                {
                    if (history == null)
                    {
                    }
                    else
                    {
                        task.Histories.Add(history);
                    }
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #20
0
        public async Task <Unit> Handle(UpdatePlannerCommand request, CancellationToken cancellationToken)
        {
            // For test
            var planner = await _context.Planners.FindAsync(request.Id);

            if (planner == null)
            {
                throw new NotFoundException(nameof(Planner), request.Id);
            }
            // update Tags
            List <Tag> tags = new List <Tag>();

            if (request.Tags != null)
            {
                foreach (TagModel tag in request.Tags)
                {
                    tags.Add(new Tag()
                    {
                        TagName = tag.TagName
                    });
                }
            }
            //update milestones
            List <Milestone> milestones = new List <Milestone>();

            if (request.Milestones != null)
            {
                foreach (var m in request.Milestones)
                {
                    List <Guid> listTaskId = new List <Guid>();
                    if (m.Tasks != null)
                    {
                        foreach (var task in m.Tasks)
                        {
                            listTaskId.Add(task.Id);
                        }
                    }
                    milestones.Add(new Milestone()
                    {
                        Title       = m.Title,
                        Date        = m.Date,
                        Description = m.Description,
                        TaskIds     = listTaskId,
                    });
                }
            }
            // update operation
            planner.Tags       = tags;
            planner.Milestones = milestones;
            // update title
            if (request.Title != null && planner.Title != request.Title)
            {
                planner.Title = request.Title;
            }
            // update Purpose
            if (request.Purpose != null && planner.Purpose != request.Purpose)
            {
                planner.Purpose = request.Purpose;
            }
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(ChangeStatusTaskItemCommand request, CancellationToken cancellationToken)
        {
            var task = await _context.TaskItems.FindAsync(request.Id);

            if (task == null)
            {
                throw new NotFoundException(nameof(TaskItem), request.Id);
            }

            var team = await _context.Teams.FindAsync(task.TeamId);

            // check login user own task
            if (_currentUserService.UserId != task.CreatedBy.UserOid &&
                _currentUserService.UserId != task.Assignee.UserOid &&
                team != null)
            {
                var loginUser = team.Users.First(u => u.UserOid == _currentUserService.UserId);
                if (loginUser.TeamRole != "Leader")
                {
                    throw new NotOwnedException(nameof(TaskItem), task.Id);
                }
            }

            if (task.Histories == null || task.Histories?.Count == 0)
            {
                List <History> histories = new List <History>();
                histories.Add(createHistory(request, task));
                task.Histories = histories;
            }
            else
            {
                task.Histories.Add(createHistory(request, task));
            }

            // team leader
            var teamLeader = GetTeamLeader(team);

            task.Status   = request.Status;
            task.Assignee = new User {
                UserOid = request.Assignee.UserId.ToString(), DisplayName = request.Assignee.DisplayName
            };

            // tao notification neu hoan thanh task
            if (task.Status == 3 && teamLeader.UserOid != _currentUserService.UserId)
            {
                var notification = new Notification
                {
                    isRead      = false,
                    TaskItemId  = task.Id.ToString(),
                    TeamId      = team.Id.ToString(),
                    ToUser      = teamLeader,
                    Type        = "DoneTask",
                    RegUserName = _currentUserService.UserName,
                    RegUserId   = _currentUserService.UserId,
                    Title       = task.Assignee.DisplayName + " finished task" + task.Name
                };

                _context.Notifications.Add(notification);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Guid> Handle(CreateMessageChatCommand request, CancellationToken cancellationToken)
        {
            // check conversation exists
            var conversation = await _context.Conversations.FindAsync(request.ConversationId);

            if (conversation == null)
            {
                throw new NotFoundException(nameof(Conversation), request.ConversationId);
            }

            // check login user belong to member of conversation
            if (!conversation.Members.Any(user => user.UserOid.Equals(request.RegUserId)))
            {
                throw new NotOwnedException(nameof(Conversation), request.ConversationId);
            }


            List <AttachFile> attachFiles = new List <AttachFile>();

            if (request.AttachFileList != null)
            {
                foreach (AttachFileModel file in request.AttachFileList)
                {
                    attachFiles.Add(new AttachFile()
                    {
                        FileName        = file.FileName,
                        FileStorageName = file.FileStorageName,
                        BlobStorageUrl  = file.BlobStorageUrl,
                        FileSize        = file.FileSize,
                        ThumbnailImage  = file.ThumbnailImage
                    });
                }
            }

            // copy prop from request
            var messageChat = new MessageChat
            {
                Content           = request.Content,
                ConversationId    = request.ConversationId,
                ConversationName  = conversation.Name,
                RegUserName       = request.RegUserName,
                AttachFileList    = attachFiles,
                CreatedBy         = request.RegUserId,
                ReactionList      = new List <Reaction>(),
                IsPin             = false,
                PinnedDate        = DateTime.UtcNow,
                RelatedMessagesId = request.RelatedMessagesId,
                RelatedTaskId     = request.RelatedTaskId,
            };

            // register new conversation
            _context.MessageChats.Add(messageChat);
            // isHide false for all
            foreach (var member in conversation.Members)
            {
                member.isHidden = false;
            }
            await _context.SaveChangesAsync(cancellationToken);

            return(messageChat.Id);
        }