117 lines
5.2 KiB
Plaintext
117 lines
5.2 KiB
Plaintext
@page "/bulk-sites"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IBulkSiteService BulkSvc
|
|
@inject ICsvValidationService CsvValidation
|
|
@inject BulkResultCsvExportService ExportSvc
|
|
@inject WebExportService WebExport
|
|
@inject TranslationSource T
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">@T["bulksites.page.title"]</h1>
|
|
<p class="page-subtitle">@T["bulksites.page.subtitle"]</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<div class="form-group">
|
|
<label class="form-label">@T["bulksites.adminurl"]<HelpTip Text="@T["help.adminUrl"]" Wide="true" /></label>
|
|
<input class="form-input" @bind="_adminUrl" placeholder="https://contoso-admin.sharepoint.com" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">@T["bulksites.csvfile.label"]</label>
|
|
<InputFile OnChange="LoadFile" accept=".csv" class="form-input" />
|
|
</div>
|
|
|
|
@if (_rows.Count > 0)
|
|
{
|
|
<div class="alert alert-info mt-8">@string.Format(T["bulksites.validcount"], _rows.Count(r => r.IsValid), _rows.Count(r => !r.IsValid))</div>
|
|
<div class="data-table-wrap" style="max-height:200px;overflow-y:auto">
|
|
<table class="data-table">
|
|
<thead><tr><th>@T["bulksites.name"]</th><th>@T["bulksites.type"]<HelpTip Text="@T["help.siteType"]" Wide="true" /></th><th>@T["bulksites.alias"]<HelpTip Text="@T["help.siteAlias"]" Wide="true" /></th><th>@T["bulksites.col.status"]</th></tr></thead>
|
|
<tbody>
|
|
@foreach (var row in _rows.Take(50))
|
|
{
|
|
<tr class="@(row.IsValid ? "val-valid" : "val-error")">
|
|
<td>@(row.Record?.Name ?? "—")</td>
|
|
<td>@(row.Record?.Type ?? "—")</td>
|
|
<td>@(row.Record?.Alias ?? "—")</td>
|
|
<td>@(row.IsValid ? "✓" : string.Join("; ", row.Errors))</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
}
|
|
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-primary" @onclick="RunBulk" disabled="@(_running || _rows.Count == 0 || _rows.All(r => !r.IsValid))">
|
|
@(_running ? T["bulksites.creating"] : T["bulksites.execute"])
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">@T["btn.cancel"]</button> }
|
|
</div>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" Current="_current" Total="_total" />
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error">@_error</div> }
|
|
|
|
@if (_summary != null)
|
|
{
|
|
<div class="card">
|
|
<div class="alert @(_summary.HasFailures ? "alert-warn" : "alert-success")">
|
|
@string.Format(T["bulksites.summary.created"], _summary.SuccessCount, _summary.TotalCount, _summary.FailedCount)
|
|
</div>
|
|
@if (_summary.HasFailures)
|
|
{
|
|
<button class="btn btn-secondary btn-sm mt-8" @onclick="ExportErrors">@T["bulksites.export.errors"]</button>
|
|
}
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private string _adminUrl = string.Empty;
|
|
private List<CsvValidationRow<BulkSiteRow>> _rows = new();
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private BulkOperationSummary<BulkSiteRow>? _summary;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private async Task LoadFile(InputFileChangeEventArgs e)
|
|
{
|
|
_rows.Clear();
|
|
using var stream = e.File.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024);
|
|
_rows = CsvValidation.ParseAndValidateSites(stream);
|
|
}
|
|
|
|
private async Task RunBulk()
|
|
{
|
|
_error = string.Empty; _summary = null; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var validRows = _rows.Where(r => r.IsValid && r.Record != null).Select(r => r.Record!).ToList();
|
|
var adminUrl = string.IsNullOrWhiteSpace(_adminUrl)
|
|
? Session.CurrentProfile!.TenantUrl.Replace(".sharepoint.com", "-admin.sharepoint.com")
|
|
: _adminUrl.Trim();
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(adminUrl, Session.CurrentProfile!, _cts.Token);
|
|
_summary = await BulkSvc.CreateSitesAsync(ctx, validRows, progress, _cts.Token);
|
|
_status = string.Format(T["bulksites.status.complete"], _summary.SuccessCount, _summary.FailedCount);
|
|
}
|
|
catch (OperationCanceledException) { _status = T["bulksites.status.cancelled"]; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private void Cancel() => _cts?.Cancel();
|
|
private async Task ExportErrors()
|
|
{
|
|
if (_summary == null) return;
|
|
var csv = ExportSvc.BuildFailedItemsCsv(_summary.Results.ToList());
|
|
await WebExport.DownloadCsvAsync(csv, $"bulk_sites_errors_{DateTime.Now:yyyyMMdd_HHmmss}.csv");
|
|
}
|
|
}
|