From b64ab7fc96838eb669f0fd62d973003880bef16f Mon Sep 17 00:00:00 2001 From: barhen-pfw Date: Wed, 20 Dec 2023 17:59:12 -0500 Subject: [PATCH] Initial Commit --- Cloud_Manager/BiskAcdbContext.cs | 10 +- Cloud_Manager/BiskilogContext.cs | 23 ++ .../ClientContractModels/Clientapikey.cs | 15 ++ .../Models/Interfaces/IKeyService.cs | 31 +++ .../Models/Interfaces/ITokenService.cs | 18 -- .../Models/ServiceRepo/TokenService.cs | 183 +------------- Cloud_Manager/Program.cs | 12 +- .../Services/AuthenticationService.cs | 58 +---- Cloud_Manager/Services/CompanyService.cs | 48 ++-- Cloud_Manager/Services/CustomerService.cs | 47 +--- Cloud_Manager/Services/ProductRepo.cs | 155 +++--------- Cloud_Manager/Services/SalesService.cs | 233 +++++------------- Cloud_Manager/Services/UserService.cs | 18 +- 13 files changed, 225 insertions(+), 626 deletions(-) create mode 100644 Cloud_Manager/Models/ClientContractModels/Clientapikey.cs create mode 100644 Cloud_Manager/Models/Interfaces/IKeyService.cs delete mode 100644 Cloud_Manager/Models/Interfaces/ITokenService.cs diff --git a/Cloud_Manager/BiskAcdbContext.cs b/Cloud_Manager/BiskAcdbContext.cs index dc812cf..a8265c4 100644 --- a/Cloud_Manager/BiskAcdbContext.cs +++ b/Cloud_Manager/BiskAcdbContext.cs @@ -11,11 +11,11 @@ public partial class BiskAcdbContext : DbContext { private readonly HttpContext m_httpContext; private readonly IConnectionService m_connection; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; public BiskAcdbContext() { } - public BiskAcdbContext(DbContextOptions options, ITokenService tokenService, IConnectionService connection, IHttpContextAccessor a_httpContextAccessor = null) + public BiskAcdbContext(DbContextOptions options, IKeyService tokenService, IConnectionService connection, IHttpContextAccessor a_httpContextAccessor = null) : base(options) { m_tokenService = tokenService; @@ -27,10 +27,10 @@ public partial class BiskAcdbContext : DbContext { if (!optionsBuilder.IsConfigured) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - int? databaseId = m_tokenService.GetDatabaseIdFromToken(token); + int? databaseId = m_tokenService.GetDatabaseIdFromKey(apiKey); string connectionString = m_connection.GetClientConnectionString(databaseId!.Value); optionsBuilder.UseMySql(connectionString, new MariaDbServerVersion(new Version())); } diff --git a/Cloud_Manager/BiskilogContext.cs b/Cloud_Manager/BiskilogContext.cs index 28d7a91..5df82b0 100644 --- a/Cloud_Manager/BiskilogContext.cs +++ b/Cloud_Manager/BiskilogContext.cs @@ -30,6 +30,7 @@ public partial class BiskilogContext : DbContext public virtual DbSet Siteaccesspermissions { get; set; } public virtual DbSet Userauths { get; set; } + public virtual DbSet Clientapikeys { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -162,7 +163,29 @@ public partial class BiskilogContext : DbContext .HasColumnType("datetime") .HasColumnName("start_date"); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.Id).HasName("PRIMARY"); + + entity.ToTable("clientapikey"); + + entity.HasIndex(e => e.ContractId, "contractId"); + entity.Property(e => e.Id) + .HasColumnType("int(11)") + .HasColumnName("id"); + entity.Property(e => e.ContractId) + .HasColumnType("int(11)") + .HasColumnName("contractId"); + entity.Property(e => e.IsActive) + .HasDefaultValueSql("b'1'") + .HasColumnType("bit(1)") + .HasColumnName("isActive"); + entity.Property(e => e.Key) + .HasMaxLength(50) + .HasDefaultValueSql("'0'") + .HasColumnName("key"); + }); modelBuilder.Entity(entity => { entity.HasKey(e => e.DbNo).HasName("PRIMARY"); diff --git a/Cloud_Manager/Models/ClientContractModels/Clientapikey.cs b/Cloud_Manager/Models/ClientContractModels/Clientapikey.cs new file mode 100644 index 0000000..c1cc957 --- /dev/null +++ b/Cloud_Manager/Models/ClientContractModels/Clientapikey.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Cloud_Manager.Models.ClientContractModels; + +public partial class Clientapikey +{ + public int Id { get; set; } + + public string Key { get; set; } = null!; + + public int ContractId { get; set; } + + public ulong IsActive { get; set; } +} diff --git a/Cloud_Manager/Models/Interfaces/IKeyService.cs b/Cloud_Manager/Models/Interfaces/IKeyService.cs new file mode 100644 index 0000000..5ac7a51 --- /dev/null +++ b/Cloud_Manager/Models/Interfaces/IKeyService.cs @@ -0,0 +1,31 @@ +using Cloud_Manager.Models.ClientContractModels; +using Cloud_Manager.Models.Enums; + +namespace Cloud_Manager.Models.Interfaces +{ + public interface IKeyService + { + /// + /// Validates specified API + /// + /// AuthEnums.Valid if key is a valid and unexpired token + AuthEnums ValidateKey(string a_Key); + + /// + /// Generates an API Key based on the specified client + /// + /// A tokenized string + string GenerateKey(Contract a_clientContract, Databasemap a_database); + /// + ///Returns the API if valid to return the related database id + /// + /// + int? GetDatabaseIdFromKey(string a_Key); + /// + /// Gets the branch associated with the specified API if valid + /// + /// + /// + string GetBaseBranch(string a_Key); + } +} diff --git a/Cloud_Manager/Models/Interfaces/ITokenService.cs b/Cloud_Manager/Models/Interfaces/ITokenService.cs deleted file mode 100644 index c8c9c2d..0000000 --- a/Cloud_Manager/Models/Interfaces/ITokenService.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Cloud_Manager.Models.ClientContractModels; -using Cloud_Manager.Models.Enums; - -namespace Cloud_Manager.Models.Interfaces -{ - public interface ITokenService - { - AuthEnums ValidateToken(string a_token); - string GenerateToken(Userauth a_user, Contract a_clientContract, Databasemap a_database, List a_business, bool a_comparison); - int? GetDatabaseIdFromToken(string a_token); - int? GetUserIdFromToken(string a_token); - string? GetUserNameFromToken(string a_token); - string? GetBaseBranch(string a_token); - bool? GetComparison(string a_token); - IEnumerable BranchIds(string a_token); - string? GetAllBranch(string a_token); - } -} diff --git a/Cloud_Manager/Models/ServiceRepo/TokenService.cs b/Cloud_Manager/Models/ServiceRepo/TokenService.cs index 8eef96b..74524ff 100644 --- a/Cloud_Manager/Models/ServiceRepo/TokenService.cs +++ b/Cloud_Manager/Models/ServiceRepo/TokenService.cs @@ -9,191 +9,34 @@ using System.Text; namespace Cloud_Manager.Models.ServiceRepo { - public class TokenService : ITokenService + public class TokenService : IKeyService { private IConfiguration m_configuration { get; } - public TokenService(IConfiguration a_configuration) + private BiskilogContext m_context; + public TokenService(IConfiguration a_configuration,BiskilogContext a_context) { m_configuration = a_configuration; + m_context = a_context; } - /// - /// Validates a user access token - /// - /// AuthEnums.Valid if token is a valid and unexpired token - public AuthEnums ValidateToken(string a_token) + public AuthEnums ValidateKey(string a_Key) { - try - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - - if (jwtToken.ValidFrom <= DateTime.Now && jwtToken.ValidTo > DateTime.Now) - return AuthEnums.Valid; - return AuthEnums.Expired; - } - catch (Exception ex) - { - return AuthEnums.Invalid; - } + throw new NotImplementedException(); } - /// - /// Generates an access token based on the user - /// - /// A tokenized string - public string GenerateToken(Userauth a_user, Contract a_clientContract, Databasemap a_database, List a_business, bool a_comparison) - { - try - { - //create claims details based on the user information - var claims = new[] { - new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), - new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()), - new Claim("ContractStart",a_clientContract.StartDate !.Value.ToString()), - new Claim("ContractEnd",a_clientContract.EndDate!.Value.ToString()), - new Claim("UserId", a_user.UserId.ToString()), - new Claim("Username", a_user.Username.ToString()), - new Claim("DbId",a_database.DbNo.ToString()), - new Claim("ComparisonMode",a_comparison.ToString()), - new Claim("BranchId",a_business[0].ToString()), - new Claim("BranchAccess",string.Join(", ", a_business.ToArray())), - new Claim("ClientId", a_user.ClientId.ToString()), - }; - - var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(m_configuration["Jwt:Key"]!)); - - var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); - var token = new JwtSecurityToken(m_configuration["Jwt:Issuer"], m_configuration["Jwt:Audience"], claims, expires: DateTime.UtcNow.AddDays(14), signingCredentials: signIn); - return $"{new JwtSecurityTokenHandler().WriteToken(token)}"; - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - return AuthEnums.Error.ToString(); - } - } - /// - ///Deserializes the token string if valid to return the specified user role id in the token string - /// - /// - /// RoleId - public int? GetDatabaseIdFromToken(string a_token) - { - if (ValidateToken(a_token) == AuthEnums.Valid) - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - return int.Parse(jwtToken.Claims.First(claim => claim.Type == "DbId").Value); - } - return null; - } - /// - ///Deserializes the token string if valid to return the specified user id in the token string - /// - /// - /// UserId - public int? GetUserIdFromToken(string a_token) + public string GenerateKey(Contract a_clientContract, Databasemap a_database) { - if (ValidateToken(a_token) == AuthEnums.Valid) - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - return int.Parse(jwtToken.Claims.First(claim => claim.Type == "UserId").Value); - } - return null; - } - /// - ///Deserializes the token string if valid to return the specified username in the token string - /// - /// - /// Username - public string? GetUserNameFromToken(string a_token) - { - if (ValidateToken(a_token) == AuthEnums.Valid) - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - return jwtToken.Claims.First(claim => claim.Type == "Username").Value; - } - return null; - } - /// - ///Deserializes the token string if valid to return the specified branchId in the token string - /// - /// - /// Username - public string? GetBaseBranch(string a_token) - { - if (ValidateToken(a_token) == AuthEnums.Valid) - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - return jwtToken.Claims.First(claim => claim.Type == "BranchId").Value; - } - return null; + throw new NotImplementedException(); } - public bool? GetComparison(string a_token) + public int? GetDatabaseIdFromKey(string a_Key) { - if (ValidateToken(a_token) == AuthEnums.Valid) - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - return bool.Parse(jwtToken.Claims.First(claim => claim.Type == "ComparisonMode").Value); - } - return null; + throw new NotImplementedException(); } - /// - ///Deserializes the token string if valid to return the specified list of branches a user has access to in the token string - /// - /// - /// Username - public string? GetAllBranch(string a_token) - { - if (ValidateToken(a_token) == AuthEnums.Valid) - { - string token = a_token.Substring(6).Trim(); - var handler = new JwtSecurityTokenHandler(); - JwtSecurityToken jwtToken = (JwtSecurityToken)handler.ReadToken(token); - return jwtToken.Claims.First(claim => claim.Type == "BranchAccess").Value; - } - return null; - } - /// - /// Return a specified list of branches a user has access if comparison mode is set otherwise returns only the - /// active branch on the list - /// - /// - /// - public IEnumerable BranchIds(string a_token) + + public string GetBaseBranch(string a_Key) { - List branchIds = new List(); - if (ValidateToken(a_token) == AuthEnums.Valid) - { - bool comparison = GetComparison(a_token)!.Value; - if (comparison) - { - string? branches = GetAllBranch(a_token); - if (branches != null) - { - string[] branchArray = branches!.Split(); - branchIds.AddRange(branchArray); - } - } - else - { - string? baseBranch = GetBaseBranch(a_token); - branchIds.Add(baseBranch!); - } - } - return branchIds.AsEnumerable(); + throw new NotImplementedException(); } } } diff --git a/Cloud_Manager/Program.cs b/Cloud_Manager/Program.cs index 6b8e15c..f1b085a 100644 --- a/Cloud_Manager/Program.cs +++ b/Cloud_Manager/Program.cs @@ -23,7 +23,7 @@ builder.Services.AddSingleton(); builder.Services.AddDbContext(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -35,14 +35,6 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddCors(options => -{ - options.AddPolicy("CorsPolicy", - builder => builder.AllowAnyOrigin() - .AllowAnyMethod() - .AllowAnyHeader() - ); -}); var app = builder.Build(); app.UseSwagger(); @@ -50,7 +42,7 @@ app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "SecureSwagg app.UseHttpsRedirection(); -app.UseCors("CorsPolicy"); + app.UseAuthentication(); app.UseAuthorization(); diff --git a/Cloud_Manager/Services/AuthenticationService.cs b/Cloud_Manager/Services/AuthenticationService.cs index 2591a9c..ee4342a 100644 --- a/Cloud_Manager/Services/AuthenticationService.cs +++ b/Cloud_Manager/Services/AuthenticationService.cs @@ -9,9 +9,9 @@ namespace Cloud_Manager.Services public class AuthenticationService : IAuthService { private readonly BiskilogContext m_context; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; - public AuthenticationService(BiskilogContext a_context, ITokenService a_tokenService) + public AuthenticationService(BiskilogContext a_context, IKeyService a_tokenService) { m_context = a_context; m_tokenService = a_tokenService; @@ -32,36 +32,10 @@ namespace Cloud_Manager.Services return AuthEnums.NotFound; } } - /// - /// Autenticates a user and returns a tokenized string - /// - /// - /// - /// strings - public async Task AuthenticateClient(string a_username, string a_password) + public Task AuthenticateClient(string a_username, string a_password) { - var user = await GetUserAsync(a_username, a_password); - - if (user == null) - { - return null; - } - user.LastLogin = DateTime.Now; - m_context.Userauths.Update(user); - m_context.SaveChanges(); - - - Databasemap databasemap = GetClientDB(user.ClientId); - - List businessIds = GetSiteaccesspermission(user.ClientId, user.UserId).Select(t => t.BusinessId).ToList(); - Contract? contract = GetContract(user.ClientId, businessIds); - List businesses = GetClientbusiness(user.ClientId, user.UserId).Select(t => t.BusinessExternalId).ToList(); - - if (contract == null) - return AuthEnums.Invalid.ToString(); - - return m_tokenService.GenerateToken(user, contract, databasemap, businesses, false); + throw new NotImplementedException(); } /// @@ -118,29 +92,5 @@ namespace Cloud_Manager.Services { return m_context.Siteaccesspermissions.Where(t => t.ClientId == a_clientId && t.UserId == a_userId).ToList(); } - - private async Task GetUserAsync(string username, string password) - { - //Todo have complete implementation after means of creating user is done - //try - //{ - // string pa = await m_context.Userauths.Where(u => u.Username == username).Select(u => u.Password).FirstAsync(); - // bool verified = BCrypt.Net.BCrypt.Verify(password, pa); - // if (verified) - // { - - //TODO have a complete implementation - return await m_context.Userauths.FirstAsync(u => u.Username == username && u.Passsword == password); - // } - // else - // { - // return null; - // } - //}catch(Exception ex) - //{ - // //possible is user not found - // return null; - //} - } } } diff --git a/Cloud_Manager/Services/CompanyService.cs b/Cloud_Manager/Services/CompanyService.cs index 13cc9fe..8be4312 100644 --- a/Cloud_Manager/Services/CompanyService.cs +++ b/Cloud_Manager/Services/CompanyService.cs @@ -12,12 +12,12 @@ namespace Cloud_Manager.Services public class CompanyService : ICompanyInfo { private readonly BiskAcdbContext m_context; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; private readonly HttpContext m_httpContext; private Tblcompanydetail m_companyInfo { get; set; } private IEnumerable m_companyBranches { get; set; } - public CompanyService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) + public CompanyService(BiskAcdbContext a_context, IKeyService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; @@ -56,8 +56,8 @@ namespace Cloud_Manager.Services } public async Task SyncBranches(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -72,10 +72,10 @@ namespace Cloud_Manager.Services } public DateTime GetLastSyncDate(string a_tablename) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - string activeBranch = m_tokenService.GetBaseBranch(token)!; + string activeBranch = m_tokenService.GetBaseBranch(apiKey)!; DateTime? lastSync = m_context.Tblsyncinfos.FirstOrDefault(p => p.TableName == a_tablename && p.BranchId == activeBranch!)?.LastSyncDate; if (lastSync != null) @@ -88,10 +88,10 @@ namespace Cloud_Manager.Services public void SetLastSyncDate(string a_tableName, DateTime a_timestamp) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - string activeBranch = m_tokenService.GetBaseBranch(token)!; + string activeBranch = m_tokenService.GetBaseBranch(apiKey)!; using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); @@ -108,8 +108,8 @@ namespace Cloud_Manager.Services public async Task SyncSystemRoles(List a_roles) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_roles); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -124,8 +124,8 @@ namespace Cloud_Manager.Services } public async Task SyncCompanyDetails(List a_details) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -141,8 +141,8 @@ namespace Cloud_Manager.Services public async Task SyncDriverDetails(List a_details) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -158,8 +158,8 @@ namespace Cloud_Manager.Services public async Task SyncTrucks(List a_trucks) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_trucks); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -175,8 +175,8 @@ namespace Cloud_Manager.Services public async Task SyncTruckAssignments(List a_assignments) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_assignments); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -192,8 +192,8 @@ namespace Cloud_Manager.Services public async Task SyncTruckMappings(List a_mapping) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_mapping); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -209,8 +209,8 @@ namespace Cloud_Manager.Services public async Task SyncTruckInventory(List a_inventories) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_inventories); using (var command = m_context.Database.GetDbConnection().CreateCommand()) diff --git a/Cloud_Manager/Services/CustomerService.cs b/Cloud_Manager/Services/CustomerService.cs index aac20cb..25038d7 100644 --- a/Cloud_Manager/Services/CustomerService.cs +++ b/Cloud_Manager/Services/CustomerService.cs @@ -13,10 +13,10 @@ namespace Cloud_Manager.Services public class CustomerService : ICustomer { private readonly BiskAcdbContext m_context; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; private readonly HttpContext m_httpContext; - public CustomerService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) + public CustomerService(BiskAcdbContext a_context, IKeyService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; @@ -24,44 +24,7 @@ namespace Cloud_Manager.Services } public IEnumerable FetchCustomers() { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - using (var command = m_context.Database.GetDbConnection().CreateCommand()) - { - command.CommandText = "CALL GetCustomers(@p0)"; - command.Parameters.Add(new MySqlParameter("@p0", string.Join(", ", accessiblebranches.ToArray()))); - - m_context.Database.OpenConnection(); - - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - yield return new CustomerAccounts - { - Customer = new Tblcustomer - { - CustomerId = reader.GetString(0), - BranchId = reader.GetString(1), - Firstname = reader.GetString(2), - Surname = reader.GetString(3), - Address = reader.GetString(4), - Telephone = reader.GetString(5), - DateAdded = reader.GetDateTime(6), - Status = reader.GetString(7), - Email = reader.GetString(8), - FinancialStatus = reader.GetString(9), - }, - Debt = reader.GetDecimal(10) - }; - } - } - } - } + throw new NotImplementedException(); } public Task> GetCustomers() @@ -71,8 +34,8 @@ namespace Cloud_Manager.Services public async Task SyncCustomers(List a_details) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) diff --git a/Cloud_Manager/Services/ProductRepo.cs b/Cloud_Manager/Services/ProductRepo.cs index 52161a8..abccec6 100644 --- a/Cloud_Manager/Services/ProductRepo.cs +++ b/Cloud_Manager/Services/ProductRepo.cs @@ -15,7 +15,7 @@ namespace Cloud_Manager.Services public class ProductRepo : IProduct { private readonly BiskAcdbContext m_context; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; private readonly HttpContext m_httpContext; public event EventHandler ProductsChanged; @@ -23,7 +23,7 @@ namespace Cloud_Manager.Services public event EventHandler BrandsChanged; public event EventHandler CategoriesChanged; - public ProductRepo(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) + public ProductRepo(BiskAcdbContext a_context, IKeyService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; @@ -35,124 +35,35 @@ namespace Cloud_Manager.Services /// public IEnumerable GetProducts(string a_productKey = "") { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - using (var command = m_context.Database.GetDbConnection().CreateCommand()) - { - command.CommandText = "CALL GetProducts(@p0)"; - command.Parameters.Add(new MySqlParameter("@p0", string.Join(", ", accessiblebranches.ToArray()))); - - m_context.Database.OpenConnection(); - - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - List pUnits = new List(); - - yield return new ProductItem - { - Product = new Tblproduct - { - Pcode = reader.GetString(0), - ProductName = reader.GetString(1), - Pdesc = reader.GetString(2), - BaseUnit = reader.GetString(3), - Costprice = reader.GetDecimal(4), - Status = reader.GetString(5), - Price = reader.GetDecimal(6), - BranchId = reader.GetString(7), - }, - BaseUnit = reader.GetString(3), - Stock = new Tblinventory - { - Quantity = reader.GetInt32(8) - }, - Restocklevel = new Restocklevel - { - WarnLevel = reader.GetInt32(9), - Unit = reader.GetString(10), - }, - Units = GetAltUnits(reader) - }; - } - } - } - } + throw new NotImplementedException(); } private List GetAltUnits(DbDataReader a_reader) { - List pUnits = new List(); - for (int i = 1; i < 5; i++) - { - if (!a_reader.IsDBNull(a_reader.GetOrdinal($"AltUnit{i}"))) - { - pUnits.Add(new ProductUnits - { - UnitCode = a_reader.GetFieldValue($"AltUnit{i}"), - QuantityUnit = a_reader.GetFieldValue($"AltUnit{i}QTY"), - PriceUnit = a_reader.GetFieldValue($"AltUnit{i}Price"), - DistinctiveCode = a_reader.GetFieldValue($"AltUnit{i}distinctiveCode") - }); - } - else - { - return pUnits; - } - } - return pUnits; + throw new NotImplementedException(); } public IEnumerable GetUnitofmeasures() { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - return m_context.Unitofmeasures.Where(b => accessiblebranches.Contains(b.BranchId)); - } - return new List(); + throw new NotImplementedException(); } public IEnumerable GetBrands(string a_brandKey = "") { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - return m_context.Tblbrands.Where(b => accessiblebranches.Contains(b.BranchId)); - } - return new List(); + throw new NotImplementedException(); } public IEnumerable GetCategories(string a_categoryKey = "") { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - return m_context.Tblcategories.Where(b => accessiblebranches.Contains(b.BranchId)); - } - return new List(); + throw new NotImplementedException(); } public async Task SyncProducts(List a_item) { try { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_item); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -173,8 +84,8 @@ namespace Cloud_Manager.Services public async Task SyncInventory(List a_item) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_item); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -190,8 +101,8 @@ namespace Cloud_Manager.Services public async Task SyncInventoryEntries(List a_item) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_item); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -207,8 +118,8 @@ namespace Cloud_Manager.Services public async Task SyncPriceChanges(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -224,8 +135,8 @@ namespace Cloud_Manager.Services public async Task SyncProductAltUnit(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -241,8 +152,8 @@ namespace Cloud_Manager.Services public async Task SyncRestockAsync(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -258,8 +169,8 @@ namespace Cloud_Manager.Services public async Task SyncUnitOfMeasureAsync(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -275,8 +186,8 @@ namespace Cloud_Manager.Services public async Task SyncStockAsync(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -292,8 +203,8 @@ namespace Cloud_Manager.Services public async Task SyncBrandsAsync(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -309,8 +220,8 @@ namespace Cloud_Manager.Services public async Task SyncCategoriesAsync(List a_items) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_items); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -325,10 +236,10 @@ namespace Cloud_Manager.Services } public DateTime GetLastSyncDate(string a_tablename) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - string activeBranch = m_tokenService.GetBaseBranch(token)!; + string activeBranch = m_tokenService.GetBaseBranch(apiKey)!; DateTime? lastSync = m_context.Tblsyncinfos.FirstOrDefault(p => p.TableName == a_tablename && p.BranchId == activeBranch!)?.LastSyncDate; if (lastSync != null) @@ -341,10 +252,10 @@ namespace Cloud_Manager.Services public void SetLastSyncDate(string a_tableName, DateTime a_timestamp) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - string activeBranch = m_tokenService.GetBaseBranch(token)!; + string activeBranch = m_tokenService.GetBaseBranch(apiKey)!; using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); diff --git a/Cloud_Manager/Services/SalesService.cs b/Cloud_Manager/Services/SalesService.cs index 922caf2..4766770 100644 --- a/Cloud_Manager/Services/SalesService.cs +++ b/Cloud_Manager/Services/SalesService.cs @@ -14,7 +14,7 @@ namespace Cloud_Manager.Services public class SalesService : ISalesInterface { private readonly BiskAcdbContext m_context; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; private readonly HttpContext m_httpContext; private readonly IHubContext m_salesHub; @@ -22,7 +22,7 @@ namespace Cloud_Manager.Services public event EventHandler FetchComplete; public event EventHandler FetchStart; - public SalesService(BiskAcdbContext a_context, ITokenService a_tokenService, + public SalesService(BiskAcdbContext a_context, IKeyService a_tokenService, IHttpContextAccessor a_httpContextAccessor, IHubContext a_salesHub) { m_context = a_context; @@ -31,151 +31,11 @@ namespace Cloud_Manager.Services m_salesHub = a_salesHub; } - public Task FetchRecentTransaction(int a_limit) - { - throw new NotImplementedException(); - } - - public IEnumerable GetRecentTransaction() - { - throw new NotImplementedException(); - } - - public IEnumerable GetTransactions(DateTime a_start, DateTime a_end) - { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - using (var command = m_context.Database.GetDbConnection().CreateCommand()) - { - command.CommandText = "CALL GetTransactionsByDate(@p0,@p1,@p2)"; - command.Parameters.Add(new MySqlParameter("@p0", a_start.ToString("yyyy-MM-dd"))); - command.Parameters.Add(new MySqlParameter("@p1", a_end.ToString("yyyy-MM-dd"))); - command.Parameters.Add(new MySqlParameter("@p2", string.Join(", ", accessiblebranches.ToArray()))); - - m_context.Database.OpenConnection(); - - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - yield return new SaleItem - { - Transno = reader.GetString(0), - Total = (decimal)reader.GetDouble(1), - Date = reader.GetDateTime(2), - Cashier = reader.GetString(3), - BranchId = reader.GetString(4), - Customer = reader.GetString(5), - Status = reader.GetString(6), - }; - } - } - } - } - } - - public Task FetchTransaction(DateTime a_start, DateTime a_end) - { - throw new NotImplementedException(); - } - - public Task FetchReceipt(string a_receiptId) - { - throw new NotImplementedException(); - } - - public IEnumerable GetReceipt(string a_receiptId) - { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - using (var command = m_context.Database.GetDbConnection().CreateCommand()) - { - command.CommandText = "CALL GetTransactionsById(@p0,@p1)"; - command.Parameters.Add(new MySqlParameter("@p0", a_receiptId)); - command.Parameters.Add(new MySqlParameter("@p1", string.Join(", ", accessiblebranches.ToArray()))); - - m_context.Database.OpenConnection(); - - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - yield return new SaleItem - { - Transno = reader.GetString(0), - Total = (decimal)reader.GetDouble(1), - Date = reader.GetDateTime(2), - Cashier = reader.GetString(3), - BranchId = reader.GetString(4), - Customer = reader.GetString(5), - Status = reader.GetString(6), - }; - } - } - // Close the connection explicitly - m_context.Database.CloseConnection(); - } - } - } - - public Task> GetReceiptDetail(string a_receiptId) - { - List details = new List(); - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - using (var command = m_context.Database.GetDbConnection().CreateCommand()) - { - command.CommandText = "CALL GetReceiptDetails(@p0,@p1)"; - command.Parameters.Add(new MySqlParameter("@p0", a_receiptId)); - command.Parameters.Add(new MySqlParameter("@p1", string.Join(", ", accessiblebranches.ToArray()))); - - m_context.Database.OpenConnection(); - - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - details.Add(new Tblcart - { - Transno = a_receiptId, - Id = reader.GetString(0), - Quantity = reader.GetInt32(1), - Date = reader.GetDateTime(2), - Price = reader.GetDecimal(3), - Cashier = reader.GetString(4), - Status = reader.GetString(5), - Total = (decimal)reader.GetDouble(6), - Unit = reader.GetString(7), - Costprice = reader.GetDecimal(8), - BranchId = reader.GetString(9), - CountId = reader.GetString(10), - Tendered = reader.GetDecimal(11), - Balance = reader.GetDecimal(12), - ValueAddTax = reader.GetDecimal(13) - }); - } - } - } - } - return Task.FromResult(details.AsEnumerable()); - } public async Task SyncCart(List a_item) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_item); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -191,10 +51,10 @@ namespace Cloud_Manager.Services public DateTime GetLastSyncDate(string a_tablename) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - string activeBranch = m_tokenService.GetBaseBranch(token)!; + string activeBranch = m_tokenService.GetBaseBranch(apiKey)!; DateTime? lastSync = m_context.Tblsyncinfos.FirstOrDefault(p => p.TableName == a_tablename && p.BranchId == activeBranch!)?.LastSyncDate; if (lastSync != null) @@ -207,10 +67,10 @@ namespace Cloud_Manager.Services public void SetLastSyncDate(string a_tableName, DateTime a_timestamp) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { - string activeBranch = m_tokenService.GetBaseBranch(token)!; + string activeBranch = m_tokenService.GetBaseBranch(apiKey)!; using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); @@ -227,8 +87,8 @@ namespace Cloud_Manager.Services public async Task SyncCancelledTransaction(List a_item) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_item); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -244,8 +104,8 @@ namespace Cloud_Manager.Services public async Task SyncCreditPurchase(List a_item) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_item); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -261,8 +121,8 @@ namespace Cloud_Manager.Services public async Task SyncCustomerAccount(List a_customerAccounts) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_customerAccounts); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -278,8 +138,8 @@ namespace Cloud_Manager.Services public async Task SyncCustomerPurchase(List a_customerPurchase) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_customerPurchase); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -295,8 +155,8 @@ namespace Cloud_Manager.Services public async Task SyncDiscountLogs(List a_discountLog) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_discountLog); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -312,8 +172,8 @@ namespace Cloud_Manager.Services public async Task SyncDeliveryDetails(List a_details) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -329,8 +189,8 @@ namespace Cloud_Manager.Services public async Task SyncDeliveryHead(List a_heads) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_heads); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -346,8 +206,8 @@ namespace Cloud_Manager.Services public async Task SyncDeliveryRecipients(List a_recipients) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_recipients); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -363,8 +223,8 @@ namespace Cloud_Manager.Services public async Task SyncInvoice(List a_invoice) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_invoice); using (var command = m_context.Database.GetDbConnection().CreateCommand()) @@ -377,5 +237,42 @@ namespace Cloud_Manager.Services } } } + + #region NotImplemented + public Task FetchRecentTransaction(int a_limit) + { + throw new NotImplementedException(); + } + + public Task FetchTransaction(DateTime a_start, DateTime a_end) + { + throw new NotImplementedException(); + } + + public IEnumerable GetTransactions(DateTime a_start, DateTime a_end) + { + throw new NotImplementedException(); + } + + public IEnumerable GetRecentTransaction() + { + throw new NotImplementedException(); + } + + public Task FetchReceipt(string a_receiptId) + { + throw new NotImplementedException(); + } + + public IEnumerable GetReceipt(string a_receiptId) + { + throw new NotImplementedException(); + } + + public Task> GetReceiptDetail(string a_receiptId) + { + throw new NotImplementedException(); + } + #endregion } } diff --git a/Cloud_Manager/Services/UserService.cs b/Cloud_Manager/Services/UserService.cs index b120fe3..e238cab 100644 --- a/Cloud_Manager/Services/UserService.cs +++ b/Cloud_Manager/Services/UserService.cs @@ -12,10 +12,10 @@ namespace Cloud_Manager.Services public class UserService : IUser { private readonly BiskAcdbContext m_context; - private readonly ITokenService m_tokenService; + private readonly IKeyService m_tokenService; private readonly HttpContext m_httpContext; - public UserService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) + public UserService(BiskAcdbContext a_context, IKeyService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; @@ -23,15 +23,7 @@ namespace Cloud_Manager.Services } public IEnumerable FetchUsers() { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) - { - IEnumerable accessiblebranches = m_tokenService.BranchIds(token); - - return m_context.Tblusers.Where(b => accessiblebranches.Contains(b.BranchId)); - } - return new List(); + throw new NotImplementedException(); } public Task> GetUsers() @@ -41,8 +33,8 @@ namespace Cloud_Manager.Services public async Task SyncUserAsync(List a_users) { - string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; - if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + string apiKey = m_httpContext.Request.Headers["BISK-API-KEY"]!; + if (AuthEnums.Valid == m_tokenService.ValidateKey(apiKey)) { string jsonString = JsonSerializer.Serialize(a_users); using (var command = m_context.Database.GetDbConnection().CreateCommand())