Add FetchEmailUidsQuery and its handler for IMAP UIDs
Introduced `FetchEmailUidsQuery` to retrieve UIDs of matching emails from an IMAP mailbox. Added the `FetchEmailUidsQuery` record with properties for email account (`GetSenderQuery`) and mail filtering (`MailSearchFilter`). Implemented `FetchEmailUidsQueryHandler` to handle the query. The handler validates the email account, ensures IMAP configuration, and uses `IImapEmailService.FetchEmailUidsAsync` to fetch UIDs. Added necessary `using` directives for dependencies.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using DigitalData.MessagingService.Application.Common.Interfaces;
|
||||
using DigitalData.MessagingService.Application.Common.Models.MailSearch;
|
||||
using DigitalData.MessagingService.Application.EmailAccount.Queries;
|
||||
using DigitalData.MessagingService.Domain.Exceptions;
|
||||
using MediatR;
|
||||
|
||||
namespace DigitalData.MessagingService.Application.EmailReceiving.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// Query to fetch only the UIDs of matching emails from an IMAP mailbox.
|
||||
/// </summary>
|
||||
public record FetchEmailUidsQuery : IRequest<IEnumerable<long>>
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifies the email account to use.
|
||||
/// </summary>
|
||||
public required GetSenderQuery Account { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Mail query used to filter and limit the emails retrieved.
|
||||
/// </summary>
|
||||
public MailSearchFilter Mail { get; init; } = new();
|
||||
}
|
||||
|
||||
public class FetchEmailUidsQueryHandler(ISender Sender, IImapEmailService ImapService) : IRequestHandler<FetchEmailUidsQuery, IEnumerable<long>>
|
||||
{
|
||||
public async Task<IEnumerable<long>> Handle(FetchEmailUidsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var account = await Sender.Send(request.Account, cancellationToken)
|
||||
?? throw new NotFoundException(
|
||||
$"No email account found for the given criteria (Id: {request.Account.Id}, Username: {request.Account.Username}).");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(account.ImapServer))
|
||||
throw new InvalidOperationException(
|
||||
$"IMAP is not configured for account '{account.Username}' (Id: {account.Id}). Set ImapServer in EmailAccounts configuration.");
|
||||
|
||||
return await ImapService.FetchEmailUidsAsync(
|
||||
account,
|
||||
request.Mail,
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user