| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using OzonDomains.Models; |
| | | 6 | | using OzonDomains.Models.Authorization.Enums; |
| | | 7 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.EmailRouting; |
| | | 8 | | using OzonOrdersWeb.Authorization; |
| | | 9 | | using OzonOrdersWeb.WebServices.Grid; |
| | | 10 | | using OzonOrdersWeb.WebServices.Journals; |
| | | 11 | | using OzonRepositories.Context; |
| | | 12 | | using Services.ApiServices.DropBoxApi; |
| | | 13 | | using Services.DataServices; |
| | | 14 | | using Services.EmailRoutingServices; |
| | | 15 | | |
| | | 16 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 17 | | |
| | | 18 | | [Area("Studio2")] |
| | | 19 | | [Authorize(Roles = "User,Admin")] |
| | | 20 | | public class EmailRoutingResultsController : Controller |
| | | 21 | | { |
| | | 22 | | private readonly EmailAttachmentProcessingResultDataServices _resultDataServices; |
| | | 23 | | private readonly EmailRoutingRuleDataServices _rulesDataServices; |
| | | 24 | | private readonly IEmailAttachmentRoutingService _routingService; |
| | | 25 | | private readonly IEmailInboxClient _emailInboxClient; |
| | | 26 | | private readonly IImapSettingsStore _imapSettingsStore; |
| | | 27 | | private readonly DropboxApiClient _dropboxClient; |
| | | 28 | | private readonly IOptionsMonitor<ImapSettings> _imapSettings; |
| | | 29 | | private readonly ApplicationDbContext _context; |
| | | 30 | | |
| | 0 | 31 | | public EmailRoutingResultsController( |
| | 0 | 32 | | EmailAttachmentProcessingResultDataServices resultDataServices, |
| | 0 | 33 | | EmailRoutingRuleDataServices rulesDataServices, |
| | 0 | 34 | | IEmailAttachmentRoutingService routingService, |
| | 0 | 35 | | IEmailInboxClient emailInboxClient, |
| | 0 | 36 | | IImapSettingsStore imapSettingsStore, |
| | 0 | 37 | | DropboxApiClient dropboxClient, |
| | 0 | 38 | | IOptionsMonitor<ImapSettings> imapOptions, |
| | 0 | 39 | | ApplicationDbContext context) |
| | | 40 | | { |
| | 0 | 41 | | _resultDataServices = resultDataServices; |
| | 0 | 42 | | _rulesDataServices = rulesDataServices; |
| | 0 | 43 | | _routingService = routingService; |
| | 0 | 44 | | _emailInboxClient = emailInboxClient; |
| | 0 | 45 | | _imapSettingsStore = imapSettingsStore; |
| | 0 | 46 | | _dropboxClient = dropboxClient; |
| | 0 | 47 | | _imapSettings = imapOptions; |
| | 0 | 48 | | _context = context; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | private const string IndexPageCode = "Studio2.EmailRoutingResults.Index"; |
| | | 52 | | private const string UploadedPageCode = "Studio2.EmailRoutingResults.UploadedFiles"; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Каркас страницы: IMAP-карточка, счётчики по статусам и последние письма. Результаты обработки вложений грид |
| | | 56 | | /// подгружает блоками через <see cref="GridRows"/>. |
| | | 57 | | /// </summary> |
| | | 58 | | [HttpGet] |
| | | 59 | | public async Task<IActionResult> Index(CancellationToken cancellationToken) |
| | | 60 | | { |
| | 0 | 61 | | var messagesLimit = Math.Max(1, _imapSettings.CurrentValue.FetchLimit); |
| | | 62 | | // Проекция без вложений: FileBytes писем в список не нужны. |
| | 0 | 63 | | var messages = await _context.EmailMessageInboxes.AsNoTracking() |
| | 0 | 64 | | .OrderByDescending(m => m.ReceivedAtUtc ?? m.CreatedAt).ThenByDescending(m => m.Id) |
| | 0 | 65 | | .Take(messagesLimit) |
| | 0 | 66 | | .Select(m => new EmailInboxMessageRow(m.Id, m.ReceivedAtUtc, m.CreatedAt, m.SenderEmail, m.Subject, |
| | 0 | 67 | | m.Attachments.Count, m.ProcessingStatus.ToString())) |
| | 0 | 68 | | .ToListAsync(cancellationToken); |
| | 0 | 69 | | var statusCounts = await _resultDataServices.GetStatusCounts(); |
| | | 70 | | |
| | | 71 | | // Живая проверка IMAP (до 8с на подключение к почте) сюда не идёт — она тормозила открытие страницы |
| | | 72 | | // на каждый заход. Карточка рендерится пустой и сразу подтягивается через GetImapStatus. |
| | 0 | 73 | | var vm = new EmailRoutingResultsViewModel |
| | 0 | 74 | | { |
| | 0 | 75 | | MessageRows = messages, |
| | 0 | 76 | | CountUploaded = statusCounts.GetValueOrDefault(EmailAttachmentProcessingStatus.Uploaded), |
| | 0 | 77 | | CountOrdersImported = statusCounts.GetValueOrDefault(EmailAttachmentProcessingStatus.OrdersImported), |
| | 0 | 78 | | CountRejected = statusCounts.GetValueOrDefault(EmailAttachmentProcessingStatus.RejectedByRule), |
| | 0 | 79 | | CountFailed = statusCounts.GetValueOrDefault(EmailAttachmentProcessingStatus.Failed), |
| | 0 | 80 | | CountDuplicate = statusCounts.GetValueOrDefault(EmailAttachmentProcessingStatus.Duplicate), |
| | 0 | 81 | | LastMessagesLimit = messagesLimit, |
| | 0 | 82 | | ImapSettingsEdit = User.IsInRole("Admin") ? MapSettingsEdit(_imapSettingsStore.GetCurrent()) : null |
| | 0 | 83 | | }; |
| | 0 | 84 | | return View(vm); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary>Загруженные в Dropbox вложения (грид — <see cref="UploadedGridRows"/>) и просмотр папок Dropbox.</summa |
| | | 88 | | public async Task<IActionResult> UploadedFiles(string? dropboxFolder = null) |
| | | 89 | | { |
| | 0 | 90 | | var rules = await _rulesDataServices.GetRules(); |
| | | 91 | | |
| | 0 | 92 | | Dictionary<string, Dictionary<string, string>> dropboxFolders = new(); |
| | 0 | 93 | | if (!string.IsNullOrWhiteSpace(dropboxFolder)) |
| | | 94 | | { |
| | | 95 | | try |
| | | 96 | | { |
| | 0 | 97 | | dropboxFolders = await _dropboxClient.GetFolderContentsAsync(dropboxFolder); |
| | 0 | 98 | | } |
| | 0 | 99 | | catch |
| | | 100 | | { |
| | | 101 | | // если Dropbox недоступен — продолжаем без папок |
| | 0 | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | var vm = new EmailRoutingResultsViewModel |
| | 0 | 106 | | { |
| | 0 | 107 | | TotalCount = await _context.EmailAttachmentProcessingResults |
| | 0 | 108 | | .CountAsync(r => r.Status == EmailAttachmentProcessingStatus.Uploaded), |
| | 0 | 109 | | AvailableRules = rules, |
| | 0 | 110 | | DropboxFolders = dropboxFolders, |
| | 0 | 111 | | DropboxBaseFolder = dropboxFolder, |
| | 0 | 112 | | }; |
| | 0 | 113 | | return View(vm); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | [HttpPost("Studio2/EmailRoutingResults/grid")] |
| | | 117 | | [ValidateAntiForgeryToken] |
| | | 118 | | [PageAuthorize(IndexPageCode, AccessType.View)] |
| | | 119 | | public Task<IActionResult> GridRows([FromBody] ServerGridRequest? request, CancellationToken cancellationToken) => |
| | 0 | 120 | | ResultRowsAsync(_context.EmailAttachmentProcessingResults.AsNoTracking(), request, cancellationToken); |
| | | 121 | | |
| | | 122 | | [HttpPost("Studio2/EmailRoutingResults/grid/facets")] |
| | | 123 | | [ValidateAntiForgeryToken] |
| | | 124 | | [PageAuthorize(IndexPageCode, AccessType.View)] |
| | | 125 | | public Task<IActionResult> GridFacets([FromBody] ServerGridRequest? request, CancellationToken cancellationToken) => |
| | 0 | 126 | | ResultFacetsAsync(_context.EmailAttachmentProcessingResults.AsNoTracking(), request, cancellationToken); |
| | | 127 | | |
| | | 128 | | [HttpPost("Studio2/EmailRoutingResults/uploaded/grid")] |
| | | 129 | | [ValidateAntiForgeryToken] |
| | | 130 | | [PageAuthorize(UploadedPageCode, AccessType.View)] |
| | | 131 | | public Task<IActionResult> UploadedGridRows([FromBody] ServerGridRequest? request, CancellationToken cancellationTok |
| | 0 | 132 | | ResultRowsAsync(UploadedQuery(), request, cancellationToken); |
| | | 133 | | |
| | | 134 | | [HttpPost("Studio2/EmailRoutingResults/uploaded/grid/facets")] |
| | | 135 | | [ValidateAntiForgeryToken] |
| | | 136 | | [PageAuthorize(UploadedPageCode, AccessType.View)] |
| | | 137 | | public Task<IActionResult> UploadedGridFacets([FromBody] ServerGridRequest? request, CancellationToken cancellationT |
| | 0 | 138 | | ResultFacetsAsync(UploadedQuery(), request, cancellationToken); |
| | | 139 | | |
| | | 140 | | private IQueryable<EmailAttachmentProcessingResult> UploadedQuery() => |
| | 0 | 141 | | _context.EmailAttachmentProcessingResults.AsNoTracking().Where(r => r.Status == EmailAttachmentProcessingStatus. |
| | | 142 | | |
| | | 143 | | private async Task<IActionResult> ResultRowsAsync(IQueryable<EmailAttachmentProcessingResult> query, ServerGridReque |
| | | 144 | | CancellationToken cancellationToken) |
| | | 145 | | { |
| | 0 | 146 | | var error = ServerGridQuery.Validate(request); |
| | 0 | 147 | | if (error != null) return BadRequest(new { success = false, error }); |
| | | 148 | | |
| | 0 | 149 | | var def = JournalGridDefinitions.EmailResults; |
| | 0 | 150 | | var filtered = ServerGridQuery.ApplyFilters(query, def, request!); |
| | 0 | 151 | | var total = await filtered.CountAsync(cancellationToken); |
| | 0 | 152 | | var start = Math.Max(0, request!.StartRow); |
| | 0 | 153 | | var take = Math.Clamp(request.EndRow - start, 1, ServerGridQuery.MaxBlockSize); |
| | 0 | 154 | | var rows = start >= total |
| | 0 | 155 | | ? [] |
| | 0 | 156 | | : await ServerGridQuery.ApplySort(filtered, def, request.SortModel).Skip(start).Take(take) |
| | 0 | 157 | | .Select(r => new |
| | 0 | 158 | | { |
| | 0 | 159 | | r.Id, |
| | 0 | 160 | | Date = r.EmailMessage != null && r.EmailMessage.ReceivedAtUtc != null ? r.EmailMessage.ReceivedAtUtc |
| | 0 | 161 | | DateIsReceived = r.EmailMessage != null && r.EmailMessage.ReceivedAtUtc != null, |
| | 0 | 162 | | r.CreatedAt, |
| | 0 | 163 | | Status = r.Status.ToString(), |
| | 0 | 164 | | RuleName = r.Rule != null ? r.Rule.Name : null, |
| | 0 | 165 | | Sender = r.EmailMessage != null ? r.EmailMessage.SenderEmail : null, |
| | 0 | 166 | | Subject = r.EmailMessage != null ? r.EmailMessage.Subject : null, |
| | 0 | 167 | | FileName = r.Attachment != null ? r.Attachment.FileName : r.DropboxFileName, |
| | 0 | 168 | | r.DropboxFolderPath, r.DropboxFilePath, r.DropboxDownloadLink, |
| | 0 | 169 | | Reason = r.Error ?? r.Reason, |
| | 0 | 170 | | IsError = r.Error != null |
| | 0 | 171 | | }) |
| | 0 | 172 | | .ToListAsync(cancellationToken); |
| | 0 | 173 | | var lastRow = start + rows.Count >= total ? total : -1; |
| | 0 | 174 | | return Ok(new { success = true, rows, totalCount = total, lastRow }); |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | private async Task<IActionResult> ResultFacetsAsync(IQueryable<EmailAttachmentProcessingResult> query, ServerGridReq |
| | | 178 | | CancellationToken cancellationToken) |
| | | 179 | | { |
| | 0 | 180 | | if (request is null || string.IsNullOrWhiteSpace(request.ColId)) return BadRequest(new { success = false, error |
| | 0 | 181 | | request.StartRow = 0; |
| | 0 | 182 | | request.EndRow = 1; |
| | 0 | 183 | | var error = ServerGridQuery.Validate(request); |
| | 0 | 184 | | if (error != null) return BadRequest(new { success = false, error }); |
| | 0 | 185 | | var values = await ServerGridQuery.FacetsAsync(query, JournalGridDefinitions.EmailResults, request, cancellation |
| | 0 | 186 | | return Ok(new { success = true, values }); |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// JSON-эндпоинт для онлайн-обновления статуса IMAP на странице результатов. |
| | | 191 | | /// </summary> |
| | | 192 | | [HttpGet] |
| | | 193 | | public async Task<IActionResult> GetImapStatus(CancellationToken cancellationToken) |
| | | 194 | | { |
| | 0 | 195 | | var status = await GetImapConnectionStatusAsync(cancellationToken); |
| | 0 | 196 | | return Json(status); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Проверка IMAP-подключения с указанными настройками (без сохранения). |
| | | 201 | | /// </summary> |
| | | 202 | | [HttpPost] |
| | | 203 | | [Authorize(Roles = "Admin")] |
| | | 204 | | [ValidateAntiForgeryToken] |
| | | 205 | | public async Task<IActionResult> TestImapSettings( |
| | | 206 | | [FromBody] EmailImapSettingsEditViewModel model, |
| | | 207 | | CancellationToken cancellationToken) |
| | | 208 | | { |
| | 0 | 209 | | if (!TryValidateImapSettings(model, out var error)) |
| | 0 | 210 | | return BadRequest(new { error }); |
| | | 211 | | |
| | 0 | 212 | | using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 0 | 213 | | cts.CancelAfter(TimeSpan.FromSeconds(12)); |
| | | 214 | | |
| | 0 | 215 | | var merged = _imapSettingsStore.MergeWithCurrent(MapSettingsInput(model)); |
| | 0 | 216 | | var status = await ImapConnectionTester.CheckAsync(merged, cancellationToken: cts.Token); |
| | 0 | 217 | | return Json(MapImapConnection(status)); |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Сохранение IMAP-настроек в appsettings.json. |
| | | 222 | | /// </summary> |
| | | 223 | | [HttpPost] |
| | | 224 | | [Authorize(Roles = "Admin")] |
| | | 225 | | [ValidateAntiForgeryToken] |
| | | 226 | | public async Task<IActionResult> SaveImapSettings( |
| | | 227 | | [FromBody] EmailImapSettingsEditViewModel model, |
| | | 228 | | CancellationToken cancellationToken) |
| | | 229 | | { |
| | 0 | 230 | | if (!TryValidateImapSettings(model, out var error)) |
| | 0 | 231 | | return BadRequest(new { error }); |
| | | 232 | | |
| | | 233 | | try |
| | | 234 | | { |
| | 0 | 235 | | await _imapSettingsStore.SaveAsync(MapSettingsInput(model), cancellationToken); |
| | 0 | 236 | | } |
| | 0 | 237 | | catch (Exception ex) |
| | | 238 | | { |
| | 0 | 239 | | return BadRequest(new { error = $"Не удалось сохранить настройки: {ex.Message}" }); |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | var status = await GetImapConnectionStatusAsync(cancellationToken); |
| | 0 | 243 | | return Json(new |
| | 0 | 244 | | { |
| | 0 | 245 | | success = true, |
| | 0 | 246 | | message = "Настройки почты сохранены.", |
| | 0 | 247 | | status |
| | 0 | 248 | | }); |
| | 0 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// JSON-эндпоинт для динамической загрузки содержимого папки Dropbox |
| | | 253 | | /// </summary> |
| | | 254 | | [HttpGet] |
| | | 255 | | public async Task<IActionResult> GetDropboxFolders(string path) |
| | | 256 | | { |
| | 0 | 257 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 258 | | return Json(new { }); |
| | | 259 | | |
| | | 260 | | try |
| | | 261 | | { |
| | 0 | 262 | | var contents = await _dropboxClient.GetFolderContentsAsync(path); |
| | 0 | 263 | | return Json(contents); |
| | | 264 | | } |
| | 0 | 265 | | catch (Exception ex) |
| | | 266 | | { |
| | 0 | 267 | | return Json(new { error = ex.Message }); |
| | | 268 | | } |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | public async Task<IActionResult> Details(int id) |
| | | 272 | | { |
| | 0 | 273 | | var result = await _resultDataServices.GetResult(id); |
| | 0 | 274 | | if (result == null) |
| | 0 | 275 | | return NotFound(); |
| | | 276 | | |
| | 0 | 277 | | return View(result); |
| | 0 | 278 | | } |
| | | 279 | | |
| | | 280 | | [HttpPost] |
| | | 281 | | [Authorize(Roles = "Admin")] |
| | | 282 | | [ValidateAntiForgeryToken] |
| | | 283 | | public async Task<IActionResult> RunNow() |
| | | 284 | | { |
| | 0 | 285 | | var result = await _routingService.ProcessInboxAsync(false); |
| | 0 | 286 | | TempData["TransactionResult"] = $"Обработка завершена. {result.ToSummaryString()}"; |
| | 0 | 287 | | return RedirectToAction(nameof(Index)); |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | [HttpPost] |
| | | 291 | | [Authorize(Roles = "Admin")] |
| | | 292 | | [ValidateAntiForgeryToken] |
| | | 293 | | public async Task<IActionResult> DryRun() |
| | | 294 | | { |
| | 0 | 295 | | var result = await _routingService.ProcessInboxAsync(true); |
| | 0 | 296 | | TempData["TransactionResult"] = $"Тестовый прогон завершён. {result.ToSummaryString()} Файлы в Dropbox не загруж |
| | 0 | 297 | | return RedirectToAction(nameof(Index)); |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | [HttpPost] |
| | | 301 | | [Authorize(Roles = "Admin")] |
| | | 302 | | [ValidateAntiForgeryToken] |
| | | 303 | | public async Task<IActionResult> Reprocess(long messageId) |
| | | 304 | | { |
| | 0 | 305 | | var processed = await _routingService.ReprocessMessageAsync(messageId); |
| | 0 | 306 | | TempData["TransactionResult"] = processed > 0 |
| | 0 | 307 | | ? "Письмо успешно переобработано." |
| | 0 | 308 | | : "Письмо не найдено или уже обработано."; |
| | 0 | 309 | | return RedirectToAction(nameof(Index)); |
| | 0 | 310 | | } |
| | | 311 | | |
| | | 312 | | private async Task<EmailImapConnectionViewModel> GetImapConnectionStatusAsync(CancellationToken cancellationToken = |
| | | 313 | | { |
| | 0 | 314 | | using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 0 | 315 | | cts.CancelAfter(TimeSpan.FromSeconds(8)); |
| | | 316 | | try |
| | | 317 | | { |
| | 0 | 318 | | return MapImapConnection(await _emailInboxClient.GetConnectionStatusAsync(cts.Token)); |
| | | 319 | | } |
| | 0 | 320 | | catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) |
| | | 321 | | { |
| | 0 | 322 | | var settings = _imapSettings.CurrentValue; |
| | 0 | 323 | | return new EmailImapConnectionViewModel |
| | 0 | 324 | | { |
| | 0 | 325 | | IsConfigured = !string.IsNullOrWhiteSpace(settings.Host) && |
| | 0 | 326 | | !string.IsNullOrWhiteSpace(settings.UserName), |
| | 0 | 327 | | UserName = settings.UserName, |
| | 0 | 328 | | Host = settings.Host, |
| | 0 | 329 | | Port = settings.Port, |
| | 0 | 330 | | Folder = settings.Folder, |
| | 0 | 331 | | FetchLimit = settings.FetchLimit, |
| | 0 | 332 | | IsConnected = false, |
| | 0 | 333 | | ErrorMessage = "Превышено время ожидания проверки IMAP (8 сек).", |
| | 0 | 334 | | CheckedAtUtc = DateTime.UtcNow |
| | 0 | 335 | | }; |
| | | 336 | | } |
| | 0 | 337 | | } |
| | | 338 | | |
| | | 339 | | private static EmailImapConnectionViewModel MapImapConnection(ImapConnectionStatus status) => |
| | 0 | 340 | | new() |
| | 0 | 341 | | { |
| | 0 | 342 | | IsConfigured = status.IsConfigured, |
| | 0 | 343 | | IsConnected = status.IsConnected, |
| | 0 | 344 | | UserName = status.UserName, |
| | 0 | 345 | | Host = status.Host, |
| | 0 | 346 | | Port = status.Port, |
| | 0 | 347 | | Folder = status.Folder, |
| | 0 | 348 | | FetchLimit = status.FetchLimit, |
| | 0 | 349 | | UnseenCount = status.UnseenCount, |
| | 0 | 350 | | UnseenReadyCount = status.UnseenReadyCount, |
| | 0 | 351 | | ErrorMessage = status.ErrorMessage, |
| | 0 | 352 | | CheckedAtUtc = status.CheckedAtUtc |
| | 0 | 353 | | }; |
| | | 354 | | |
| | | 355 | | private static EmailImapSettingsEditViewModel MapSettingsEdit(ImapSettings settings) => |
| | 0 | 356 | | new() |
| | 0 | 357 | | { |
| | 0 | 358 | | Host = settings.Host, |
| | 0 | 359 | | Port = settings.Port, |
| | 0 | 360 | | UserName = settings.UserName, |
| | 0 | 361 | | UseSsl = settings.UseSsl, |
| | 0 | 362 | | Folder = settings.Folder, |
| | 0 | 363 | | FetchLimit = settings.FetchLimit, |
| | 0 | 364 | | HasSavedPassword = !string.IsNullOrWhiteSpace(settings.Password) |
| | 0 | 365 | | }; |
| | | 366 | | |
| | | 367 | | private static EmailImapSettingsInput MapSettingsInput(EmailImapSettingsEditViewModel model) => |
| | 0 | 368 | | new() |
| | 0 | 369 | | { |
| | 0 | 370 | | Host = model.Host, |
| | 0 | 371 | | Port = model.Port, |
| | 0 | 372 | | UserName = model.UserName, |
| | 0 | 373 | | Password = model.Password, |
| | 0 | 374 | | UseSsl = model.UseSsl, |
| | 0 | 375 | | Folder = model.Folder, |
| | 0 | 376 | | FetchLimit = model.FetchLimit |
| | 0 | 377 | | }; |
| | | 378 | | |
| | | 379 | | private static bool TryValidateImapSettings(EmailImapSettingsEditViewModel model, out string error) |
| | | 380 | | { |
| | 0 | 381 | | error = string.Empty; |
| | 0 | 382 | | if (model == null) |
| | | 383 | | { |
| | 0 | 384 | | error = "Настройки не переданы."; |
| | 0 | 385 | | return false; |
| | | 386 | | } |
| | | 387 | | |
| | 0 | 388 | | if (string.IsNullOrWhiteSpace(model.Host)) |
| | | 389 | | { |
| | 0 | 390 | | error = "Укажите IMAP-сервер."; |
| | 0 | 391 | | return false; |
| | | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | if (string.IsNullOrWhiteSpace(model.UserName)) |
| | | 395 | | { |
| | 0 | 396 | | error = "Укажите email."; |
| | 0 | 397 | | return false; |
| | | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | if (model.Port is < 1 or > 65535) |
| | | 401 | | { |
| | 0 | 402 | | error = "Порт должен быть от 1 до 65535."; |
| | 0 | 403 | | return false; |
| | | 404 | | } |
| | | 405 | | |
| | 0 | 406 | | if (model.FetchLimit is < 1 or > 500) |
| | | 407 | | { |
| | 0 | 408 | | error = "Лимит писем должен быть от 1 до 500."; |
| | 0 | 409 | | return false; |
| | | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | return true; |
| | | 413 | | } |
| | | 414 | | } |