예제 #1
0
        public async Task <ApiResponse> Get([FromQuery] ConversationsFilterParams arg)
        {
            try
            {
                var data = await conversationService.GetAllAsync(arg);

                return(new ApiResponse(data, HttpStatusCode.OK.ToInt()));
            }
            catch (CustomException ex)
            {
                throw new ApiException(ex, ex.StatusCode);
            }
            catch (Exception ex)
            {
                throw new ApiException(ex);
            }
        }
예제 #2
0
        public async Task <PaginatedList <GetConversationDTO> > GetAllAsync(ConversationsFilterParams arg)
        {
            try
            {
                var query = db.Conversations.Where(w => w.TicketId.ToString() == arg.TicketId);


                //to restrict the query access to just admins or those
                //who are some how assigned to this conversation or are the owner of the ticket
                if (!appUser.Roles.Any(a => a == Roles.Admin || a == Roles.Observer))
                {
                    if (appUser.Roles.Any(a => a == Roles.Employee))
                    {
                        query = query.Where(w =>
                                            w.CreatedById == appUser.Id ||
                                            appUser.Groups.Any(a => a == w.Ticket.NomineeGroupId) ||
                                            w.Ticket.AssigneeId == appUser.Id.ToString() ||
                                            w.Ticket.OpenedById == appUser.Id.ToString() ||
                                            w.Ticket.ClosedById == appUser.Id.ToString());
                    }

                    else if (appUser.Roles.Any(a => a == Roles.User))
                    {
                        query = query.Where(w => w.Ticket.OpenedById == appUser.Id.ToString());
                    }
                }

                //if user is not an employee of the company only can see the public posts
                if (!appUser.IsCompanyMember)
                {
                    query = query.Where(w => w.IsPublic);
                }

                if (arg.Title != null)
                {
                    query = query.Where(data => data.Message.StartsWith(arg.Title) ||
                                        data.Message.Contains(arg.Title));
                }

                if (arg.FromDate != null)
                {
                    query = query.Where(w => w.Created >= arg.FromDate);
                }

                if (arg.ToDate != null)
                {
                    query = query.Where(w => w.Created <= arg.ToDate);
                }

                //sort by date of the ticket so newer ones come first.
                query = query.OrderByDescending(o => o.Created);

                var list = await new PaginatedListBuilder <Conversation, GetConversationDTO>(mapper)
                           .CreateAsync(query, arg.PageNumber, arg.PageSize);

                foreach (var item in list.Items)
                {
                    var creatorUser = await db.Users.FirstOrDefaultAsync(u => u.Id == item.CreatedById.ToString());

                    item.FullName = creatorUser.FullName;
                    if (creatorUser.ProfileImage != null && creatorUser.ProfileImage.ImageThumbnail != null)
                    {
                        item.ProfileImage = Convert.ToBase64String(creatorUser.ProfileImage.ImageThumbnail);
                    }
                    else if (creatorUser.ProfileImage != null && creatorUser.ProfileImage.Image != null)
                    {
                        item.ProfileImage = Convert.ToBase64String(creatorUser.ProfileImage.Image);
                    }
                }

                list.Items.ForEach(async f => f.FullName =
                                       (await db.Users.FirstOrDefaultAsync(u => u.Id == f.CreatedById.ToString())).FullName
                                   );

                return(list);
            }
            catch (Exception ex)
            {
                throw;
            }
        }