mirror of
https://github.com/bitwarden/server
synced 2025-12-25 04:33:26 +00:00
Organization report tables, repos, services, and endpoints (#6158)
* PM-23754 initial commit * pm-23754 fixing controller tests * pm-23754 adding commands and queries * pm-23754 adding endpoints, command/queries, repositories, and sql migrations * pm-23754 add new sql scripts * PM-23754 adding sql scripts * pm-23754 * PM-23754 fixing migration script * PM-23754 fixing migration script again * PM-23754 fixing migration script validation * PM-23754 fixing db validation script issue * PM-23754 fixing endpoint and db validation * PM-23754 fixing unit tests * PM-23754 fixing implementation based on comments and tests * PM-23754 updating logging statements * PM-23754 making changes based on PR comments. * updating migration scripts * removing old migration files * update code based testing for whole data object for OrganizationReport and add a stored procedure. * updating services, unit tests, repository tests * fixing unit tests * fixing migration script * fixing migration script again * fixing migration script * another fix * fixing sql file, updating controller to account for different orgIds in the url and body. * updating error message in controllers without a body * making a change to the command * Refactor ReportsController by removing organization reports The IDropOrganizationReportCommand is no longer needed * will code based on PR comments. * fixing unit test * fixing migration script based on last changes. * adding another check in endpoint and adding unit tests * fixing route parameter. * PM-23754 updating data fields to return just the column * PM-23754 fixing repository method signatures * PM-23754 making change to orgId parameter through out code to align with api naming --------- Co-authored-by: Tom <144813356+ttalty@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
using AutoMapper;
|
||||
using Bit.Core.Dirt.Entities;
|
||||
using Bit.Core.Dirt.Models.Data;
|
||||
using Bit.Core.Dirt.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using LinqToDB;
|
||||
@@ -19,18 +20,6 @@ public class OrganizationReportRepository :
|
||||
IMapper mapper) : base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationReports)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<OrganizationReport>> GetByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.OrganizationReports
|
||||
.Where(p => p.OrganizationId == organizationId)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<ICollection<OrganizationReport>>(results);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReport> GetLatestByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
@@ -38,14 +27,161 @@ public class OrganizationReportRepository :
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var result = await dbContext.OrganizationReports
|
||||
.Where(p => p.OrganizationId == organizationId)
|
||||
.OrderByDescending(p => p.Date)
|
||||
.OrderByDescending(p => p.RevisionDate)
|
||||
.Take(1)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (result == null)
|
||||
return default;
|
||||
if (result == null) return default;
|
||||
|
||||
return Mapper.Map<OrganizationReport>(result);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReport> UpdateSummaryDataAsync(Guid organizationId, Guid reportId, string summaryData)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
// Update only SummaryData and RevisionDate
|
||||
await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId && p.OrganizationId == organizationId)
|
||||
.UpdateAsync(p => new Models.OrganizationReport
|
||||
{
|
||||
SummaryData = summaryData,
|
||||
RevisionDate = DateTime.UtcNow
|
||||
});
|
||||
|
||||
// Return the updated report
|
||||
var updatedReport = await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return Mapper.Map<OrganizationReport>(updatedReport);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReportSummaryDataResponse> GetSummaryDataAsync(Guid reportId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var result = await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId)
|
||||
.Select(p => new OrganizationReportSummaryDataResponse
|
||||
{
|
||||
SummaryData = p.SummaryData
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrganizationReportSummaryDataResponse>> GetSummaryDataByDateRangeAsync(
|
||||
Guid organizationId,
|
||||
DateTime startDate,
|
||||
DateTime endDate)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var results = await dbContext.OrganizationReports
|
||||
.Where(p => p.OrganizationId == organizationId &&
|
||||
p.CreationDate >= startDate && p.CreationDate <= endDate)
|
||||
.Select(p => new OrganizationReportSummaryDataResponse
|
||||
{
|
||||
SummaryData = p.SummaryData
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReportDataResponse> GetReportDataAsync(Guid reportId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var result = await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId)
|
||||
.Select(p => new OrganizationReportDataResponse
|
||||
{
|
||||
ReportData = p.ReportData
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReport> UpdateReportDataAsync(Guid organizationId, Guid reportId, string reportData)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
// Update only ReportData and RevisionDate
|
||||
await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId && p.OrganizationId == organizationId)
|
||||
.UpdateAsync(p => new Models.OrganizationReport
|
||||
{
|
||||
ReportData = reportData,
|
||||
RevisionDate = DateTime.UtcNow
|
||||
});
|
||||
|
||||
// Return the updated report
|
||||
var updatedReport = await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return Mapper.Map<OrganizationReport>(updatedReport);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReportApplicationDataResponse> GetApplicationDataAsync(Guid reportId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var result = await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId)
|
||||
.Select(p => new OrganizationReportApplicationDataResponse
|
||||
{
|
||||
ApplicationData = p.ApplicationData
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationReport> UpdateApplicationDataAsync(Guid organizationId, Guid reportId, string applicationData)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
// Update only ApplicationData and RevisionDate
|
||||
await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId && p.OrganizationId == organizationId)
|
||||
.UpdateAsync(p => new Models.OrganizationReport
|
||||
{
|
||||
ApplicationData = applicationData,
|
||||
RevisionDate = DateTime.UtcNow
|
||||
});
|
||||
|
||||
// Return the updated report
|
||||
var updatedReport = await dbContext.OrganizationReports
|
||||
.Where(p => p.Id == reportId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return Mapper.Map<OrganizationReport>(updatedReport);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user