File size: 12,850 Bytes
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
c7beb09
ecb51fa
 
 
c7beb09
 
 
 
 
 
ecb51fa
c7beb09
 
 
 
 
 
 
 
 
 
ecb51fa
 
 
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
 
 
c7beb09
ecb51fa
 
 
c7beb09
 
 
 
 
 
 
ecb51fa
c7beb09
 
 
 
 
ecb51fa
c7beb09
ecb51fa
 
c7beb09
 
 
 
ecb51fa
 
c7beb09
 
 
 
 
 
ecb51fa
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
 
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4671199
 
 
 
c7beb09
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
c7beb09
 
 
ecb51fa
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
 
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
4671199
 
 
 
c7beb09
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
c7beb09
 
 
 
 
 
 
 
 
 
 
ecb51fa
 
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
c7beb09
 
 
 
ecb51fa
c7beb09
 
 
 
ecb51fa
c7beb09
 
 
 
 
 
 
 
 
 
 
 
ecb51fa
c7beb09
ecb51fa
c7beb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4671199
 
 
 
c7beb09
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TaskTrackingSystem.Database.AppDbContextModels;
using TaskTrackingSystem.Shared;
using TaskTrackingSystem.Shared.Enums;
using TaskTrackingSystem.Shared.Models.Issue;
using TaskTrackingSystem.WebApi.Infrastructure;
using IssueEntity = TaskTrackingSystem.Database.AppDbContextModels.Issue;

namespace TaskTrackingSystem.WebApi.Features.Issue
{
    public class IssueService
    {
        private readonly AppDbContext _db;

        public IssueService(AppDbContext db)
        {
            _db = db;
        }

        public async Task<IEnumerable<IssueDto>> GetAllIssuesAsync(long roleId, long currentUserId)
        {
            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            var isManager = await DataScopeAuthorization.IsManagerScopeAsync(_db, roleId);
            return await BuildAccessibleIssueQuery(isAdmin, isManager, currentUserId)
                .OrderByDescending(i => i.CreatedAt ?? DateTime.UtcNow)
                .Select(ToDtoProjection())
                .ToListAsync();
        }

        public async Task<PagedResult<IssueDto>> GetPagedIssuesAsync(
            long roleId,
            long currentUserId,
            string? search,
            long? taskId,
            long? projectId,
            AppTaskStatus? statusId,
            TaskPriority? priorityId,
            bool assignedOnly,
            int page,
            int pageSize)
        {
            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            var isManager = await DataScopeAuthorization.IsManagerScopeAsync(_db, roleId);
            var query = BuildAccessibleIssueQuery(isAdmin, isManager, currentUserId);

            if (assignedOnly)
            {
                query = query.Where(i => i.AssignedTo == currentUserId);
            }

            if (!string.IsNullOrWhiteSpace(search))
            {
                var searchTerm = search.Trim().ToLower();
                query = query.Where(i =>
                    (i.Title != null && i.Title.ToLower().Contains(searchTerm)) ||
                    (i.Description != null && i.Description.ToLower().Contains(searchTerm)));
            }

            if (taskId.HasValue && taskId.Value > 0)
            {
                query = query.Where(i => i.TaskId == taskId.Value);
            }

            if (projectId.HasValue && projectId.Value > 0)
            {
                query = query.Where(i => i.Task.ProjectId == projectId.Value);
            }

            if (statusId.HasValue)
            {
                query = query.Where(i => i.StatusId == statusId.Value);
            }

            if (priorityId.HasValue)
            {
                query = query.Where(i => i.PriorityId == priorityId.Value);
            }

            return await query
                .OrderByDescending(i => i.CreatedAt ?? DateTime.UtcNow)
                .Select(ToDtoProjection())
                .ToPagedResultAsync(page, pageSize);
        }



        public async Task<IssueDto?> GetIssueByIdAsync(long id, long roleId, long currentUserId)
        {
            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            var isManager = await DataScopeAuthorization.IsManagerScopeAsync(_db, roleId);
            var issue = await BuildAccessibleIssueQuery(isAdmin, isManager, currentUserId)
                .FirstOrDefaultAsync(i => i.Id == id);

            if (issue == null)
            {
                return null;
            }

            return await BuildAccessibleIssueQuery(isAdmin, isManager, currentUserId)
                .Where(i => i.Id == id)
                .Select(ToDtoProjection())
                .FirstOrDefaultAsync();
        }

        public async Task<IEnumerable<IssueDto>> GetIssuesByTaskIdAsync(long taskId, long roleId, long currentUserId)
        {
            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            if (!await CanAccessTaskAsync(taskId, isAdmin, currentUserId))
            {
                return Array.Empty<IssueDto>();
            }

            var isManager = await DataScopeAuthorization.IsManagerScopeAsync(_db, roleId);
            return await BuildAccessibleIssueQuery(isAdmin, isManager, currentUserId)
                .Where(i => i.TaskId == taskId)
                .OrderByDescending(i => i.CreatedAt ?? DateTime.UtcNow)
                .Select(ToDtoProjection())
                .ToListAsync();
        }

        public async Task<Result<IssueDto>> CreateIssueAsync(CreateIssueDto dto, long roleId, long currentUserId)
        {
            if (string.IsNullOrWhiteSpace(dto.Title))
            {
                return Result<IssueDto>.Failure("Issue title is required.", 400);
            }

            var task = await _db.Tasks
                .Include(t => t.Project)
                .FirstOrDefaultAsync(t => t.Id == dto.TaskId && t.IsDeleted != true && !t.IsArchived && t.Project.IsDeleted != true);

            if (task == null)
            {
                return Result<IssueDto>.Failure($"Task with ID {dto.TaskId} not found.", 404);
            }

            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            if (!await CanAccessTaskAsync(task.Id, isAdmin, currentUserId))
            {
                return Result<IssueDto>.Failure("You do not have access to this task.", 403);
            }

            if (dto.AssignedTo.HasValue && !await IsTaskMemberAsync(task.ProjectId, dto.AssignedTo.Value))
            {
                return Result<IssueDto>.Failure("Issue assignee must belong to the selected project.", 400);
            }

            var issue = new IssueEntity
            {
                TaskId = dto.TaskId,
                Title = dto.Title.Trim(),
                Description = dto.Description,
                AssignedTo = dto.AssignedTo,
                EstimatedHours = dto.EstimatedHours,
                ActualHours = dto.ActualHours,
                DelayReason = dto.DelayReason,
                IsBlocked = dto.IsBlocked,
                BlockedBy = dto.BlockedBy,
                EscalationLevel = dto.EscalationLevel,
                StartDate = dto.StartDate,
                DueDate = dto.DueDate,
                StatusId = dto.StatusId == 0 ? AppTaskStatus.Todo : dto.StatusId,
                PriorityId = dto.PriorityId == 0 ? TaskPriority.Medium : dto.PriorityId,
                CreatedAt = DateTime.UtcNow,
                CreatedBy = currentUserId,
                IsDeleted = false
            };

            _db.Issues.Add(issue);
            await _db.SaveChangesAsync();

            var result = await GetIssueByIdAsync(issue.Id, roleId, currentUserId);
            return Result<IssueDto>.Success(result!, 201);
        }

        public async Task<Result> UpdateIssueAsync(long id, UpdateIssueDto dto, long roleId, long currentUserId)
        {
            if (string.IsNullOrWhiteSpace(dto.Title))
            {
                return Result.Failure("Issue title is required.", 400);
            }

            var issue = await _db.Issues
                .Include(i => i.Task)
                .ThenInclude(t => t.Project)
                .FirstOrDefaultAsync(i => i.Id == id && i.IsDeleted != true);

            if (issue == null)
            {
                return Result.Failure($"Issue with ID {id} not found.", 404);
            }

            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            if (!await CanAccessTaskAsync(issue.TaskId, isAdmin, currentUserId))
            {
                return Result.Failure("You do not have access to this task.", 403);
            }

            if (dto.AssignedTo.HasValue && !await IsTaskMemberAsync(issue.Task.ProjectId, dto.AssignedTo.Value))
            {
                return Result.Failure("Issue assignee must belong to the selected project.", 400);
            }

            issue.Title = dto.Title.Trim();
            issue.Description = dto.Description;
            issue.AssignedTo = dto.AssignedTo;
            issue.EstimatedHours = dto.EstimatedHours;
            issue.ActualHours = dto.ActualHours;
            issue.DelayReason = dto.DelayReason;
            issue.IsBlocked = dto.IsBlocked;
            issue.BlockedBy = dto.BlockedBy;
            issue.EscalationLevel = dto.EscalationLevel;
            issue.StartDate = dto.StartDate;
            issue.DueDate = dto.DueDate;
            issue.StatusId = dto.StatusId;
            issue.PriorityId = dto.PriorityId;
            issue.UpdatedAt = DateTime.UtcNow;
            issue.UpdatedBy = currentUserId;

            _db.Issues.Update(issue);
            await _db.SaveChangesAsync();
            return Result.Success(200);
        }

        public async Task<Result> SoftDeleteIssueAsync(long id, long roleId, long currentUserId)
        {
            var issue = await _db.Issues
                .Include(i => i.Task)
                .ThenInclude(t => t.Project)
                .FirstOrDefaultAsync(i => i.Id == id && i.IsDeleted != true);

            if (issue == null)
            {
                return Result.Failure($"Issue with ID {id} not found.", 404);
            }

            var isAdmin = await DataScopeAuthorization.IsAdminScopeAsync(_db, roleId);
            if (!await CanAccessTaskAsync(issue.TaskId, isAdmin, currentUserId))
            {
                return Result.Failure("You do not have access to this task.", 403);
            }

            issue.IsDeleted = true;
            issue.UpdatedAt = DateTime.UtcNow;
            issue.UpdatedBy = currentUserId;

            _db.Issues.Update(issue);
            await _db.SaveChangesAsync();
            return Result.Success(200);
        }

        private IQueryable<IssueEntity> BuildAccessibleIssueQuery(bool isAdmin, bool isManager, long currentUserId)
        {
            var query = _db.Issues
                .Where(i => i.IsDeleted != true && i.Task.IsDeleted != true && i.Task.Project.IsDeleted != true);

            if (isAdmin)
            {
                return query;
            }

            if (isManager)
            {
                return query.Where(i =>
                    i.AssignedTo == currentUserId ||
                    i.CreatedBy == currentUserId ||
                    i.Task.Project.ProjectMembers.Any(pm => pm.UserId == currentUserId));
            }

            return query.Where(i =>
                i.AssignedTo == currentUserId ||
                i.CreatedBy == currentUserId);
        }

        private async Task<bool> CanAccessTaskAsync(long taskId, bool isAdmin, long currentUserId)
        {
            if (isAdmin)
            {
                return await _db.Tasks.AnyAsync(t => t.Id == taskId && t.IsDeleted != true && !t.IsArchived && t.Project.IsDeleted != true);
            }

            return await _db.Tasks.AnyAsync(t =>
                t.Id == taskId &&
                t.IsDeleted != true &&
                !t.IsArchived &&
                t.Project.IsDeleted != true &&
                (t.AssignedTo == currentUserId ||
                 t.CreatedBy == currentUserId ||
                 t.Project.ProjectMembers.Any(pm => pm.UserId == currentUserId)));
        }

        private async Task<bool> IsTaskMemberAsync(long projectId, long userId)
        {
            return await _db.ProjectMembers.AnyAsync(pm => pm.ProjectId == projectId && pm.UserId == userId);
        }

        private static System.Linq.Expressions.Expression<Func<IssueEntity, IssueDto>> ToDtoProjection()
        {
            return i => new IssueDto
            {
                Id = i.Id,
                TaskId = i.TaskId,
                TaskTitle = i.Task.Title,
                ProjectId = i.Task.ProjectId,
                ProjectName = i.Task.Project.Name,
                Title = i.Title,
                Description = i.Description,
                AssignedTo = i.AssignedTo,
                AssignedToName = i.AssignedToNavigation != null
                    ? i.AssignedToNavigation.FirstName + " " + i.AssignedToNavigation.LastName
                    : null,
                EstimatedHours = i.EstimatedHours,
                ActualHours = i.ActualHours,
                DelayReason = i.DelayReason,
                IsBlocked = i.IsBlocked,
                BlockedBy = i.BlockedBy,
                EscalationLevel = i.EscalationLevel,
                StartDate = i.StartDate,
                DueDate = i.DueDate,
                StatusId = i.StatusId,
                PriorityId = i.PriorityId,
                CreatedAt = i.CreatedAt ?? DateTime.UtcNow,
                UpdatedAt = i.UpdatedAt
            };
        }
    }
}