-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-11127] Write
OrganizationInstallation
record when license is re…
…trieved (#5090) * Add SQL files * Add SQL Server migration * Add Core entity * Add Dapper repository * Add EF repository * Add EF migrations * Save OrganizationInstallation during GetLicense invocation * Run dotnet format
- Loading branch information
1 parent
4c502f8
commit 2d891b3
Showing
28 changed files
with
9,751 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Bit.Core.Entities; | ||
using Bit.Core.Utilities; | ||
|
||
namespace Bit.Core.Billing.Entities; | ||
|
||
#nullable enable | ||
|
||
public class OrganizationInstallation : ITableObject<Guid> | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public Guid OrganizationId { get; set; } | ||
public Guid InstallationId { get; set; } | ||
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; | ||
public DateTime? RevisionDate { get; set; } | ||
|
||
public void SetNewId() | ||
{ | ||
if (Id == default) | ||
{ | ||
Id = CoreHelpers.GenerateComb(); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Core/Billing/Repositories/IOrganizationInstallationRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Bit.Core.Billing.Entities; | ||
using Bit.Core.Repositories; | ||
|
||
namespace Bit.Core.Billing.Repositories; | ||
|
||
public interface IOrganizationInstallationRepository : IRepository<OrganizationInstallation, Guid> | ||
{ | ||
Task<OrganizationInstallation> GetByInstallationIdAsync(Guid installationId); | ||
Task<ICollection<OrganizationInstallation>> GetByOrganizationIdAsync(Guid organizationId); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Infrastructure.Dapper/Billing/Repositories/OrganizationInstallationRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Data; | ||
using Bit.Core.Billing.Entities; | ||
using Bit.Core.Billing.Repositories; | ||
using Bit.Core.Settings; | ||
using Bit.Infrastructure.Dapper.Repositories; | ||
using Dapper; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace Bit.Infrastructure.Dapper.Billing.Repositories; | ||
|
||
public class OrganizationInstallationRepository( | ||
GlobalSettings globalSettings) : Repository<OrganizationInstallation, Guid>( | ||
globalSettings.SqlServer.ConnectionString, | ||
globalSettings.SqlServer.ReadOnlyConnectionString), IOrganizationInstallationRepository | ||
{ | ||
public async Task<OrganizationInstallation> GetByInstallationIdAsync(Guid installationId) | ||
{ | ||
var sqlConnection = new SqlConnection(ConnectionString); | ||
|
||
var results = await sqlConnection.QueryAsync<OrganizationInstallation>( | ||
"[dbo].[OrganizationInstallation_ReadByInstallationId]", | ||
new { InstallationId = installationId }, | ||
commandType: CommandType.StoredProcedure); | ||
|
||
return results.FirstOrDefault(); | ||
} | ||
|
||
public async Task<ICollection<OrganizationInstallation>> GetByOrganizationIdAsync(Guid organizationId) | ||
{ | ||
var sqlConnection = new SqlConnection(ConnectionString); | ||
|
||
var results = await sqlConnection.QueryAsync<OrganizationInstallation>( | ||
"[dbo].[OrganizationInstallation_ReadByOrganizationId]", | ||
new { OrganizationId = organizationId }, | ||
commandType: CommandType.StoredProcedure); | ||
|
||
return results.ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...EntityFramework/Billing/Configurations/OrganizationInstallationEntityTypeConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Bit.Infrastructure.EntityFramework.Billing.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Bit.Infrastructure.EntityFramework.Billing.Configurations; | ||
|
||
public class OrganizationInstallationEntityTypeConfiguration : IEntityTypeConfiguration<OrganizationInstallation> | ||
{ | ||
public void Configure(EntityTypeBuilder<OrganizationInstallation> builder) | ||
{ | ||
builder | ||
.Property(oi => oi.Id) | ||
.ValueGeneratedNever(); | ||
|
||
builder | ||
.HasKey(oi => oi.Id) | ||
.IsClustered(); | ||
|
||
builder | ||
.HasIndex(oi => oi.OrganizationId) | ||
.IsClustered(false); | ||
|
||
builder | ||
.HasIndex(oi => oi.InstallationId) | ||
.IsClustered(false); | ||
|
||
builder.ToTable(nameof(OrganizationInstallation)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Infrastructure.EntityFramework/Billing/Models/OrganizationInstallation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using AutoMapper; | ||
using Bit.Infrastructure.EntityFramework.AdminConsole.Models; | ||
using Bit.Infrastructure.EntityFramework.Models; | ||
|
||
namespace Bit.Infrastructure.EntityFramework.Billing.Models; | ||
|
||
public class OrganizationInstallation : Core.Billing.Entities.OrganizationInstallation | ||
{ | ||
public virtual Installation Installation { get; set; } | ||
public virtual Organization Organization { get; set; } | ||
} | ||
|
||
public class OrganizationInstallationMapperProfile : Profile | ||
{ | ||
public OrganizationInstallationMapperProfile() | ||
{ | ||
CreateMap<Core.Billing.Entities.OrganizationInstallation, OrganizationInstallation>().ReverseMap(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...Infrastructure.EntityFramework/Billing/Repositories/OrganizationInstallationRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using AutoMapper; | ||
using Bit.Core.Billing.Entities; | ||
using Bit.Core.Billing.Repositories; | ||
using Bit.Infrastructure.EntityFramework.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using EFOrganizationInstallation = Bit.Infrastructure.EntityFramework.Billing.Models.OrganizationInstallation; | ||
|
||
namespace Bit.Infrastructure.EntityFramework.Billing.Repositories; | ||
|
||
public class OrganizationInstallationRepository( | ||
IMapper mapper, | ||
IServiceScopeFactory serviceScopeFactory) : Repository<OrganizationInstallation, EFOrganizationInstallation, Guid>( | ||
serviceScopeFactory, | ||
mapper, | ||
context => context.OrganizationInstallations), IOrganizationInstallationRepository | ||
{ | ||
public async Task<OrganizationInstallation> GetByInstallationIdAsync(Guid installationId) | ||
{ | ||
using var serviceScope = ServiceScopeFactory.CreateScope(); | ||
|
||
var databaseContext = GetDatabaseContext(serviceScope); | ||
|
||
var query = | ||
from organizationInstallation in databaseContext.OrganizationInstallations | ||
where organizationInstallation.Id == installationId | ||
select organizationInstallation; | ||
|
||
return await query.FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<ICollection<OrganizationInstallation>> GetByOrganizationIdAsync(Guid organizationId) | ||
{ | ||
using var serviceScope = ServiceScopeFactory.CreateScope(); | ||
|
||
var databaseContext = GetDatabaseContext(serviceScope); | ||
|
||
var query = | ||
from organizationInstallation in databaseContext.OrganizationInstallations | ||
where organizationInstallation.OrganizationId == organizationId | ||
select organizationInstallation; | ||
|
||
return await query.ToArrayAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Sql/Billing/dbo/Stored Procedures/OrganizationInstallation_Create.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CREATE PROCEDURE [dbo].[OrganizationInstallation_Create] | ||
@Id UNIQUEIDENTIFIER OUTPUT, | ||
@OrganizationId UNIQUEIDENTIFIER, | ||
@InstallationId UNIQUEIDENTIFIER, | ||
@CreationDate DATETIME2 (7), | ||
@RevisionDate DATETIME2 (7) = NULL | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
INSERT INTO [dbo].[OrganizationInstallation] | ||
( | ||
[Id], | ||
[OrganizationId], | ||
[InstallationId], | ||
[CreationDate], | ||
[RevisionDate] | ||
) | ||
VALUES | ||
( | ||
@Id, | ||
@OrganizationId, | ||
@InstallationId, | ||
@CreationDate, | ||
@RevisionDate | ||
) | ||
END |
12 changes: 12 additions & 0 deletions
12
src/Sql/Billing/dbo/Stored Procedures/OrganizationInstallation_DeleteById.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE PROCEDURE [dbo].[OrganizationInstallation_DeleteById] | ||
@Id UNIQUEIDENTIFIER | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
DELETE | ||
FROM | ||
[dbo].[OrganizationInstallation] | ||
WHERE | ||
[Id] = @Id | ||
END |
13 changes: 13 additions & 0 deletions
13
src/Sql/Billing/dbo/Stored Procedures/OrganizationInstallation_ReadById.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE PROCEDURE [dbo].[OrganizationInstallation_ReadById] | ||
@Id UNIQUEIDENTIFIER | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
* | ||
FROM | ||
[dbo].[OrganizationInstallationView] | ||
WHERE | ||
[Id] = @Id | ||
END |
13 changes: 13 additions & 0 deletions
13
src/Sql/Billing/dbo/Stored Procedures/OrganizationInstallation_ReadByInstallationId.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE PROCEDURE [dbo].[OrganizationInstallation_ReadByInstallationId] | ||
@InstallationId UNIQUEIDENTIFIER | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
* | ||
FROM | ||
[dbo].[OrganizationInstallationView] | ||
WHERE | ||
[InstallationId] = @InstallationId | ||
END |
13 changes: 13 additions & 0 deletions
13
src/Sql/Billing/dbo/Stored Procedures/OrganizationInstallation_ReadByOrganizationId.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE PROCEDURE [dbo].[OrganizationInstallation_ReadByOrganizationId] | ||
@OrganizationId UNIQUEIDENTIFIER | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
* | ||
FROM | ||
[dbo].[OrganizationInstallationView] | ||
WHERE | ||
[OrganizationId] = @OrganizationId | ||
END |
17 changes: 17 additions & 0 deletions
17
src/Sql/Billing/dbo/Stored Procedures/OrganizationInstallation_Update.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE PROCEDURE [dbo].[OrganizationInstallation_Update] | ||
@Id UNIQUEIDENTIFIER OUTPUT, | ||
@OrganizationId UNIQUEIDENTIFIER, | ||
@InstallationId UNIQUEIDENTIFIER, | ||
@CreationDate DATETIME2 (7), | ||
@RevisionDate DATETIME2 (7) | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
UPDATE | ||
[dbo].[OrganizationInstallation] | ||
SET | ||
[RevisionDate] = @RevisionDate | ||
WHERE | ||
[Id] = @Id | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CREATE TABLE [dbo].[OrganizationInstallation] ( | ||
[Id] UNIQUEIDENTIFIER NOT NULL, | ||
[OrganizationId] UNIQUEIDENTIFIER NOT NULL, | ||
[InstallationId] UNIQUEIDENTIFIER NOT NULL, | ||
[CreationDate] DATETIME2 (7) NOT NULL, | ||
[RevisionDate] DATETIME2 (7) NULL, | ||
CONSTRAINT [PK_OrganizationInstallation] PRIMARY KEY CLUSTERED ([Id] ASC), | ||
CONSTRAINT [FK_OrganizationInstallation_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE, | ||
CONSTRAINT [FK_OrganizationInstallation_Installation] FOREIGN KEY ([InstallationId]) REFERENCES [dbo].[Installation] ([Id]) ON DELETE CASCADE | ||
); | ||
GO | ||
|
||
CREATE NONCLUSTERED INDEX [IX_OrganizationInstallation_OrganizationId] | ||
ON [dbo].[OrganizationInstallation]([OrganizationId] ASC); | ||
GO | ||
|
||
CREATE NONCLUSTERED INDEX [IX_OrganizationInstallation_InstallationId] | ||
ON [dbo].[OrganizationInstallation]([InstallationId] ASC); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE VIEW [dbo].[OrganizationInstallationView] | ||
AS | ||
SELECT | ||
* | ||
FROM | ||
[dbo].[OrganizationInstallation]; |
Oops, something went wrong.