Initial commit
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
@page "/bulk-members"
|
||||
@attribute [Authorize]
|
||||
@inject IUserSessionService Session
|
||||
@inject IUserContextAccessor UserContext
|
||||
@inject ISessionManager SessionMgr
|
||||
@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">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Site URL</label>
|
||||
<input class="form-input" @bind="_siteUrl" placeholder="@Session.CurrentProfile!.TenantUrl" />
|
||||
</div>
|
||||
<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 string _siteUrl = string.Empty;
|
||||
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 = string.IsNullOrWhiteSpace(_siteUrl) ? Session.CurrentProfile!.TenantUrl : _siteUrl.Trim();
|
||||
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
||||
try
|
||||
{
|
||||
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, _cts.Token);
|
||||
_summary = await BulkSvc.AddMembersAsync(ctx, Session.CurrentProfile!, validRows, progress, _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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user