1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

calculate storage updates

This commit is contained in:
Kyle Spearrin 2020-01-10 20:05:58 -05:00
parent 5bb440563f
commit bc0901348b
2 changed files with 41 additions and 6 deletions

View File

@ -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<ICollection<DataModel.OrganizationAbility>> GetManyAbilitiesAsync()

View File

@ -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)