| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.Http; |
| | | 6 | | using SlqStudio.Application.Services.EmailService; |
| | | 7 | | using OzonDomains; |
| | | 8 | | using OzonDomains.Models; |
| | | 9 | | using OzonOrdersWeb.ViewModels.OzonClientViewModels; |
| | | 10 | | using OzonOrdersWeb.WebServices.Directories; |
| | | 11 | | using Services.ApiServices; |
| | | 12 | | using Services.ApiServices.OzonApi; |
| | | 13 | | using Services.ApiServices.OzonApi.Filters; |
| | | 14 | | using Services.ApiServices.YandexApi; |
| | | 15 | | using Services.DataServices; |
| | | 16 | | using Services.Authorization.Services; |
| | | 17 | | using System.Security.Claims; |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories |
| | | 21 | | { |
| | | 22 | | [Authorize(Roles = "Admin")] |
| | | 23 | | [Area("Studio2")] |
| | | 24 | | public class OzonClientsController : Controller |
| | | 25 | | { |
| | | 26 | | private readonly OzonClientServices _ozonClientServices; |
| | | 27 | | private readonly OzonJsonDataBuilder _jsonDataBuilder; |
| | | 28 | | private readonly YandexDataManager _yandexDataManager; |
| | | 29 | | private readonly OneCDataManager _oneCDataManager; |
| | | 30 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 31 | | private readonly IEmailService? _emailService; |
| | | 32 | | private readonly IScopeFilterValuesService _scopeFilterValuesService; |
| | | 33 | | |
| | 0 | 34 | | public OzonClientsController(OzonClientServices ozonClientServices, |
| | 0 | 35 | | OzonJsonDataBuilder jsonDataBuilder, |
| | 0 | 36 | | YandexDataManager yandexDataManager, |
| | 0 | 37 | | OneCDataManager oneCDataManager, |
| | 0 | 38 | | IScopeFilterValuesService scopeFilterValuesService, |
| | 0 | 39 | | IHttpClientFactory httpClientFactory, |
| | 0 | 40 | | IEmailService? emailService = null) |
| | | 41 | | { |
| | 0 | 42 | | _ozonClientServices = ozonClientServices; |
| | 0 | 43 | | _jsonDataBuilder = jsonDataBuilder; |
| | 0 | 44 | | _yandexDataManager = yandexDataManager; |
| | 0 | 45 | | _oneCDataManager = oneCDataManager; |
| | 0 | 46 | | _scopeFilterValuesService = scopeFilterValuesService; |
| | 0 | 47 | | _httpClientFactory = httpClientFactory; |
| | 0 | 48 | | _emailService = emailService; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary>Весь список клиентов для грида AG Grid: фильтры и сортировка — на клиенте, правка — на странице Edi |
| | | 52 | | public async Task<IActionResult> Index() |
| | | 53 | | { |
| | 0 | 54 | | var ozonClients = (await _ozonClientServices.GetOzonClients()).OrderBy(c => c.Name).ToList(); |
| | | 55 | | |
| | | 56 | | List<SupplierResponse> counterparties; |
| | | 57 | | try |
| | | 58 | | { |
| | 0 | 59 | | counterparties = (await _oneCDataManager.GetSupplierListAsync())?.ToList() ?? new List<SupplierResponse> |
| | 0 | 60 | | } |
| | 0 | 61 | | catch (Exception) |
| | | 62 | | { |
| | 0 | 63 | | counterparties = []; |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return View(DirectoryGridRows.BuildOzonClients(ozonClients, counterparties)); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<IActionResult> Create() |
| | | 70 | | { |
| | 0 | 71 | | if (TempData.ContainsKey("CreateInfo") && TempData["CreateInfo"] != null) |
| | | 72 | | { |
| | 0 | 73 | | string? result = TempData["CreateInfo"] as string; |
| | 0 | 74 | | ViewData["CreateInfo"] = result; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | var viewModel = new CreateOzonClientViewModel() |
| | 0 | 78 | | { |
| | 0 | 79 | | CurrencyCodes = new List<SelectListItem> |
| | 0 | 80 | | { |
| | 0 | 81 | | new SelectListItem { Value = CurrencyCode.RUB.ToString(), Text = "RUB" }, |
| | 0 | 82 | | new SelectListItem { Value = CurrencyCode.USD.ToString(), Text = "USD" }, |
| | 0 | 83 | | new SelectListItem { Value = CurrencyCode.EUR.ToString(), Text = "EUR" }, |
| | 0 | 84 | | new SelectListItem { Value = CurrencyCode.BYN.ToString(), Text = "BYN" }, |
| | 0 | 85 | | }, |
| | 0 | 86 | | ClientTypes = new List<SelectListItem> |
| | 0 | 87 | | { |
| | 0 | 88 | | new SelectListItem { Value = ClientType.ALL.ToString(), Text = "Другое" }, |
| | 0 | 89 | | new SelectListItem { Value = ClientType.OZON.ToString(), Text = "Ozon" }, |
| | 0 | 90 | | new SelectListItem { Value = ClientType.YANDEX.ToString(), Text = "Yandex" } |
| | 0 | 91 | | }, |
| | 0 | 92 | | YandexClients = await _yandexDataManager.GetAccountCampaigns(), |
| | 0 | 93 | | YandexApiKey = await _yandexDataManager.GetApiKey(), |
| | 0 | 94 | | BitrixWarehouseNames = (await GetAllowedBitrixWarehouseSelectItemsAsync()) |
| | 0 | 95 | | }; |
| | | 96 | | |
| | 0 | 97 | | return View(viewModel); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | [HttpPost] |
| | | 101 | | [ValidateAntiForgeryToken] |
| | | 102 | | public async Task<IActionResult> Create( |
| | | 103 | | [Bind("Id,Name,WarehouseName,ClientId,ApiKey,CurrencyCode,ClientType,INNCode")] |
| | | 104 | | OzonClient ozonClient) |
| | | 105 | | { |
| | 0 | 106 | | if (!await IsAllowedBitrixWarehouseNameAsync(ozonClient.WarehouseName)) |
| | | 107 | | { |
| | 0 | 108 | | TempData["CreateInfo"] = "Название склада должно быть выбрано из разрешенного списка складов Bitrix."; |
| | 0 | 109 | | return RedirectToAction(nameof(Create)); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | if (await _ozonClientServices.GetOzonClientAsync(ozonClient) == null) |
| | | 113 | | { |
| | 0 | 114 | | if (ozonClient.ClientId != null && ozonClient.ApiKey != null) |
| | | 115 | | { |
| | 0 | 116 | | var clients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 117 | | if (ozonClient.ClientType == ClientType.OZON) |
| | | 118 | | { |
| | 0 | 119 | | if (!clients.Where(c => c.DecryptApiKey == ozonClient.ApiKey).Any()) |
| | | 120 | | { |
| | 0 | 121 | | var (isValid, errorDetails) = |
| | 0 | 122 | | await ValidateOzonApiCredentialsAsync(ozonClient.ClientId, ozonClient.ApiKey); |
| | 0 | 123 | | if (isValid) |
| | | 124 | | { |
| | 0 | 125 | | await _ozonClientServices.AddOzonClient(ozonClient); |
| | 0 | 126 | | return RedirectToAction(nameof(Index)); |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | 0 | 130 | | TempData["CreateInfo"] = |
| | 0 | 131 | | $"Api данные некорректны. Не удалось пройти тестовый запрос Ozon API. " + |
| | 0 | 132 | | $"{errorDetails}"; |
| | 0 | 133 | | return RedirectToAction(nameof(Create)); |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | else |
| | | 137 | | { |
| | 0 | 138 | | TempData["CreateInfo"] = "Клиент с таким Api Key уже существует."; |
| | 0 | 139 | | return RedirectToAction(nameof(Create)); |
| | | 140 | | } |
| | | 141 | | } |
| | 0 | 142 | | else if (ozonClient.ClientType == ClientType.YANDEX) |
| | | 143 | | { |
| | 0 | 144 | | if (!clients.Where(c => c.DecryptClientId == ozonClient.ClientId).Any()) |
| | | 145 | | { |
| | 0 | 146 | | if (await _yandexDataManager.GetTestRequest(ozonClient.ClientId, ozonClient.ApiKey)) |
| | | 147 | | { |
| | 0 | 148 | | await _ozonClientServices.AddOzonClient(ozonClient); |
| | 0 | 149 | | return RedirectToAction(nameof(Index)); |
| | | 150 | | } |
| | | 151 | | else |
| | | 152 | | { |
| | 0 | 153 | | TempData["CreateInfo"] = "Api данные некорректны. Ошибка при отправке тестового запроса |
| | 0 | 154 | | return RedirectToAction(nameof(Create)); |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | else |
| | | 158 | | { |
| | 0 | 159 | | TempData["CreateInfo"] = "Клиент с таким ClientId уже существует."; |
| | 0 | 160 | | return RedirectToAction(nameof(Create)); |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | else |
| | | 164 | | { |
| | 0 | 165 | | await _ozonClientServices.AddOzonClient(ozonClient); |
| | 0 | 166 | | return RedirectToAction(nameof(Index)); |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | else |
| | | 170 | | { |
| | 0 | 171 | | await _ozonClientServices.AddOzonClient(ozonClient); |
| | 0 | 172 | | return RedirectToAction(nameof(Index)); |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | else |
| | | 176 | | { |
| | 0 | 177 | | TempData["CreateInfo"] = $"Клиент `{ozonClient.Name}` уже существует."; |
| | 0 | 178 | | return RedirectToAction(nameof(Create)); |
| | | 179 | | } |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | public async Task<IActionResult> Edit(int id, string? info = null) |
| | | 183 | | { |
| | 0 | 184 | | var innInfo = await _oneCDataManager.GetSupplierListAsync(); |
| | | 185 | | |
| | 0 | 186 | | if (TempData.ContainsKey("EditInfo") && TempData["EditInfo"] != null) |
| | | 187 | | { |
| | 0 | 188 | | string? result = TempData["EditInfo"] as string; |
| | 0 | 189 | | ViewData["EditInfo"] = result; |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | if (info != null) |
| | | 193 | | { |
| | 0 | 194 | | ViewData["EditInfo"] = info; |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | var ozonClient = await _ozonClientServices.GetOzonClientAsync(id); |
| | 0 | 198 | | if (ozonClient == null) |
| | | 199 | | { |
| | 0 | 200 | | return NotFound(); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | // Подготовка списка подсказок для ИНН |
| | 0 | 204 | | var innSuggestions = new List<SupplierResponse>(); |
| | 0 | 205 | | if (innInfo != null) |
| | | 206 | | { |
| | 0 | 207 | | innSuggestions = innInfo |
| | 0 | 208 | | .Where(s => !string.IsNullOrEmpty(s.Unp)) |
| | 0 | 209 | | .OrderBy(s => s.Unp) |
| | 0 | 210 | | .ToList(); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | // Проверка ролей API для клиента Ozon |
| | 0 | 214 | | List<Role> apiRoles = new List<Role>(); |
| | 0 | 215 | | bool hasApiAccess = false; |
| | 0 | 216 | | SellerInfoResponse? sellerInfo = null; |
| | 0 | 217 | | bool hasSellerInfo = false; |
| | | 218 | | |
| | 0 | 219 | | if (ozonClient.ClientType == ClientType.OZON && |
| | 0 | 220 | | !string.IsNullOrEmpty(ozonClient.DecryptClientId) && |
| | 0 | 221 | | !string.IsNullOrEmpty(ozonClient.DecryptApiKey)) |
| | | 222 | | { |
| | | 223 | | try |
| | | 224 | | { |
| | 0 | 225 | | var ozonApiManager = new OzonApiDataManager(_httpClientFactory, _emailService, ozonClient.DecryptCli |
| | 0 | 226 | | var rolesResponse = await ozonApiManager.GetApiRoles(); |
| | | 227 | | |
| | 0 | 228 | | if (rolesResponse?.Roles != null) |
| | | 229 | | { |
| | 0 | 230 | | apiRoles = rolesResponse.Roles; |
| | 0 | 231 | | hasApiAccess = true; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | // Получение информации о продавце |
| | | 235 | | try |
| | | 236 | | { |
| | 0 | 237 | | sellerInfo = await ozonApiManager.GetSellerInfo(); |
| | 0 | 238 | | if (sellerInfo != null && (sellerInfo.Company != null || sellerInfo.Ratings != null || |
| | 0 | 239 | | sellerInfo.Subscription != null)) |
| | | 240 | | { |
| | 0 | 241 | | hasSellerInfo = true; |
| | | 242 | | } |
| | 0 | 243 | | } |
| | 0 | 244 | | catch (Exception ex) |
| | | 245 | | { |
| | | 246 | | // Игнорируем ошибки при получении информации о продавце |
| | 0 | 247 | | Console.WriteLine($"Ошибка при получении информации о продавце: {ex.Message}"); |
| | 0 | 248 | | } |
| | 0 | 249 | | } |
| | 0 | 250 | | catch (Exception ex) |
| | | 251 | | { |
| | 0 | 252 | | apiRoles.Add(new Role |
| | 0 | 253 | | { |
| | 0 | 254 | | Name = "Ошибка", |
| | 0 | 255 | | Methods = new List<string> { $"Ошибка при проверке прав: {ex.Message}" } |
| | 0 | 256 | | }); |
| | 0 | 257 | | } |
| | | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | var viewModel = new EditOzonClientViewModel |
| | 0 | 261 | | { |
| | 0 | 262 | | OzonClient = ozonClient, |
| | 0 | 263 | | CurrencyCodes = new List<SelectListItem> |
| | 0 | 264 | | { |
| | 0 | 265 | | new SelectListItem { Value = CurrencyCode.RUB.ToString(), Text = "RUB" }, |
| | 0 | 266 | | new SelectListItem { Value = CurrencyCode.USD.ToString(), Text = "USD" }, |
| | 0 | 267 | | new SelectListItem { Value = CurrencyCode.EUR.ToString(), Text = "EUR" }, |
| | 0 | 268 | | new SelectListItem { Value = CurrencyCode.BYN.ToString(), Text = "BYN" }, |
| | 0 | 269 | | }, |
| | 0 | 270 | | ClientTypes = new List<SelectListItem> |
| | 0 | 271 | | { |
| | 0 | 272 | | new SelectListItem { Value = ClientType.ALL.ToString(), Text = "Другое" }, |
| | 0 | 273 | | new SelectListItem { Value = ClientType.OZON.ToString(), Text = "Ozon" }, |
| | 0 | 274 | | new SelectListItem { Value = ClientType.YANDEX.ToString(), Text = "Yandex" } |
| | 0 | 275 | | }, |
| | 0 | 276 | | INNSuggestions = innSuggestions, |
| | 0 | 277 | | ApiRoles = apiRoles, |
| | 0 | 278 | | HasApiAccess = hasApiAccess, |
| | 0 | 279 | | SellerInfo = sellerInfo ?? new SellerInfoResponse(), |
| | 0 | 280 | | HasSellerInfo = hasSellerInfo, |
| | 0 | 281 | | BitrixWarehouseNames = (await GetAllowedBitrixWarehouseSelectItemsAsync()) |
| | 0 | 282 | | }; |
| | | 283 | | |
| | 0 | 284 | | return View(viewModel); |
| | 0 | 285 | | } |
| | | 286 | | |
| | | 287 | | |
| | | 288 | | [HttpPost] |
| | | 289 | | [ValidateAntiForgeryToken] |
| | | 290 | | public async Task<IActionResult> Edit(int id, OzonClient ozonClient) |
| | | 291 | | { |
| | 0 | 292 | | if (id != ozonClient.Id) |
| | | 293 | | { |
| | 0 | 294 | | return NotFound(); |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | if (!await IsAllowedBitrixWarehouseNameAsync(ozonClient.WarehouseName)) |
| | | 298 | | { |
| | 0 | 299 | | return RedirectToAction(nameof(Edit), new |
| | 0 | 300 | | { |
| | 0 | 301 | | id, |
| | 0 | 302 | | info = "Название склада должно быть выбрано из разрешенного списка складов Bitrix." |
| | 0 | 303 | | }); |
| | | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | if (ModelState.IsValid) |
| | | 307 | | { |
| | | 308 | | try |
| | | 309 | | { |
| | 0 | 310 | | if (ozonClient.ClientId != null && ozonClient.ApiKey != null) |
| | | 311 | | { |
| | 0 | 312 | | if (ozonClient.ClientType == ClientType.OZON) |
| | | 313 | | { |
| | 0 | 314 | | var (isValid, errorDetails) = |
| | 0 | 315 | | await ValidateOzonApiCredentialsAsync(ozonClient.ClientId, ozonClient.ApiKey); |
| | 0 | 316 | | if (isValid) |
| | | 317 | | { |
| | 0 | 318 | | await _ozonClientServices.Update(ozonClient); |
| | 0 | 319 | | return RedirectToAction(nameof(Index)); |
| | | 320 | | } |
| | | 321 | | else |
| | | 322 | | { |
| | 0 | 323 | | return RedirectToAction(nameof(Edit), new |
| | 0 | 324 | | { |
| | 0 | 325 | | id, |
| | 0 | 326 | | info = $"Api данные некорректны. Не удалось пройти тестовый запрос Ozon API. {errorD |
| | 0 | 327 | | }); |
| | | 328 | | } |
| | | 329 | | } |
| | 0 | 330 | | else if (ozonClient.ClientType == ClientType.YANDEX) |
| | | 331 | | { |
| | 0 | 332 | | if (await _yandexDataManager.GetTestRequest(ozonClient.ClientId, ozonClient.ApiKey)) |
| | | 333 | | { |
| | 0 | 334 | | await _ozonClientServices.Update(ozonClient); |
| | 0 | 335 | | return RedirectToAction(nameof(Index)); |
| | | 336 | | } |
| | | 337 | | else |
| | | 338 | | { |
| | 0 | 339 | | return RedirectToAction(nameof(Edit), new |
| | 0 | 340 | | { |
| | 0 | 341 | | id, |
| | 0 | 342 | | info = "Api данные некорректны. Ошибка при отправке тестового запроса Yandex API." |
| | 0 | 343 | | }); |
| | | 344 | | } |
| | | 345 | | } |
| | | 346 | | else |
| | | 347 | | { |
| | 0 | 348 | | await _ozonClientServices.Update(ozonClient); |
| | 0 | 349 | | return RedirectToAction(nameof(Index)); |
| | | 350 | | } |
| | | 351 | | } |
| | | 352 | | else |
| | | 353 | | { |
| | 0 | 354 | | await _ozonClientServices.Update(ozonClient); |
| | 0 | 355 | | return RedirectToAction(nameof(Index)); |
| | | 356 | | } |
| | | 357 | | } |
| | 0 | 358 | | catch (DbUpdateConcurrencyException) |
| | | 359 | | { |
| | 0 | 360 | | throw; |
| | | 361 | | } |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | return RedirectToAction(nameof(Edit), new { id }); |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | |
| | | 368 | | // GET: OzonClients/Delete/5 |
| | | 369 | | public async Task<IActionResult> Delete(int id) |
| | | 370 | | { |
| | 0 | 371 | | var ozonClient = await _ozonClientServices.GetOzonClientAsync(id); |
| | 0 | 372 | | if (ozonClient == null) |
| | 0 | 373 | | return NotFound(); |
| | | 374 | | |
| | 0 | 375 | | ozonClient.DecryptApiKey = new string('•', ozonClient.DecryptApiKey?.Length ?? 0); |
| | 0 | 376 | | return View(ozonClient); |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | // POST: OzonClients/Delete/5 |
| | | 380 | | [HttpPost, ActionName("Delete")] |
| | | 381 | | [ValidateAntiForgeryToken] |
| | | 382 | | public async Task<IActionResult> DeleteConfirmed(int id) |
| | | 383 | | { |
| | 0 | 384 | | var ozonClient = await _ozonClientServices.GetOzonClientAsync(id); |
| | 0 | 385 | | if (ozonClient != null) |
| | | 386 | | { |
| | 0 | 387 | | await _ozonClientServices.DeleteOzonClient(ozonClient); |
| | | 388 | | } |
| | | 389 | | |
| | 0 | 390 | | return RedirectToAction(nameof(Index)); |
| | 0 | 391 | | } |
| | | 392 | | |
| | | 393 | | [HttpGet] |
| | | 394 | | public async Task<IActionResult> SearchCounterparties(string? q, int limit = 15) |
| | | 395 | | { |
| | 0 | 396 | | limit = Math.Min(limit, 30); |
| | 0 | 397 | | var results = await _oneCDataManager.SearchCounterpartiesAsync(q, limit); |
| | 0 | 398 | | return Json(results.Select(c => new { inn = c.Identifier, name = c.Description })); |
| | 0 | 399 | | } |
| | | 400 | | |
| | | 401 | | [HttpGet] |
| | | 402 | | public async Task<IActionResult> SearchOrganizations(string? q, int limit = 15) |
| | | 403 | | { |
| | 0 | 404 | | limit = Math.Min(limit, 30); |
| | 0 | 405 | | var results = await _oneCDataManager.SearchOrganizationsAsync(q, limit); |
| | 0 | 406 | | return Json(results.Select(o => new { inn = o.Inn, name = o.Description })); |
| | 0 | 407 | | } |
| | | 408 | | |
| | | 409 | | private async Task<List<SelectListItem>> GetAllowedBitrixWarehouseSelectItemsAsync() |
| | | 410 | | { |
| | 0 | 411 | | var names = (await _oneCDataManager.GetWarehouseListAsync()) |
| | 0 | 412 | | .Select(x => x.Name?.Trim()) |
| | 0 | 413 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 0 | 414 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 415 | | .OrderBy(x => x) |
| | 0 | 416 | | .ToList(); |
| | | 417 | | |
| | 0 | 418 | | var userId = User?.FindFirstValue(ClaimTypes.NameIdentifier); |
| | 0 | 419 | | if (!string.IsNullOrWhiteSpace(userId)) |
| | | 420 | | { |
| | 0 | 421 | | var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync( |
| | 0 | 422 | | userId, |
| | 0 | 423 | | "BitrixWarehouse", |
| | 0 | 424 | | HttpContext.RequestAborted); |
| | | 425 | | |
| | 0 | 426 | | if (allowed.TryGetValue("Name", out var allowedNames) && allowedNames.Count > 0) |
| | | 427 | | { |
| | 0 | 428 | | names = names |
| | 0 | 429 | | .Where(n => allowedNames.Contains(n!)) |
| | 0 | 430 | | .OrderBy(n => n) |
| | 0 | 431 | | .ToList(); |
| | | 432 | | } |
| | 0 | 433 | | } |
| | | 434 | | |
| | 0 | 435 | | return names |
| | 0 | 436 | | .Select(x => new SelectListItem { Value = x, Text = x }) |
| | 0 | 437 | | .ToList(); |
| | 0 | 438 | | } |
| | | 439 | | |
| | | 440 | | private async Task<bool> IsAllowedBitrixWarehouseNameAsync(string? warehouseName) |
| | | 441 | | { |
| | 0 | 442 | | if (string.IsNullOrWhiteSpace(warehouseName)) |
| | | 443 | | { |
| | 0 | 444 | | return false; |
| | | 445 | | } |
| | | 446 | | |
| | 0 | 447 | | var allowedValues = await GetAllowedBitrixWarehouseSelectItemsAsync(); |
| | 0 | 448 | | return allowedValues.Any(x => string.Equals(x.Value, warehouseName.Trim(), StringComparison.OrdinalIgnoreCas |
| | 0 | 449 | | } |
| | | 450 | | |
| | | 451 | | private async Task<(bool IsValid, string ErrorDetails)> ValidateOzonApiCredentialsAsync( |
| | | 452 | | string? clientId, |
| | | 453 | | string? apiKey) |
| | | 454 | | { |
| | 0 | 455 | | if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(apiKey)) |
| | | 456 | | { |
| | 0 | 457 | | return (false, "Не заполнены ClientId и/или ApiKey."); |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | try |
| | | 461 | | { |
| | 0 | 462 | | var ozonApiManager = new OzonApiDataManager(_httpClientFactory, _emailService, clientId, apiKey); |
| | 0 | 463 | | await ozonApiManager.GetApiRoles(); |
| | 0 | 464 | | return (true, string.Empty); |
| | | 465 | | } |
| | 0 | 466 | | catch (Exception ex) |
| | | 467 | | { |
| | 0 | 468 | | var message = ex.Message?.Replace("\r", " ").Replace("\n", " ").Trim(); |
| | 0 | 469 | | if (string.IsNullOrWhiteSpace(message)) |
| | | 470 | | { |
| | 0 | 471 | | message = "Неизвестная ошибка при обращении к Ozon API."; |
| | | 472 | | } |
| | | 473 | | |
| | 0 | 474 | | return (false, $"Детали: {message}"); |
| | | 475 | | } |
| | 0 | 476 | | } |
| | | 477 | | } |
| | | 478 | | } |