@@ -338,6 +338,13 @@ public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationT
338338 if ( authResult != null )
339339 return authResult ;
340340
341+ // Check for active assignments before attempting delete
342+ if ( rotation . ServerAssignments ? . Any ( a => a . DeploymentState != DeploymentState . Removed ) == true )
343+ {
344+ this . AddAlertDanger ( "Cannot delete a map rotation that has active server assignments. Unassign all servers first." ) ;
345+ return RedirectToAction ( nameof ( Details ) , new { id } ) ;
346+ }
347+
341348 var deleteResponse = await repositoryApiClient . MapRotations . V1 . DeleteMapRotation ( id , cancellationToken ) . ConfigureAwait ( false ) ;
342349
343350 if ( ! deleteResponse . IsSuccess )
@@ -489,30 +496,42 @@ public async Task<IActionResult> DeleteAssignment(Guid assignmentId, Guid mapRot
489496 if ( authResult != null )
490497 return authResult ;
491498
492- // Verify the assignment belongs to this rotation
493499 if ( rotation . ServerAssignments == null || ! rotation . ServerAssignments . Any ( a => a . MapRotationServerAssignmentId == assignmentId ) )
494- {
495500 return BadRequest ( "The specified assignment does not belong to this rotation." ) ;
501+
502+ var assignment = rotation . ServerAssignments . First ( a => a . MapRotationServerAssignmentId == assignmentId ) ;
503+
504+ // If already removed, delete the DB record directly
505+ if ( assignment . DeploymentState == DeploymentState . Removed )
506+ {
507+ var deleteResponse = await repositoryApiClient . MapRotations . V1 . DeleteServerAssignment ( assignmentId , cancellationToken ) . ConfigureAwait ( false ) ;
508+
509+ if ( ! deleteResponse . IsSuccess )
510+ {
511+ this . AddAlertDanger ( "An error occurred while removing the server assignment." ) ;
512+ }
513+ else
514+ {
515+ this . AddAlertSuccess ( "Server assignment has been removed." ) ;
516+ }
517+
518+ return RedirectToAction ( nameof ( Details ) , new { id = mapRotationId } ) ;
496519 }
497520
498- var deleteResponse = await repositoryApiClient . MapRotations . V1 . DeleteServerAssignment ( assignmentId , cancellationToken ) . ConfigureAwait ( false ) ;
521+ // Trigger the Remove orchestration to clean up maps from the server
522+ var result = await syncApiClient . TriggerRemove ( assignmentId , cancellationToken ) . ConfigureAwait ( false ) ;
499523
500- if ( ! deleteResponse . IsSuccess )
524+ if ( result . Success )
501525 {
502- this . AddAlertDanger ( "An error occurred while removing the server assignment." ) ;
526+ this . AddAlertSuccess ( "Unassign triggered. Maps are being removed from the server." ) ;
527+ TempData [ "PendingInstanceId" ] = $ "maprot-remove-{ assignmentId } ";
503528 }
504529 else
505530 {
506- TrackSuccessTelemetry ( nameof ( DeleteAssignment ) , "MapRotationAssignmentDeleted" , new Dictionary < string , string >
507- {
508- { "AssignmentId" , assignmentId . ToString ( ) } ,
509- { "MapRotationId" , mapRotationId . ToString ( ) }
510- } ) ;
511-
512- this . AddAlertSuccess ( "Server assignment has been removed." ) ;
531+ this . AddAlertDanger ( $ "Failed to trigger unassign: { result . Error } ") ;
513532 }
514533
515- return RedirectToAction ( nameof ( Details ) , new { id = mapRotationId } ) ;
534+ return RedirectToAction ( nameof ( AssignmentStatus ) , new { id = assignmentId } ) ;
516535 } , nameof ( DeleteAssignment ) ) . ConfigureAwait ( false ) ;
517536 }
518537
0 commit comments