1
0
mirror of https://github.com/bitwarden/server synced 2025-12-06 00:03:34 +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:
Graham Walker
2025-09-08 15:06:13 -05:00
committed by GitHub
parent cb0d5a5ba6
commit 226f274a72
79 changed files with 24744 additions and 1047 deletions

View File

@@ -0,0 +1,96 @@
IF EXISTS (
SELECT * FROM sys.indexes WHERE name = 'IX_OrganizationReport_OrganizationId_Date'
AND object_id = OBJECT_ID('dbo.OrganizationReport')
)
BEGIN
DROP INDEX [IX_OrganizationReport_OrganizationId_Date] ON [dbo].[OrganizationReport];
END
GO
IF COL_LENGTH('[dbo].[OrganizationReport]', 'Date') IS NOT NULL
BEGIN
ALTER TABLE [dbo].[OrganizationReport]
DROP COLUMN [Date];
END
GO
IF OBJECT_ID('dbo.OrganizationReport') IS NOT NULL
BEGIN
ALTER TABLE [dbo].[OrganizationReport]
ADD [SummaryData] NVARCHAR(MAX) NULL,
[ApplicationData] NVARCHAR(MAX) NULL,
[RevisionDate] DATETIME2 (7) NULL;
END
GO
IF NOT EXISTS (
SELECT * FROM sys.indexes WHERE name = 'IX_OrganizationReport_OrganizationId_RevisionDate'
AND object_id = OBJECT_ID('dbo.OrganizationReport')
)
BEGIN
CREATE NONCLUSTERED INDEX [IX_OrganizationReport_OrganizationId_RevisionDate]
ON [dbo].[OrganizationReport]([OrganizationId] ASC, [RevisionDate] DESC);
END
GO
IF OBJECT_ID('dbo.OrganizationReportView') IS NOT NULL
BEGIN
DROP VIEW [dbo].[OrganizationReportView];
END
GO
IF OBJECT_ID('dbo.OrganizationReportView') IS NULL
BEGIN
EXEC('CREATE VIEW [dbo].[OrganizationReportView]
AS
SELECT
*
FROM
[dbo].[OrganizationReport]');
END
GO
IF OBJECT_ID('dbo.OrganizationReport_Create') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[OrganizationReport_Create];
END
GO
IF OBJECT_ID('dbo.OrganizationReport_Create') IS NULL
BEGIN
EXEC('CREATE PROCEDURE [dbo].[OrganizationReport_Create]
@Id UNIQUEIDENTIFIER OUTPUT,
@OrganizationId UNIQUEIDENTIFIER,
@ReportData NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@ContentEncryptionKey VARCHAR(MAX),
@SummaryData NVARCHAR(MAX),
@ApplicationData NVARCHAR(MAX),
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[OrganizationReport](
[Id],
[OrganizationId],
[ReportData],
[CreationDate],
[ContentEncryptionKey],
[SummaryData],
[ApplicationData],
[RevisionDate]
)
VALUES (
@Id,
@OrganizationId,
@ReportData,
@CreationDate,
@ContentEncryptionKey,
@SummaryData,
@ApplicationData,
@RevisionDate
);
END');
END
GO

View File

@@ -0,0 +1,156 @@
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_GetLatestByOrganizationId]
@OrganizationId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT TOP 1
[Id],
[OrganizationId],
[ReportData],
[CreationDate],
[ContentEncryptionKey],
[SummaryData],
[ApplicationData],
[RevisionDate]
FROM [dbo].[OrganizationReportView]
WHERE [OrganizationId] = @OrganizationId
ORDER BY [RevisionDate] DESC
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_GetSummariesByDateRange]
@OrganizationId UNIQUEIDENTIFIER,
@StartDate DATETIME2(7),
@EndDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
SELECT
[SummaryData]
FROM [dbo].[OrganizationReportView]
WHERE [OrganizationId] = @OrganizationId
AND [RevisionDate] >= @StartDate
AND [RevisionDate] <= @EndDate
ORDER BY [RevisionDate] DESC
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_GetSummaryDataById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
[SummaryData]
FROM [dbo].[OrganizationReportView]
WHERE [Id] = @Id
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_UpdateSummaryData]
@Id UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@SummaryData NVARCHAR(MAX),
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[OrganizationReport]
SET
[SummaryData] = @SummaryData,
[RevisionDate] = @RevisionDate
WHERE [Id] = @Id
AND [OrganizationId] = @OrganizationId;
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_GetReportDataById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
[ReportData]
FROM [dbo].[OrganizationReportView]
WHERE [Id] = @Id
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_UpdateReportData]
@Id UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@ReportData NVARCHAR(MAX),
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[OrganizationReport]
SET
[ReportData] = @ReportData,
[RevisionDate] = @RevisionDate
WHERE [Id] = @Id
AND [OrganizationId] = @OrganizationId;
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_GetApplicationDataById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
[ApplicationData]
FROM [dbo].[OrganizationReportView]
WHERE [Id] = @Id;
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_UpdateApplicationData]
@Id UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@ApplicationData NVARCHAR(MAX),
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[OrganizationReport]
SET
[ApplicationData] = @ApplicationData,
[RevisionDate] = @RevisionDate
WHERE [Id] = @Id
AND [OrganizationId] = @OrganizationId;
END
GO
CREATE OR ALTER PROCEDURE [dbo].[OrganizationReport_Update]
@Id UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@ReportData NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@ContentEncryptionKey VARCHAR(MAX),
@SummaryData NVARCHAR(MAX),
@ApplicationData NVARCHAR(MAX),
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[OrganizationReport]
SET
[OrganizationId] = @OrganizationId,
[ReportData] = @ReportData,
[CreationDate] = @CreationDate,
[ContentEncryptionKey] = @ContentEncryptionKey,
[SummaryData] = @SummaryData,
[ApplicationData] = @ApplicationData,
[RevisionDate] = @RevisionDate
WHERE [Id] = @Id;
END
GO

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,49 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bit.MySqlMigrations.Migrations;
/// <inheritdoc />
public partial class _20250822_00_AlterOrganizationReport : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "Date",
table: "OrganizationReport",
newName: "RevisionDate");
migrationBuilder.AddColumn<string>(
name: "ApplicationData",
table: "OrganizationReport",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "SummaryData",
table: "OrganizationReport",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ApplicationData",
table: "OrganizationReport");
migrationBuilder.DropColumn(
name: "SummaryData",
table: "OrganizationReport");
migrationBuilder.RenameColumn(
name: "RevisionDate",
table: "OrganizationReport",
newName: "Date");
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bit.MySqlMigrations.Migrations;
/// <inheritdoc />
public partial class _20250822_01_AddOrganizationReportStoredProcedures : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

View File

@@ -1011,6 +1011,9 @@ namespace Bit.MySqlMigrations.Migrations
b.Property<Guid>("Id")
.HasColumnType("char(36)");
b.Property<string>("ApplicationData")
.HasColumnType("longtext");
b.Property<string>("ContentEncryptionKey")
.IsRequired()
.HasColumnType("longtext");
@@ -1018,9 +1021,6 @@ namespace Bit.MySqlMigrations.Migrations
b.Property<DateTime>("CreationDate")
.HasColumnType("datetime(6)");
b.Property<DateTime>("Date")
.HasColumnType("datetime(6)");
b.Property<Guid>("OrganizationId")
.HasColumnType("char(36)");
@@ -1028,6 +1028,12 @@ namespace Bit.MySqlMigrations.Migrations
.IsRequired()
.HasColumnType("longtext");
b.Property<DateTime>("RevisionDate")
.HasColumnType("datetime(6)");
b.Property<string>("SummaryData")
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("Id")

View File

@@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bit.PostgresMigrations.Migrations;
/// <inheritdoc />
public partial class _20250822_00_AlterOrganizationReport : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "Date",
table: "OrganizationReport",
newName: "RevisionDate");
migrationBuilder.AddColumn<string>(
name: "ApplicationData",
table: "OrganizationReport",
type: "text",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "SummaryData",
table: "OrganizationReport",
type: "text",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ApplicationData",
table: "OrganizationReport");
migrationBuilder.DropColumn(
name: "SummaryData",
table: "OrganizationReport");
migrationBuilder.RenameColumn(
name: "RevisionDate",
table: "OrganizationReport",
newName: "Date");
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bit.PostgresMigrations.Migrations;
/// <inheritdoc />
public partial class _20250822_01_AddOrganizationReportStoredProcedures : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

View File

@@ -1016,6 +1016,9 @@ namespace Bit.PostgresMigrations.Migrations
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<string>("ApplicationData")
.HasColumnType("text");
b.Property<string>("ContentEncryptionKey")
.IsRequired()
.HasColumnType("text");
@@ -1023,9 +1026,6 @@ namespace Bit.PostgresMigrations.Migrations
b.Property<DateTime>("CreationDate")
.HasColumnType("timestamp with time zone");
b.Property<DateTime>("Date")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("OrganizationId")
.HasColumnType("uuid");
@@ -1033,6 +1033,12 @@ namespace Bit.PostgresMigrations.Migrations
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("RevisionDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("SummaryData")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("Id")

View File

@@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bit.SqliteMigrations.Migrations;
/// <inheritdoc />
public partial class _20250822_00_AlterOrganizationReport : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "Date",
table: "OrganizationReport",
newName: "RevisionDate");
migrationBuilder.AddColumn<string>(
name: "ApplicationData",
table: "OrganizationReport",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "SummaryData",
table: "OrganizationReport",
type: "TEXT",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ApplicationData",
table: "OrganizationReport");
migrationBuilder.DropColumn(
name: "SummaryData",
table: "OrganizationReport");
migrationBuilder.RenameColumn(
name: "RevisionDate",
table: "OrganizationReport",
newName: "Date");
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bit.SqliteMigrations.Migrations;
/// <inheritdoc />
public partial class _20250822_01_AddOrganizationReportStoredProcedures : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

View File

@@ -1000,6 +1000,9 @@ namespace Bit.SqliteMigrations.Migrations
b.Property<Guid>("Id")
.HasColumnType("TEXT");
b.Property<string>("ApplicationData")
.HasColumnType("TEXT");
b.Property<string>("ContentEncryptionKey")
.IsRequired()
.HasColumnType("TEXT");
@@ -1007,9 +1010,6 @@ namespace Bit.SqliteMigrations.Migrations
b.Property<DateTime>("CreationDate")
.HasColumnType("TEXT");
b.Property<DateTime>("Date")
.HasColumnType("TEXT");
b.Property<Guid>("OrganizationId")
.HasColumnType("TEXT");
@@ -1017,6 +1017,12 @@ namespace Bit.SqliteMigrations.Migrations
.IsRequired()
.HasColumnType("TEXT");
b.Property<DateTime>("RevisionDate")
.HasColumnType("TEXT");
b.Property<string>("SummaryData")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("Id")