update offboard function

This commit is contained in:
Oliver Kieselbach
2026-05-18 12:37:35 +02:00
parent e64b795b28
commit 884f29b019
3 changed files with 36 additions and 6 deletions
@@ -1 +1 @@
770
771
@@ -19,15 +19,18 @@ public class TenantOffboardFunction
{
private readonly ILogger<TenantOffboardFunction> _logger;
private readonly IMaintenanceRepository _maintenanceRepo;
private readonly IConfigRepository _configRepo;
private readonly OpsEventService _opsEventService;
public TenantOffboardFunction(
ILogger<TenantOffboardFunction> logger,
IMaintenanceRepository maintenanceRepo,
IConfigRepository configRepo,
OpsEventService opsEventService)
{
_logger = logger;
_maintenanceRepo = maintenanceRepo;
_configRepo = configRepo;
_opsEventService = opsEventService;
}
@@ -65,6 +68,19 @@ public class TenantOffboardFunction
var result = new OffboardResult { TenantId = requestCtx.TargetTenantId, InitiatedBy = upn!, InitiatedAt = DateTime.UtcNow };
// Capture domain name BEFORE deletion — tenant config is wiped by DeleteAllTenantDataAsync,
// and the ops alert / Telegram message is friendlier with the domain than the bare GUID.
string? domainName = null;
try
{
var tenantConfig = await _configRepo.GetTenantConfigurationAsync(requestCtx.TargetTenantId);
domainName = tenantConfig?.DomainName;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to read tenant config for {TenantId} before offboard; proceeding without domain name", requestCtx.TargetTenantId);
}
try
{
var deletedCounts = await _maintenanceRepo.DeleteAllTenantDataAsync(requestCtx.TargetTenantId);
@@ -87,7 +103,7 @@ public class TenantOffboardFunction
upn!,
details);
await _opsEventService.RecordTenantOffboardedAsync(requestCtx.TargetTenantId, upn!, deletedCounts);
await _opsEventService.RecordTenantOffboardedAsync(requestCtx.TargetTenantId, upn!, deletedCounts, domainName);
var okResponse = req.CreateResponse(HttpStatusCode.OK);
await okResponse.WriteAsJsonAsync(result);
@@ -256,10 +256,24 @@ namespace AutopilotMonitor.Functions.Services
// ── Tenant ─────────────────────────────────────────────────────────────
public Task RecordTenantOffboardedAsync(string tenantId, string performedBy, Dictionary<string, int> deletedCounts)
=> WriteAsync(OpsEventCategory.Tenant, "TenantOffboarded", OpsEventSeverity.Warning,
$"Tenant {tenantId} offboarded — all data deleted",
tenantId, performedBy, deletedCounts);
public Task RecordTenantOffboardedAsync(string tenantId, string performedBy, Dictionary<string, int> deletedCounts, string? domainName = null)
{
var tenantLabel = string.IsNullOrWhiteSpace(domainName)
? tenantId
: $"{domainName} ({tenantId})";
// Wrap deletedCounts + domainName so the OpsEvents details panel still surfaces the
// per-table counts AND the domain (needed because Table Storage is gone by emit time).
var details = new Dictionary<string, object?>
{
["domainName"] = domainName,
["deletedCounts"] = deletedCounts,
};
return WriteAsync(OpsEventCategory.Tenant, "TenantOffboarded", OpsEventSeverity.Warning,
$"Tenant {tenantLabel} offboarded — all data deleted",
tenantId, performedBy, details);
}
// ── Agent ──────────────────────────────────────────────────────────────