diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs index bbf38c3..8c1fbdd 100644 --- a/Controllers/RecruitController.cs +++ b/Controllers/RecruitController.cs @@ -755,6 +755,7 @@ namespace BMA.EHR.Recruit.Controllers ImportDocId = import_doc_id, UserId = UserId, FullName = FullName, + Token = token, Request = req, }); await _importJobQueue.EnqueueAsync(job); @@ -943,6 +944,7 @@ namespace BMA.EHR.Recruit.Controllers ImportDocId = import_doc_id, UserId = UserId, FullName = FullName, + Token = token, }); await _importJobQueue.EnqueueAsync(job); @@ -1013,6 +1015,7 @@ namespace BMA.EHR.Recruit.Controllers ImportDocId = import_doc_id, UserId = UserId, FullName = FullName, + Token = token, }); await _importJobQueue.EnqueueAsync(job); @@ -1082,6 +1085,7 @@ namespace BMA.EHR.Recruit.Controllers RecruitImportId = id, UserId = UserId, FullName = FullName, + Token = token, }); await _importJobQueue.EnqueueAsync(job); diff --git a/Program.cs b/Program.cs index f39a56c..3475b74 100644 --- a/Program.cs +++ b/Program.cs @@ -69,6 +69,7 @@ builder.Services.AddAuthorization(); // Register Services builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 230a37c..f041390 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -47,6 +47,7 @@ public class ImportBackgroundService : BackgroundService var context = scope.ServiceProvider.GetRequiredService(); var minioService = scope.ServiceProvider.GetRequiredService(); var recruitService = scope.ServiceProvider.GetRequiredService(); + var notificationService = scope.ServiceProvider.GetRequiredService(); var webHostEnv = scope.ServiceProvider.GetRequiredService(); var logger = scope.ServiceProvider.GetRequiredService>(); @@ -71,11 +72,15 @@ public class ImportBackgroundService : BackgroundService } _tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount); + + await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ"); } catch (Exception ex) { logger.LogError(ex, "Import job {JobId} failed: {Message}", job.JobId, ex.Message); - _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.InnerException?.Message ?? ex.Message); + _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message); + + await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message); // cleanup minio file on failure if (!string.IsNullOrEmpty(job.ImportDocId)) @@ -321,6 +326,7 @@ public class ImportBackgroundService : BackgroundService _context.ChangeTracker.Clear(); var importId = imported.Id; + var importRef = _context.Attach(new RecruitImport { Id = importId }).Entity; using var c_package = new ExcelPackage(new FileInfo(job.ImportFile)); for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++) @@ -344,133 +350,145 @@ public class ImportBackgroundService : BackgroundService var cell1 = workSheet?.Cells[row, 1]?.GetValue(); if (cell1 == "" || cell1 == null) break; - var r = new Models.Recruits.Recruit(); - r.ExamId = workSheet?.Cells[row, 1]?.GetValue() ?? ""; - r.PositionName = workSheet?.Cells[row, 3]?.GetValue() ?? ""; - r.HddPosition = workSheet?.Cells[row, 4]?.GetValue() ?? ""; - r.Prefix = workSheet?.Cells[row, 5]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 6]?.GetValue() ?? "" : workSheet?.Cells[row, 5]?.GetValue() ?? ""; - r.FirstName = workSheet?.Cells[row, 7]?.GetValue() ?? ""; - r.LastName = workSheet?.Cells[row, 8]?.GetValue() ?? ""; - r.Gendor = workSheet?.Cells[row, 98]?.GetValue() ?? ""; - r.National = workSheet?.Cells[row, 9]?.GetValue() ?? ""; - r.Race = ""; - r.Religion = workSheet?.Cells[row, 10]?.GetValue() ?? ""; - r.DateOfBirth = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 11]?.GetValue() ?? "", "dd/MM/yyyy") : null; - r.CitizenId = workSheet?.Cells[row, 12]?.GetValue() ?? ""; - r.typeTest = workSheet?.Cells[row, 13]?.GetValue() ?? ""; - r.Marry = ""; - r.Isspecial = "N"; - r.CitizenCardExpireDate = null; - r.ModifiedDate = null; - r.ApplyDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 87]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue() ?? "", "dd/MM/yyyy") : null; - r.PositionType = ""; - r.PositionLevel = ""; - r.CreatedAt = DateTime.Now; - r.CreatedUserId = job.UserId ?? ""; - r.CreatedFullName = job.FullName ?? "System Administrator"; - r.LastUpdatedAt = DateTime.Now; - r.LastUpdateUserId = job.UserId ?? ""; - r.LastUpdateFullName = job.FullName ?? "System Administrator"; - - // Store child entities in separate lists for bulk insert - var education = new RecruitEducation() + try { - Degree = workSheet?.Cells[row, 18]?.GetValue() ?? "", - Major = workSheet?.Cells[row, 19]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 20]?.GetValue() ?? "" : workSheet?.Cells[row, 19]?.GetValue() ?? "", - MajorGroupId = "", - MajorGroupName = "", - University = workSheet?.Cells[row, 21]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue() ?? "" : workSheet?.Cells[row, 21]?.GetValue() ?? "", - GPA = (double)workSheet?.Cells[row, 26]?.GetValue(), - Specialist = "", - HighDegree = workSheet?.Cells[row, 27]?.GetValue() ?? "", - BachelorDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 25]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 25]?.GetValue() ?? "", "dd/MM/yyyy") : null, - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; + var r = new Models.Recruits.Recruit(); + r.Id = Guid.NewGuid(); + r.ExamId = workSheet?.Cells[row, 1]?.GetValue() ?? ""; + r.PositionName = workSheet?.Cells[row, 3]?.GetValue() ?? ""; + r.HddPosition = workSheet?.Cells[row, 4]?.GetValue() ?? ""; + r.Prefix = workSheet?.Cells[row, 5]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 6]?.GetValue() ?? "" : workSheet?.Cells[row, 5]?.GetValue() ?? ""; + r.FirstName = workSheet?.Cells[row, 7]?.GetValue() ?? ""; + r.LastName = workSheet?.Cells[row, 8]?.GetValue() ?? ""; + r.Gendor = workSheet?.Cells[row, 98]?.GetValue() ?? ""; + r.National = workSheet?.Cells[row, 9]?.GetValue() ?? ""; + r.Race = ""; + r.Religion = workSheet?.Cells[row, 10]?.GetValue() ?? ""; + r.DateOfBirth = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 11]?.GetValue() ?? "", "dd/MM/yyyy") : null; + r.CitizenId = workSheet?.Cells[row, 12]?.GetValue() ?? ""; + r.typeTest = workSheet?.Cells[row, 13]?.GetValue() ?? ""; + r.Marry = ""; + r.Isspecial = "N"; + r.CitizenCardExpireDate = null; + r.ModifiedDate = null; + r.ApplyDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 87]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue() ?? "", "dd/MM/yyyy") : null; + r.PositionType = ""; + r.PositionLevel = ""; + r.CreatedAt = DateTime.Now; + r.CreatedUserId = job.UserId ?? ""; + r.CreatedFullName = job.FullName ?? "System Administrator"; + r.LastUpdatedAt = DateTime.Now; + r.LastUpdateUserId = job.UserId ?? ""; + r.LastUpdateFullName = job.FullName ?? "System Administrator"; + r.RecruitImport = importRef; - var occupation = new RecruitOccupation() + // Store child entities in separate lists for bulk insert + var education = new RecruitEducation() + { + Id = Guid.NewGuid(), + Degree = workSheet?.Cells[row, 18]?.GetValue() ?? "", + Major = workSheet?.Cells[row, 19]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 20]?.GetValue() ?? "" : workSheet?.Cells[row, 19]?.GetValue() ?? "", + MajorGroupId = "", + MajorGroupName = "", + University = workSheet?.Cells[row, 21]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue() ?? "" : workSheet?.Cells[row, 21]?.GetValue() ?? "", + GPA = (double)workSheet?.Cells[row, 26]?.GetValue(), + Specialist = "", + HighDegree = workSheet?.Cells[row, 27]?.GetValue() ?? "", + BachelorDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 25]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 25]?.GetValue() ?? "", "dd/MM/yyyy") : null, + Recruit = r, + CreatedAt = DateTime.Now, + CreatedUserId = job.UserId ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + var occupation = new RecruitOccupation() + { + Id = Guid.NewGuid(), + Occupation = workSheet?.Cells[row, 33]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 34]?.GetValue() ?? "" : workSheet?.Cells[row, 33]?.GetValue() ?? "", + Position = workSheet?.Cells[row, 37]?.GetValue() ?? "", + Workplace = $"{(workSheet?.Cells[row, 36]?.GetValue() ?? "")} {(workSheet?.Cells[row, 35]?.GetValue() ?? "")}", + Telephone = workSheet?.Cells[row, 9999]?.GetValue() ?? "", + WorkAge = workSheet?.Cells[row, 9999]?.GetValue() ?? "", + Recruit = r, + CreatedAt = DateTime.Now, + CreatedUserId = job.UserId ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + var address = new RecruitAddress() + { + Id = Guid.NewGuid(), + Address = $"{(workSheet?.Cells[row, 49]?.GetValue() ?? "")} {(workSheet?.Cells[row, 50]?.GetValue() ?? "")}", + Moo = workSheet?.Cells[row, 51]?.GetValue() ?? "", + Soi = workSheet?.Cells[row, 52]?.GetValue() ?? "", + Road = workSheet?.Cells[row, 53]?.GetValue() ?? "", + District = workSheet?.Cells[row, 54]?.GetValue() ?? "", + Amphur = workSheet?.Cells[row, 55]?.GetValue() ?? "", + Province = workSheet?.Cells[row, 56]?.GetValue() ?? "", + ZipCode = (workSheet?.Cells[row, 57]?.GetValue() ?? "").Trim(), + Telephone = workSheet?.Cells[row, 58]?.GetValue() ?? "", + Mobile = "", + Address1 = $"{(workSheet?.Cells[row, 61]?.GetValue() ?? "")} {(workSheet?.Cells[row, 62]?.GetValue() ?? "")}", + Moo1 = workSheet?.Cells[row, 63]?.GetValue() ?? "", + Soi1 = workSheet?.Cells[row, 64]?.GetValue() ?? "", + Road1 = workSheet?.Cells[row, 65]?.GetValue() ?? "", + District1 = workSheet?.Cells[row, 66]?.GetValue() ?? "", + Amphur1 = workSheet?.Cells[row, 67]?.GetValue() ?? "", + Province1 = workSheet?.Cells[row, 68]?.GetValue() ?? "", + ZipCode1 = (workSheet?.Cells[row, 69]?.GetValue() ?? "").Trim(), + Recruit = r, + CreatedAt = DateTime.Now, + CreatedUserId = job.UserId ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + var payment = new RecruitPayment() + { + Id = Guid.NewGuid(), + PaymentId = workSheet?.Cells[row, 104]?.GetValue() ?? "", + CompanyCode = workSheet?.Cells[row, 105]?.GetValue() ?? "", + TextFile = workSheet?.Cells[row, 106]?.GetValue() ?? "", + BankCode = workSheet?.Cells[row, 107]?.GetValue() ?? "", + AccountNumber = workSheet?.Cells[row, 108]?.GetValue() ?? "", + TransDate = workSheet?.Cells[row, 109]?.GetValue() ?? "", + TransTime = workSheet?.Cells[row, 110]?.GetValue() ?? "", + CustomerName = workSheet?.Cells[row, 111]?.GetValue() ?? "", + RefNo1 = workSheet?.Cells[row, 112]?.GetValue() ?? "", + TermBranch = workSheet?.Cells[row, 113]?.GetValue() ?? "", + TellerId = workSheet?.Cells[row, 114]?.GetValue() ?? "", + CreditDebit = workSheet?.Cells[row, 115]?.GetValue() ?? "", + PaymentType = workSheet?.Cells[row, 116]?.GetValue() ?? "", + ChequeNo = workSheet?.Cells[row, 117]?.GetValue() ?? "", + Amount = (decimal)workSheet?.Cells[row, 118]?.GetValue(), + ChqueBankCode = workSheet?.Cells[row, 119]?.GetValue() ?? "", + Recruit = r, + CreatedAt = DateTime.Now, + CreatedUserId = job.UserId ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + batchRecruits.Add(r); + batchEducations.Add(education); + batchOccupations.Add(occupation); + batchAddresses.Add(address); + batchPayments.Add(payment); + } + catch (Exception ex) { - Occupation = workSheet?.Cells[row, 33]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 34]?.GetValue() ?? "" : workSheet?.Cells[row, 33]?.GetValue() ?? "", - Position = workSheet?.Cells[row, 37]?.GetValue() ?? "", - Workplace = $"{(workSheet?.Cells[row, 36]?.GetValue() ?? "")} {(workSheet?.Cells[row, 35]?.GetValue() ?? "")}", - Telephone = workSheet?.Cells[row, 9999]?.GetValue() ?? "", - WorkAge = workSheet?.Cells[row, 9999]?.GetValue() ?? "", - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; - - var address = new RecruitAddress() - { - Address = $"{(workSheet?.Cells[row, 49]?.GetValue() ?? "")} {(workSheet?.Cells[row, 50]?.GetValue() ?? "")}", - Moo = workSheet?.Cells[row, 51]?.GetValue() ?? "", - Soi = workSheet?.Cells[row, 52]?.GetValue() ?? "", - Road = workSheet?.Cells[row, 53]?.GetValue() ?? "", - District = workSheet?.Cells[row, 54]?.GetValue() ?? "", - Amphur = workSheet?.Cells[row, 55]?.GetValue() ?? "", - Province = workSheet?.Cells[row, 56]?.GetValue() ?? "", - ZipCode = (workSheet?.Cells[row, 57]?.GetValue() ?? "").Trim(), - Telephone = workSheet?.Cells[row, 58]?.GetValue() ?? "", - Mobile = "", - Address1 = $"{(workSheet?.Cells[row, 61]?.GetValue() ?? "")} {(workSheet?.Cells[row, 62]?.GetValue() ?? "")}", - Moo1 = workSheet?.Cells[row, 63]?.GetValue() ?? "", - Soi1 = workSheet?.Cells[row, 64]?.GetValue() ?? "", - Road1 = workSheet?.Cells[row, 65]?.GetValue() ?? "", - District1 = workSheet?.Cells[row, 66]?.GetValue() ?? "", - Amphur1 = workSheet?.Cells[row, 67]?.GetValue() ?? "", - Province1 = workSheet?.Cells[row, 68]?.GetValue() ?? "", - ZipCode1 = (workSheet?.Cells[row, 69]?.GetValue() ?? "").Trim(), - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; - - var payment = new RecruitPayment() - { - PaymentId = workSheet?.Cells[row, 104]?.GetValue() ?? "", - CompanyCode = workSheet?.Cells[row, 105]?.GetValue() ?? "", - TextFile = workSheet?.Cells[row, 106]?.GetValue() ?? "", - BankCode = workSheet?.Cells[row, 107]?.GetValue() ?? "", - AccountNumber = workSheet?.Cells[row, 108]?.GetValue() ?? "", - TransDate = workSheet?.Cells[row, 109]?.GetValue() ?? "", - TransTime = workSheet?.Cells[row, 110]?.GetValue() ?? "", - CustomerName = workSheet?.Cells[row, 111]?.GetValue() ?? "", - RefNo1 = workSheet?.Cells[row, 112]?.GetValue() ?? "", - TermBranch = workSheet?.Cells[row, 113]?.GetValue() ?? "", - TellerId = workSheet?.Cells[row, 114]?.GetValue() ?? "", - CreditDebit = workSheet?.Cells[row, 115]?.GetValue() ?? "", - PaymentType = workSheet?.Cells[row, 116]?.GetValue() ?? "", - ChequeNo = workSheet?.Cells[row, 117]?.GetValue() ?? "", - Amount = (decimal)workSheet?.Cells[row, 118]?.GetValue(), - ChqueBankCode = workSheet?.Cells[row, 119]?.GetValue() ?? "", - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; - - r.Educations.Add(education); - r.Occupations.Add(occupation); - r.Addresses.Add(address); - r.Payments.Add(payment); - - batchRecruits.Add(r); - batchEducations.Add(education); - batchOccupations.Add(occupation); - batchAddresses.Add(address); - batchPayments.Add(payment); + throw new Exception($"Row {row}: {ex.Message}", ex); + } row++; batchCount++; @@ -478,26 +496,19 @@ public class ImportBackgroundService : BackgroundService if (batchCount >= batchSize) { - // BulkInsert Recruits first (with SetOutputIdentity to get generated Ids) - await _context.BulkInsertAsync(batchRecruits, options => + try { - options.SetOutputIdentity = true; - }); - - // Assign generated Recruit Id to child entities - for (int j = 0; j < batchRecruits.Count; j++) - { - batchEducations[j].Recruit = batchRecruits[j]; - batchOccupations[j].Recruit = batchRecruits[j]; - batchAddresses[j].Recruit = batchRecruits[j]; - batchPayments[j].Recruit = batchRecruits[j]; + await _context.BulkInsertAsync(batchRecruits); + await _context.BulkInsertAsync(batchEducations); + await _context.BulkInsertAsync(batchOccupations); + await _context.BulkInsertAsync(batchAddresses); + await _context.BulkInsertAsync(batchPayments); + } + catch (Exception ex) + { + var batchStartRow = row - batchCount + 1; + throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex); } - - // BulkInsert child entities (no identity output needed) - await _context.BulkInsertAsync(batchEducations); - await _context.BulkInsertAsync(batchOccupations); - await _context.BulkInsertAsync(batchAddresses); - await _context.BulkInsertAsync(batchPayments); // Clear all lists for next batch batchRecruits.Clear(); @@ -513,23 +524,19 @@ public class ImportBackgroundService : BackgroundService // Process remaining records if (batchRecruits.Count > 0) { - await _context.BulkInsertAsync(batchRecruits, options => + try { - options.SetOutputIdentity = true; - }); - - for (int j = 0; j < batchRecruits.Count; j++) - { - batchEducations[j].Recruit = batchRecruits[j]; - batchOccupations[j].Recruit = batchRecruits[j]; - batchAddresses[j].Recruit = batchRecruits[j]; - batchPayments[j].Recruit = batchRecruits[j]; + await _context.BulkInsertAsync(batchRecruits); + await _context.BulkInsertAsync(batchEducations); + await _context.BulkInsertAsync(batchOccupations); + await _context.BulkInsertAsync(batchAddresses); + await _context.BulkInsertAsync(batchPayments); + } + catch (Exception ex) + { + var batchStartRow = row - batchCount + 1; + throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex); } - - await _context.BulkInsertAsync(batchEducations); - await _context.BulkInsertAsync(batchOccupations); - await _context.BulkInsertAsync(batchAddresses); - await _context.BulkInsertAsync(batchPayments); } } diff --git a/Services/ImportJobTracker.cs b/Services/ImportJobTracker.cs index fcefd8b..ebd795b 100644 --- a/Services/ImportJobTracker.cs +++ b/Services/ImportJobTracker.cs @@ -36,6 +36,7 @@ public class ImportJobInfo public string ImportDocId { get; set; } = ""; public string? UserId { get; set; } public string? FullName { get; set; } + public string? Token { get; set; } // For CandidateFile public PostRecruitImportRequest? Request { get; set; } diff --git a/Services/NotificationService.cs b/Services/NotificationService.cs new file mode 100644 index 0000000..9f142e7 --- /dev/null +++ b/Services/NotificationService.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; + +namespace BMA.EHR.Recruit.Services; + +public class NotificationService +{ + private readonly IHttpClientFactory _httpClientFactory; + private readonly ILogger _logger; + private readonly IConfiguration _configuration; + + private string NotifyEndpoint = "https://hrmsbkk.case-collection.com/api/v1/org/through-socket/notify-from-token"; + + public NotificationService(IHttpClientFactory httpClientFactory, ILogger logger, IConfiguration configuration) + { + _httpClientFactory = httpClientFactory; + _logger = logger; + _configuration = configuration; + NotifyEndpoint = $"{_configuration["API"]}/org/through-socket/notify-from-token"; + } + + public async Task SendImportNotificationAsync(string? token, bool error, string message) + { + if (string.IsNullOrEmpty(token)) + { + _logger.LogWarning("Cannot send import notification: token is null or empty."); + return; + } + + try + { + var client = _httpClientFactory.CreateClient("default"); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); + + var payload = new + { + error, + message + }; + + var json = JsonConvert.SerializeObject(payload); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await client.PostAsync(NotifyEndpoint, content); + + if (!response.IsSuccessStatusCode) + { + var responseBody = await response.Content.ReadAsStringAsync(); + _logger.LogWarning("Import notification failed with status {StatusCode}: {Body}", response.StatusCode, responseBody); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to send import notification: {Message}", ex.Message); + } + } +} diff --git a/appsettings.json b/appsettings.json index 869e914..4585fbe 100644 --- a/appsettings.json +++ b/appsettings.json @@ -18,7 +18,7 @@ "MongoConnection": "mongodb://admin:adminVM123@127.0.0.1:27017", "DefaultConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", "OrgConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_organization;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", - "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None" + "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None;AllowLoadLocalInfile=true;" }, "Jwt": { "Key": "j7C9RO_p4nRtuwCH4z9Db_A_6We42tkD_p4lZtDrezc", diff --git a/bin/Debug/net7.0/appsettings.json b/bin/Debug/net7.0/appsettings.json index 869e914..4585fbe 100644 --- a/bin/Debug/net7.0/appsettings.json +++ b/bin/Debug/net7.0/appsettings.json @@ -18,7 +18,7 @@ "MongoConnection": "mongodb://admin:adminVM123@127.0.0.1:27017", "DefaultConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", "OrgConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_organization;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", - "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None" + "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None;AllowLoadLocalInfile=true;" }, "Jwt": { "Key": "j7C9RO_p4nRtuwCH4z9Db_A_6We42tkD_p4lZtDrezc",