@@ -105,7 +105,25 @@ public async Task<SummaryDto> GetSummaryAsync(string? projectId = null)
105105 public async Task < ProjectStatsDto > GetProjectStatsAsync ( string projectId )
106106 {
107107 var db = _mongoDbService . GetDatabase ( ) ;
108+ var projectsCollection = db . GetCollection < FlowModels . Project > ( "project" ) ;
109+ var mainTasksCollection = db . GetCollection < FlowModels . MainTask > ( "maintasks" ) ;
108110 var subTasksCollection = db . GetCollection < FlowModels . SubTask > ( "subtasks" ) ;
111+ var categoriesCollection = db . GetCollection < FlowModels . Category > ( "categories" ) ;
112+
113+ // Get the project
114+ var project = await projectsCollection . Find ( p => p . Id == projectId ) . FirstOrDefaultAsync ( ) ;
115+ if ( project == null )
116+ {
117+ throw new Exception ( "Project not found" ) ;
118+ }
119+
120+ // Get team member count
121+ var memberCount = project . TeamMembers ? . Count ?? 0 ;
122+
123+ // Get main tasks count for this project
124+ var mainTaskCount = ( int ) await mainTasksCollection . CountDocumentsAsync ( m => m . ProjectId == projectId ) ;
125+
126+ // Get all subtasks for this project
109127 var tasks = await subTasksCollection . Find ( s => s . ProjectId == projectId ) . ToListAsync ( ) ;
110128 var subTaskCount = tasks . Count ;
111129 var completedTasks = tasks . Count ( t => t . Status ? . ToLower ( ) == "done" || t . Status ? . ToLower ( ) == "completed" ) ;
@@ -119,27 +137,47 @@ public async Task<ProjectStatsDto> GetProjectStatsAsync(string projectId)
119137 ) ;
120138
121139 var tasksByPriority = tasks
122- . GroupBy ( t => string . IsNullOrEmpty ( t . Priority ) ? "Medium " : t . Priority )
140+ . GroupBy ( t => string . IsNullOrEmpty ( t . Priority ) ? "medium " : t . Priority . ToLower ( ) )
123141 . ToDictionary ( g => g . Key , g => g . Count ( ) ) ;
124142
125143 var tasksByStatus = tasks
126- . GroupBy ( t => string . IsNullOrEmpty ( t . Status ) ? "To Do" : t . Status )
144+ . GroupBy ( t => string . IsNullOrEmpty ( t . Status ) ? "to Do" : t . Status )
127145 . ToDictionary ( g => g . Key , g => g . Count ( ) ) ;
128146
129- var completionRate = subTaskCount > 0 ? Math . Round ( ( double ) completedTasks / subTaskCount * 100 , 2 ) : 0 ;
147+ // Get tasks by category
148+ var categories = await categoriesCollection . Find ( c => c . ProjectId == projectId ) . ToListAsync ( ) ;
149+ var tasksByCategory = new List < CategoryStats > ( ) ;
150+
151+ foreach ( var category in categories )
152+ {
153+ var categoryTasks = tasks . Where ( t => t . CategoryId == category . Id ) . ToList ( ) ;
154+ var categoryTaskCount = categoryTasks . Count ;
155+ var completedCategoryTasks = categoryTasks . Count ( t => t . Status ? . ToLower ( ) == "done" || t . Status ? . ToLower ( ) == "completed" ) ;
156+
157+ if ( categoryTaskCount > 0 )
158+ {
159+ tasksByCategory . Add ( new CategoryStats (
160+ CategoryName : category . CategoryName ?? "Uncategorized" ,
161+ TotalTasks : categoryTaskCount ,
162+ CompletedTasks : completedCategoryTasks
163+ ) ) ;
164+ }
165+ }
166+
167+ var completionRate = subTaskCount > 0 ? ( double ) completedTasks / subTaskCount : 0 ;
130168
131169 return new ProjectStatsDto (
132170 ProjectId : projectId ,
133- ProjectName : "Project" ,
134- MemberCount : 0 ,
135- MainTaskCount : 0 ,
171+ ProjectName : project . ProjectName ?? "Project" ,
172+ MemberCount : memberCount ,
173+ MainTaskCount : mainTaskCount ,
136174 SubTaskCount : subTaskCount ,
137175 CompletedSubTasks : completedTasks ,
138176 OverdueSubTasks : overdueTasks ,
139177 TasksByPriority : tasksByPriority ,
140178 TasksByStatus : tasksByStatus ,
141179 CompletionRate : completionRate ,
142- TasksByCategory : new List < CategoryStats > ( )
180+ TasksByCategory : tasksByCategory
143181 ) ;
144182 }
145183
0 commit comments