// User is paying for the subscription - this is an edge case
$edgeCases->push([
'team'=>$team,
'reason'=>'User is paying for the team\'s Stripe subscription but there are other owners. The subscription needs to be cancelled or transferred to another owner\'s payment method.',
]);
}else{
// There are other owners and user is not paying, just remove this user
$teamsToLeave->push($team);
}
}else{
// User is the only owner, check for replacement
$newOwner=$this->findNewOwner($team);
if($newOwner){
$teamsToTransfer->push([
'team'=>$team,
'new_owner'=>$newOwner,
]);
}else{
// No suitable replacement found - this is an edge case
$edgeCases->push([
'team'=>$team,
'reason'=>'No suitable owner replacement found. Team has only regular members without admin privileges.',
]);
}
}
}else{
// User is just a member - remove them from the team
$teamsToLeave->push($team);
}
}
return[
'to_delete'=>$teamsToDelete,
'to_transfer'=>$teamsToTransfer,
'to_leave'=>$teamsToLeave,
'edge_cases'=>$edgeCases,
];
}
publicfunctionexecute():array
{
if($this->isDryRun){
return[
'deleted'=>0,
'transferred'=>0,
'left'=>0,
];
}
$counts=[
'deleted'=>0,
'transferred'=>0,
'left'=>0,
];
$preview=$this->getTeamsPreview();
// Check for edge cases - should not happen here as we check earlier, but be safe
if($preview['edge_cases']->isNotEmpty()){
thrownew\Exception('Edge cases detected during execution. This should not happen.');
}
// Delete teams where user is alone
foreach($preview['to_delete']as$team){
try{
// The Team model's deleting event will handle cleanup of:
// - private keys
// - sources
// - tags
// - environment variables
// - s3 storages
// - notification settings
$team->delete();
$counts['deleted']++;
}catch(\Exception$e){
\Log::error("Failed to delete team {$team->id}: ".$e->getMessage());
throw$e;// Re-throw to trigger rollback
}
}
// Transfer ownership for teams where user is owner but not alone