Exemplo n.º 1
0
        public async Task <ActionResult> SendMessage([FromBody] NewMessageInput newMessageInput, CancellationToken cancellationToken)
        {
            if (newMessageInput.Rcps == null || newMessageInput.Rcps.Count <= 0)
            {
                return(BadRequest());
            }

            Message savedMessage = null;

            try
            {
                var message = new Message();
                message.Subject       = newMessageInput.Ttl;
                message.Body          = System.Net.WebUtility.HtmlDecode(newMessageInput.Bdy);
                message.IsBroadcast   = true;
                message.SendingUserId = UserId;
                message.Type          = newMessageInput.Typ;
                message.SentOn        = DateTime.UtcNow;

                var usersToSendTo = new List <string>();

                if (newMessageInput.Rcps.Any(x => x.Nme == "Everyone"))
                {
                    var departmentUsers = await _departmentsService.GetAllMembersForDepartmentAsync(DepartmentId);

                    foreach (var departmentMember in departmentUsers)
                    {
                        message.AddRecipient(departmentMember.UserId);
                    }
                }
                else
                {
                    // Add all the explict people
                    foreach (var person in newMessageInput.Rcps.Where(x => x.Typ == 1))
                    {
                        if (usersToSendTo.All(x => x != person.Id) && person.Id != UserId)
                        {
                            usersToSendTo.Add(person.Id);
                            message.AddRecipient(person.Id);
                        }
                    }

                    // Add all memebers of the group
                    foreach (var group in newMessageInput.Rcps.Where(x => x.Typ == 2))
                    {
                        if (!String.IsNullOrWhiteSpace(group.Id))
                        {
                            int groupId = 0;
                            if (int.TryParse(group.Id.Trim(), out groupId))
                            {
                                var members = await _departmentGroupsService.GetAllMembersForGroupAsync(groupId);

                                foreach (var member in members)
                                {
                                    if (usersToSendTo.All(x => x != member.UserId) && member.UserId != UserId)
                                    {
                                        usersToSendTo.Add(member.UserId);
                                        message.AddRecipient(member.UserId);
                                    }
                                }
                            }
                        }
                    }

                    // Add all the users of a specific role
                    foreach (var role in newMessageInput.Rcps.Where(x => x.Typ == 3))
                    {
                        var roleMembers = await _personnelRolesService.GetAllMembersOfRoleAsync(int.Parse(role.Id));

                        foreach (var member in roleMembers)
                        {
                            if (usersToSendTo.All(x => x != member.UserId) && member.UserId != UserId)
                            {
                                usersToSendTo.Add(member.UserId);
                                message.AddRecipient(member.UserId);
                            }
                        }
                    }
                }

                savedMessage = await _messageService.SaveMessageAsync(message, cancellationToken);

                await _messageService.SendMessageAsync(savedMessage, "", DepartmentId, false, cancellationToken);
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
                return(BadRequest());
            }

            return(CreatedAtAction(nameof(SendMessage), new { id = savedMessage.MessageId }, savedMessage));
        }