working #18

Merged
derek merged 19 commits from working into main 2025-08-04 21:16:01 -07:00
3 changed files with 60 additions and 46 deletions
Showing only changes of commit f093440b00 - Show all commits
@@ -94,6 +94,29 @@ namespace BoredCareers.Services.DatabaseService {
return applications.ToArray(); return applications.ToArray();
} }
public async Task<string[]> GetApplicationResponseEmailFromJobListing(int JobListingID) {
List<string> emailadds = new List<string>();
using (MySqlConnection connection = GetConnection()) {
await connection.OpenAsync();
string command = @"
SELECT ResponseEmail
FROM JobApplication
WHERE JobListingID = @JobListingID
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@JobListingID", JobListingID);
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
while (await reader.ReadAsync()) {
string _responseemail = reader.GetString("ResponseEmail");
emailadds.Add(_responseemail);
}
}
}
return emailadds.ToArray();
}
public async Task<Application?> GetApplication(int ApplicationID) { public async Task<Application?> GetApplication(int ApplicationID) {
Application? application = null; Application? application = null;
using (MySqlConnection connection = GetConnection()) { using (MySqlConnection connection = GetConnection()) {
@@ -1,4 +1,5 @@
using BoredCareers.Entities; using BoredCareers.Entities;
using BoredCareers.Services.TimerService;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
@@ -177,12 +178,12 @@ namespace BoredCareers.Services.DatabaseService {
return joblisting; return joblisting;
} }
public async Task<JobListing[]> GetJobListingsPastExipre() { public async Task<JobListingDTO[]> GetJobListingsPastExipre() {
List<JobListing> joblistings = new List<JobListing>(); List<JobListingDTO> joblistings = new List<JobListingDTO>();
using (MySqlConnection connection = GetConnection()) { using (MySqlConnection connection = GetConnection()) {
await connection.OpenAsync(); await connection.OpenAsync();
string command = @" string command = @"
SELECT * SELECT ID, CompanyID
FROM JobListing FROM JobListing
WHERE IsDeleted = FALSE WHERE IsDeleted = FALSE
AND CreatedTime < NOW() - INTERVAL 1 MONTH; AND CreatedTime < NOW() - INTERVAL 1 MONTH;
@@ -193,38 +194,9 @@ namespace BoredCareers.Services.DatabaseService {
while (await reader.ReadAsync()) { while (await reader.ReadAsync()) {
int _id = reader.GetInt32("ID"); int _id = reader.GetInt32("ID");
int _companyid = reader.GetInt32("CompanyID"); int _companyid = reader.GetInt32("CompanyID");
string _title = reader.GetString("Title"); joblistings.Add(new JobListingDTO() {
string _postalcode = reader.GetString("PostalCode"); JobListingID = _id,
string _country = reader.GetString("Country"); CompanyID = _companyid
string _state = reader.GetString("StateOrRegion");
string _city = reader.GetString("City");
int _salarymin = reader.GetInt32("SalaryMin");
int _salarymax = reader.GetInt32("SalaryMax");
string _jobtype = reader.GetString("JobType");
bool _remote = reader.GetBoolean("Remote");
string _description = reader.GetString("Description");
JobListingSkill[] _skills = await GetJobListingSkills(_id);
DateTime _createtime = reader.GetDateTime("CreatedTime");
DateTime _modifiedtime = reader.GetDateTime("ModifiedTime");
bool _isdeleted = reader.GetBoolean("IsDeleted");
joblistings.Add(new JobListing() {
ID = _id,
CompanyID = _companyid,
Title = _title,
PostalCode = _postalcode,
Country = _country,
StateOrRegion = _state,
City = _city,
SalaryMin = _salarymin,
SalaryMax = _salarymax,
JobType = _jobtype,
Remote = _remote,
Description = _description,
Skills = _skills,
CreatedTime = _createtime,
ModifiedTime = _modifiedtime,
IsDeleted = _isdeleted
}); });
} }
} }
@@ -14,26 +14,45 @@ namespace BoredCareers.Services.TimerService {
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
while (!stoppingToken.IsCancellationRequested) { while (!stoppingToken.IsCancellationRequested) {
try { try {
JobListing[] deletedJobListings = await _db.GetJobListingsPastExipre(); // Get listing's past expire
await _db.DeleteJobListingsPastExipre(); JobListingDTO[] deletedJobListings = await _db.GetJobListingsPastExipre();
foreach (JobListing listing in deletedJobListings) {
Application[] apps = await _db.GetApplicationsFromJobListing(Convert.ToInt32(listing.ID));
foreach (Application app in apps) {
_em.Send(app.ResponseEmail, EmailService.JobAutoClosedSubject, EmailService.JobAutoClosedEmail);
}
// Mark the company for the auto closed listing // Group them by CompanyID, ListingCount
Company? comp = await _db.GetCompany(listing.CompanyID); Dictionary<int, int> listingsByCompany = deletedJobListings
.GroupBy(l => l.CompanyID)
.ToDictionary(g => g.Key, g => g.Count());
// Update each company's rating
foreach (KeyValuePair<int, int> kvp in listingsByCompany) {
Company? comp = await _db.GetCompany(kvp.Key);
if (comp != null) { if (comp != null) {
comp.JobsAutoClosed++; comp.JobsAutoClosed += kvp.Value;
await _db.SetCompany(comp); await _db.SetCompany(comp);
} }
} }
// Get each listing
foreach (JobListingDTO listing in deletedJobListings) {
// Get each Person
string[] emails = await _db.GetApplicationResponseEmailFromJobListing(listing.JobListingID);
foreach (string email in emails) {
// Send Notify Email
_em.Send(email, EmailService.JobAutoClosedSubject, EmailService.JobAutoClosedEmail);
}
}
// Delete Listing's past expire
await _db.DeleteJobListingsPastExipre();
} catch (Exception e) { } catch (Exception e) {
Console.WriteLine($"Error: {e.Message}"); Console.WriteLine($"Error: {e.Message}");
} }
await Task.Delay(TimeSpan.FromHours(24), stoppingToken); await Task.Delay(TimeSpan.FromHours(2), stoppingToken);
} }
} }
} }
public class JobListingDTO {
public int JobListingID { get; set; }
public int CompanyID { get; set; }
}
} }