Add controlled FAQ auto-replies with test mode and durable sending limits

This commit is contained in:
wolf-demon
2026-09-15 09:50:52 +01:00
parent ad57a8b1bd
commit 5a93c35b18
20 changed files with 389 additions and 16 deletions
+14
View File
@@ -23,6 +23,8 @@ public interface IStore
Task<List<Conversation>> Deliveries();
Task<bool> TryInsertPmsChange(PmsChange change);
Task<bool> TryInsertPayment(PaymentRequest payment);
Task<bool> TryAutoReplyClaim(AutoReplyClaim claim);
Task<List<Conversation>> AutoReplyCandidates(string hotel,string mailbox,DateTime since);
}
public sealed class MongoStore : IStore
{
@@ -43,6 +45,8 @@ public sealed class MongoStore : IStore
}
public async Task Initialize()
{
foreach(var field in new[]{"ThreadKey","RecipientDay","DaySlot"})await Collection<AutoReplyClaim>().Indexes.CreateOneAsync(new CreateIndexModel<AutoReplyClaim>(Builders<AutoReplyClaim>.IndexKeys.Ascending(x=>x.HotelId).Ascending(field),new(){Unique=true}));
await Collection<Conversation>().Indexes.CreateOneAsync(new CreateIndexModel<Conversation>(Builders<Conversation>.IndexKeys.Ascending(x=>x.HotelId).Ascending(x=>x.MailboxId).Ascending(x=>x.AutoReplyCheckedAt).Ascending(x=>x.ReceivedAt)));
await Collection<PaymentRequest>().Indexes.CreateOneAsync(new CreateIndexModel<PaymentRequest>(Builders<PaymentRequest>.IndexKeys.Ascending(x=>x.HotelId).Ascending(x=>x.Reference),new(){Unique=true}));
await Collection<PaymentRequest>().Indexes.CreateOneAsync(new CreateIndexModel<PaymentRequest>(Builders<PaymentRequest>.IndexKeys.Ascending(x=>x.HotelId).Descending(x=>x.UpdatedAt)));
await Collection<PmsChange>().Indexes.CreateOneAsync(new CreateIndexModel<PmsChange>(Builders<PmsChange>.IndexKeys.Ascending(x=>x.HotelId).Ascending(x=>x.ReservationId),new CreateIndexOptions<PmsChange>{Unique=true,PartialFilterExpression=Builders<PmsChange>.Filter.In(x=>x.State,new[]{"Review","Applying","NeedsReview"})}));
@@ -101,6 +105,11 @@ public sealed class MongoStore : IStore
try { await Insert(message); }
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) { /* already durable */ }
}
public Task<List<Conversation>> AutoReplyCandidates(string hotel,string mailbox,DateTime since)=>Collection<Conversation>().Find(Scope<Conversation>(hotel)&Builders<Conversation>.Filter.Eq(x=>x.MailboxId,mailbox)&Builders<Conversation>.Filter.Eq(x=>x.AutoReplyCheckedAt,null)&Builders<Conversation>.Filter.Gte(x=>x.ReceivedAt,since)).SortBy(x=>x.ReceivedAt).Limit(100).ToListAsync();
public async Task<bool> TryAutoReplyClaim(AutoReplyClaim claim)
{
try{await Insert(claim);return true;}catch(MongoWriteException ex) when(ex.WriteError.Category==ServerErrorCategory.DuplicateKey){return false;}
}
public async Task<bool> TryInsertPayment(PaymentRequest payment)
{
try { await Insert(payment);return true; }
@@ -116,6 +125,11 @@ public sealed class MongoStore : IStore
// Explicit Development-only preview store. Production never falls back to this.
public sealed class PreviewStore : IStore
{
public async Task<List<Conversation>> AutoReplyCandidates(string hotel,string mailbox,DateTime since)=>(await List<Conversation>(hotel)).Where(x=>x.MailboxId==mailbox&&x.AutoReplyCheckedAt==null&&x.ReceivedAt>=since).OrderBy(x=>x.ReceivedAt).Take(100).ToList();
public Task<bool> TryAutoReplyClaim(AutoReplyClaim claim)
{
lock(gate){if(rows.Where(x=>x.Key.StartsWith("AutoReplyClaim:")).Select(x=>Clone<AutoReplyClaim>(x.Value)).Any(x=>x.HotelId==claim.HotelId&&(x.ThreadKey==claim.ThreadKey||x.RecipientDay==claim.RecipientDay||x.DaySlot==claim.DaySlot)))return Task.FromResult(false);return Task.FromResult(rows.TryAdd(Key<AutoReplyClaim>(claim.Id),Json(claim)));}
}
public Task<bool> TryInsertPayment(PaymentRequest payment)
{
lock(gate)