From dff6bc2541ae9049ec1e38396f93fdf6746e222d Mon Sep 17 00:00:00 2001 From: mahan Date: Thu, 6 Nov 2025 12:37:06 +0330 Subject: [PATCH] feat: add SMS settings configuration and refactor SMS sending methods for institution creation verification --- 0_Framework/Application/Sms/ISmsService.cs | 2 +- .../InstitutionContractApplication.cs | 4 +- .../InstitutionContractRepository.cs | 2 +- .../Services/SmsService.cs | 79 ++++++++++++++----- ServiceHost/appsettings.Development.json | 4 + ServiceHost/appsettings.json | 4 + 6 files changed, 70 insertions(+), 25 deletions(-) diff --git a/0_Framework/Application/Sms/ISmsService.cs b/0_Framework/Application/Sms/ISmsService.cs index d3915923..bf55eb00 100644 --- a/0_Framework/Application/Sms/ISmsService.cs +++ b/0_Framework/Application/Sms/ISmsService.cs @@ -27,7 +27,7 @@ public interface ISmsService Task GetCreditAmount(); - public Task SendInstitutionVerificationLink(string number, string fullName, Guid institutionId, long contractingPartyId, long institutionContractId); + public Task SendInstitutionCreationVerificationLink(string number, string fullName, Guid institutionId, long contractingPartyId, long institutionContractId); public Task SendInstitutionVerificationCode(string number, string code, string contractingPartyFullName, long contractingPartyId, long institutionContractId); diff --git a/CompanyManagment.Application/InstitutionContractApplication.cs b/CompanyManagment.Application/InstitutionContractApplication.cs index bafa7edf..85504468 100644 --- a/CompanyManagment.Application/InstitutionContractApplication.cs +++ b/CompanyManagment.Application/InstitutionContractApplication.cs @@ -1140,7 +1140,7 @@ public class InstitutionContractApplication : IInstitutionContractApplication CreateContractingPartyAccount(contractingParty.id, res.SendId); - await _smsService.SendInstitutionVerificationLink(contractingParty.Phone, contractingPartyFullName, + await _smsService.SendInstitutionCreationVerificationLink(contractingParty.Phone, contractingPartyFullName, entity.PublicId, contractingParty.id,entity.id ); await _institutionContractRepository.SaveChangesAsync(); @@ -1377,7 +1377,7 @@ public class InstitutionContractApplication : IInstitutionContractApplication if (contractingParty == null) throw new NotFoundException("طرف قرارداد یافت نشد"); var contractingPartyFullName = contractingParty.FName + " " + contractingParty.LName; - await _smsService.SendInstitutionVerificationLink(contractingParty.Phone, contractingPartyFullName, + await _smsService.SendInstitutionCreationVerificationLink(contractingParty.Phone, contractingPartyFullName, institutionContract.PublicId, contractingParty.id, institutionContract.id); return new OperationResult().Succcedded(); } diff --git a/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs b/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs index 535a43d8..12ca036f 100644 --- a/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs +++ b/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs @@ -2342,7 +2342,7 @@ public class InstitutionContractRepository : RepositoryBase _testNumbers; public SmsIr SmsIr { get; set; } @@ -27,6 +29,33 @@ public class SmsService : ISmsService _smsResultRepository = smsResultRepository; SmsIr = new SmsIr("Og5M562igmzJRhQPnq0GdtieYdLgtfikjzxOmeQBPxJjZtyge5Klc046Lfw1mxSa"); + // خواندن تنظیمات SMS از appsettings + var smsSettings = _configuration.GetSection("SmsSettings"); + _isDevEnvironment = smsSettings.GetValue("IsTestMode"); + _testNumbers = smsSettings.GetSection("TestNumbers").Get>() ?? new List(); + } + + /// + /// متد مرکزی برای ارسال پیامک که محیط dev را چک می‌کند + /// + private async Task> VerifySendSmsAsync(string number, int templateId, VerifySendParameter[] parameters) + { + // اگر محیط dev است و شماره‌های تست وجود دارد، به شماره‌های تست ارسال می‌شود + if (_isDevEnvironment && _testNumbers is { Count: > 0 }) + { + // ارسال به همه شماره‌های تست + SmsIrResult lastResult = null; + foreach (var testNumber in _testNumbers) + { + lastResult = await SmsIr.VerifySendAsync(testNumber, templateId, parameters); + } + return lastResult; // برگرداندن نتیجه آخرین ارسال + } + else + { + // ارسال به شماره واقعی + return await SmsIr.VerifySendAsync(number, templateId, parameters); + } } public void Send(string number, string message) @@ -56,12 +85,7 @@ public class SmsService : ISmsService } public bool VerifySend(string number, string message) { - - SmsIr smsIr = new SmsIr("Og5M562igmzJRhQPnq0GdtieYdLgtfikjzxOmeQBPxJjZtyge5Klc046Lfw1mxSa"); - - //var bulkSendResult = smsIr.BulkSendAsync(95007079000006, "your text message", new string[] { "9120000000" }); - - var verificationSendResult = smsIr.VerifySendAsync(number, 768382, new VerifySendParameter[] { new VerifySendParameter("VerificationCode", message) }); + var verificationSendResult = VerifySendSmsAsync(number, 768382, new VerifySendParameter[] { new VerifySendParameter("VerificationCode", message) }); Thread.Sleep(2000); if (verificationSendResult.IsCompletedSuccessfully) { @@ -90,11 +114,7 @@ public class SmsService : ISmsService public bool LoginSend(string number, string message) { - SmsIr smsIr = new SmsIr("Og5M562igmzJRhQPnq0GdtieYdLgtfikjzxOmeQBPxJjZtyge5Klc046Lfw1mxSa"); - - //var bulkSendResult = smsIr.BulkSendAsync(95007079000006, "your text message", new string[] { "9120000000" }); - - var verificationSendResult = smsIr.VerifySendAsync(number, 635330, new VerifySendParameter[] { new VerifySendParameter("LOGINCODE", message) }); + var verificationSendResult = VerifySendSmsAsync(number, 635330, new VerifySendParameter[] { new VerifySendParameter("LOGINCODE", message) }); Thread.Sleep(2000); if (verificationSendResult.IsCompletedSuccessfully) { @@ -119,11 +139,8 @@ public class SmsService : ISmsService public async Task SendVerifyCodeToClient(string number, string code) { - SmsIr smsIr = new SmsIr("Og5M562igmzJRhQPnq0GdtieYdLgtfikjzxOmeQBPxJjZtyge5Klc046Lfw1mxSa"); var result = new SentSmsViewModel(); - //var bulkSendResult = smsIr.BulkSendAsync(95007079000006, "your text message", new string[] { "9120000000" }); - - var sendResult = await smsIr.VerifySendAsync(number, 768382, new VerifySendParameter[] { new VerifySendParameter("VerificationCode", code) }); + var sendResult = await VerifySendSmsAsync(number, 768382, new VerifySendParameter[] { new VerifySendParameter("VerificationCode", code) }); Thread.Sleep(2000); if (sendResult.Message == "موفق") @@ -148,9 +165,8 @@ public class SmsService : ISmsService var checkLength = fullName.Length; if (checkLength > 25) fullName = fullName.Substring(0, 24); - SmsIr smsIr = new SmsIr("Og5M562igmzJRhQPnq0GdtieYdLgtfikjzxOmeQBPxJjZtyge5Klc046Lfw1mxSa"); - var sendResult = smsIr.VerifySendAsync(number, 725814, new VerifySendParameter[] { new VerifySendParameter("FULLNAME", fullName), new VerifySendParameter("USERNAME", userName), new VerifySendParameter("PASSWORD", userName) }); + var sendResult = VerifySendSmsAsync(number, 725814, new VerifySendParameter[] { new VerifySendParameter("FULLNAME", fullName), new VerifySendParameter("USERNAME", userName), new VerifySendParameter("PASSWORD", userName) }); Console.WriteLine(userName + " - " + sendResult.Result.Status); @@ -332,18 +348,39 @@ public class SmsService : ISmsService } } - public async Task SendInstitutionVerificationLink(string number, string fullName, Guid institutionId, long contractingPartyId, long institutionContractId) + + public async Task SendInstitutionCreationVerificationLink(string number, string fullName, Guid institutionId, long contractingPartyId, long institutionContractId) { var guidStr=institutionId.ToString(); var firstPart = guidStr.Substring(0, 15); var secondPart = guidStr.Substring(15); - var verificationSendResult =await SmsIr.VerifySendAsync(number, 527519, new VerifySendParameter[] + var verificationSendResult =await VerifySendSmsAsync(number, 527519, new VerifySendParameter[] { new("FULLNAME", fullName), new("CODE1",firstPart), new("CODE2",secondPart) }); - var smsResult = new SmsResult(verificationSendResult.Data.MessageId, verificationSendResult.Message, "لینک تاییدیه قرارداد مالی", + + var smsResult = new SmsResult(verificationSendResult.Data.MessageId, verificationSendResult.Message, "لینک تاییدیه ایجاد قرارداد مالی", + fullName, number, contractingPartyId, institutionContractId); + await _smsResultRepository.CreateAsync(smsResult); + await _smsResultRepository.SaveChangesAsync(); + return verificationSendResult.Status == 0; + } + + public async Task SendInstitutionAmendmentVerificationLink(string number, string fullName, Guid institutionId, + long contractingPartyId, long institutionContractId) + { + var guidStr=institutionId.ToString(); + var firstPart = guidStr.Substring(0, 15); + var secondPart = guidStr.Substring(15); + var verificationSendResult =await VerifySendSmsAsync(number, 527519, new VerifySendParameter[] + { + new("FULLNAME", fullName), + new("CODE1",firstPart), + new("CODE2",secondPart) + }); + var smsResult = new SmsResult(verificationSendResult.Data.MessageId, verificationSendResult.Message, "لینک تاییدیه ارتقا قرارداد مالی", fullName, number, contractingPartyId, institutionContractId); await _smsResultRepository.CreateAsync(smsResult); await _smsResultRepository.SaveChangesAsync(); @@ -353,7 +390,7 @@ public class SmsService : ISmsService public async Task SendInstitutionVerificationCode(string number, string code, string contractingPartyFullName, long contractingPartyId, long institutionContractId) { - var verificationSendResult =await SmsIr.VerifySendAsync(number, 965348, new VerifySendParameter[] + var verificationSendResult =await VerifySendSmsAsync(number, 965348, new VerifySendParameter[] { new("VERIFYCODE", code) }); diff --git a/ServiceHost/appsettings.Development.json b/ServiceHost/appsettings.Development.json index 39c0d1f3..5efba4b6 100644 --- a/ServiceHost/appsettings.Development.json +++ b/ServiceHost/appsettings.Development.json @@ -38,6 +38,10 @@ "MongoDb": { "ConnectionString": "mongodb://localhost:27017", "DatabaseName": "Gozareshgir" + }, + "SmsSettings": { + "IsTestMode": true, + "TestNumbers": [ "09116967898", "09116067106", "09114221321" ] } } diff --git a/ServiceHost/appsettings.json b/ServiceHost/appsettings.json index eb956d2d..59b613a6 100644 --- a/ServiceHost/appsettings.json +++ b/ServiceHost/appsettings.json @@ -31,5 +31,9 @@ "MongoDb": { "ConnectionString": "mongodb://localhost:27017", "DatabaseName": "Gozareshgir" + }, + "SmsSettings": { + "IsTestMode": false, + "TestNumbers": [] } }