| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using SVETA.Api.Helpers.Authorize; |
| | | 3 | | using SVETA.Api.Services.Interfaces; |
| | | 4 | | using System; |
| | | 5 | | using System.Linq; |
| | | 6 | | using CrmVtbc; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | using SVETA.Api.Data.Domain; |
| | | 10 | | using WinSolutions.Sveta.Common; |
| | | 11 | | using System.Net.Http; |
| | | 12 | | using System.Security.Claims; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using Newtonsoft.Json; |
| | | 15 | | using Newtonsoft.Json.Converters; |
| | | 16 | | using Newtonsoft.Json.Serialization; |
| | | 17 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 18 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 19 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 20 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 21 | | using Microsoft.Extensions.Logging; |
| | | 22 | | using SVETA.Api.Helpers; |
| | | 23 | | using SVETA.API.IntegrationTest.Common; |
| | | 24 | | using SVETA.Api.Data.DTO.Sms; |
| | | 25 | | using WinSolutions.Sveta.Server.Services.Implements; |
| | | 26 | | |
| | | 27 | | namespace SVETA.Api.Services.Implements |
| | | 28 | | { |
| | | 29 | | public class SmsWorker : ISmsWorker |
| | | 30 | | { |
| | | 31 | | private readonly ILogger<SmsWorker> _logger; |
| | | 32 | | private readonly SmsSettings _smsOptions; |
| | | 33 | | private readonly HttpClient httpClient; |
| | | 34 | | private readonly ISmsHistoryService _smsHistoryService; |
| | | 35 | | |
| | 83 | 36 | | public SmsWorker(ILogger<SmsWorker> logger, IOptions<SmsSettings> smsOptions, ISmsHistoryService smsHistoryServi |
| | 83 | 37 | | { |
| | 83 | 38 | | _logger = logger; |
| | 83 | 39 | | _smsHistoryService = smsHistoryService; |
| | 83 | 40 | | _smsOptions = smsOptions.Value; |
| | 83 | 41 | | httpClient = new HttpClient(); |
| | 83 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// получает токен для методов работы с смс |
| | | 46 | | /// </summary> |
| | | 47 | | /// <returns></returns> |
| | | 48 | | private async Task<string> GetToken() |
| | 0 | 49 | | { |
| | 0 | 50 | | var response = await httpClient.PostAsync(_smsOptions.AuthService, |
| | 0 | 51 | | StringContentCreator.CreateContent(new { login = _smsOptions.Login, Password = _smsOptions.Password.Encr |
| | 0 | 52 | | SymmetricCrypto.DecryptData(Convert.FromBase64String(_smsOptions.Password.Value)) : |
| | 0 | 53 | | _smsOptions.Password.Value })); |
| | 0 | 54 | | var token = JsonConvert.DeserializeObject<SmsToken>(await response.Content.ReadAsStringAsync()).Token; |
| | 0 | 55 | | return token; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Отправляет смс по шаблону |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="patternId">id шаблона</param> |
| | | 62 | | /// <param name="phone">телефон в формате +7</param> |
| | | 63 | | /// <param name="contragentExtKey">внешний ключ контрагента</param> |
| | | 64 | | /// <param name="properties">словарь с переменными для шаблона</param> |
| | | 65 | | /// <returns></returns> |
| | | 66 | | public async Task SendSmsByPattern(string patternId, string phone, string contragentExtKey, Dictionary<string,st |
| | 152 | 67 | | { |
| | | 68 | | try |
| | 152 | 69 | | { |
| | 152 | 70 | | if (!_smsOptions.SmsSendOn) |
| | 152 | 71 | | return; |
| | 0 | 72 | | var data = new SmsPatternRequestDTO() |
| | 0 | 73 | | { |
| | 0 | 74 | | CompanyId = contragentExtKey, |
| | 0 | 75 | | PatternId = patternId, |
| | 0 | 76 | | Phone = phone, |
| | 0 | 77 | | Properties = properties |
| | 0 | 78 | | }; |
| | 0 | 79 | | var token = await GetToken(); |
| | 0 | 80 | | if (string.IsNullOrWhiteSpace(token)) |
| | 0 | 81 | | { |
| | 0 | 82 | | _logger.LogError("Не удалось отправить смс. Не получен токен для отправки смс."); |
| | 0 | 83 | | await _smsHistoryService.Create(PrepareRecord(data, 0, "Не удалось получить токен для отправки смс." |
| | 0 | 84 | | return; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | StringContent requestContent = StringContentCreator.CreateContent(data); |
| | 0 | 88 | | httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(" |
| | 0 | 89 | | var response = await httpClient.PostAsync(_smsOptions.SmsService + _smsOptions.SendByPattern, requestCon |
| | 0 | 90 | | var content = await response.Content.ReadAsStringAsync(); |
| | 0 | 91 | | if (!response.IsSuccessStatusCode) |
| | 0 | 92 | | _logger.LogError($"Не удалось отправить смс. Статус {(int)response.StatusCode}. Ответ {content}"); |
| | 0 | 93 | | await _smsHistoryService.Create(PrepareRecord(data, (int)response.StatusCode, content)); |
| | 0 | 94 | | } |
| | 0 | 95 | | catch (Exception e) |
| | 0 | 96 | | { |
| | 0 | 97 | | _logger.LogError($"Не удалось отправить смс"); |
| | 0 | 98 | | _logger.LogError(e.Message + "\n" + e.StackTrace); |
| | 0 | 99 | | } |
| | 152 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Преобразует объект SmsPatternRequestDTO в SmsHistory |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="data">объект SmsPatternRequestDTO</param> |
| | | 106 | | /// <param name="statusCode">код ответа от смс гейта</param> |
| | | 107 | | /// <param name="error">ошибка</param> |
| | | 108 | | /// <returns></returns> |
| | 0 | 109 | | private SmsHistory PrepareRecord(SmsPatternRequestDTO data, int statusCode, string error) => new SmsHistory() |
| | 0 | 110 | | { |
| | 0 | 111 | | PatternId = data.PatternId, |
| | 0 | 112 | | ContragentExtKey = data.CompanyId, |
| | 0 | 113 | | Phone = data.Phone, |
| | 0 | 114 | | StatusCode = statusCode, |
| | 0 | 115 | | Error = error |
| | 0 | 116 | | }; |
| | | 117 | | } |
| | | 118 | | } |