Esempio n. 1
0
        public IHttpActionResult PostDiscussion()
        {
            if (!Common.GetAuthorization(Request))
            {
                return(Ok(new { Code = 401, Message = "Unauthorization" }));
            }
            if (ModelState.IsValid)
            {
                var user = _userBusinessService.GetByToken(Request.Headers.GetValues("AccessToken").First());

                if (user != null)
                {
                    string   msg                = HttpContext.Current.Request.Params.Get("Description");
                    int      typeMsg            = int.Parse(HttpContext.Current.Request.Params.Get("Type"));
                    int      ticketId           = int.Parse(HttpContext.Current.Request.Params.Get("TicketId"));
                    int      ticketDiscussionId = 0;
                    int      ticketNoteId       = 0;
                    DateTime dateTimeNow        = DateTime.Now;
                    Ticket   ticket             = _ticketBusinessService.GetDetail(ticketId);
                    User     userData           = _userBusinessService.GetDetail(int.Parse(HttpContext.Current.Request.Params.Get("UserId")));
                    var      dataUid            = new List <int>();
                    var      playerid           = new List <string>();
                    var      participants       = _ticketParcipantBusinessService.GetByTicket(ticketId);

                    TimeSpan respond = ticket.LastReply == null ? (DateTime.Now.Subtract(ticket.CreatedAt.Value)) : (DateTime.Now.Subtract(ticket.LastReply.Value));
                    if (typeMsg == 1)
                    {
                        TicketDiscussion ticketDiscussion = new TicketDiscussion()
                        {
                            TicketId     = ticketId,
                            CreatedAt    = dateTimeNow,
                            UserId       = int.Parse(HttpContext.Current.Request.Params.Get("UserId")),
                            Description  = msg,
                            TicketNoteId = 0,// -> new comment don't have note
                            Status       = 1,
                            RespondTime  = (respond.Days < 1 ? "" : respond.Days.ToString() + ". ") + respond.Hours.ToString() + ":" + respond.Minutes.ToString() + ":" + respond.Seconds.ToString()
                        };
                        foreach (var i in participants)
                        {
                            dataUid.Add(i.UserId);
                        }
                        var iduserdevice = _ticketParcipantBusinessService.Getuserdeviceforasnote(dataUid);
                        foreach (var p in iduserdevice)
                        {
                            playerid.Add(p.PlayerId);
                        }
                        playerid.Add(_userBusinessService.GetDetail(ticket.Responder).PlayerId);
                        playerid.Add(_userBusinessService.GetDetail(ticket.Submiter).PlayerId);

                        title   = ticket.TicketNo + " - " + ticket.Title + " - Reply";
                        content = userData.Name + (ticket.Submiter == userData.UserId ? " (Submiter)" : ticket.Responder == userData.UserId ? " (Responder)" : " (Participant)") + " : " + msg;
                        Onesignal.PushNotif(content, playerid, title, ticketId, ticket.TicketNo, ticket.TicketCategoryId, ticket.Description);
                        ticketDiscussionId = _ticketDiscussionBusinessService.Add(ticketDiscussion).TicketDiscussionId;
                    }
                    else if (typeMsg == 2)
                    {
                        TicketNote ticketNote = new TicketNote()
                        {
                            TicketId    = ticketId,
                            CreatedAt   = dateTimeNow,
                            UserId      = int.Parse(HttpContext.Current.Request.Params.Get("UserId")),
                            Description = msg,
                            Status      = 1,
                            RespondTime = (respond.Days < 1 ? "" : respond.Days.ToString() + ". ") + respond.Hours.ToString() + ":" + respond.Minutes.ToString() + ":" + respond.Seconds.ToString()
                        };
                        foreach (var i in participants)
                        {
                            dataUid.Add(i.UserId);
                        }
                        var iduserdevice = _ticketParcipantBusinessService.Getuserdeviceforasnote(dataUid);
                        foreach (var p in iduserdevice)
                        {
                            playerid.Add(p.PlayerId);
                        }
                        playerid.Add(_userBusinessService.GetDetail(ticket.Responder).PlayerId);
                        playerid.Add(_userBusinessService.GetDetail(ticket.Submiter).PlayerId);

                        title   = ticket.TicketNo + " - " + ticket.Title + " - Reply As Note";
                        content = userData.Name + (ticket.Submiter == userData.UserId ? " (Submiter)" : " (Responder)") + " : " + msg;
                        Onesignal.PushNotif(content, playerid, title, ticketId, ticket.TicketNo, ticket.TicketCategoryId, ticket.Description);

                        ticketNoteId = _ticketNoteBusinessService.Add(ticketNote).TicketNoteId;
                        _ticketDiscussionBusinessService.SetNote(ticketId, ticketNoteId);
                    }

                    if (HttpContext.Current.Request.Files.Count > 0)
                    {
                        for (int i = 0, iLen = HttpContext.Current.Request.Files.Count; i < iLen; i++)
                        {
                            string dateString = DateTime.Now.ToString("yyyyMMddHmmss");
                            var    postedFile = HttpContext.Current.Request.Files[i];

                            switch (typeMsg)
                            {
                            case 1:    // -> Comment
                                var fileName = Path.GetFileName(postedFile.FileName);
                                var path     = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload/Discussion/"), fileName);
                                postedFile.SaveAs(path);
                                DiscussionAttachment discussionAttachment = new DiscussionAttachment()
                                {
                                    TicketDiscussionId = ticketDiscussionId,
                                    Name   = fileName,
                                    Status = 1
                                };

                                _discussionAttachmentBusinessService.Add(discussionAttachment);
                                break;

                            default:    // -> Note
                                var fileNameNote = Path.GetFileName(postedFile.FileName);
                                var pathNote     = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload/Note/"), fileNameNote);
                                postedFile.SaveAs(pathNote);
                                NoteAttachment noteAttachment = new NoteAttachment()
                                {
                                    TicketNoteId = ticketNoteId,
                                    Name         = fileNameNote,
                                    Status       = 1
                                };

                                _noteAttachmentBusinessService.Add(noteAttachment);
                                break;
                            }
                        }
                    }

                    List <ApiJsonCommentTR> apiJsonCommentTrList = new List <ApiJsonCommentTR>();

                    ApiJsonCommentDateTR apiJsonCommentDateTr = new ApiJsonCommentDateTR()
                    {
                        day  = dateTimeNow.ToString("dddd, dd MMMM yyyy"),
                        time = dateTimeNow.ToString("hh:mm")
                    };

                    ApiJsonCommentSenderTR apiJsonCommentSenderTr = new ApiJsonCommentSenderTR()
                    {
                        name        = _userBusinessService.GetDetail(int.Parse(HttpContext.Current.Request.Params.Get("UserId"))).Name,
                        is_verified = true
                    };

                    if (ticket.Responder == int.Parse(HttpContext.Current.Request.Params.Get("UserId")))
                    {
                        apiJsonCommentSenderTr.type = "Responder";
                        if (typeMsg == 1)
                        {
                            ticket.LastReply = dateTimeNow;
                            Ticket           = _ticketBusinessService.Edit(ticket);
                        }
                        else if (typeMsg == 2)
                        {
                            ticket.NextCommenter  = ticket.Submiter;
                            ticket.LastStatusDate = dateTimeNow;
                            ticket.LastReply      = dateTimeNow;
                            ticket.DueDateAnswer  = dateTimeNow.AddDays(Common.NumberOfWorkDays(dateTimeNow, WebConfigure.GetRulesDay()));
                            Ticket = _ticketBusinessService.Edit(ticket);
                        }
                    }
                    else
                    {
                        if (ticket.Submiter == int.Parse(HttpContext.Current.Request.Params.Get("UserId")))
                        {
                            apiJsonCommentSenderTr.type = "Submitter";
                            if (typeMsg == 1)
                            {
                                ticket.LastReply = dateTimeNow;
                                Ticket           = _ticketBusinessService.Edit(ticket);
                            }
                            else if (typeMsg == 2)
                            {
                                ticket.LastStatusDate = dateTimeNow;
                                ticket.LastReply      = dateTimeNow;
                                ticket.NextCommenter  = ticket.Responder;
                                ticket.DueDateAnswer  = DateTime.Now.AddDays(Common.NumberOfWorkDays(DateTime.Now, WebConfigure.GetRulesDay()));
                                Ticket = _ticketBusinessService.Edit(ticket);
                            }
                        }
                        else
                        {
                            apiJsonCommentSenderTr.type = "Participant";
                            ticket.LastReply            = dateTimeNow;
                            Ticket = _ticketBusinessService.Edit(ticket);
                        }
                    }

                    List <ApiJsonCommentImageTR> listAttachment = new List <ApiJsonCommentImageTR>();

                    var    attachments     = _discussionAttachmentBusinessService.GetByDiscussionId(ticketDiscussionId);
                    string attachmentsPath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/Discussion/");

                    if (typeMsg == 2)
                    {
                        attachments     = _noteAttachmentBusinessService.GetByNoteId(ticketNoteId);
                        attachmentsPath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/Note/");
                    }

                    if (attachments != null)
                    {
                        foreach (var attachment in attachments)
                        {
                            listAttachment.Add(new ApiJsonCommentImageTR {
                                src  = WebConfigure.GetDomain() + "/Upload/TechnicalRequestAttachments/" + attachment.Name,
                                Type = Path.GetExtension(attachment.Name),
                                nama = attachment.Name
                            });
                        }
                    }

                    ApiJsonCommentTR apiJsonCommentTr = new ApiJsonCommentTR()
                    {
                        date   = apiJsonCommentDateTr,
                        sender = apiJsonCommentSenderTr,
                        text   = msg,
                        image  = listAttachment
                    };

                    switch (typeMsg)
                    {
                    case 1:
                        apiJsonCommentTr.type = "sent";
                        break;

                    default:
                        apiJsonCommentTr.type = "notes";
                        break;
                    }

                    apiJsonCommentTrList.Add(apiJsonCommentTr);

                    ApiJsonStatus apiJsonStatus = new ApiJsonStatus()
                    {
                        code    = 200,
                        message = "ok"
                    };
                    if (typeMsg != 1)
                    {
                        if (ticket.TicketCategoryId != 9)
                        {
                            Email.GetEmailTagTsicsCommentTR(ticket);
                            Email.GetEmailTagTsicsCommentTR(ticket, true);
                        }
                        else
                        {
                            Email.GetEmailTagTsicsCommentHelpDesk(ticketId, user.UserId, false);
                            Email.GetEmailTagTsicsCommentHelpDesk(ticketId, user.UserId, true);
                        }
                    }
                    return(Json(new { status = apiJsonStatus, data = apiJsonCommentTrList }));
                }
                else
                {
                    _webStatus.code        = 403;
                    _webStatus.description = "Invalid AccessToken";

                    return(Ok(new { status = _webStatus, result = new object() }));
                }
            }
            return(BadRequest(ModelState));
        }