✨ Make the resend magic spell easiler to do so
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Text.Json;
|
|||||||
using DysonNetwork.Pass.Email;
|
using DysonNetwork.Pass.Email;
|
||||||
using DysonNetwork.Pass.Pages.Emails;
|
using DysonNetwork.Pass.Pages.Emails;
|
||||||
using DysonNetwork.Pass.Permission;
|
using DysonNetwork.Pass.Permission;
|
||||||
|
using DysonNetwork.Shared.Cache;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
@@ -15,7 +16,8 @@ public class MagicSpellService(
|
|||||||
IConfiguration configuration,
|
IConfiguration configuration,
|
||||||
ILogger<MagicSpellService> logger,
|
ILogger<MagicSpellService> logger,
|
||||||
IStringLocalizer<EmailResource> localizer,
|
IStringLocalizer<EmailResource> localizer,
|
||||||
EmailService email
|
EmailService email,
|
||||||
|
ICacheService cache
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
public async Task<MagicSpell> CreateMagicSpell(
|
public async Task<MagicSpell> CreateMagicSpell(
|
||||||
@@ -35,11 +37,8 @@ public class MagicSpellService(
|
|||||||
.Where(s => s.Type == type)
|
.Where(s => s.Type == type)
|
||||||
.Where(s => s.ExpiresAt == null || s.ExpiresAt > now)
|
.Where(s => s.ExpiresAt == null || s.ExpiresAt > now)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
if (existingSpell is not null)
|
||||||
if (existingSpell != null)
|
return existingSpell;
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Account already has an active magic spell of type {type}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var spellWord = _GenerateRandomString(128);
|
var spellWord = _GenerateRandomString(128);
|
||||||
@@ -59,8 +58,18 @@ public class MagicSpellService(
|
|||||||
return spell;
|
return spell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private const string SpellNotifyCacheKeyPrefix = "spells:notify:";
|
||||||
|
|
||||||
public async Task NotifyMagicSpell(MagicSpell spell, bool bypassVerify = false)
|
public async Task NotifyMagicSpell(MagicSpell spell, bool bypassVerify = false)
|
||||||
{
|
{
|
||||||
|
var cacheKey = SpellNotifyCacheKeyPrefix + spell.Id;
|
||||||
|
var (found, _) = await cache.GetAsyncWithStatus<bool?>(cacheKey);
|
||||||
|
if (found)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Skip sending magic spell {SpellId} due to already sent.", spell.Id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var contact = await db.AccountContacts
|
var contact = await db.AccountContacts
|
||||||
.Where(c => c.Account.Id == spell.AccountId)
|
.Where(c => c.Account.Id == spell.AccountId)
|
||||||
.Where(c => c.Type == AccountContactType.Email)
|
.Where(c => c.Type == AccountContactType.Email)
|
||||||
@@ -138,6 +147,8 @@ public class MagicSpellService(
|
|||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await cache.SetAsync(cacheKey, true, TimeSpan.FromMinutes(5));
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user