11 KiB
11 KiB
phase, plan, type, wave, depends_on, files_modified, autonomous, requirements, must_haves
| phase | plan | type | wave | depends_on | files_modified | autonomous | requirements | must_haves | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 10-branding-data-foundation | 02 | execute | 1 |
|
true |
|
|
Purpose: Phase 13 (User Directory ViewModel) needs a service that enumerates all enabled member users from a tenant via Microsoft Graph with pagination. This plan builds the infrastructure service and its tests.
Output: GraphDirectoryUser model, IGraphUserDirectoryService interface, GraphUserDirectoryService implementation with PageIterator, and unit tests.
<execution_context> @C:/Users/dev/.claude/get-shit-done/workflows/execute-plan.md @C:/Users/dev/.claude/get-shit-done/templates/summary.md </execution_context>
@.planning/PROJECT.md @.planning/ROADMAP.md @.planning/STATE.md @.planning/phases/10-branding-data-foundation/10-RESEARCH.mdFrom SharepointToolbox/Services/IGraphUserSearchService.cs:
namespace SharepointToolbox.Services;
public interface IGraphUserSearchService
{
Task<IReadOnlyList<GraphUserResult>> SearchUsersAsync(
string clientId,
string query,
int maxResults = 10,
CancellationToken ct = default);
}
public record GraphUserResult(string DisplayName, string UserPrincipalName, string? Mail);
From SharepointToolbox/Services/GraphUserSearchService.cs:
public class GraphUserSearchService : IGraphUserSearchService
{
private readonly GraphClientFactory _graphClientFactory;
public GraphUserSearchService(GraphClientFactory graphClientFactory)
{
_graphClientFactory = graphClientFactory;
}
public async Task<IReadOnlyList<GraphUserResult>> SearchUsersAsync(
string clientId, string query, int maxResults = 10, CancellationToken ct = default)
{
var graphClient = await _graphClientFactory.CreateClientAsync(clientId, ct);
var response = await graphClient.Users.GetAsync(config =>
{
config.QueryParameters.Filter = $"startsWith(displayName,'{escapedQuery}')...";
config.QueryParameters.Select = new[] { "displayName", "userPrincipalName", "mail" };
config.QueryParameters.Top = maxResults;
config.Headers.Add("ConsistencyLevel", "eventual");
config.QueryParameters.Count = true;
}, ct);
// ...map response.Value to GraphUserResult list
}
}
From SharepointToolbox/Infrastructure/Auth/GraphClientFactory.cs:
public class GraphClientFactory
{
private readonly MsalClientFactory _msalFactory;
public GraphClientFactory(MsalClientFactory msalFactory) { _msalFactory = msalFactory; }
public async Task<GraphServiceClient> CreateClientAsync(string clientId, CancellationToken ct) { /* ... */ }
}
2. Create `IGraphUserDirectoryService.cs` in `Services/`:
```csharp
namespace SharepointToolbox.Services;
public interface IGraphUserDirectoryService
{
Task<IReadOnlyList<GraphDirectoryUser>> GetUsersAsync(
string clientId,
IProgress<int>? progress = null,
CancellationToken ct = default);
}
```
The `IProgress<int>` parameter reports the running count of users fetched so far — Phase 13's ViewModel will use this to show "Loading... X users" feedback. It's optional (null = no reporting).
dotnet build --no-restore -warnaserror
GraphDirectoryUser record and IGraphUserDirectoryService interface exist and compile without warnings.
Task 2: Implement GraphUserDirectoryService with PageIterator and tests
SharepointToolbox/Services/GraphUserDirectoryService.cs,
SharepointToolbox.Tests/Services/GraphUserDirectoryServiceTests.cs
- Test 1: GetUsersAsync with mocked GraphClientFactory returns mapped GraphDirectoryUser records with all 5 fields
- Test 2: GetUsersAsync reports progress via IProgress<int> with incrementing user count
- Test 3: GetUsersAsync with cancelled token throws OperationCanceledException or returns partial results
1. Create `GraphUserDirectoryService.cs`:
- Constructor takes `GraphClientFactory` (same pattern as `GraphUserSearchService`).
- `GetUsersAsync` implementation:
a. Get `GraphServiceClient` via `_graphClientFactory.CreateClientAsync(clientId, ct)`.
b. Call `graphClient.Users.GetAsync(config => { ... }, ct)` with:
- `config.QueryParameters.Filter = "accountEnabled eq true and userType eq 'Member'"` — standard equality filter, does NOT require ConsistencyLevel: eventual (unlike GraphUserSearchService which uses startsWith). Do NOT add ConsistencyLevel header. Do NOT add $count.
- `config.QueryParameters.Select = new[] { "displayName", "userPrincipalName", "mail", "department", "jobTitle" }`
- `config.QueryParameters.Top = 999`
c. If response is null, return empty list.
d. Create `PageIterator.CreatePageIterator(graphClient, response, callback)`.
e. In the callback:
- Check `ct.IsCancellationRequested` — if true, `return false` to stop iteration (see RESEARCH Pitfall 2).
- Map User to GraphDirectoryUser: `new GraphDirectoryUser(user.DisplayName ?? user.UserPrincipalName ?? string.Empty, user.UserPrincipalName ?? string.Empty, user.Mail, user.Department, user.JobTitle)`.
- Add to results list.
- Report progress: `progress?.Report(results.Count)`.
- Return true to continue.
f. Call `await pageIterator.IterateAsync(ct)`.
g. Return results as `IReadOnlyList`.
- Add a comment on the filter line: `// Pending real-tenant verification — see STATE.md pending todos`
2. Create `GraphUserDirectoryServiceTests.cs`:
- Use `[Trait("Category", "Unit")]`.
- Testing PageIterator with mocks is complex because `PageIterator` requires a real `GraphServiceClient`. Instead, test at a higher level:
a. Create a mock `GraphClientFactory` using Moq that returns a mock `GraphServiceClient`.
b. For the basic mapping test: mock `graphClient.Users.GetAsync()` to return a `UserCollectionResponse` with a list of test `User` objects (no `@odata.nextLink` = single page). Verify the returned `GraphDirectoryUser` list has correct field mapping.
c. For the progress test: same setup, verify `IProgress<int>.Report` is called with incrementing counts.
d. For cancellation: use a pre-cancelled `CancellationTokenSource`. The `GetAsync` call should throw `OperationCanceledException` or the callback should detect cancellation.
- If mocking `GraphServiceClient.Users.GetAsync` proves too complex with the Graph SDK's request builder pattern, mark the test with `[Fact(Skip = "Requires integration test with real Graph client")]` and add a comment explaining why. The critical thing is the test FILE exists with the intent documented.
- Focus on what IS testable without a real Graph endpoint: the mapping logic. Consider extracting a static `MapUser(User user)` method and testing that directly.
dotnet test --filter "FullyQualifiedName~GraphUserDirectoryServiceTests" --no-build
GraphUserDirectoryService exists with PageIterator pagination, cancellation support via callback check, progress reporting, and correct filter (no ConsistencyLevel). Tests verify mapping logic and exist for pagination/cancellation scenarios.
```bash
dotnet build --no-restore -warnaserror
dotnet test --filter "FullyQualifiedName~GraphUserDirectoryService" --no-build
```
Both commands must succeed. No warnings, no test failures.
<success_criteria>
- GraphDirectoryUser record has all 5 fields (DisplayName, UPN, Mail, Department, JobTitle)
- IGraphUserDirectoryService interface declares GetUsersAsync with clientId, progress, and cancellation
- GraphUserDirectoryService uses PageIterator for pagination, checks cancellation in callback, reports progress
- Filter is "accountEnabled eq true and userType eq 'Member'" WITHOUT ConsistencyLevel header
- Tests exist and pass for mapping logic; pagination/cancellation tests are either passing or skipped with clear justification </success_criteria>