diff --git a/src/Core/Repositories/EntityFramework/OrganizationRepository.cs b/src/Core/Repositories/EntityFramework/OrganizationRepository.cs index 0aaae9a474..09ad7bf93b 100644 --- a/src/Core/Repositories/EntityFramework/OrganizationRepository.cs +++ b/src/Core/Repositories/EntityFramework/OrganizationRepository.cs @@ -49,10 +49,28 @@ namespace Bit.Core.Repositories.EntityFramework } } - public Task UpdateStorageAsync(Guid id) + public async Task UpdateStorageAsync(Guid id) { - // TODO - return Task.FromResult(0); + using(var scope = ServiceScopeFactory.CreateScope()) + { + var dbContext = GetDatabaseContext(scope); + var ciphers = await dbContext.Ciphers + .Where(e => e.UserId == null && e.OrganizationId == id).ToListAsync(); + var storage = ciphers.Sum(e => e.AttachmentsJson?.RootElement.EnumerateArray() + .Sum(p => p.GetProperty("Size").GetInt64()) ?? 0); + var organization = new EFModel.Organization + { + Id = id, + RevisionDate = DateTime.UtcNow, + Storage = storage, + }; + var set = GetDbSet(dbContext); + set.Attach(organization); + var entry = dbContext.Entry(organization); + entry.Property(e => e.RevisionDate).IsModified = true; + entry.Property(e => e.Storage).IsModified = true; + await dbContext.SaveChangesAsync(); + } } public async Task> GetManyAbilitiesAsync() diff --git a/src/Core/Repositories/EntityFramework/UserRepository.cs b/src/Core/Repositories/EntityFramework/UserRepository.cs index 61ad51ff8e..770176cad1 100644 --- a/src/Core/Repositories/EntityFramework/UserRepository.cs +++ b/src/Core/Repositories/EntityFramework/UserRepository.cs @@ -83,10 +83,27 @@ namespace Bit.Core.Repositories.EntityFramework } } - public Task UpdateStorageAsync(Guid id) + public async Task UpdateStorageAsync(Guid id) { - // TODO - return Task.FromResult(0); + using(var scope = ServiceScopeFactory.CreateScope()) + { + var dbContext = GetDatabaseContext(scope); + var ciphers = await dbContext.Ciphers.Where(e => e.UserId == id).ToListAsync(); + var storage = ciphers.Sum(e => e.AttachmentsJson?.RootElement.EnumerateArray() + .Sum(p => p.GetProperty("Size").GetInt64()) ?? 0); + var user = new EFModel.User + { + Id = id, + RevisionDate = DateTime.UtcNow, + Storage = storage, + }; + var set = GetDbSet(dbContext); + set.Attach(user); + var entry = dbContext.Entry(user); + entry.Property(e => e.RevisionDate).IsModified = true; + entry.Property(e => e.Storage).IsModified = true; + await dbContext.SaveChangesAsync(); + } } public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)