From 7139effa945421b2c41e7a6f9877e31d3264895e Mon Sep 17 00:00:00 2001 From: Matt Bishop Date: Mon, 7 Apr 2025 07:20:18 -0700 Subject: [PATCH] Organization integration database / repository logic (#5602) * Organization integration creation, update, and deletion database logic * Additional procs and entity tweaks * Use check * Couple newlines * Forgot to script the two org procs --- .../Entities/OrganizationIntegration.cs | 2 +- .../OrganizationIntegrationConfiguration.cs | 2 +- ...ionConfigurationEntityTypeConfiguration.cs | 2 +- ...ationIntegrationEntityTypeConfiguration.cs | 6 +- .../Repositories/OrganizationRepository.cs | 2 + ...izationIntegrationConfiguration_Create.sql | 33 ++ ...ionIntegrationConfiguration_DeleteById.sql | 12 + ...ationIntegrationConfiguration_ReadById.sql | 13 + ...izationIntegrationConfiguration_Update.sql | 24 ++ .../OrganizationIntegration_Create.sql | 30 ++ .../OrganizationIntegration_DeleteById.sql | 12 + ...izationIntegration_OrganizationDeleted.sql | 12 + .../OrganizationIntegration_ReadById.sql | 13 + .../OrganizationIntegration_Update.sql | 22 ++ .../Organization_DeleteById.sql | 15 +- .../OrganizationIntegrationConfiguration.sql | 2 +- ...25-04-03_00_OrganizationIntegrationCUD.sql | 351 ++++++++++++++++++ 17 files changed, 539 insertions(+), 14 deletions(-) create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Create.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_DeleteById.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_ReadById.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Update.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegration_Create.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegration_DeleteById.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegration_OrganizationDeleted.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegration_ReadById.sql create mode 100644 src/Sql/dbo/Stored Procedures/OrganizationIntegration_Update.sql create mode 100644 util/Migrator/DbScripts/2025-04-03_00_OrganizationIntegrationCUD.sql diff --git a/src/Core/AdminConsole/Entities/OrganizationIntegration.cs b/src/Core/AdminConsole/Entities/OrganizationIntegration.cs index 18f8be8667..86de25ce9a 100644 --- a/src/Core/AdminConsole/Entities/OrganizationIntegration.cs +++ b/src/Core/AdminConsole/Entities/OrganizationIntegration.cs @@ -12,7 +12,7 @@ public class OrganizationIntegration : ITableObject public Guid OrganizationId { get; set; } public IntegrationType Type { get; set; } public string? Configuration { get; set; } - public DateTime CreationDate { get; set; } = DateTime.UtcNow; + public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; public DateTime RevisionDate { get; set; } = DateTime.UtcNow; public void SetNewId() => Id = CoreHelpers.GenerateComb(); } diff --git a/src/Core/AdminConsole/Entities/OrganizationIntegrationConfiguration.cs b/src/Core/AdminConsole/Entities/OrganizationIntegrationConfiguration.cs index 7592d0c763..25b669622f 100644 --- a/src/Core/AdminConsole/Entities/OrganizationIntegrationConfiguration.cs +++ b/src/Core/AdminConsole/Entities/OrganizationIntegrationConfiguration.cs @@ -13,7 +13,7 @@ public class OrganizationIntegrationConfiguration : ITableObject public EventType EventType { get; set; } public string? Configuration { get; set; } public string? Template { get; set; } - public DateTime CreationDate { get; set; } = DateTime.UtcNow; + public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; public DateTime RevisionDate { get; set; } = DateTime.UtcNow; public void SetNewId() => Id = CoreHelpers.GenerateComb(); } diff --git a/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationConfigurationEntityTypeConfiguration.cs b/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationConfigurationEntityTypeConfiguration.cs index 29712f5e38..a5df0845bd 100644 --- a/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationConfigurationEntityTypeConfiguration.cs +++ b/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationConfigurationEntityTypeConfiguration.cs @@ -9,7 +9,7 @@ public class OrganizationIntegrationConfigurationEntityTypeConfiguration : IEnti public void Configure(EntityTypeBuilder builder) { builder - .Property(p => p.Id) + .Property(oic => oic.Id) .ValueGeneratedNever(); builder.ToTable(nameof(OrganizationIntegrationConfiguration)); diff --git a/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationEntityTypeConfiguration.cs b/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationEntityTypeConfiguration.cs index c2134c1b7d..3434d735d0 100644 --- a/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationEntityTypeConfiguration.cs +++ b/src/Infrastructure.EntityFramework/AdminConsole/Configurations/OrganizationIntegrationEntityTypeConfiguration.cs @@ -9,15 +9,15 @@ public class OrganizationIntegrationEntityTypeConfiguration : IEntityTypeConfigu public void Configure(EntityTypeBuilder builder) { builder - .Property(p => p.Id) + .Property(oi => oi.Id) .ValueGeneratedNever(); builder - .HasIndex(p => p.OrganizationId) + .HasIndex(oi => oi.OrganizationId) .IsClustered(false); builder - .HasIndex(p => new { p.OrganizationId, p.Type }) + .HasIndex(oi => new { oi.OrganizationId, oi.Type }) .IsUnique() .IsClustered(false); diff --git a/src/Infrastructure.EntityFramework/AdminConsole/Repositories/OrganizationRepository.cs b/src/Infrastructure.EntityFramework/AdminConsole/Repositories/OrganizationRepository.cs index c095b07030..a5f4f0bd9d 100644 --- a/src/Infrastructure.EntityFramework/AdminConsole/Repositories/OrganizationRepository.cs +++ b/src/Infrastructure.EntityFramework/AdminConsole/Repositories/OrganizationRepository.cs @@ -199,6 +199,8 @@ public class OrganizationRepository : Repository po.OrganizationId == organization.Id) .ExecuteDeleteAsync(); + await dbContext.OrganizationIntegrations.Where(oi => oi.OrganizationId == organization.Id) + .ExecuteDeleteAsync(); await dbContext.GroupServiceAccountAccessPolicy.Where(ap => ap.GrantedServiceAccount.OrganizationId == organization.Id) .ExecuteDeleteAsync(); diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Create.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Create.sql new file mode 100644 index 0000000000..18160c9ea9 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Create.sql @@ -0,0 +1,33 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegrationConfiguration_Create] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationIntegrationId UNIQUEIDENTIFIER, + @EventType SMALLINT, + @Configuration VARCHAR(MAX), + @Template VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[OrganizationIntegrationConfiguration] + ( + [Id], + [OrganizationIntegrationId], + [EventType], + [Configuration], + [Template], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @OrganizationIntegrationId, + @EventType, + @Configuration, + @Template, + @CreationDate, + @RevisionDate + ) +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_DeleteById.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_DeleteById.sql new file mode 100644 index 0000000000..6e3c9ab83c --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_DeleteById.sql @@ -0,0 +1,12 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegrationConfiguration_DeleteById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[OrganizationIntegrationConfiguration] + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_ReadById.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_ReadById.sql new file mode 100644 index 0000000000..2db9ba3bf8 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_ReadById.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegrationConfiguration_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[OrganizationIntegrationConfiguration] + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Update.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Update.sql new file mode 100644 index 0000000000..b613774d04 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegrationConfiguration_Update.sql @@ -0,0 +1,24 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegrationConfiguration_Update] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationIntegrationId UNIQUEIDENTIFIER, + @EventType SMALLINT, + @Configuration VARCHAR(MAX), + @Template VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[OrganizationIntegrationConfiguration] + SET + [OrganizationIntegrationId] = @OrganizationIntegrationId, + [EventType] = @EventType, + [Configuration] = @Configuration, + [Template] = @Template, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegration_Create.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_Create.sql new file mode 100644 index 0000000000..fe8ed7559b --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_Create.sql @@ -0,0 +1,30 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegration_Create] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationId UNIQUEIDENTIFIER, + @Type SMALLINT, + @Configuration VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[OrganizationIntegration] + ( + [Id], + [OrganizationId], + [Type], + [Configuration], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @OrganizationId, + @Type, + @Configuration, + @CreationDate, + @RevisionDate + ) +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegration_DeleteById.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_DeleteById.sql new file mode 100644 index 0000000000..2ade1c074e --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_DeleteById.sql @@ -0,0 +1,12 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegration_DeleteById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[OrganizationIntegration] + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegration_OrganizationDeleted.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_OrganizationDeleted.sql new file mode 100644 index 0000000000..2cdd77edc2 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_OrganizationDeleted.sql @@ -0,0 +1,12 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegration_OrganizationDeleted] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[OrganizationIntegration] + WHERE + [OrganizationId] = @OrganizationId +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegration_ReadById.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_ReadById.sql new file mode 100644 index 0000000000..352c26bb24 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_ReadById.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegration_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[OrganizationIntegration] + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/OrganizationIntegration_Update.sql b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_Update.sql new file mode 100644 index 0000000000..8dd91cc3d7 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/OrganizationIntegration_Update.sql @@ -0,0 +1,22 @@ +CREATE PROCEDURE [dbo].[OrganizationIntegration_Update] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationId UNIQUEIDENTIFIER, + @Type SMALLINT, + @Configuration VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[OrganizationIntegration] + SET + [OrganizationId] = @OrganizationId, + [Type] = @Type, + [Configuration] = @Configuration, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql index e0b7d47469..2daa12209f 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql @@ -45,11 +45,11 @@ BEGIN [OrganizationId] = @Id DELETE CU - FROM + FROM [dbo].[CollectionUser] CU - INNER JOIN + INNER JOIN [dbo].[OrganizationUser] OU ON [CU].[OrganizationUserId] = [OU].[Id] - WHERE + WHERE [OU].[OrganizationId] = @Id DELETE AP @@ -69,9 +69,9 @@ BEGIN [OU].[OrganizationId] = @Id DELETE - FROM + FROM [dbo].[OrganizationUser] - WHERE + WHERE [OrganizationId] = @Id DELETE @@ -84,6 +84,7 @@ BEGIN EXEC [dbo].[OrganizationConnection_OrganizationDeleted] @Id EXEC [dbo].[OrganizationSponsorship_OrganizationDeleted] @Id EXEC [dbo].[OrganizationDomain_OrganizationDeleted] @Id + EXEC [dbo].[OrganizationIntegration_OrganizationDeleted] @Id DELETE FROM @@ -128,14 +129,14 @@ BEGIN [dbo].[Notification] N ON N.[Id] = NS.[NotificationId] WHERE N.[OrganizationId] = @Id - + -- Delete Notification DELETE FROM [dbo].[Notification] WHERE [OrganizationId] = @Id - + DELETE FROM [dbo].[Organization] diff --git a/src/Sql/dbo/Tables/OrganizationIntegrationConfiguration.sql b/src/Sql/dbo/Tables/OrganizationIntegrationConfiguration.sql index 9dbb2341a7..28283f8015 100644 --- a/src/Sql/dbo/Tables/OrganizationIntegrationConfiguration.sql +++ b/src/Sql/dbo/Tables/OrganizationIntegrationConfiguration.sql @@ -8,6 +8,6 @@ CREATE TABLE [dbo].[OrganizationIntegrationConfiguration] [CreationDate] DATETIME2 (7) NOT NULL, [RevisionDate] DATETIME2 (7) NOT NULL, CONSTRAINT [PK_OrganizationIntegrationConfiguration] PRIMARY KEY CLUSTERED ([Id] ASC), - CONSTRAINT [FK_OrganizationIntegrationConfiguration_OrganizationIntegration] FOREIGN KEY ([OrganizationIntegrationId]) REFERENCES [dbo].[OrganizationIntegration] ([Id]) + CONSTRAINT [FK_OrganizationIntegrationConfiguration_OrganizationIntegration] FOREIGN KEY ([OrganizationIntegrationId]) REFERENCES [dbo].[OrganizationIntegration] ([Id]) ON DELETE CASCADE ); GO diff --git a/util/Migrator/DbScripts/2025-04-03_00_OrganizationIntegrationCUD.sql b/util/Migrator/DbScripts/2025-04-03_00_OrganizationIntegrationCUD.sql new file mode 100644 index 0000000000..35a8a1442d --- /dev/null +++ b/util/Migrator/DbScripts/2025-04-03_00_OrganizationIntegrationCUD.sql @@ -0,0 +1,351 @@ +-- Configure FK to cascade on delete +IF EXISTS(SELECT * +FROM information_schema.table_constraints +WHERE table_name='OrganizationIntegrationConfiguration' + AND constraint_name='FK_OrganizationIntegrationConfiguration_OrganizationIntegration') +BEGIN + ALTER TABLE [dbo].[OrganizationIntegrationConfiguration] DROP FK_OrganizationIntegrationConfiguration_OrganizationIntegration; + ALTER TABLE [dbo].[OrganizationIntegrationConfiguration] ADD CONSTRAINT [FK_OrganizationIntegrationConfiguration_OrganizationIntegration] FOREIGN KEY ([OrganizationIntegrationId]) REFERENCES [dbo].[OrganizationIntegration] ([Id]) ON DELETE CASCADE; +END +GO + +-- New procedures for CRUD on OrganizationIntegration and OrganizationIntegrationConfiguration +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegration_Create] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationId UNIQUEIDENTIFIER, + @Type SMALLINT, + @Configuration VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[OrganizationIntegration] + ( + [Id], + [OrganizationId], + [Type], + [Configuration], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @OrganizationId, + @Type, + @Configuration, + @CreationDate, + @RevisionDate + ) +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegrationConfiguration_Create] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationIntegrationId UNIQUEIDENTIFIER, + @EventType SMALLINT, + @Configuration VARCHAR(MAX), + @Template VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[OrganizationIntegrationConfiguration] + ( + [Id], + [OrganizationIntegrationId], + [EventType], + [Configuration], + [Template], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @OrganizationIntegrationId, + @EventType, + @Configuration, + @Template, + @CreationDate, + @RevisionDate + ) +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegration_Update] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationId UNIQUEIDENTIFIER, + @Type SMALLINT, + @Configuration VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[OrganizationIntegration] + SET + [OrganizationId] = @OrganizationId, + [Type] = @Type, + [Configuration] = @Configuration, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegrationConfiguration_Update] + @Id UNIQUEIDENTIFIER OUTPUT, + @OrganizationIntegrationId UNIQUEIDENTIFIER, + @EventType SMALLINT, + @Configuration VARCHAR(MAX), + @Template VARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[OrganizationIntegrationConfiguration] + SET + [OrganizationIntegrationId] = @OrganizationIntegrationId, + [EventType] = @EventType, + [Configuration] = @Configuration, + [Template] = @Template, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegration_DeleteById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[OrganizationIntegration] + WHERE + [Id] = @Id +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegrationConfiguration_DeleteById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[OrganizationIntegrationConfiguration] + WHERE + [Id] = @Id +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegration_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[OrganizationIntegration] + WHERE + [Id] = @Id +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegrationConfiguration_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[OrganizationIntegrationConfiguration] + WHERE + [Id] = @Id +END +GO + +-- Organization cleanup +CREATE OR ALTER PROCEDURE [dbo].[Organization_DeleteById] + @Id UNIQUEIDENTIFIER +WITH + RECOMPILE +AS +BEGIN + SET NOCOUNT ON + + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @Id + + DECLARE @BatchSize INT = 100 + WHILE @BatchSize > 0 + BEGIN + BEGIN TRANSACTION Organization_DeleteById_Ciphers + + DELETE TOP(@BatchSize) + FROM + [dbo].[Cipher] + WHERE + [UserId] IS NULL + AND [OrganizationId] = @Id + + SET @BatchSize = @@ROWCOUNT + + COMMIT TRANSACTION Organization_DeleteById_Ciphers + END + + BEGIN TRANSACTION Organization_DeleteById + + DELETE + FROM + [dbo].[AuthRequest] + WHERE + [OrganizationId] = @Id + + DELETE + FROM + [dbo].[SsoUser] + WHERE + [OrganizationId] = @Id + + DELETE + FROM + [dbo].[SsoConfig] + WHERE + [OrganizationId] = @Id + + DELETE CU + FROM + [dbo].[CollectionUser] CU + INNER JOIN + [dbo].[OrganizationUser] OU ON [CU].[OrganizationUserId] = [OU].[Id] + WHERE + [OU].[OrganizationId] = @Id + + DELETE AP + FROM + [dbo].[AccessPolicy] AP + INNER JOIN + [dbo].[OrganizationUser] OU ON [AP].[OrganizationUserId] = [OU].[Id] + WHERE + [OU].[OrganizationId] = @Id + + DELETE GU + FROM + [dbo].[GroupUser] GU + INNER JOIN + [dbo].[OrganizationUser] OU ON [GU].[OrganizationUserId] = [OU].[Id] + WHERE + [OU].[OrganizationId] = @Id + + DELETE + FROM + [dbo].[OrganizationUser] + WHERE + [OrganizationId] = @Id + + DELETE + FROM + [dbo].[ProviderOrganization] + WHERE + [OrganizationId] = @Id + + EXEC [dbo].[OrganizationApiKey_OrganizationDeleted] @Id + EXEC [dbo].[OrganizationConnection_OrganizationDeleted] @Id + EXEC [dbo].[OrganizationSponsorship_OrganizationDeleted] @Id + EXEC [dbo].[OrganizationDomain_OrganizationDeleted] @Id + EXEC [dbo].[OrganizationIntegration_OrganizationDeleted] @Id + + DELETE + FROM + [dbo].[Project] + WHERE + [OrganizationId] = @Id + + DELETE + FROM + [dbo].[Secret] + WHERE + [OrganizationId] = @Id + + DELETE AK + FROM + [dbo].[ApiKey] AK + INNER JOIN + [dbo].[ServiceAccount] SA ON [AK].[ServiceAccountId] = [SA].[Id] + WHERE + [SA].[OrganizationId] = @Id + + DELETE AP + FROM + [dbo].[AccessPolicy] AP + INNER JOIN + [dbo].[ServiceAccount] SA ON [AP].[GrantedServiceAccountId] = [SA].[Id] + WHERE + [SA].[OrganizationId] = @Id + + DELETE + FROM + [dbo].[ServiceAccount] + WHERE + [OrganizationId] = @Id + + -- Delete Notification Status + DELETE + NS + FROM + [dbo].[NotificationStatus] NS + INNER JOIN + [dbo].[Notification] N ON N.[Id] = NS.[NotificationId] + WHERE + N.[OrganizationId] = @Id + + -- Delete Notification + DELETE + FROM + [dbo].[Notification] + WHERE + [OrganizationId] = @Id + + DELETE + FROM + [dbo].[Organization] + WHERE + [Id] = @Id + + COMMIT TRANSACTION Organization_DeleteById +END +GO + +CREATE OR ALTER PROCEDURE [dbo].[OrganizationIntegration_OrganizationDeleted] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[OrganizationIntegration] + WHERE + [OrganizationId] = @OrganizationId +END +GO