using System.Data; using Bit.Core.Entities; using Bit.Core.Models.Data; using Bit.Core.Repositories; using Bit.Core.Settings; using Dapper; using Microsoft.Data.SqlClient; namespace Bit.Infrastructure.Dapper.Repositories; public class EmergencyAccessRepository : Repository, IEmergencyAccessRepository { public EmergencyAccessRepository(GlobalSettings globalSettings) : this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) { } public EmergencyAccessRepository(string connectionString, string readOnlyConnectionString) : base(connectionString, readOnlyConnectionString) { } public async Task GetCountByGrantorIdEmailAsync(Guid grantorId, string email, bool onlyRegisteredUsers) { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.ExecuteScalarAsync( "[dbo].[EmergencyAccess_ReadCountByGrantorIdEmail]", new { GrantorId = grantorId, Email = email, OnlyUsers = onlyRegisteredUsers }, commandType: CommandType.StoredProcedure); return results; } } public async Task> GetManyDetailsByGrantorIdAsync(Guid grantorId) { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[EmergencyAccessDetails_ReadByGrantorId]", new { GrantorId = grantorId }, commandType: CommandType.StoredProcedure); return results.ToList(); } } public async Task> GetManyDetailsByGranteeIdAsync(Guid granteeId) { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[EmergencyAccessDetails_ReadByGranteeId]", new { GranteeId = granteeId }, commandType: CommandType.StoredProcedure); return results.ToList(); } } public async Task GetDetailsByIdGrantorIdAsync(Guid id, Guid grantorId) { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[EmergencyAccessDetails_ReadByIdGrantorId]", new { Id = id, GrantorId = grantorId }, commandType: CommandType.StoredProcedure); return results.FirstOrDefault(); } } public async Task> GetManyToNotifyAsync() { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[EmergencyAccess_ReadToNotify]", commandType: CommandType.StoredProcedure); return results.ToList(); } } public async Task> GetExpiredRecoveriesAsync() { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[EmergencyAccessDetails_ReadExpiredRecoveries]", commandType: CommandType.StoredProcedure); return results.ToList(); } } }