Removed tenantid filed from Dtos
This commit is contained in:
parent
c338140210
commit
c1a55fa17b
@ -9,6 +9,5 @@
|
|||||||
public DateTime SentAt { get; set; }
|
public DateTime SentAt { get; set; }
|
||||||
public Guid? ParentMessageId { get; set; } // For threaded replies
|
public Guid? ParentMessageId { get; set; } // For threaded replies
|
||||||
public ICollection<ForumAttachmentDto>? Attachments { get; set; }
|
public ICollection<ForumAttachmentDto>? Attachments { get; set; }
|
||||||
public int TenantId { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,5 @@
|
|||||||
public ICollection<ForumAttachmentDto>? Attachments { get; set; }
|
public ICollection<ForumAttachmentDto>? Attachments { get; set; }
|
||||||
public Guid PriorityId { get; set; }
|
public Guid PriorityId { get; set; }
|
||||||
public ICollection<Guid>? TagIds { get; set; }
|
public ICollection<Guid>? TagIds { get; set; }
|
||||||
public int TenantId { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,122 +47,122 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
||||||
}
|
}
|
||||||
int tenantId = _userHelper.GetTenantId();
|
int tenantId = _userHelper.GetTenantId();
|
||||||
if (tenantId == createTicketDto.TenantId)
|
//if (tenantId == createTicketDto.TenantId)
|
||||||
|
//{
|
||||||
|
TicketForum ticketForum = createTicketDto.ToTicketForumFromCreateTicketDto(tenantId);
|
||||||
|
_context.Tickets.Add(ticketForum);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
List<TicketAttachment> attachments = new List<TicketAttachment>();
|
||||||
|
List<TicketTag> ticketTags = new List<TicketTag>();
|
||||||
|
List<Document> documents = new List<Document>();
|
||||||
|
List<TicketTagVM> tagVMs = new List<TicketTagVM>();
|
||||||
|
|
||||||
|
if (createTicketDto.Attachments != null)
|
||||||
{
|
{
|
||||||
TicketForum ticketForum = createTicketDto.ToTicketForumFromCreateTicketDto();
|
foreach (var attachmentDto in createTicketDto.Attachments)
|
||||||
_context.Tickets.Add(ticketForum);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
List<TicketAttachment> attachments = new List<TicketAttachment>();
|
|
||||||
List<TicketTag> ticketTags = new List<TicketTag>();
|
|
||||||
List<Document> documents = new List<Document>();
|
|
||||||
List<TicketTagVM> tagVMs = new List<TicketTagVM>();
|
|
||||||
|
|
||||||
if (createTicketDto.Attachments != null)
|
|
||||||
{
|
{
|
||||||
foreach (var attachmentDto in createTicketDto.Attachments)
|
byte[] fileBytes;
|
||||||
|
var Image = attachmentDto;
|
||||||
|
if (string.IsNullOrEmpty(Image.Base64Data))
|
||||||
{
|
{
|
||||||
byte[] fileBytes;
|
_logger.LogError("Base64 data is missing");
|
||||||
var Image = attachmentDto;
|
return BadRequest(ApiResponse<object>.ErrorResponse("Base64 data is missing", "Base64 data is missing", 400));
|
||||||
if (string.IsNullOrEmpty(Image.Base64Data))
|
|
||||||
{
|
|
||||||
_logger.LogError("Base64 data is missing");
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Base64 data is missing", "Base64 data is missing", 400));
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//If base64 has a data URI prefix, strip it
|
|
||||||
var base64 = Image.Base64Data.Contains(",")
|
|
||||||
? Image.Base64Data.Substring(Image.Base64Data.IndexOf(",") + 1)
|
|
||||||
: Image.Base64Data;
|
|
||||||
|
|
||||||
fileBytes = Convert.FromBase64String(base64);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError("{error}", ex.Message);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400)); ;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var stream = new MemoryStream(fileBytes);
|
|
||||||
|
|
||||||
|
|
||||||
string fileName = _s3Service.GenerateFileName(Image.ContentType, tenantId, string.Empty);
|
|
||||||
|
|
||||||
var objectKey = await _s3Service.UploadFileAsync(stream, fileName, Image.ContentType);
|
|
||||||
|
|
||||||
Document document = attachmentDto.ToDocumentFromForumAttachmentDto(objectKey, objectKey, createTicketDto.CreatedAt, tenantId);
|
|
||||||
_context.Documents.Add(document);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
documents.Add(document);
|
|
||||||
|
|
||||||
var attachment = attachmentDto.ToTicketAttachmentFromForumAttachmentDto(ticketForum.Id, document.Id);
|
|
||||||
attachments.Add(attachment);
|
|
||||||
}
|
}
|
||||||
_context.TicketAttachments.AddRange(attachments);
|
try
|
||||||
|
{
|
||||||
|
//If base64 has a data URI prefix, strip it
|
||||||
|
var base64 = Image.Base64Data.Contains(",")
|
||||||
|
? Image.Base64Data.Substring(Image.Base64Data.IndexOf(",") + 1)
|
||||||
|
: Image.Base64Data;
|
||||||
|
|
||||||
|
fileBytes = Convert.FromBase64String(base64);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("{error}", ex.Message);
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400)); ;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var stream = new MemoryStream(fileBytes);
|
||||||
|
|
||||||
|
|
||||||
|
string fileName = _s3Service.GenerateFileName(Image.ContentType, tenantId, string.Empty);
|
||||||
|
|
||||||
|
var objectKey = await _s3Service.UploadFileAsync(stream, fileName, Image.ContentType);
|
||||||
|
|
||||||
|
Document document = attachmentDto.ToDocumentFromForumAttachmentDto(objectKey, objectKey, createTicketDto.CreatedAt, tenantId);
|
||||||
|
_context.Documents.Add(document);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
documents.Add(document);
|
||||||
|
|
||||||
|
var attachment = attachmentDto.ToTicketAttachmentFromForumAttachmentDto(ticketForum.Id, document.Id);
|
||||||
|
attachments.Add(attachment);
|
||||||
}
|
}
|
||||||
if (createTicketDto.TagIds != null)
|
_context.TicketAttachments.AddRange(attachments);
|
||||||
{
|
|
||||||
List<TicketTagMaster>? tagMasters = await _context.TicketTagMasters.Where(t => createTicketDto.TagIds.Contains(t.Id)).ToListAsync();
|
|
||||||
foreach (var ticketTag in tagMasters)
|
|
||||||
{
|
|
||||||
TicketTagVM tagVM = ticketTag.ToTicketTagVMFromTicketTagMaster();
|
|
||||||
|
|
||||||
TicketTag tag = new TicketTag
|
|
||||||
{
|
|
||||||
TicketId = ticketForum.Id,
|
|
||||||
TagId = ticketTag.Id
|
|
||||||
};
|
|
||||||
tagVMs.Add(tagVM);
|
|
||||||
ticketTags.Add(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
_context.TicketTags.AddRange(ticketTags);
|
|
||||||
}
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var ticket = await _context.Tickets.Include(t => t.TicketTypeMaster).Include(t => t.TicketStatusMaster).Include(t => t.Priority).FirstOrDefaultAsync(t => t.Id == ticketForum.Id);
|
|
||||||
|
|
||||||
Employee? employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == ticketForum.CreatedById);
|
|
||||||
|
|
||||||
|
|
||||||
ForumTicketVM ticketVM = ticket.ToForumTicketVMFromTicketForum(employee);
|
|
||||||
|
|
||||||
ticketVM.Tags = tagVMs;
|
|
||||||
|
|
||||||
List<TicketAttachmentVM> attachmentVMs = new List<TicketAttachmentVM>();
|
|
||||||
foreach (var attachment in attachments)
|
|
||||||
{
|
|
||||||
var document = documents.Find(d => d.Id == attachment.FileId);
|
|
||||||
string preSignedUrl = string.Empty;
|
|
||||||
if (document != null)
|
|
||||||
{
|
|
||||||
preSignedUrl = await _s3Service.GeneratePreSignedUrlAsync(document.S3Key);
|
|
||||||
}
|
|
||||||
attachmentVMs.Add(attachment.ToTicketAttachmentVMFromTicketAttachment(preSignedUrl, preSignedUrl));
|
|
||||||
}
|
|
||||||
ticketVM.Attachments = attachmentVMs;
|
|
||||||
Project project = await _context.Projects.FirstOrDefaultAsync(p => p.Id == ticketForum.LinkedProjectId) ?? new Project();
|
|
||||||
ticketVM.ProjectName = project.Name;
|
|
||||||
if (createTicketDto.LinkedActivityId != null && createTicketDto.LinkedActivityId != 0)
|
|
||||||
{
|
|
||||||
WorkItem workItem = await _context.WorkItems.Include(w => w.ActivityMaster).FirstOrDefaultAsync(w => w.Id == ticketForum.LinkedActivityId) ?? new WorkItem();
|
|
||||||
if (workItem.ActivityMaster != null)
|
|
||||||
{
|
|
||||||
ticketVM.ActivityName = workItem.ActivityMaster.ActivityName;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ticketVM.ActivityName = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_logger.LogInfo("Ticket created by Employee {EmployeeId} for project {ProjectId}", ticketForum.CreatedById, project.Id);
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(ticketVM, "Ticket Created Successfully", 200));
|
|
||||||
}
|
}
|
||||||
_logger.LogWarning("Employee {EmployeeId} tries to create ticket in different Tenant", createTicketDto.CreatedById);
|
if (createTicketDto.TagIds != null)
|
||||||
return Unauthorized(ApiResponse<object>.ErrorResponse("Not Authorized", "Not Authorized", 401));
|
{
|
||||||
|
List<TicketTagMaster>? tagMasters = await _context.TicketTagMasters.Where(t => createTicketDto.TagIds.Contains(t.Id)).ToListAsync();
|
||||||
|
foreach (var ticketTag in tagMasters)
|
||||||
|
{
|
||||||
|
TicketTagVM tagVM = ticketTag.ToTicketTagVMFromTicketTagMaster();
|
||||||
|
|
||||||
|
TicketTag tag = new TicketTag
|
||||||
|
{
|
||||||
|
TicketId = ticketForum.Id,
|
||||||
|
TagId = ticketTag.Id
|
||||||
|
};
|
||||||
|
tagVMs.Add(tagVM);
|
||||||
|
ticketTags.Add(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.TicketTags.AddRange(ticketTags);
|
||||||
|
}
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var ticket = await _context.Tickets.Include(t => t.TicketTypeMaster).Include(t => t.TicketStatusMaster).Include(t => t.Priority).FirstOrDefaultAsync(t => t.Id == ticketForum.Id);
|
||||||
|
|
||||||
|
Employee? employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == ticketForum.CreatedById);
|
||||||
|
|
||||||
|
|
||||||
|
ForumTicketVM ticketVM = ticket.ToForumTicketVMFromTicketForum(employee);
|
||||||
|
|
||||||
|
ticketVM.Tags = tagVMs;
|
||||||
|
|
||||||
|
List<TicketAttachmentVM> attachmentVMs = new List<TicketAttachmentVM>();
|
||||||
|
foreach (var attachment in attachments)
|
||||||
|
{
|
||||||
|
var document = documents.Find(d => d.Id == attachment.FileId);
|
||||||
|
string preSignedUrl = string.Empty;
|
||||||
|
if (document != null)
|
||||||
|
{
|
||||||
|
preSignedUrl = await _s3Service.GeneratePreSignedUrlAsync(document.S3Key);
|
||||||
|
}
|
||||||
|
attachmentVMs.Add(attachment.ToTicketAttachmentVMFromTicketAttachment(preSignedUrl, preSignedUrl));
|
||||||
|
}
|
||||||
|
ticketVM.Attachments = attachmentVMs;
|
||||||
|
Project project = await _context.Projects.FirstOrDefaultAsync(p => p.Id == ticketForum.LinkedProjectId) ?? new Project();
|
||||||
|
ticketVM.ProjectName = project.Name;
|
||||||
|
if (createTicketDto.LinkedActivityId != null && createTicketDto.LinkedActivityId != 0)
|
||||||
|
{
|
||||||
|
WorkItem workItem = await _context.WorkItems.Include(w => w.ActivityMaster).FirstOrDefaultAsync(w => w.Id == ticketForum.LinkedActivityId) ?? new WorkItem();
|
||||||
|
if (workItem.ActivityMaster != null)
|
||||||
|
{
|
||||||
|
ticketVM.ActivityName = workItem.ActivityMaster.ActivityName;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ticketVM.ActivityName = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_logger.LogInfo("Ticket created by Employee {EmployeeId} for project {ProjectId}", ticketForum.CreatedById, project.Id);
|
||||||
|
return Ok(ApiResponse<object>.SuccessResponse(ticketVM, "Ticket Created Successfully", 200));
|
||||||
|
//}
|
||||||
|
//_logger.LogWarning("Employee {EmployeeId} tries to create ticket in different Tenant", createTicketDto.CreatedById);
|
||||||
|
// return Unauthorized(ApiResponse<object>.ErrorResponse("Not Authorized", "Not Authorized", 401));
|
||||||
}
|
}
|
||||||
[HttpPost("ticket/edit")]
|
[HttpPost("ticket/edit")]
|
||||||
public async Task<IActionResult> UpdateNewTicket([FromBody] UpdateTicketDto updateTicketDto)
|
public async Task<IActionResult> UpdateNewTicket([FromBody] UpdateTicketDto updateTicketDto)
|
||||||
@ -368,7 +368,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
List<TicketAttachment> attachments = new List<TicketAttachment>();
|
List<TicketAttachment> attachments = new List<TicketAttachment>();
|
||||||
List<Document> documents = new List<Document>();
|
List<Document> documents = new List<Document>();
|
||||||
|
|
||||||
TicketComment comment = addCommentDto.ToTicketCommentFromAddCommentDto();
|
TicketComment comment = addCommentDto.ToTicketCommentFromAddCommentDto(tenantId);
|
||||||
_context.TicketComments.Add(comment);
|
_context.TicketComments.Add(comment);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user