Implemented the image capture while writing comment in task allocation and add presignedurl in task get by in API
This commit is contained in:
parent
f275d08215
commit
1a51860517
@ -1,11 +1,12 @@
|
||||
namespace Marco.Pms.Model.Dtos.Activities
|
||||
using Marco.Pms.Model.Utilities;
|
||||
|
||||
namespace Marco.Pms.Model.Dtos.Activities
|
||||
{
|
||||
public class CreateCommentDto
|
||||
{
|
||||
public Guid TaskAllocationId { get; set; }
|
||||
|
||||
public DateTime CommentDate { get; set; }
|
||||
public string? Comment { get; set; }
|
||||
|
||||
public List<FileUploadModel>? Images { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -8,5 +8,6 @@
|
||||
public string? Comment { get; set; }
|
||||
public Guid CommentedBy { get; set; }
|
||||
public BasicEmployeeVM? Employee { get; set; }
|
||||
public List<string>? PreSignedUrls { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ namespace Marco.Pms.Model.ViewModels.Activities
|
||||
public string? Description { get; set; }
|
||||
public string? AssignBy { get; set; }
|
||||
public WorkItem? WorkItem { get; set; }
|
||||
List<string>? preSignedUrls { get; set; }
|
||||
public List<string>? PreSignedUrls { get; set; }
|
||||
public List<CommentVM>? Comments { get; set; }
|
||||
public List<EmployeeVM>? TeamMembers { get; set; }
|
||||
}
|
||||
|
@ -214,10 +214,62 @@ namespace MarcoBMS.Services.Controllers
|
||||
var tenantId = GetTenantId();
|
||||
var Employee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
|
||||
var taskAllocation = await _context.TaskAllocations.Include(t => t.WorkItem).FirstOrDefaultAsync(t => t.Id == createComment.TaskAllocationId);
|
||||
if (taskAllocation == null || taskAllocation.WorkItem == null)
|
||||
{
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("No such task has been allocated.", "No such task has been allocated.", 400));
|
||||
}
|
||||
WorkArea workArea = await _context.WorkAreas.Include(a => a.Floor).FirstOrDefaultAsync(a => a.Id == taskAllocation.WorkItem.WorkAreaId) ?? new WorkArea();
|
||||
|
||||
var bulding = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == (workArea.Floor != null ? workArea.Floor.BuildingId : Guid.Empty));
|
||||
|
||||
var comment = createComment.ToCommentFromCommentDto(tenantId, Employee.Id);
|
||||
_context.TaskComments.Add(comment);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var Images = createComment.Images;
|
||||
|
||||
if (Images != null && Images.Count > 0)
|
||||
{
|
||||
|
||||
foreach (var Image in Images)
|
||||
{
|
||||
|
||||
if (string.IsNullOrEmpty(Image.Base64Data))
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Base64 data is missing", "Base64 data is missing", 400));
|
||||
|
||||
//If base64 has a data URI prefix, strip it
|
||||
var base64 = Image.Base64Data.Contains(",")
|
||||
? Image.Base64Data.Substring(Image.Base64Data.IndexOf(",") + 1)
|
||||
: Image.Base64Data;
|
||||
|
||||
string fileType = _s3Service.GetContentTypeFromBase64(base64);
|
||||
string fileName = _s3Service.GenerateFileName(fileType, tenantId, "task_report");
|
||||
|
||||
string objectKey = $"tenant-{tenantId}/project-{bulding?.ProjectId}/Actitvity/{fileName}";
|
||||
await _s3Service.UploadFileAsync(base64, fileType, objectKey);
|
||||
|
||||
Document document = new Document
|
||||
{
|
||||
FileName = Image.FileName ?? "",
|
||||
ContentType = Image.ContentType ?? "",
|
||||
S3Key = objectKey,
|
||||
Base64Data = Image.Base64Data,
|
||||
FileSize = Image.FileSize,
|
||||
UploadedAt = DateTime.UtcNow,
|
||||
TenantId = tenantId
|
||||
};
|
||||
_context.Documents.Add(document);
|
||||
TaskAttachment attachment = new TaskAttachment
|
||||
{
|
||||
DocumentId = document.Id,
|
||||
ReferenceId = comment.Id
|
||||
};
|
||||
_context.TaskAttachments.Add(attachment);
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
CommentVM response = comment.ToCommentVMFromTaskComment();
|
||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Comment saved successfully", 200));
|
||||
}
|
||||
@ -330,8 +382,23 @@ namespace MarcoBMS.Services.Controllers
|
||||
if (taskAllocation == null) return NotFound(ApiResponse<object>.ErrorResponse("Task Not Found", "Task not found", 404));
|
||||
var taskVM = taskAllocation.TaskAllocationToTaskVM(employeeName);
|
||||
var comments = await _context.TaskComments.Where(c => c.TaskAllocationId == taskAllocation.Id).ToListAsync();
|
||||
var commentIds = comments.Select(c => c.Id).ToList();
|
||||
var taskAttachments = await _context.TaskAttachments.Where(t => t.ReferenceId == taskAllocation.Id || commentIds.Contains(t.ReferenceId)).ToListAsync();
|
||||
var documentIds = taskAttachments.Select(t => t.DocumentId).ToList();
|
||||
var documents = await _context.Documents.Where(d => documentIds.Contains(d.Id)).ToListAsync();
|
||||
var team = await _context.TaskMembers.Where(m => m.TaskAllocationId == taskAllocation.Id).Include(m => m.Employee).ToListAsync();
|
||||
var teamMembers = new List<EmployeeVM> { };
|
||||
|
||||
var taskDocumentIds = taskAttachments.Where(t => t.ReferenceId == taskAllocation.Id).Select(t => t.DocumentId).ToList();
|
||||
var taskDocuments = documents.Where(d => taskDocumentIds.Contains(d.Id)).ToList();
|
||||
|
||||
List<string> taskPreSignedUrls = new List<string>();
|
||||
foreach (var document in taskDocuments)
|
||||
{
|
||||
string preSignedUrl = _s3Service.GeneratePreSignedUrlAsync(document.S3Key);
|
||||
taskPreSignedUrls.Add(preSignedUrl);
|
||||
}
|
||||
taskVM.PreSignedUrls = taskPreSignedUrls;
|
||||
foreach (var member in team)
|
||||
{
|
||||
var result = member.Employee != null ? member.Employee.ToEmployeeVMFromEmployee() : new EmployeeVM();
|
||||
@ -340,7 +407,17 @@ namespace MarcoBMS.Services.Controllers
|
||||
List<CommentVM> Comments = new List<CommentVM> { };
|
||||
foreach (var comment in comments)
|
||||
{
|
||||
Comments.Add(comment.ToCommentVMFromTaskComment());
|
||||
var commentDocumentIds = taskAttachments.Where(t => t.ReferenceId == comment.Id).Select(t => t.DocumentId).ToList();
|
||||
var commentDocuments = documents.Where(d => commentDocumentIds.Contains(d.Id)).ToList();
|
||||
List<string> commentPreSignedUrls = new List<string>();
|
||||
foreach (var document in commentDocuments)
|
||||
{
|
||||
string preSignedUrl = _s3Service.GeneratePreSignedUrlAsync(document.S3Key);
|
||||
commentPreSignedUrls.Add(preSignedUrl);
|
||||
}
|
||||
CommentVM commentVM = comment.ToCommentVMFromTaskComment();
|
||||
commentVM.PreSignedUrls = commentPreSignedUrls;
|
||||
Comments.Add(commentVM);
|
||||
}
|
||||
taskVM.Comments = Comments;
|
||||
taskVM.TeamMembers = teamMembers;
|
||||
|
Loading…
x
Reference in New Issue
Block a user