71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Socialize.Api.Data;
|
|
using Socialize.Api.Infrastructure.Security;
|
|
using Socialize.Api.Modules.Clients.Data;
|
|
|
|
namespace Socialize.Api.Modules.Clients.Handlers;
|
|
|
|
public record GetClientsRequest(Guid? WorkspaceId);
|
|
|
|
public record ClientDto(
|
|
Guid Id,
|
|
Guid WorkspaceId,
|
|
string Name,
|
|
string Status,
|
|
string? PortraitUrl,
|
|
string? PrimaryContactName,
|
|
string? PrimaryContactEmail,
|
|
string? PrimaryContactPortraitUrl);
|
|
|
|
public class GetClientsHandler(
|
|
AppDbContext dbContext,
|
|
AccessScopeService accessScopeService)
|
|
: Endpoint<GetClientsRequest, IReadOnlyCollection<ClientDto>>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/clients");
|
|
Options(o => o.WithTags("Clients"));
|
|
}
|
|
|
|
public override async Task HandleAsync(GetClientsRequest request, CancellationToken ct)
|
|
{
|
|
IQueryable<Client> query = dbContext.Clients.AsQueryable();
|
|
|
|
if (!accessScopeService.IsManager(User))
|
|
{
|
|
IReadOnlyCollection<Guid> workspaceScopeIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct);
|
|
IReadOnlyCollection<Guid> clientScopeIds = User.GetClientScopeIds();
|
|
|
|
query = query.Where(client => workspaceScopeIds.Contains(client.WorkspaceId));
|
|
|
|
if (clientScopeIds.Count > 0)
|
|
{
|
|
query = query.Where(client => clientScopeIds.Contains(client.Id));
|
|
}
|
|
|
|
}
|
|
|
|
if (request.WorkspaceId.HasValue)
|
|
{
|
|
query = query.Where(client => client.WorkspaceId == request.WorkspaceId.Value);
|
|
}
|
|
|
|
List<ClientDto> clients = await query
|
|
.OrderBy(client => client.Name)
|
|
.Select(client => new ClientDto(
|
|
client.Id,
|
|
client.WorkspaceId,
|
|
client.Name,
|
|
client.Status,
|
|
client.PortraitUrl,
|
|
client.PrimaryContactName,
|
|
client.PrimaryContactEmail,
|
|
client.PrimaryContactPortraitUrl))
|
|
.ToListAsync(ct);
|
|
|
|
await SendOkAsync(clients, ct);
|
|
}
|
|
}
|