-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstructorScheduleController.cs
More file actions
545 lines (489 loc) · 23.8 KB
/
InstructorScheduleController.cs
File metadata and controls
545 lines (489 loc) · 23.8 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
using Microsoft.AspNetCore.Mvc;
using Viper.Areas.ClinicalScheduler.Services;
using Viper.Areas.ClinicalScheduler.Extensions;
using Viper.Areas.ClinicalScheduler.Constants;
using Viper.Areas.ClinicalScheduler.Models;
using Viper.Areas.ClinicalScheduler.Models.DTOs.Requests;
using Viper.Areas.ClinicalScheduler.Models.DTOs.Responses;
using Viper.Areas.ClinicalScheduler.Validators;
using Viper.Classes.SQLContext;
using Viper.Models.ClinicalScheduler;
using Web.Authorization;
using Viper.Classes.Utilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
namespace Viper.Areas.ClinicalScheduler.Controllers
{
/// <summary>
/// Controller for managing instructor schedule assignments
/// </summary>
[Route("api/clinicalscheduler/instructor-schedules")]
[ApiController]
[Permission(Allow = ClinicalSchedulePermissions.Base)]
public class InstructorScheduleController : BaseClinicalSchedulerController
{
private readonly IScheduleEditService _scheduleEditService;
private readonly IScheduleAuditService _auditService;
private readonly ISchedulePermissionService _permissionService;
private readonly IUserHelper _userHelper;
private readonly AddInstructorValidator _validator;
public InstructorScheduleController(
IScheduleEditService scheduleEditService,
IScheduleAuditService auditService,
ISchedulePermissionService permissionService,
IUserHelper userHelper,
IGradYearService gradYearService,
ILogger<InstructorScheduleController> logger,
AddInstructorValidator validator)
: base(gradYearService, logger)
{
_scheduleEditService = scheduleEditService;
_auditService = auditService;
_permissionService = permissionService;
_userHelper = userHelper;
_validator = validator;
}
/// <summary>
/// Add an instructor to specific rotation weeks
/// </summary>
/// <param name="request">Add instructor request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Response containing created instructor schedule entries and optional warning</returns>
[HttpPost]
[ProducesResponseType(typeof(AddInstructorResponse), 200)]
[ProducesResponseType(typeof(ErrorResponse), 400)]
[ProducesResponseType(403)]
[ProducesResponseType(typeof(ErrorResponse), 409)] // Conflict
public async Task<IActionResult> AddInstructor(
[FromBody] AddInstructorRequest request,
CancellationToken cancellationToken = default)
{
var correlationId = Guid.NewGuid().ToString();
try
{
// Step 1: Validate request
var validationResult = await _validator.ValidateRequestAsync(request, correlationId, cancellationToken);
if (!validationResult.IsValid)
{
return BadRequest(new ErrorResponse(
ErrorCodes.ValidationError,
validationResult.ErrorMessage!,
correlationId));
}
// Step 2: Check permissions - include own schedule check
if (!await CheckPermissionsForAddAsync(request.RotationId!.Value, request.MothraId!, correlationId, cancellationToken))
{
return Forbid();
}
// Step 3: Check for conflicts and build warning message
var warningMessage = await BuildConflictWarningAsync(
request.MothraId!,
request.WeekIds,
request.GradYear!.Value,
request.RotationId!.Value,
correlationId,
cancellationToken);
// Step 4: Add instructor through service layer
var response = await ProcessAddInstructorAsync(
request,
warningMessage,
correlationId,
cancellationToken);
return Ok(response);
}
catch (UnauthorizedAccessException ex)
{
return HandleUnauthorizedAccess(ex, request.RotationId!.Value, correlationId);
}
catch (InvalidOperationException ex)
{
return HandleInvalidOperation(ex, correlationId);
}
catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
return HandleSystemError(ex, request.RotationId!.Value, correlationId);
}
}
private async Task<bool> CheckPermissionsForAddAsync(
int rotationId,
string targetMothraId,
string correlationId,
CancellationToken cancellationToken)
{
// First check regular rotation edit permissions
if (await _permissionService.HasEditPermissionForRotationAsync(rotationId, cancellationToken))
{
return true;
}
// Check if user has EditOwnSchedule permission and is adding themselves
var currentUser = _userHelper.GetCurrentUser();
if (currentUser != null)
{
// Check if user has EditOwnSchedule permission
var rapsContext = HttpContext.RequestServices.GetService<RAPSContext>();
if (_userHelper.HasPermission(rapsContext, currentUser, ClinicalSchedulePermissions.EditOwnSchedule) &&
currentUser.MothraId.Equals(targetMothraId, StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation("User {MothraId} is adding their own schedule with EditOwnSchedule permission (CorrelationId: {CorrelationId})",
LogSanitizer.SanitizeId(currentUser.MothraId), correlationId);
return true;
}
}
_logger.LogWarning("User attempted to add instructor {TargetMothraId} to rotation {RotationId} without permission (CorrelationId: {CorrelationId})",
LogSanitizer.SanitizeId(targetMothraId), rotationId, correlationId);
return false;
}
private async Task<string?> BuildConflictWarningAsync(
string mothraId,
int[] weekIds,
int gradYear,
int rotationId,
string correlationId,
CancellationToken cancellationToken)
{
var otherRotations = await _scheduleEditService.GetOtherRotationSchedulesAsync(
mothraId, weekIds, gradYear, rotationId, cancellationToken);
if (!otherRotations.Any())
return null;
// Get unique rotation names
var rotationNames = otherRotations
.Select(s => s.Rotation?.Name ?? $"Rotation {s.RotationId}")
.Distinct()
.OrderBy(name => name);
var warningMessage = $"Note: This instructor is also scheduled for {string.Join(", ", rotationNames)}";
_logger.LogInformation("Instructor {MothraId} has other rotation assignments (CorrelationId: {CorrelationId}): {Details}",
LogSanitizer.SanitizeId(mothraId), correlationId, warningMessage);
return warningMessage;
}
private async Task<AddInstructorResponse> ProcessAddInstructorAsync(
AddInstructorRequest request,
string? warningMessage,
string correlationId,
CancellationToken cancellationToken)
{
// Add instructor to schedule
var createdSchedules = await _scheduleEditService.AddInstructorAsync(
request.MothraId!,
request.RotationId!.Value,
request.WeekIds,
request.GradYear!.Value,
request.IsPrimaryEvaluator,
cancellationToken);
var scheduleResponses = new List<InstructorScheduleResponse>();
foreach (var schedule in createdSchedules)
{
var canRemove = await _scheduleEditService.CanRemoveInstructorAsync(
schedule.InstructorScheduleId, cancellationToken);
scheduleResponses.Add(schedule.ToResponse(canRemove));
}
_logger.LogInformation("Successfully added instructor to rotation {RotationId} for {WeekCount} weeks (CorrelationId: {CorrelationId})",
request.RotationId!.Value, request.WeekIds.Length, correlationId);
return new AddInstructorResponse
{
Schedules = scheduleResponses,
ScheduleIds = scheduleResponses.Select(s => s.InstructorScheduleId).ToList(),
WarningMessage = warningMessage
};
}
private IActionResult HandleUnauthorizedAccess(
UnauthorizedAccessException ex,
int rotationId,
string correlationId)
{
_logger.LogWarning(ex, "Unauthorized attempt to add instructor to rotation {RotationId} (CorrelationId: {CorrelationId})",
rotationId, correlationId);
return Forbid();
}
private IActionResult HandleInvalidOperation(
InvalidOperationException ex,
string correlationId)
{
_logger.LogWarning(ex, "Invalid operation (CorrelationId: {CorrelationId}): {ErrorMessage}",
correlationId, ex.Message);
// Map specific error messages to user-friendly messages
string userMessage = UserMessages.GenericError;
string errorCode = ErrorCodes.InvalidOperation;
if (ex.Message.Contains("Person with MothraId") && ex.Message.Contains("not found"))
{
userMessage = UserMessages.InvalidPerson;
errorCode = ErrorCodes.ResourceNotFound;
}
else if (ex.Message.Contains("Rotation with ID") && ex.Message.Contains("not found"))
{
userMessage = UserMessages.InvalidRotation;
errorCode = ErrorCodes.ResourceNotFound;
}
else if (ex.Message.Contains("Week(s) not found"))
{
userMessage = UserMessages.InvalidWeeks;
errorCode = ErrorCodes.ResourceNotFound;
}
else if (ex.Message.Contains("already scheduled") || ex.Message.Contains("duplicate"))
{
userMessage = UserMessages.DuplicateSchedule;
errorCode = ErrorCodes.DuplicateSchedule;
}
return BadRequest(new ErrorResponse(errorCode, userMessage, correlationId));
}
private IActionResult HandleSystemError(
Exception ex,
int rotationId,
string correlationId)
{
_logger.LogError(ex, "Error adding instructor to rotation {RotationId} (CorrelationId: {CorrelationId})",
rotationId, correlationId);
return StatusCode(500, new ErrorResponse(
ErrorCodes.SystemError,
UserMessages.GenericError,
correlationId));
}
/// <summary>
/// Remove an instructor from a schedule assignment
/// </summary>
/// <param name="instructorScheduleId">Instructor schedule ID to remove</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>200 OK with RemoveInstructorResponse containing removal details, or error response</returns>
[HttpDelete("{instructorScheduleId:int}")]
[ProducesResponseType(typeof(RemoveInstructorResponse), 200)]
[ProducesResponseType(typeof(ErrorResponse), 400)]
[ProducesResponseType(403)]
[ProducesResponseType(typeof(ErrorResponse), 404)]
public async Task<IActionResult> RemoveInstructor(
int instructorScheduleId,
CancellationToken cancellationToken = default)
{
var correlationId = Guid.NewGuid().ToString();
try
{
// Validate input parameters
if (instructorScheduleId <= 0)
{
return BadRequest(new ErrorResponse(
ErrorCodes.ValidationError,
ValidationMessages.InstructorScheduleIdRequired,
correlationId));
}
var (success, wasPrimaryEvaluator, instructorName) = await _scheduleEditService.RemoveInstructorScheduleAsync(instructorScheduleId, cancellationToken);
if (!success)
{
return NotFound(new ErrorResponse(
ErrorCodes.ResourceNotFound,
"The specified schedule was not found.",
correlationId));
}
_logger.LogInformation("Successfully removed instructor schedule {ScheduleId} (Primary: {WasPrimary}) (CorrelationId: {CorrelationId})",
instructorScheduleId, wasPrimaryEvaluator, correlationId);
return Ok(new RemoveInstructorResponse
{
Success = true,
WasPrimaryEvaluator = wasPrimaryEvaluator,
InstructorName = instructorName
});
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, "Unauthorized attempt to remove instructor schedule {ScheduleId} (CorrelationId: {CorrelationId})",
instructorScheduleId, correlationId);
return Forbid();
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Invalid operation when removing instructor schedule {ScheduleId} (CorrelationId: {CorrelationId}): {ErrorMessage}",
instructorScheduleId, correlationId, ex.Message);
// Map to user-friendly message
string userMessage = "Cannot remove this instructor at this time. Please try again later.";
if (ex.Message.Contains("primary evaluator"))
{
userMessage = "Cannot remove the primary evaluator when they are the only instructor assigned.";
}
return BadRequest(new ErrorResponse(
ErrorCodes.InvalidOperation,
userMessage,
correlationId));
}
catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
_logger.LogError(ex, "Error removing instructor schedule {ScheduleId} (CorrelationId: {CorrelationId})",
instructorScheduleId, correlationId);
return StatusCode(500, new ErrorResponse(
ErrorCodes.SystemError,
UserMessages.GenericError,
correlationId));
}
}
/// <summary>
/// Set or unset an instructor as primary evaluator
/// </summary>
/// <param name="instructorScheduleId">Instructor schedule ID</param>
/// <param name="request">Primary evaluator request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Success or failure result</returns>
[HttpPut("{instructorScheduleId:int}/primary")]
[ProducesResponseType(200)]
[ProducesResponseType(typeof(ErrorResponse), 400)]
[ProducesResponseType(403)]
[ProducesResponseType(typeof(ErrorResponse), 404)]
public async Task<IActionResult> SetPrimaryEvaluator(
int instructorScheduleId,
[FromBody] SetPrimaryEvaluatorRequest request,
CancellationToken cancellationToken = default)
{
var correlationId = Guid.NewGuid().ToString();
try
{
if (!ModelState.IsValid)
{
_logger.LogWarning("Model validation failed (CorrelationId: {CorrelationId}): {Errors}",
correlationId, string.Join("; ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)));
return BadRequest(new ErrorResponse(
ErrorCodes.ValidationError,
"Please check your input and try again.",
correlationId));
}
// Validate input parameters
if (instructorScheduleId <= 0)
{
return BadRequest(new ErrorResponse(
ErrorCodes.ValidationError,
ValidationMessages.InstructorScheduleIdRequired,
correlationId));
}
var (success, previousPrimaryName) = await _scheduleEditService.SetPrimaryEvaluatorAsync(
instructorScheduleId, request.IsPrimary!.Value, cancellationToken, request.RequiresPrimaryEvaluator);
if (!success)
{
return NotFound(new ErrorResponse(
ErrorCodes.ResourceNotFound,
"The specified schedule was not found.",
correlationId));
}
var action = request.IsPrimary!.Value ? "set as" : "removed as";
_logger.LogInformation("Successfully {Action} primary evaluator for instructor schedule {ScheduleId} (CorrelationId: {CorrelationId})",
action, instructorScheduleId, correlationId);
return Ok(new
{
message = $"Instructor successfully {action} primary evaluator",
isPrimaryEvaluator = request.IsPrimary!.Value,
previousPrimaryName = previousPrimaryName
});
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, "Unauthorized attempt to modify primary evaluator for instructor schedule {ScheduleId} (CorrelationId: {CorrelationId})",
instructorScheduleId, correlationId);
return Forbid();
}
catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
_logger.LogError(ex, "Error setting primary evaluator for instructor schedule {ScheduleId} (CorrelationId: {CorrelationId})",
instructorScheduleId, correlationId);
return StatusCode(500, new ErrorResponse(
ErrorCodes.SystemError,
UserMessages.GenericError,
correlationId));
}
}
/// <summary>
/// Check for schedule conflicts for an instructor
/// </summary>
/// <param name="mothraId">Instructor's MothraID</param>
/// <param name="weekIds">Comma-separated week IDs</param>
/// <param name="gradYear">Graduation year</param>
/// <param name="excludeRotationId">Optional rotation ID to exclude from conflict check</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Other rotation assignments for the instructor (informational)</returns>
[HttpGet("other-assignments")]
[ProducesResponseType(typeof(ScheduleConflictResponse), 200)]
[ProducesResponseType(400)]
public async Task<IActionResult> CheckScheduleConflicts(
[FromQuery] string mothraId,
[FromQuery] string weekIds,
[FromQuery] int gradYear,
[FromQuery] int? excludeRotationId = null,
CancellationToken cancellationToken = default)
{
try
{
if (string.IsNullOrEmpty(mothraId))
{
return BadRequest(ValidationMessages.MothraIdRequired);
}
if (string.IsNullOrEmpty(weekIds))
{
return BadRequest(ValidationMessages.WeekIdsRequiredForConflicts);
}
// Validate gradYear
if (gradYear <= 0)
{
return BadRequest(new ErrorResponse(
ErrorCodes.ValidationError,
"GradYear must be greater than zero.",
Guid.NewGuid().ToString()));
}
// Parse week IDs and track invalid ones
var weekIdTokens = weekIds.Split(',').Select(id => id.Trim()).ToArray();
var parsedWeekIds = new List<int>();
var invalidTokens = new List<string>();
foreach (var token in weekIdTokens)
{
if (int.TryParse(token, out var weekId) && weekId > 0)
{
parsedWeekIds.Add(weekId);
}
else
{
invalidTokens.Add(token);
}
}
if (invalidTokens.Any())
{
return BadRequest($"Invalid week IDs found: {string.Join(", ", invalidTokens)}. All week IDs must be positive integers.");
}
if (!parsedWeekIds.Any())
{
return BadRequest(ValidationMessages.AtLeastOneValidWeekId);
}
var weekIdArray = parsedWeekIds.Distinct().ToArray();
var otherRotations = await _scheduleEditService.GetOtherRotationSchedulesAsync(
mothraId, weekIdArray, gradYear, excludeRotationId, cancellationToken);
var response = new ScheduleConflictResponse
{
Conflicts = otherRotations.Select(c => c.ToResponse()).ToList(),
Message = otherRotations.Any()
? $"Note: Instructor {mothraId} is also scheduled for {otherRotations.Count} other rotation(s) during these weeks"
: $"No other rotation assignments found for instructor {mothraId}"
};
return Ok(response);
}
catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
_logger.LogError(ex, "Error checking schedule conflicts for {MothraId}", LogSanitizer.SanitizeId(mothraId));
return StatusCode(500, "An error occurred while checking for schedule conflicts");
}
}
/// <summary>
/// Get audit history for an instructor schedule
/// </summary>
/// <param name="instructorScheduleId">Instructor schedule ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>List of audit entries</returns>
[HttpGet("{instructorScheduleId:int}/audit")]
[ProducesResponseType(typeof(List<ScheduleAudit>), 200)]
[ProducesResponseType(404)]
public async Task<IActionResult> GetAuditHistory(
int instructorScheduleId,
CancellationToken cancellationToken = default)
{
try
{
var auditHistory = await _auditService.GetInstructorScheduleAuditHistoryAsync(instructorScheduleId, cancellationToken);
return Ok(auditHistory);
}
catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
_logger.LogError(ex, "Error retrieving audit history for instructor schedule {ScheduleId}", instructorScheduleId);
return StatusCode(500, "An error occurred while retrieving audit history");
}
}
}
}