119 lines
5.0 KiB
Plaintext
119 lines
5.0 KiB
Plaintext
@page "/bulk-members"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IElevationCoordinator Elevation
|
|
@inject IBulkMemberService BulkSvc
|
|
@inject ICsvValidationService CsvValidation
|
|
@inject BulkResultCsvExportService ExportSvc
|
|
@inject WebExportService WebExport
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">Bulk Members</h1>
|
|
<p class="page-subtitle">Add users to SharePoint groups from a CSV file.</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_sites" Single="true" />
|
|
<div class="form-group">
|
|
<label class="form-label">CSV File (GroupName, GroupUrl, Email, Role)</label>
|
|
<InputFile OnChange="LoadFile" accept=".csv" class="form-input" />
|
|
</div>
|
|
|
|
@if (_rows.Count > 0)
|
|
{
|
|
<div class="alert alert-info mt-8">
|
|
@_rows.Count(r => r.IsValid) valid rows, @_rows.Count(r => !r.IsValid) errors.
|
|
</div>
|
|
<div class="data-table-wrap" style="max-height:200px;overflow-y:auto">
|
|
<table class="data-table">
|
|
<thead><tr><th>Group</th><th>Email</th><th>Role</th><th>Status</th></tr></thead>
|
|
<tbody>
|
|
@foreach (var row in _rows.Take(50))
|
|
{
|
|
<tr class="@(row.IsValid ? "val-valid" : "val-error")">
|
|
<td>@(row.Record?.GroupName ?? "—")</td>
|
|
<td>@(row.Record?.Email ?? "—")</td>
|
|
<td>@(row.Record?.Role ?? "—")</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 ? "Processing…" : "Add Members")
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">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")">
|
|
Processed: @_summary.SuccessCount / @_summary.TotalCount. Failures: @_summary.FailedCount
|
|
</div>
|
|
@if (_summary.HasFailures)
|
|
{
|
|
<button class="btn btn-secondary btn-sm mt-8" @onclick="ExportErrors">Export Errors CSV</button>
|
|
}
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private List<SiteInfo> _sites = new();
|
|
private List<CsvValidationRow<BulkMemberRow>> _rows = new();
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private BulkOperationSummary<BulkMemberRow>? _summary;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private async Task LoadFile(InputFileChangeEventArgs e)
|
|
{
|
|
_rows.Clear();
|
|
var file = e.File;
|
|
using var stream = file.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024);
|
|
_rows = CsvValidation.ParseAndValidateMembers(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 siteUrl = _sites.FirstOrDefault()?.Url;
|
|
if (string.IsNullOrWhiteSpace(siteUrl)) { _error = "Please select a site."; _running = false; return; }
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
_summary = await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await BulkSvc.AddMembersAsync(ctx, Session.CurrentProfile!, validRows, progress, c);
|
|
}, _cts.Token);
|
|
_status = $"Complete: {_summary.SuccessCount} added, {_summary.FailedCount} failed.";
|
|
}
|
|
catch (OperationCanceledException) { _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_members_errors_{DateTime.Now:yyyyMMdd_HHmmss}.csv");
|
|
}
|
|
}
|