public async Task<IEnumerable<ProjectDocument>> GetAsync(string projectId, string stateId)
        {
            try
            {
                var project = await _dbContext.Projects.FirstOrDefaultAsync(p => p.Id == projectId);
                if (project == null)
                {
                    throw new InvalidOperationException("Project not found");
                }

                var users = await _dbContext.Users.ToListAsync();
                var documents = await _dbContext.ProjectDocuments.Where(p => p.ProjectId == projectId).ToListAsync();
                var result = new List<ProjectDocument>();
                foreach(var link in _dbContext.DocumentToProjectLinks.Where(link => !link.IsDeleted && link.StateId == stateId && link.CompanyId == project.CompanyId && link.DocType == StateDocumentType.ExternalDocument))
                {
                    var doc = documents.FirstOrDefault(d => d.Link2StateId == link.Id);
                    var item = new ProjectDocument
                    {
                        Body = doc == null ? null : doc.Body,
                        CreatedBy = doc == null ? null : users.FirstOrDefault(u => u.Id == doc.CreatedById),
                        CreatedOn = doc == null ? DateTime.MinValue : doc.CreatedOn,
                        Id = doc == null ? 0 : doc.Id,
                        Link2StateId = link.Id,
                        Link2State = new DocumentToProjectLink
                        {
                            ActionType = link.ActionType,
                            CompanyId = link.CompanyId,
                            DocName = link.DocName,
                            DocType = link.DocType,
                            Id = link.Id,
                            DocumentTemplateId = link.DocumentTemplateId,
                            StateId = link.StateId
                        },
                        ProjectId = doc == null ? "" : doc.ProjectId,
                        Comment = doc == null ? "" : doc.Comment,
                        StateId = stateId
                    };
                    result.Add(item);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 public async Task<ActionResult> ProcessDocumentsAsync(ProjectWorkflow prjWorkflow, DocumentToProjectLink link2doc, string comment, AppUser currentUser)
 {
     try
     {
         var dbDoc = await _dbContext.ProjectDocuments.FirstOrDefaultAsync(pd => pd.ProjectId == prjWorkflow.Id && pd.Link2StateId == link2doc.Id);
         if (dbDoc == null)
         {
             dbDoc = new ProjectDocument
             {
                 Body = System.Text.Encoding.UTF8.GetBytes(link2doc.DocName),
                 CreatedBy = currentUser,
                 CreatedOn = DateTime.UtcNow,
                 Link2StateId = link2doc.Id,
                 ProjectId = prjWorkflow.Id,
                 StateId = link2doc.StateId,
                 Comment = comment
             };
             _dbContext.ProjectDocuments.Add(dbDoc);
         }
         else
         {
             dbDoc.Body = System.Text.Encoding.UTF8.GetBytes(link2doc.DocName);
             dbDoc.CreatedBy = currentUser;
             dbDoc.CreatedOn = DateTime.UtcNow;
             dbDoc.Link2StateId = link2doc.Id;
             dbDoc.ProjectId = prjWorkflow.Id;
             dbDoc.StateId = link2doc.StateId;
             dbDoc.Comment = comment;
         }
         await _dbContext.SaveChangesAsync();
         return ActionResult.Success(dbDoc);
     }
     catch(Exception ex)
     {
         return ActionResult.Failed(ex);
     }
 }
        public async Task<ActionResult> SaveAsync(ProjectDocument document)
        {
            try
            {
                _dbContext.ProjectDocuments.Add(document);
                await _dbContext.SaveChangesAsync();

                return ActionResult<ProjectDocument>.Success(document);
            }
            catch (Exception ex)
            {
                return ActionResult.Failed(ex);
            }
        }