83 lines
2.6 KiB
C#
83 lines
2.6 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Socialize.Api.Data;
|
|
using Socialize.Api.Infrastructure.Security;
|
|
using Socialize.Api.Modules.Campaigns.Data;
|
|
|
|
namespace Socialize.Api.Modules.Campaigns.Handlers;
|
|
|
|
public record GetCampaignsRequest(Guid? WorkspaceId, Guid? ClientId);
|
|
|
|
public record CampaignDto(
|
|
Guid Id,
|
|
Guid WorkspaceId,
|
|
Guid ClientId,
|
|
string Name,
|
|
string? Description,
|
|
string? Notes,
|
|
string Status,
|
|
DateTimeOffset StartDate,
|
|
DateTimeOffset EndDate);
|
|
|
|
public class GetCampaignsHandler(
|
|
AppDbContext dbContext,
|
|
AccessScopeService accessScopeService)
|
|
: Endpoint<GetCampaignsRequest, IReadOnlyCollection<CampaignDto>>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/campaigns");
|
|
Options(o => o.WithTags("Campaigns"));
|
|
}
|
|
|
|
public override async Task HandleAsync(GetCampaignsRequest request, CancellationToken ct)
|
|
{
|
|
IQueryable<Campaign> query = dbContext.Campaigns.AsQueryable();
|
|
|
|
if (!accessScopeService.IsManager(User))
|
|
{
|
|
IReadOnlyCollection<Guid> workspaceScopeIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct);
|
|
IReadOnlyCollection<Guid> clientScopeIds = User.GetClientScopeIds();
|
|
IReadOnlyCollection<Guid> campaignScopeIds = User.GetCampaignScopeIds();
|
|
|
|
query = query.Where(campaign => workspaceScopeIds.Contains(campaign.WorkspaceId));
|
|
|
|
if (clientScopeIds.Count > 0)
|
|
{
|
|
query = query.Where(campaign => clientScopeIds.Contains(campaign.ClientId));
|
|
}
|
|
|
|
if (campaignScopeIds.Count > 0)
|
|
{
|
|
query = query.Where(campaign => campaignScopeIds.Contains(campaign.Id));
|
|
}
|
|
}
|
|
|
|
if (request.ClientId.HasValue)
|
|
{
|
|
query = query.Where(campaign => campaign.ClientId == request.ClientId.Value);
|
|
}
|
|
|
|
if (request.WorkspaceId.HasValue)
|
|
{
|
|
query = query.Where(campaign => campaign.WorkspaceId == request.WorkspaceId.Value);
|
|
}
|
|
|
|
List<CampaignDto> campaigns = await query
|
|
.OrderBy(campaign => campaign.Name)
|
|
.Select(campaign => new CampaignDto(
|
|
campaign.Id,
|
|
campaign.WorkspaceId,
|
|
campaign.ClientId,
|
|
campaign.Name,
|
|
campaign.Description,
|
|
campaign.Notes,
|
|
campaign.Status,
|
|
campaign.StartDate,
|
|
campaign.EndDate))
|
|
.ToListAsync(ct);
|
|
|
|
await SendOkAsync(campaigns, ct);
|
|
}
|
|
}
|