Add hotel team invitations, assisted account recovery and onboarding
This commit is contained in:
@@ -13,6 +13,9 @@ public interface IStore
|
||||
Task<bool> Replace<T>(string hotel, string id, long version, T document) where T : TenantDocument;
|
||||
Task Delete<T>(string hotel, string id) where T : TenantDocument;
|
||||
Task<StaffUser?> FindLogin(string email);
|
||||
Task<StaffUser?> FindAccountLink(string hash);
|
||||
Task<bool> TryInsertStaff(StaffUser user);
|
||||
Task<bool> ConsumeAccountLink(StaffUser user,long version,string hash);
|
||||
Task<OAuthRequest?> ConsumeOAuth(string id, string hotel, string user);
|
||||
Task<List<Mailbox>> Mailboxes();
|
||||
Task SaveMailbox(Mailbox mailbox);
|
||||
@@ -45,6 +48,7 @@ public sealed class MongoStore : IStore
|
||||
}
|
||||
public async Task Initialize()
|
||||
{
|
||||
await Collection<StaffUser>().Indexes.CreateOneAsync(new CreateIndexModel<StaffUser>(Builders<StaffUser>.IndexKeys.Ascending(x=>x.AccountLinkHash)));
|
||||
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}));
|
||||
@@ -76,10 +80,19 @@ public sealed class MongoStore : IStore
|
||||
public async Task<bool> Replace<T>(string hotel, string id, long version, T document) where T : TenantDocument
|
||||
{
|
||||
if (document.HotelId != hotel || document.Id != id) throw new InvalidOperationException("Invalid document scope.");
|
||||
var result = await Collection<T>().ReplaceOneAsync(Scope<T>(hotel) & Builders<T>.Filter.Eq(x => x.Id, id) & Builders<T>.Filter.Eq("Version", version), document);
|
||||
var versionFilter=Builders<T>.Filter.Eq("Version",version);
|
||||
if(typeof(T)==typeof(StaffUser)&&version==0)versionFilter|=Builders<T>.Filter.Exists("Version",false);
|
||||
var result = await Collection<T>().ReplaceOneAsync(Scope<T>(hotel) & Builders<T>.Filter.Eq(x => x.Id, id) & versionFilter, document);
|
||||
return result.ModifiedCount == 1;
|
||||
}
|
||||
public async Task Delete<T>(string hotel, string id) where T : TenantDocument => await Collection<T>().DeleteOneAsync(Scope<T>(hotel) & Builders<T>.Filter.Eq(x => x.Id, id));
|
||||
public async Task<StaffUser?> FindAccountLink(string hash)=>await Collection<StaffUser>().Find(x=>x.AccountLinkHash==hash&&x.AccountLinkExpiresAt>DateTime.UtcNow).FirstOrDefaultAsync();
|
||||
public async Task<bool> TryInsertStaff(StaffUser user){try{await Insert(user);return true;}catch(MongoWriteException ex) when(ex.WriteError.Category==ServerErrorCategory.DuplicateKey){return false;}}
|
||||
public async Task<bool> ConsumeAccountLink(StaffUser user,long version,string hash)
|
||||
{
|
||||
var filter=Scope<StaffUser>(user.HotelId)&Builders<StaffUser>.Filter.Eq(x=>x.Id,user.Id)&Builders<StaffUser>.Filter.Eq(x=>x.Version,version)&Builders<StaffUser>.Filter.Eq(x=>x.AccountLinkHash,hash)&Builders<StaffUser>.Filter.Gt(x=>x.AccountLinkExpiresAt,DateTime.UtcNow);
|
||||
return (await Collection<StaffUser>().ReplaceOneAsync(filter,user)).ModifiedCount==1;
|
||||
}
|
||||
public async Task<StaffUser?> FindLogin(string email) => await Collection<StaffUser>().Find(x => x.Email == email).FirstOrDefaultAsync();
|
||||
public async Task<OAuthRequest?> ConsumeOAuth(string id, string hotel, string user) => await Collection<OAuthRequest>().FindOneAndDeleteAsync(x => x.Id == id && x.HotelId == hotel && x.UserId == user && x.ExpiresAt > DateTime.UtcNow);
|
||||
public Task<List<Mailbox>> Mailboxes() => Collection<Mailbox>().Find(x => x.Status == "Connected").ToListAsync();
|
||||
@@ -167,6 +180,12 @@ public sealed class PreviewStore : IStore
|
||||
}
|
||||
}
|
||||
public async Task Delete<T>(string hotel, string id) where T : TenantDocument { if (await Get<T>(hotel, id) != null) rows.TryRemove(Key<T>(id), out _); }
|
||||
public Task<StaffUser?> FindAccountLink(string hash)=>Task.FromResult(rows.Where(x=>x.Key.StartsWith("StaffUser:")).Select(x=>Clone<StaffUser>(x.Value)).SingleOrDefault(x=>x.AccountLinkHash==hash&&x.AccountLinkExpiresAt>DateTime.UtcNow));
|
||||
public Task<bool> TryInsertStaff(StaffUser user){lock(gate){if(rows.Where(x=>x.Key.StartsWith("StaffUser:")).Select(x=>Clone<StaffUser>(x.Value)).Any(x=>x.Email==user.Email))return Task.FromResult(false);return Task.FromResult(rows.TryAdd(Key<StaffUser>(user.Id),Json(user)));}}
|
||||
public Task<bool> ConsumeAccountLink(StaffUser user,long version,string hash)
|
||||
{
|
||||
lock(gate){if(!rows.TryGetValue(Key<StaffUser>(user.Id),out var raw))return Task.FromResult(false);var old=Clone<StaffUser>(raw);if(old.HotelId!=user.HotelId||old.Version!=version||old.AccountLinkHash!=hash||old.AccountLinkExpiresAt<=DateTime.UtcNow||old.AccountLinkExpiresAt==null)return Task.FromResult(false);rows[Key<StaffUser>(user.Id)]=Json(user);return Task.FromResult(true);}
|
||||
}
|
||||
public Task<StaffUser?> FindLogin(string email) => Task.FromResult(rows.Where(x => x.Key.StartsWith("StaffUser:")).Select(x => Clone<StaffUser>(x.Value)).SingleOrDefault(x => x.Email == email));
|
||||
public Task<OAuthRequest?> ConsumeOAuth(string id, string hotel, string user) { lock(gate) { var x = rows.TryGetValue(Key<OAuthRequest>(id), out var raw) ? Clone<OAuthRequest>(raw) : null; if(x?.HotelId != hotel || x.UserId != user || x.ExpiresAt <= DateTime.UtcNow) return Task.FromResult<OAuthRequest?>(null); rows.TryRemove(Key<OAuthRequest>(id),out _); return Task.FromResult<OAuthRequest?>(x); } }
|
||||
public Task<List<Mailbox>> Mailboxes() => Task.FromResult(new List<Mailbox>());
|
||||
|
||||
Reference in New Issue
Block a user