74 lines
2.8 KiB
C#
74 lines
2.8 KiB
C#
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
public interface IStoryIntelligenceEntitlementService
|
|
{
|
|
Task EnsureCanQueueAsync(int userId);
|
|
Task EnsureCanExecuteAsync(int userId);
|
|
}
|
|
|
|
public sealed class StoryIntelligenceEntitlementService(ISubscriptionService subscriptions) : IStoryIntelligenceEntitlementService
|
|
{
|
|
public Task EnsureCanQueueAsync(int userId) => EnsureAllowedAsync(userId);
|
|
|
|
public Task EnsureCanExecuteAsync(int userId) => EnsureAllowedAsync(userId);
|
|
|
|
private async Task EnsureAllowedAsync(int userId)
|
|
{
|
|
var decision = await subscriptions.CanUseFeatureAsync(userId, SubscriptionFeature.StoryIntelligence);
|
|
if (!decision.Allowed)
|
|
{
|
|
throw new SubscriptionLimitException($"{decision.Message} Story Intelligence requires active access before paid AI work can be queued or processed.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public interface IStoryIntelligenceCompletionNotificationService
|
|
{
|
|
Task NotifyIfImportTerminalAsync(StoryIntelligenceQueuedRun run, CancellationToken cancellationToken = default);
|
|
Task NotifyIfImportTerminalAsync(int bookId, int userId, CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
public sealed class StoryIntelligenceCompletionNotificationService(
|
|
IStoryIntelligencePipelineRepository pipelines,
|
|
IUserRepository users,
|
|
IEmailService emails,
|
|
ILogger<StoryIntelligenceCompletionNotificationService> logger) : IStoryIntelligenceCompletionNotificationService
|
|
{
|
|
public async Task NotifyIfImportTerminalAsync(StoryIntelligenceQueuedRun run, CancellationToken cancellationToken = default)
|
|
{
|
|
if (run.BookID is not int bookId)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await NotifyIfImportTerminalAsync(bookId, run.UserID, cancellationToken);
|
|
}
|
|
|
|
public async Task NotifyIfImportTerminalAsync(int bookId, int userId, CancellationToken cancellationToken = default)
|
|
{
|
|
var target = await pipelines.TryBeginCompletionNotificationAsync(bookId, userId);
|
|
if (target is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var user = await users.GetByIdAsync(target.UserID);
|
|
if (user is null || string.IsNullOrWhiteSpace(user.Email))
|
|
{
|
|
logger.LogWarning("Story Intelligence import notification skipped because user {UserID} has no email target.", target.UserID);
|
|
return;
|
|
}
|
|
|
|
await emails.SendStoryIntelligenceImportNotificationAsync(user.Email, user.DisplayName, target);
|
|
logger.LogInformation(
|
|
"Queued Story Intelligence import {NotificationKind} notification for import session {ImportSessionID}, book {BookID}, user {UserID}.",
|
|
target.NotificationKind,
|
|
target.ImportSessionID,
|
|
target.BookID,
|
|
target.UserID);
|
|
}
|
|
}
|