1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-24 17:17:40 +01:00

added transactions table

This commit is contained in:
Kyle Spearrin 2019-01-31 16:45:01 -05:00
parent 9f876d9bff
commit 25f3b76e6b
16 changed files with 503 additions and 0 deletions

View File

@ -0,0 +1,10 @@
namespace Bit.Core.Enums
{
public enum TransactionType : byte
{
Charge = 0,
Credit = 1,
PromotionalCredit = 2,
ReferralCredit = 3
}
}

View File

@ -0,0 +1,27 @@
using System;
using Bit.Core.Enums;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Table
{
public class Transaction : ITableObject<Guid>
{
public Guid Id { get; set; }
public Guid? UserId { get; set; }
public Guid? OrganizationId { get; set; }
public TransactionType Type { get; set; }
public decimal Amount { get; set; }
public bool? Refunded { get; set; }
public decimal? RefundedAmount { get; set; }
public string Details { get; set; }
public PaymentMethodType? PaymentMethodType { get; set; }
public GatewayType? Gateway { get; set; }
public string GatewayId { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public void SetNewId()
{
Id = CoreHelpers.GenerateComb();
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using Bit.Core.Models.Table;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Bit.Core.Repositories
{
public interface ITransactionRepository : IRepository<Transaction, Guid>
{
Task<ICollection<Transaction>> GetManyByUserIdAsync(Guid userId);
Task<ICollection<Transaction>> GetManyByOrganizationIdAsync(Guid organizationId);
}
}

View File

@ -0,0 +1,48 @@
using System;
using Bit.Core.Models.Table;
using System.Collections.Generic;
using System.Threading.Tasks;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace Bit.Core.Repositories.SqlServer
{
public class TransactionRepository : Repository<Transaction, Guid>, ITransactionRepository
{
public TransactionRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
{ }
public TransactionRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString)
{ }
public async Task<ICollection<Transaction>> GetManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Transaction>(
$"[{Schema}].[Transaction_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<ICollection<Transaction>> GetManyByOrganizationIdAsync(Guid organizationId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Transaction>(
$"[{Schema}].[Transaction_ReadByOrganizationId]",
new { OrganizationId = organizationId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
}
}

View File

@ -52,6 +52,7 @@ namespace Bit.Core.Utilities
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
services.AddSingleton<IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
services.AddSingleton<ITransactionRepository, SqlServerRepos.TransactionRepository>();
}
if(globalSettings.SelfHosted)

View File

@ -240,5 +240,16 @@
<Build Include="dbo\Stored Procedures\Cipher_CreateWithCollections.sql" />
<Build Include="dbo\Stored Procedures\Cipher_UpdateCollections.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_CreateWithCollections.sql" />
<Build Include="dbo\Tables\Transaction.sql" />
<Build Include="dbo\Stored Procedures\Transaction_Create.sql" />
<Build Include="dbo\Stored Procedures\Transaction_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Transaction_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Transaction_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Transaction_Update.sql" />
<Build Include="dbo\Views\TransactionView.sql" />
<Build Include="dbo\Stored Procedures\Transaction_ReadByUserId.sql" />
</ItemGroup>
<ItemGroup>
<RefactorLog Include="Sql.refactorlog" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,48 @@
CREATE PROCEDURE [dbo].[Transaction_Create]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@Type TINYINT,
@Amount MONEY,
@Refunded BIT,
@RefundedAmount MONEY,
@Details NVARCHAR(100),
@PaymentMethodType TINYINT,
@Gateway TINYINT,
@GatewayId VARCHAR(50),
@CreationDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[Transaction]
(
[Id],
[UserId],
[OrganizationId],
[Type],
[Amount],
[Refunded],
[RefundedAmount],
[Details],
[PaymentMethodType],
[Gateway],
[GatewayId],
[CreationDate]
)
VALUES
(
@Id,
@UserId,
@OrganizationId,
@Type,
@Amount,
@Refunded,
@RefundedAmount,
@Details,
@PaymentMethodType,
@Gateway,
@GatewayId,
@CreationDate
)
END

View File

@ -0,0 +1,12 @@
CREATE PROCEDURE [dbo].[Transaction_DeleteById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DELETE
FROM
[dbo].[Transaction]
WHERE
[Id] = @Id
END

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[Transaction_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[Id] = @Id
END

View File

@ -0,0 +1,14 @@
CREATE PROCEDURE [dbo].[Transaction_ReadByOrganizationId]
@OrganizationId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[UserId] = NULL
AND [OrganizationId] = @OrganizationId
END

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[Transaction_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[UserId] = @UserId
END

View File

@ -0,0 +1,34 @@
CREATE PROCEDURE [dbo].[Transaction_Update]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@Type TINYINT,
@Amount MONEY,
@Refunded BIT,
@RefundedAmount MONEY,
@Details NVARCHAR(100),
@PaymentMethodType TINYINT,
@Gateway TINYINT,
@GatewayId VARCHAR(50),
@CreationDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
UPDATE
[dbo].[Transaction]
SET
[UserId] = @UserId,
[OrganizationId] = @OrganizationId,
[Type] = @Type,
[Amount] = @Amount,
[Refunded] = @Refunded,
[RefundedAmount] = @RefundedAmount,
[Details] = @Details,
[PaymentMethodType] = @PaymentMethodType,
[Gateway] = @Gateway,
[GatewayId] = @GatewayId,
[CreationDate] = @CreationDate
WHERE
[Id] = @Id
END

View File

@ -0,0 +1,28 @@
CREATE TABLE [dbo].[Transaction] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[UserId] UNIQUEIDENTIFIER NULL,
[OrganizationId] UNIQUEIDENTIFIER NULL,
[Type] TINYINT NOT NULL,
[Amount] MONEY NOT NULL,
[Refunded] BIT NULL,
[RefundedAmount] MONEY NULL,
[Details] NVARCHAR(100) NULL,
[PaymentMethodType] TINYINT NULL,
[Gateway] TINYINT NULL,
[GatewayId] VARCHAR(50) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_Transaction] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Transaction_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Transaction_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_Transaction_Gateway_GatewayId]
ON [dbo].[Transaction]([Gateway] ASC, [GatewayId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Transaction_UserId_OrganizationId_CreationDate]
ON [dbo].[Transaction]([UserId] ASC, [OrganizationId] ASC, [CreationDate] ASC);

View File

@ -0,0 +1,6 @@
CREATE VIEW [dbo].[TransactionView]
AS
SELECT
*
FROM
[dbo].[Transaction]

View File

@ -0,0 +1,224 @@
IF OBJECT_ID('[dbo].[Transaction]') IS NULL
BEGIN
CREATE TABLE [dbo].[Transaction] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[UserId] UNIQUEIDENTIFIER NULL,
[OrganizationId] UNIQUEIDENTIFIER NULL,
[Type] TINYINT NOT NULL,
[Amount] MONEY NOT NULL,
[Refunded] BIT NULL,
[RefundedAmount] MONEY NULL,
[Details] NVARCHAR(100) NULL,
[PaymentMethodType] TINYINT NULL,
[Gateway] TINYINT NULL,
[GatewayId] VARCHAR(50) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_Transaction] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Transaction_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Transaction_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE
);
CREATE NONCLUSTERED INDEX [IX_Transaction_Gateway_GatewayId]
ON [dbo].[Transaction]([Gateway] ASC, [GatewayId] ASC);
CREATE NONCLUSTERED INDEX [IX_Transaction_UserId_OrganizationId_CreationDate]
ON [dbo].[Transaction]([UserId] ASC, [OrganizationId] ASC, [CreationDate] ASC);
END
GO
IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'TransactionView')
BEGIN
DROP VIEW [dbo].[TransactionView]
END
GO
CREATE VIEW [dbo].[TransactionView]
AS
SELECT
*
FROM
[dbo].[Transaction]
GO
IF OBJECT_ID('[dbo].[Transaction_Create]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Transaction_Create]
END
GO
CREATE PROCEDURE [dbo].[Transaction_Create]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@Type TINYINT,
@Amount MONEY,
@Refunded BIT,
@RefundedAmount MONEY,
@Details NVARCHAR(100),
@PaymentMethodType TINYINT,
@Gateway TINYINT,
@GatewayId VARCHAR(50),
@CreationDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[Transaction]
(
[Id],
[UserId],
[OrganizationId],
[Type],
[Amount],
[Refunded],
[RefundedAmount],
[Details],
[PaymentMethodType],
[Gateway],
[GatewayId],
[CreationDate]
)
VALUES
(
@Id,
@UserId,
@OrganizationId,
@Type,
@Amount,
@Refunded,
@RefundedAmount,
@Details,
@PaymentMethodType,
@Gateway,
@GatewayId,
@CreationDate
)
END
GO
IF OBJECT_ID('[dbo].[Transaction_DeleteById]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Transaction_DeleteById]
END
GO
CREATE PROCEDURE [dbo].[Transaction_DeleteById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DELETE
FROM
[dbo].[Transaction]
WHERE
[Id] = @Id
END
GO
IF OBJECT_ID('[dbo].[Transaction_ReadById]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Transaction_ReadById]
END
GO
CREATE PROCEDURE [dbo].[Transaction_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[Id] = @Id
END
GO
IF OBJECT_ID('[dbo].[Transaction_ReadByOrganizationId]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Transaction_ReadByOrganizationId]
END
GO
CREATE PROCEDURE [dbo].[Transaction_ReadByOrganizationId]
@OrganizationId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[UserId] = NULL
AND [OrganizationId] = @OrganizationId
END
GO
IF OBJECT_ID('[dbo].[Transaction_ReadByUserId]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Transaction_ReadByUserId]
END
GO
CREATE PROCEDURE [dbo].[Transaction_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[UserId] = @UserId
END
GO
IF OBJECT_ID('[dbo].[Transaction_Update]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Transaction_Update]
END
GO
CREATE PROCEDURE [dbo].[Transaction_Update]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@Type TINYINT,
@Amount MONEY,
@Refunded BIT,
@RefundedAmount MONEY,
@Details NVARCHAR(100),
@PaymentMethodType TINYINT,
@Gateway TINYINT,
@GatewayId VARCHAR(50),
@CreationDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
UPDATE
[dbo].[Transaction]
SET
[UserId] = @UserId,
[OrganizationId] = @OrganizationId,
[Type] = @Type,
[Amount] = @Amount,
[Refunded] = @Refunded,
[RefundedAmount] = @RefundedAmount,
[Details] = @Details,
[PaymentMethodType] = @PaymentMethodType,
[Gateway] = @Gateway,
[GatewayId] = @GatewayId,
[CreationDate] = @CreationDate
WHERE
[Id] = @Id
END
GO

View File

@ -16,6 +16,7 @@
<None Remove="DbScripts\2018-09-25_00_OrgPurge.sql" />
<None Remove="DbScripts\2018-10-17_00_ManagerRole.sql" />
<None Remove="DbScripts\2018-12-19_00_OrgUserTwoFactorEnabled.sql" />
<None Remove="DbScripts\2019-01-31_00_Transactions.sql" />
</ItemGroup>
<ItemGroup>