From 83f92ea6c82cf13a75458a65226b2863078fae7b Mon Sep 17 00:00:00 2001 From: dibakor Date: Thu, 6 Aug 2026 16:57:29 +0600 Subject: [PATCH] upate reqyest --- .../Enums.cs | 7 + .../IntegrationCustomerRequest.cs | 3 +- .../Integrations/IntegrationService.cs | 18 +- .../MobileApp/MobileMasterDataService.cs | 14 +- .../Services/Systems/UserService.cs | 518 +++++++++--------- .../Converter/EmptyStringToNullConverter.cs | 23 + Api/OnlineSalesAutoCrop.CoreAPI/Startup.cs | 21 +- App/ClientApp/src/app/app.menu.component.ts | 1 + .../src/app/login/login.component.html | 33 +- .../src/app/login/login.component.ts | 6 +- .../providers/user/authentication.service.ts | 1 + .../src/providers/user/user.service..ts | 4 +- 12 files changed, 323 insertions(+), 326 deletions(-) create mode 100644 Api/OnlineSalesAutoCrop.CoreAPI/Converter/EmptyStringToNullConverter.cs diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Enums.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Enums.cs index cdeeb25..bde1a30 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Enums.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Enums.cs @@ -719,6 +719,13 @@ namespace OnlineSalesAutoCrop.CoreAPI.Models ZDS2 = 3 } + public enum UserTypeEnum : short + { + SuperAdmin = 1, + Admin = 2, + SalesAppOfficer = 3 + } + public enum MaterialPriceTypeEnum : short { BasePrice = 1, diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationCustomerRequest.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationCustomerRequest.cs index 4eec937..18a887c 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationCustomerRequest.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationCustomerRequest.cs @@ -64,9 +64,8 @@ public class IntegrationCustomerRequest [EmailAddress(ErrorMessage = "Email address is not valid.")] public string? EmailAddress { get; set; } - [Required(ErrorMessage = "Business/Tax number is required.")] [StringLength(16, ErrorMessage = "Business/Tax number cannot exceed 16 characters.")] - public string BusinessTaxNumber { get; set; } + public string? BusinessTaxNumber { get; set; } [Required(ErrorMessage = "Credit limit is required.")] [Range(0, 9999999999999.99, ErrorMessage = "Credit limit must be between 0 and 9,999,999,999,999.99.")] diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs index bf39771..0c98d58 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Integrations/IntegrationService.cs @@ -534,16 +534,16 @@ public class IntegrationService : IIntegrationService DivisionDescription = dr.GetString(12), MobileNumber = dr.GetString(13), EmailAddress = dr.GetString(14), - BusinessTaxNumber = dr.GetString(15), + BusinessTaxNumber =dr.IsDBNull(15)? null: dr.GetString(15), CreditLimit = dr.GetDecimal(16), - SalesGroup = dr.GetString(17), - SalesGroupDescription = dr.GetString(18), - CustomerGroup = dr.GetString(19), - CustomerGroupDescription = dr.GetString(20), - CustomerPriceGroup = dr.GetString(21), - CustomerPriceGroupDescription = dr.GetString(22), - StatusCode = dr.GetString(23), - StatusDescription = dr.GetString(24), + SalesGroup = dr.IsDBNull(17) ? null: dr.GetString(17), + SalesGroupDescription = dr.IsDBNull(18) ? null : dr.GetString(18), + CustomerGroup = dr.IsDBNull(19) ? null : dr.GetString(19), + CustomerGroupDescription = dr.IsDBNull(20) ? null : dr.GetString(20), + CustomerPriceGroup = dr.IsDBNull(21) ? null : dr.GetString(21), + CustomerPriceGroupDescription = dr.IsDBNull(22) ? null : dr.GetString(22), + StatusCode = dr.IsDBNull(23) ? null : dr.GetString(23), + StatusDescription = dr.IsDBNull(24) ? null : dr.GetString(24), PaymentTerms = dr.GetString(25), PaymentTermsDescription = dr.GetString(26), SearchTerm = dr.GetString(27), diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/MobileApp/MobileMasterDataService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/MobileApp/MobileMasterDataService.cs index e57952c..9d5c8c0 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/MobileApp/MobileMasterDataService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/MobileApp/MobileMasterDataService.cs @@ -1,4 +1,5 @@ -using Ease.NetCore.DataAccess; +using DocumentFormat.OpenXml.Office2016.Excel; +using Ease.NetCore.DataAccess; using Ease.NetCore.DataAccess.SQL; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Options; @@ -10,6 +11,7 @@ using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.MobileApp; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Systems; +using Org.BouncyCastle.Asn1.Ocsp; using System; using System.Collections.Generic; using System.Data; @@ -374,10 +376,14 @@ public class MobileMasterDataService : IMobileMasterDataService decimal totalApprovedAmount = 0; try { - - using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetCustomerApprovedAmount")) + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@CustomerCode", pType: SqlDbType.NVarChar, pValue: customerCode), + SqlHelperExtension.CreateInParam(pName: "@SalesOrgCode", pType: SqlDbType.NVarChar, pValue: salesOrgCode), + ]; + using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetCustomerApprovedAmount",p)) { - while (dr.Read()) + if (dr.Read()) { totalApprovedAmount = dr.GetDecimal(0); } diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Systems/UserService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Systems/UserService.cs index a25e216..c36eaed 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Systems/UserService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Systems/UserService.cs @@ -39,314 +39,290 @@ namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.Systems public async Task LoginAsync(LoginRequest request, string ipAddress, bool checkPwd) { User user = new() { LoginId = request.LoginId, LoginStatus = EnumLoginStatus.Unsuccessful }; - //try - //{ - // string password = EncryptPassword(password: request.Password); + try + { + string password = EncryptPassword(password: request.Password); - // using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); - // try - // { - // #region Attendance Login and Valid Ip Address + using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); + try + { + + bool batchEnabled = false; + DateTime sysDate = DateTime.Today.Date; + string appVer = string.Empty, alParams = string.Empty; + int maxTryCount = 5, lockTime = 1, marMonths = 24, idleTime = 0, timeoutTime = 0, pingTime = 0, bmProcessId = 0, prProcessId = 0; - // if (request.AttendanceLogin && !request.LoginId.ToLower().Equals(User.SuperUser_LoginId)) - // { - // string errMsg = string.Empty; - // SqlParameter[] p = - // [ - // SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: request.IpAddress, size: 20), - // SqlHelperExtension.CreateInParam(pName: "@MacAddress", pType: SqlDbType.VarChar, pValue: request.MacAddress, size: 30), - // SqlHelperExtension.CreateInParam(pName: "@HostName", pType: SqlDbType.VarChar, pValue: request.HostName, size: 100), - // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: request.LoginId, size: 50), - // SqlHelperExtension.CreateOutParam(pName: "@ErrMsg", pType: SqlDbType.VarChar, pValue: errMsg, size: 300) - // ]; - // _ = tc.ExecuteNonQuerySp(spName: "dbo.IsValidIpOrMacAddress", parameterValues: p); + #region Read Params from ThisSystem - // errMsg = (p[4] == null || p[4].Value == null || p[4].Value == DBNull.Value) ? string.Empty : Convert.ToString(p[4].Value); - // if (!string.IsNullOrEmpty(errMsg)) - // { - // tc.End(); - // throw new InvalidOperationException(errMsg); - // } - // } + using (IDataReader dr = tc.ExecuteReader("SELECT MaxTryCount, LockTime, MarMonths, CAST(GETDATE() as date), AppVersion, AutoLogoutParams, BatchEnabled, BmProcessId, PrProcessId FROM ThisSystem")) + { + if (dr.Read()) + { + maxTryCount = dr.GetInt16(0); + lockTime = dr.GetInt16(1); + marMonths = dr.GetInt16(2); + sysDate = dr.GetDateTime(3); + appVer = dr.GetString(4); + alParams = dr.IsDBNull(5) ? string.Empty : dr.GetString(5); + batchEnabled = !dr.IsDBNull(6) && dr.GetInt16(6) != 0; + bmProcessId = dr.IsDBNull(7) ? 0 : dr.GetInt16(7); + prProcessId = dr.IsDBNull(8) ? 0 : dr.GetInt16(8); + } + dr.Close(); + } - // #endregion + if (!string.IsNullOrEmpty(alParams)) + { + string[] times = alParams.Split(separator: ',', options: StringSplitOptions.RemoveEmptyEntries); + if (times.Length == 3) + { + if (!int.TryParse(times[0], out idleTime)) + idleTime = 0; - // bool batchEnabled = false; - // DateTime sysDate = DateTime.Today.Date; - // string appVer = string.Empty, alParams = string.Empty; - // int maxTryCount = 5, lockTime = 1, marMonths = 24, idleTime = 0, timeoutTime = 0, pingTime = 0, bmProcessId = 0, prProcessId = 0; + if (!int.TryParse(times[1], out timeoutTime)) + timeoutTime = 0; - // #region Read Params from ThisSystem + if (!int.TryParse(times[2], out pingTime)) + pingTime = 0; + } + } - // using (IDataReader dr = tc.ExecuteReader("SELECT MaxTryCount, LockTime, MarMonths, CAST(GETDATE() as date), AppVersion, AutoLogoutParams, BatchEnabled, BmProcessId, PrProcessId FROM ThisSystem")) - // { - // if (dr.Read()) - // { - // maxTryCount = dr.GetInt16(0); - // lockTime = dr.GetInt16(1); - // marMonths = dr.GetInt16(2); - // sysDate = dr.GetDateTime(3); - // appVer = dr.GetString(4); - // alParams = dr.IsDBNull(5) ? string.Empty : dr.GetString(5); - // batchEnabled = !dr.IsDBNull(6) && dr.GetInt16(6) != 0; - // bmProcessId = dr.IsDBNull(7) ? 0 : dr.GetInt16(7); - // prProcessId = dr.IsDBNull(8) ? 0 : dr.GetInt16(8); - // } - // dr.Close(); - // } + if (!request.LoginId.ToLower().Equals(User.SuperUser_LoginId) && !request.AppVersion.Equals(appVer)) + { + user.UnsuccessfulMsg = appVer; + user.LoginStatus = EnumLoginStatus.VersionMismatch; + } - // if (!string.IsNullOrEmpty(alParams)) - // { - // string[] times = alParams.Split(separator: ',', options: StringSplitOptions.RemoveEmptyEntries); - // if (times.Length == 3) - // { - // if (!int.TryParse(times[0], out idleTime)) - // idleTime = 0; + #endregion - // if (!int.TryParse(times[1], out timeoutTime)) - // timeoutTime = 0; + //if (user.LoginStatus != EnumLoginStatus.VersionMismatch) + //{ + // #region Read User data using authentication data - // if (!int.TryParse(times[2], out pingTime)) - // pingTime = 0; - // } - // } + // string commandText; + // if (!checkPwd) + // { + // commandText = SQLParser.MakeSQL("SELECT UserId, UserName, Status, MobileNo, EmailAddress, AuthReqAtlogin, AuthMethod," + // + " AuthKey, AppId, AccessStatus, NeverExpire, LastPasswords, LastPassChgDate, ExpireDate, ThemeName, SchemeName, MenuLayout," + // + " IsLocked, NextLoginTime, DBOnStartup, DAMultiLogin, ViewOwnTaskOnly, EmployeeId, LoginID, EmployeeCode FROM Users WHERE LoginID=%s", request.LoginId); + // } + // else + // { + // commandText = SQLParser.MakeSQL("SELECT UserId, UserName, Status, MobileNo, EmailAddress, AuthReqAtlogin, AuthMethod," + // + " AuthKey, AppId, AccessStatus, NeverExpire, LastPasswords, LastPassChgDate, ExpireDate, ThemeName, SchemeName, MenuLayout," + // + " IsLocked, NextLoginTime, DBOnStartup, DAMultiLogin, ViewOwnTaskOnly, EmployeeId, LoginID, EmployeeCode FROM Users" + // + " WHERE (LoginID=%s OR MobileNo=%s OR EmailAddress=%s) AND Password=%s", request.LoginId, request.LoginId, request.LoginId, password); + // } - // if (!request.LoginId.ToLower().Equals(User.SuperUser_LoginId) && !request.AppVersion.Equals(appVer)) - // { - // user.UnsuccessfulMsg = appVer; - // user.LoginStatus = EnumLoginStatus.VersionMismatch; - // } + // using (IDataReader dr = tc.ExecuteReader(commandText: commandText)) + // { + // if (dr.Read()) + // { + // user = new User + // { + // Id = dr.GetInt32(0), + // UserName = dr.GetString(1), + // Status = (EnumStatus)dr.GetInt16(2), + // MobileNo = dr.GetString(3), + // EmailAddress = dr.GetString(4), + // AuthRequiredAtLogin = !dr.IsDBNull(5) && dr.GetInt16(5) > 0, + // AuthMethod = (EnumAuthenticationMethod)dr.GetInt16(6), + // AuthKey = dr.IsDBNull(7) ? string.Empty : dr.GetString(7), + // AppId = dr.IsDBNull(8) ? string.Empty : dr.GetString(8), + // AccessStatus = (EnumAccessStatus)dr.GetInt16(9), + // NeverExpires = !dr.IsDBNull(10) && dr.GetInt16(10) > 0, + // LastPasswords = dr.IsDBNull(11) ? string.Empty : dr.GetString(11), + // LastPassChgDate = dr.IsDBNull(12) ? null : dr.GetDateTime(12), + // ExpireDate = dr.IsDBNull(13) ? null : dr.GetDateTime(13), + // ThemeName = dr.IsDBNull(14) ? "yellow" : dr.GetString(14), + // SchemeName = dr.IsDBNull(15) ? "dark" : dr.GetString(15), + // MenuLayout = dr.IsDBNull(16) ? "static" : dr.GetString(16), + // IsLocked = !dr.IsDBNull(17) && dr.GetInt16(17) > 0, + // NextLoginTime = dr.IsDBNull(18) ? null : dr.GetDateTime(18), + // DbOnStartup = dr.GetInt16(19) != 0, + // DisallowMultiLogin = dr.GetInt16(20) != 0, + // ViewOwnTaskOnly = dr.GetInt16(21) != 0, + // EmployeeId = dr.IsDBNull(22) ? null : dr.GetInt32(22), + // LoginId = dr.GetString(23), + // EmployeeCode = dr.IsDBNull(24) ? string.Empty : dr.GetString(24), - // #endregion + // TeamSpaceIds = [], + // IdleTime = idleTime, + // PingTime = pingTime, + // SystemDate = sysDate, + // TimeoutTime = timeoutTime, + // PrProcessId = prProcessId, + // BmProcessId = bmProcessId, + // BatchEnabled = batchEnabled, + // LoginStatus = EnumLoginStatus.Success + // }; + // } + // dr.Close(); - // if (user.LoginStatus != EnumLoginStatus.VersionMismatch) - // { - // #region Read User data using authentication data + // user.MinReportDate = marMonths <= 0 ? new DateTime(year: 2015, month: 1, day: 1, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Local) : sysDate.AddMonths(-1 * marMonths); + // } - // string commandText; - // if (!checkPwd) - // { - // commandText = SQLParser.MakeSQL("SELECT UserId, UserName, Status, MobileNo, EmailAddress, AuthReqAtlogin, AuthMethod," - // + " AuthKey, AppId, AccessStatus, NeverExpire, LastPasswords, LastPassChgDate, ExpireDate, ThemeName, SchemeName, MenuLayout," - // + " IsLocked, NextLoginTime, DBOnStartup, DAMultiLogin, ViewOwnTaskOnly, EmployeeId, LoginID, EmployeeCode FROM Users WHERE LoginID=%s", request.LoginId); - // } - // else - // { - // commandText = SQLParser.MakeSQL("SELECT UserId, UserName, Status, MobileNo, EmailAddress, AuthReqAtlogin, AuthMethod," - // + " AuthKey, AppId, AccessStatus, NeverExpire, LastPasswords, LastPassChgDate, ExpireDate, ThemeName, SchemeName, MenuLayout," - // + " IsLocked, NextLoginTime, DBOnStartup, DAMultiLogin, ViewOwnTaskOnly, EmployeeId, LoginID, EmployeeCode FROM Users" - // + " WHERE (LoginID=%s OR MobileNo=%s OR EmailAddress=%s) AND Password=%s", request.LoginId, request.LoginId, request.LoginId, password); - // } + // #endregion - // using (IDataReader dr = tc.ExecuteReader(commandText: commandText)) - // { - // if (dr.Read()) - // { - // user = new User - // { - // Id = dr.GetInt32(0), - // UserName = dr.GetString(1), - // Status = (EnumStatus)dr.GetInt16(2), - // MobileNo = dr.GetString(3), - // EmailAddress = dr.GetString(4), - // AuthRequiredAtLogin = !dr.IsDBNull(5) && dr.GetInt16(5) > 0, - // AuthMethod = (EnumAuthenticationMethod)dr.GetInt16(6), - // AuthKey = dr.IsDBNull(7) ? string.Empty : dr.GetString(7), - // AppId = dr.IsDBNull(8) ? string.Empty : dr.GetString(8), - // AccessStatus = (EnumAccessStatus)dr.GetInt16(9), - // NeverExpires = !dr.IsDBNull(10) && dr.GetInt16(10) > 0, - // LastPasswords = dr.IsDBNull(11) ? string.Empty : dr.GetString(11), - // LastPassChgDate = dr.IsDBNull(12) ? null : dr.GetDateTime(12), - // ExpireDate = dr.IsDBNull(13) ? null : dr.GetDateTime(13), - // ThemeName = dr.IsDBNull(14) ? "yellow" : dr.GetString(14), - // SchemeName = dr.IsDBNull(15) ? "dark" : dr.GetString(15), - // MenuLayout = dr.IsDBNull(16) ? "static" : dr.GetString(16), - // IsLocked = !dr.IsDBNull(17) && dr.GetInt16(17) > 0, - // NextLoginTime = dr.IsDBNull(18) ? null : dr.GetDateTime(18), - // DbOnStartup = dr.GetInt16(19) != 0, - // DisallowMultiLogin = dr.GetInt16(20) != 0, - // ViewOwnTaskOnly = dr.GetInt16(21) != 0, - // EmployeeId = dr.IsDBNull(22) ? null : dr.GetInt32(22), - // LoginId = dr.GetString(23), - // EmployeeCode = dr.IsDBNull(24) ? string.Empty : dr.GetString(24), + // #region If the user was locked, try set set unlock if Time expired - // TeamSpaceIds = [], - // IdleTime = idleTime, - // PingTime = pingTime, - // SystemDate = sysDate, - // TimeoutTime = timeoutTime, - // PrProcessId = prProcessId, - // BmProcessId = bmProcessId, - // BatchEnabled = batchEnabled, - // LoginStatus = EnumLoginStatus.Success - // }; - // } - // dr.Close(); + // if (!request.LoginId.ToLower().Equals(User.SuperUser_LoginId) && user.IsLocked) + // { + // int isSuccessful = 0; + // SqlParameter[] p = + // [ + // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: request.LoginId, size: 30), + // SqlHelperExtension.CreateInParam(pName: "@LockTime", pType: SqlDbType.Int, pValue: lockTime), + // SqlHelperExtension.CreateOutParam(pName: "@IsSuccessful", pType: SqlDbType.Int, pValue: isSuccessful), + // ]; + // _ = tc.ExecuteNonQuerySp(spName: "dbo.DoUnlockUser", parameterValues: p); + // if (p[2] != null && p[2].Value != null && p[2].Value != DBNull.Value) + // isSuccessful = Convert.ToInt32(p[2].Value); - // user.MinReportDate = marMonths <= 0 ? new DateTime(year: 2015, month: 1, day: 1, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Local) : sysDate.AddMonths(-1 * marMonths); - // } + // if (isSuccessful == 1) + // user.IsLocked = false; + // } - // #endregion + // #endregion - // #region If the user was locked, try set set unlock if Time expired + // #region Keep log for unauthrise access and Set user lock if exceeds max try - // if (!request.LoginId.ToLower().Equals(User.SuperUser_LoginId) && user.IsLocked) - // { - // int isSuccessful = 0; - // SqlParameter[] p = - // [ - // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: request.LoginId, size: 30), - // SqlHelperExtension.CreateInParam(pName: "@LockTime", pType: SqlDbType.Int, pValue: lockTime), - // SqlHelperExtension.CreateOutParam(pName: "@IsSuccessful", pType: SqlDbType.Int, pValue: isSuccessful), - // ]; - // _ = tc.ExecuteNonQuerySp(spName: "dbo.DoUnlockUser", parameterValues: p); - // if (p[2] != null && p[2].Value != null && p[2].Value != DBNull.Value) - // isSuccessful = Convert.ToInt32(p[2].Value); + // if (!request.LoginId.ToLower().Equals(User.SuperUser_LoginId) && maxTryCount > 0 && user.LoginStatus == EnumLoginStatus.Unsuccessful) + // { + // int remainsTry = 0; + // DateTime? nextLoginTime = null; + // string tryLoginInfo = $"{request.LoginId}~{password}~13"; + // SqlParameter[] p = + // [ + // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: request.LoginId, size: 30), + // SqlHelperExtension.CreateInParam(pName: "@TryLoginInfo", pType: SqlDbType.VarChar, pValue: tryLoginInfo, size: 100), + // SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: ipAddress, size: 20), + // SqlHelperExtension.CreateInParam(pName: "@MaxTryCount", pType: SqlDbType.SmallInt, pValue: maxTryCount), + // SqlHelperExtension.CreateInParam(pName: "@LockTime", pType: SqlDbType.Int, pValue: lockTime), + // SqlHelperExtension.CreateOutParam(pName: "@RemainingTry", pType: SqlDbType.SmallInt, pValue: remainsTry), + // SqlHelperExtension.CreateOutParam(pName: "@NextLoginTime", pType: SqlDbType.DateTime, pValue: nextLoginTime), + // ]; + // _ = tc.ExecuteNonQuerySp(spName: "dbo.LogUnauthorizeAccess", parameterValues: p); + // if (p[5] != null && p[5].Value != null && p[5].Value != DBNull.Value) + // remainsTry = Convert.ToInt32(p[5].Value); - // if (isSuccessful == 1) - // user.IsLocked = false; - // } + // if (p[6] != null && p[6].Value != null && p[6].Value != DBNull.Value) + // nextLoginTime = Convert.ToDateTime(p[6].Value); - // #endregion + // if (remainsTry <= 0) + // { + // user.IsLocked = true; + // if (lockTime <= 0) + // { + // user.NextLoginTime = nextLoginTime; + // user.UnsuccessfulMsg = "Please contact with Head office to Unlock"; + // } + // else + // { + // user.NextLoginTime = nextLoginTime; + // user.UnsuccessfulMsg = $"You can Login after {user.NextLoginTime:dd-MMM-yyyy H:mm:ss}"; + // } + // } + // else + // { + // user.UnsuccessfulMsg = $"{remainsTry} More attempt{(remainsTry > 1 ? "s" : "")} remaining"; + // } + // } - // #region Keep log for unauthrise access and Set user lock if exceeds max try + // #endregion - // if (!request.LoginId.ToLower().Equals(User.SuperUser_LoginId) && maxTryCount > 0 && user.LoginStatus == EnumLoginStatus.Unsuccessful) - // { - // int remainsTry = 0; - // DateTime? nextLoginTime = null; - // string tryLoginInfo = $"{request.LoginId}~{password}~13"; - // SqlParameter[] p = - // [ - // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: request.LoginId, size: 30), - // SqlHelperExtension.CreateInParam(pName: "@TryLoginInfo", pType: SqlDbType.VarChar, pValue: tryLoginInfo, size: 100), - // SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: ipAddress, size: 20), - // SqlHelperExtension.CreateInParam(pName: "@MaxTryCount", pType: SqlDbType.SmallInt, pValue: maxTryCount), - // SqlHelperExtension.CreateInParam(pName: "@LockTime", pType: SqlDbType.Int, pValue: lockTime), - // SqlHelperExtension.CreateOutParam(pName: "@RemainingTry", pType: SqlDbType.SmallInt, pValue: remainsTry), - // SqlHelperExtension.CreateOutParam(pName: "@NextLoginTime", pType: SqlDbType.DateTime, pValue: nextLoginTime), - // ]; - // _ = tc.ExecuteNonQuerySp(spName: "dbo.LogUnauthorizeAccess", parameterValues: p); - // if (p[5] != null && p[5].Value != null && p[5].Value != DBNull.Value) - // remainsTry = Convert.ToInt32(p[5].Value); + // #region Generate and Save Otp if Otp is enabled and send thru SMS/Email - // if (p[6] != null && p[6].Value != null && p[6].Value != DBNull.Value) - // nextLoginTime = Convert.ToDateTime(p[6].Value); + // if (user.LoginStatus == EnumLoginStatus.Success && user.Status == EnumStatus.Authorized && (user.AuthMethod == EnumAuthenticationMethod.Email || user.AuthMethod == EnumAuthenticationMethod.MobileSMS)) + // { + // string secretKey = $"{user.Id}~{user.LoginId}"; + // secretKey = secretKey.EncodeAsBase32String(addPadding: false); + // user.AuthValue = TOtpService.GetCurrentPIN(secretKey: secretKey); + // SetAuthValue(tc: tc, authValue: user.AuthValue, validMinutes: 5, userId: user.Id); + // } - // if (remainsTry <= 0) - // { - // user.IsLocked = true; - // if (lockTime <= 0) - // { - // user.NextLoginTime = nextLoginTime; - // user.UnsuccessfulMsg = "Please contact with Head office to Unlock"; - // } - // else - // { - // user.NextLoginTime = nextLoginTime; - // user.UnsuccessfulMsg = $"You can Login after {user.NextLoginTime:dd-MMM-yyyy H:mm:ss}"; - // } - // } - // else - // { - // user.UnsuccessfulMsg = $"{remainsTry} More attempt{(remainsTry > 1 ? "s" : "")} remaining"; - // } - // } + // #endregion - // #endregion + // #region If login successful and user is active read module id for this user - // #region Generate and Save Otp if Otp is enabled and send thru SMS/Email + // if (user.LoginStatus == EnumLoginStatus.Success && user.Status == EnumStatus.Authorized && !user.IsLocked) + // { + // int logId = 0; + // DateTime? logoutTime = null; + // SqlParameter[] p = + // [ + // SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: user.Id), + // SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: ipAddress, size: 20), + // SqlHelperExtension.CreateInParam(pName: "@AppId", pType: SqlDbType.VarChar, pValue: request.AppId, size: 250), + // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: user.LoginId, size: 30), + // SqlHelperExtension.CreateInParam(pName: "@LockTime", pType: SqlDbType.Int, pValue: lockTime), + // SqlHelperExtension.CreateInParam(pName: "@AttendanceLogin", pType: SqlDbType.Int, pValue: request.AttendanceLogin? 1:0), + // SqlHelperExtension.CreateInParam(pName: "@LocalIp", pType: SqlDbType.VarChar, pValue: request.IpAddress, size: 20), + // SqlHelperExtension.CreateInParam(pName: "@MacAddress", pType: SqlDbType.VarChar, pValue: request.MacAddress, size: 30), + // SqlHelperExtension.CreateInParam(pName: "@HostName", pType: SqlDbType.VarChar, pValue: request.HostName, size: 100), + // SqlHelperExtension.CreateInParam(pName: "@LoginRemarks", pType: SqlDbType.VarChar, pValue: request.LoginRemarks, size: 50) + // ]; + // using (IDataReader dr = tc.ExecuteReaderSp(spName: "dbo.GetPermissionKeys", parameterValues: p)) + // { + // user.ModuleIds = []; + // while (dr.Read()) + // { + // string moduleId = dr.GetString(0); + // user.ModuleIds.Add(moduleId); - // if (user.LoginStatus == EnumLoginStatus.Success && user.Status == EnumStatus.Authorized && (user.AuthMethod == EnumAuthenticationMethod.Email || user.AuthMethod == EnumAuthenticationMethod.MobileSMS)) - // { - // string secretKey = $"{user.Id}~{user.LoginId}"; - // secretKey = secretKey.EncodeAsBase32String(addPadding: false); - // user.AuthValue = TOtpService.GetCurrentPIN(secretKey: secretKey); - // SetAuthValue(tc: tc, authValue: user.AuthValue, validMinutes: 5, userId: user.Id); - // } + // logId = dr.GetInt32(1); - // #endregion + // if (dr.GetInt16(2) != 0) //Alow add + // { + // user.ModuleIds.Add($"{moduleId}_1"); + // } + // if (dr.GetInt16(3) != 0) //Alow edit + // { + // user.ModuleIds.Add($"{moduleId}_2"); + // } - // #region If login successful and user is active read module id for this user + // if (dr.GetInt16(4) != 0) //Allow Delete + // { + // user.ModuleIds.Add($"{moduleId}_3"); + // } - // if (user.LoginStatus == EnumLoginStatus.Success && user.Status == EnumStatus.Authorized && !user.IsLocked) - // { - // int logId = 0; - // DateTime? logoutTime = null; - // SqlParameter[] p = - // [ - // SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: user.Id), - // SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: ipAddress, size: 20), - // SqlHelperExtension.CreateInParam(pName: "@AppId", pType: SqlDbType.VarChar, pValue: request.AppId, size: 250), - // SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: user.LoginId, size: 30), - // SqlHelperExtension.CreateInParam(pName: "@LockTime", pType: SqlDbType.Int, pValue: lockTime), - // SqlHelperExtension.CreateInParam(pName: "@AttendanceLogin", pType: SqlDbType.Int, pValue: request.AttendanceLogin? 1:0), - // SqlHelperExtension.CreateInParam(pName: "@LocalIp", pType: SqlDbType.VarChar, pValue: request.IpAddress, size: 20), - // SqlHelperExtension.CreateInParam(pName: "@MacAddress", pType: SqlDbType.VarChar, pValue: request.MacAddress, size: 30), - // SqlHelperExtension.CreateInParam(pName: "@HostName", pType: SqlDbType.VarChar, pValue: request.HostName, size: 100), - // SqlHelperExtension.CreateInParam(pName: "@LoginRemarks", pType: SqlDbType.VarChar, pValue: request.LoginRemarks, size: 50) - // ]; - // using (IDataReader dr = tc.ExecuteReaderSp(spName: "dbo.GetPermissionKeys", parameterValues: p)) - // { - // user.ModuleIds = []; - // while (dr.Read()) - // { - // string moduleId = dr.GetString(0); - // user.ModuleIds.Add(moduleId); + // logoutTime = dr.IsDBNull(5) ? null : dr.GetDateTime(5); + // } + // dr.Close(); + // } + // user.LogId = logId; + // user.LogoutTime = logoutTime; - // logId = dr.GetInt32(1); + // //Read User TeamSpace Ids + // using (IDataReader dr = tc.ExecuteReader("SELECT TeamSpaceId FROM TeamSpaceUsers WHERE UserId=%n", user.Id)) + // { + // while (dr.Read()) + // { + // user.TeamSpaceIds.Add(dr.GetInt32(0)); + // } + // dr.Close(); + // } - // if (dr.GetInt16(2) != 0) //Alow add - // { - // user.ModuleIds.Add($"{moduleId}_1"); - // } - // if (dr.GetInt16(3) != 0) //Alow edit - // { - // user.ModuleIds.Add($"{moduleId}_2"); - // } + // //Pending Notification count + // user.NotificationCount = GetPendingNotifCount(tc: tc, userId: user.Id); + // } - // if (dr.GetInt16(4) != 0) //Allow Delete - // { - // user.ModuleIds.Add($"{moduleId}_3"); - // } + // #endregion + //} - // logoutTime = dr.IsDBNull(5) ? null : dr.GetDateTime(5); - // } - // dr.Close(); - // } - // user.LogId = logId; - // user.LogoutTime = logoutTime; + tc.End(); + } + catch (Exception ie) + { + tc?.HandleError(); - // //Read User TeamSpace Ids - // using (IDataReader dr = tc.ExecuteReader("SELECT TeamSpaceId FROM TeamSpaceUsers WHERE UserId=%n", user.Id)) - // { - // while (dr.Read()) - // { - // user.TeamSpaceIds.Add(dr.GetInt32(0)); - // } - // dr.Close(); - // } - - // //Pending Notification count - // user.NotificationCount = GetPendingNotifCount(tc: tc, userId: user.Id); - // } - - // #endregion - // } - - // tc.End(); - // } - // catch (Exception ie) - // { - // tc?.HandleError(); - - // throw DBCustomError.GenerateCustomError(ie); - // } - //} - //catch (Exception e) - //{ - // throw new InvalidOperationException(e.Message, e); - //} + throw DBCustomError.GenerateCustomError(ie); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } return user; } diff --git a/Api/OnlineSalesAutoCrop.CoreAPI/Converter/EmptyStringToNullConverter.cs b/Api/OnlineSalesAutoCrop.CoreAPI/Converter/EmptyStringToNullConverter.cs new file mode 100644 index 0000000..ef122ae --- /dev/null +++ b/Api/OnlineSalesAutoCrop.CoreAPI/Converter/EmptyStringToNullConverter.cs @@ -0,0 +1,23 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace OnlineSalesAutoCrop.CoreAPI.Converter; + +public class EmptyStringToNullConverter : JsonConverter +{ + public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.Null) + return null; + + var value = reader.GetString(); + + return string.IsNullOrWhiteSpace(value) ? null : value; + } + + public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) + { + writer.WriteStringValue(value); + } +} \ No newline at end of file diff --git a/Api/OnlineSalesAutoCrop.CoreAPI/Startup.cs b/Api/OnlineSalesAutoCrop.CoreAPI/Startup.cs index f1879fe..f5250a4 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI/Startup.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI/Startup.cs @@ -55,11 +55,11 @@ namespace OnlineSalesAutoCrop.CoreAPI /// public IConfiguration Configuration { get; } - /// - /// Initializes a new instance of the Startup class using the specified configuration settings. - /// - /// The configuration settings used to initialize the application. Cannot be null. - public Startup(IConfiguration configuration) + /// + /// Initializes a new instance of the Startup class using the specified configuration settings. + /// + /// The configuration settings used to initialize the application. Cannot be null. + public Startup(IConfiguration configuration) { Configuration = configuration; _appSettings = Configuration.GetSection("AppSettings").Get(); @@ -86,14 +86,21 @@ namespace OnlineSalesAutoCrop.CoreAPI #region Controllers services.AddControllers(options => + { + options.Filters.Add(new ProducesAttribute("application/json")); + }).AddJsonOptions(options => { - options.Filters.Add(new ProducesAttribute("application/json")); + options.JsonSerializerOptions.Converters.Add( + new CoreAPI.Converter.EmptyStringToNullConverter()); }); services.Configure(options => { options.InvalidModelStateResponseFactory = context => { + var logger = context.HttpContext.RequestServices + .GetRequiredService>(); + var errors = context.ModelState .Where(kvp => kvp.Value?.Errors.Count > 0) .SelectMany(kvp => kvp.Value!.Errors.Select(e => e.ErrorMessage)) @@ -106,6 +113,8 @@ namespace OnlineSalesAutoCrop.CoreAPI Data = null }; + logger.LogError("Validation Failed. Request: {@Errors}", errors); + return new BadRequestObjectResult(response); }; }); diff --git a/App/ClientApp/src/app/app.menu.component.ts b/App/ClientApp/src/app/app.menu.component.ts index c8348ef..3252b46 100644 --- a/App/ClientApp/src/app/app.menu.component.ts +++ b/App/ClientApp/src/app/app.menu.component.ts @@ -31,6 +31,7 @@ export class AppMenuComponent implements OnInit, AfterViewInit ngOnInit() { + debugger; this.service.authService.sessionEvent.subscribe(value => this.onAuthenication(value)); if (this.service.authService.currentUserValue) { diff --git a/App/ClientApp/src/app/login/login.component.html b/App/ClientApp/src/app/login/login.component.html index a40b960..b4d4789 100644 --- a/App/ClientApp/src/app/login/login.component.html +++ b/App/ClientApp/src/app/login/login.component.html @@ -1,7 +1,7 @@