public async Task <IActionResult> GetTicketComment([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var ticketComment = await IncludeAllComments().FirstOrDefaultAsync(x => x.Id == id); if (ticketComment == null) { return(NotFound()); } var role = StaticHelper.GetCurrentRole(User); if (!StaticHelper.CheckTicketByRole(role, ticketComment.Ticket, User.Identity.Name, _context)) { return(BadRequest()); } var serializerSettings = new JsonSerializerSettings(); if (role == "client" || role == "superclient") { var resolver = new PropertyRenameAndIgnoreSerializerContractResolver(); resolver.IgnoreProperty(typeof(User), "Email", "Phone"); resolver.IgnoreProperty(typeof(Client), "Email", "Phone"); serializerSettings.ContractResolver = resolver; } return(Ok(JsonConvert.SerializeObject(ticketComment, serializerSettings))); }
public IActionResult GetTicketAttachments([FromQuery] int?ticketId) { if (ticketId == null) { return(BadRequest("Ticket ID is required!")); } var ticket = _context .Tickets .Include(x => x.Client) .ThenInclude(x => x.Company) .FirstOrDefault(x => x.Id == ticketId); if (ticket == null) { return(NotFound("Ticket with such id doesn't exists!")); } var role = StaticHelper.GetCurrentRole(User); if (!StaticHelper.CheckTicketByRole(role, ticket, User.Identity.Name, _context)) { return(BadRequest()); } var attachments = IncludeAllAttachments().Where(x => x.TicketId == ticketId); return(Ok(attachments)); }
public async Task <IActionResult> DeleteTicketAttachment([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var ticketAttachment = await IncludeAllAttachments().FirstOrDefaultAsync(x => x.Id == id); if (ticketAttachment == null) { return(NotFound()); } var role = StaticHelper.GetCurrentRole(User); if (!StaticHelper.CheckTicketByRole(role, ticketAttachment.Ticket, User.Identity.Name, _context)) { return(BadRequest()); } _context.TicketAttachments.Remove(ticketAttachment); await _context.SaveChangesAsync(); return(Ok(ticketAttachment)); }
public async Task <IActionResult> PostTicketComment([FromBody] TicketComment ticketComment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var role = StaticHelper.GetCurrentRole(User); var ticket = await _context.Tickets.FirstOrDefaultAsync(x => x.Id == ticketComment.TicketId); if (ticket == null) { return(BadRequest()); } if (!StaticHelper.CheckTicketByRole(role, ticket, User.Identity.Name, _context)) { return(BadRequest()); } if (role == "client" || role == "superclient") { var client = await _context.Clients.FirstOrDefaultAsync(x => x.Email == User.Identity.Name); if (client == null) { return(BadRequest()); } ticketComment.ClientId = client.Id; ticketComment.UserId = null; } else { var user = await _context.Users.FirstOrDefaultAsync(x => x.Email == User.Identity.Name); if (user == null) { return(BadRequest()); } ticketComment.UserId = user.Id; ticketComment.ClientId = null; } _context.TicketComments.Add(ticketComment); await _context.SaveChangesAsync(); await StaticHelper.RaiseEvent(EventTypes.TicketComment, ticket, _context); return(CreatedAtAction("GetTicketComment", new { id = ticketComment.Id }, ticketComment)); }
public async Task <IActionResult> GetTicket([FromRoute] int id) { var role = StaticHelper.GetCurrentRole(User); var ticket = IncludeAllTicket().FirstOrDefault(x => x.Id == id); if (ticket == null) { return(NotFound("No ticket was found!")); } if (!StaticHelper.CheckTicketByRole(role, ticket, User.Identity.Name, db)) { return(Forbid()); } await StaticHelper.RaiseEvent(EventTypes.TicketAdded, ticket, db); return(Ok(ticket)); }
public async Task <IActionResult> PostTicketAttachment([FromBody] TicketAttachment ticketAttachment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var ticket = _context.Tickets .Include(x => x.Client) .ThenInclude(x => x.Company) .FirstOrDefault(x => x.Id == ticketAttachment.TicketId); if (ticket == null) { return(NotFound("Ticket with such id doesn't exists!")); } var role = StaticHelper.GetCurrentRole(User); if (!StaticHelper.CheckTicketByRole(role, ticket, User.Identity.Name, _context)) { return(BadRequest()); } try { ticketAttachment.Type = ticketAttachment.Path.Substring(ticketAttachment.Path.LastIndexOf('.') + 1); if (String.IsNullOrWhiteSpace(ticketAttachment.Type)) { return(BadRequest("Unable to define file type.")); } } catch { return(BadRequest("Unable to define file type.")); } if (ticketAttachment.Name == null) { ticketAttachment.Name = "attachment"; } _context.TicketAttachments.Add(ticketAttachment); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTicketAttachment", new { id = ticketAttachment.Id }, ticketAttachment)); }
public async Task <IActionResult> PostTicket([FromBody] Ticket ticket) { var role = StaticHelper.GetCurrentRole(User); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!StaticHelper.CheckTicketByRole(role, ticket, User.Identity.Name, db)) { return(Forbid()); } var slaId = db.Companies.FirstOrDefault(x => x.Id == db.Clients. FirstOrDefault(t => t.Id == ticket.ClientId).CompanyId).SLAId; if (!CheckTicketBySLA(ticket, slaId)) { return(BadRequest("Some values are not allowed by SLA, associated with company of client")); } ticket.DateModified = DateTime.Now; ticket.DateCreated = DateTime.Now; ticket.StatusId = 5; try { db.Tickets.Add(ticket); await db.SaveChangesAsync(); } catch (DbUpdateException e) { return(BadRequest("Some of the fields are incorrect!")); } catch (Exception e) { throw e; } await StaticHelper.RaiseEvent(EventTypes.TicketAdded, ticket, db); return(CreatedAtAction("GetTicket", new { id = ticket.Id }, ticket)); }
public IActionResult GetTicketComments([FromQuery] int?ticketId) { if (ticketId == null) { return(BadRequest("Ticket ID is required!")); } var ticket = _context .Tickets .Include(x => x.Client) .ThenInclude(x => x.Company) .FirstOrDefault(x => x.Id == ticketId); if (ticket == null) { return(NotFound("Ticket with such id doesn't exists!")); } var role = StaticHelper.GetCurrentRole(User); if (!StaticHelper.CheckTicketByRole(role, ticket, User.Identity.Name, _context)) { return(BadRequest()); } var serializerSettings = new JsonSerializerSettings(); if (role == "client" || role == "superclient") { var resolver = new PropertyRenameAndIgnoreSerializerContractResolver(); resolver.IgnoreProperty(typeof(User), "Email", "Phone"); resolver.IgnoreProperty(typeof(Client), "Email", "Phone"); serializerSettings.ContractResolver = resolver; } var ticketComments = IncludeAllComments().Where(x => x.TicketId == ticketId); return(Ok(JsonConvert.SerializeObject(ticketComments, serializerSettings))); }
public async Task <IActionResult> PutTicket([FromRoute] int id, [FromBody] PutTicket ticket) { var role = StaticHelper.GetCurrentRole(User); var oldTicket = IncludeAllTicket().FirstOrDefault(x => x.Id == id); if (oldTicket == null) { return(BadRequest()); } var email = User.Identity.Name; AppUser user; if (role == "client" || role == "superclient") { if (oldTicket.StatusId != 5) { return(BadRequest("Ticket is in work, can't change it!")); } user = db.Clients.FirstOrDefault(x => x.Email == email); } else { user = db.Users.FirstOrDefault(x => x.Email == email); } if ((role == "admin") || ((role == "client" && oldTicket.ClientId == user.Id) || (role == "superclient" && oldTicket.Client.CompanyId == (user as Client).CompanyId))) { oldTicket.Name = ticket.Name ?? oldTicket.Name; oldTicket.Content = ticket.Content ?? oldTicket.Content; oldTicket.TypeId = ticket.TypeId ?? oldTicket.TypeId; oldTicket.PriorityId = ticket.PriorityId ?? oldTicket.PriorityId; oldTicket.DepartamentId = ticket.DepartamentId ?? oldTicket.DepartamentId; oldTicket.UserId = ticket.UserId ?? oldTicket.UserId; oldTicket.ObjectiveId = ticket.ObjectiveId ?? oldTicket.ObjectiveId; } var statusId = ticket.StatusId; if (statusId != null) { if (role != "admin" && statusId == 2) { statusId = null; } if ((role == "client" || role == "superclient") && (statusId != 5)) { statusId = null; } } oldTicket.StatusId = statusId ?? oldTicket.StatusId; oldTicket.DateModified = DateTime.Now; if (!StaticHelper.CheckTicketByRole(role, oldTicket, User.Identity.Name, db)) { return(BadRequest()); } var slaId = db.Companies.FirstOrDefault(x => x.Id == db.Clients. FirstOrDefault(t => t.Id == oldTicket.ClientId).CompanyId).SLAId; if (!CheckTicketBySLA(oldTicket, slaId)) { return(BadRequest("Some values are not allowed by SLA, associated with company of client")); } db.Entry(oldTicket).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch { return(BadRequest("Invalid data!")); } await StaticHelper.RaiseEvent(EventTypes.TicketChanged, oldTicket, db); return(NoContent()); }