Skip to content

Commit

Permalink
to: adding sort from old to new, send old news first
Browse files Browse the repository at this point in the history
  • Loading branch information
Sma1lboy committed Aug 23, 2024
1 parent 1255e94 commit b270540
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 32 deletions.
50 changes: 34 additions & 16 deletions src/providers/InternJobProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Job, JobProvider } from '../types';
* @source https://github.com/SimplifyJobs/Summer2025-Internships
*/
export class InternJobProvider implements JobProvider {
readonly jobType = 'INTERN';

private sentJobsPath: string;
private config: Config;
private githubUrl: string =
Expand Down Expand Up @@ -136,16 +138,22 @@ export class InternJobProvider implements JobProvider {
'Dec',
];

return jobs.filter((job) => {
const [month, day] = job.datePosted.split(' ');
const jobDate = new Date(today.getFullYear(), months.indexOf(month), parseInt(day));
return jobs
.filter((job) => {
const [month, day] = job.datePosted.split(' ');
const jobDate = new Date(today.getFullYear(), months.indexOf(month), parseInt(day));

if (jobDate > today) {
jobDate.setFullYear(jobDate.getFullYear() - 1);
}
if (jobDate > today) {
jobDate.setFullYear(jobDate.getFullYear() - 1);
}

return jobDate >= cutoffDate;
});
return jobDate >= cutoffDate;
})
.sort((a, b) => {
const dateA = new Date(a.datePosted);
const dateB = new Date(b.datePosted);
return dateA.getTime() - dateB.getTime();
});
}

public async getNewJobs(): Promise<Job[]> {
Expand All @@ -171,7 +179,7 @@ export class InternJobProvider implements JobProvider {
);

if (newJobs.length > 0) {
sentJobs = [...sentJobs, ...newJobs];
sentJobs = [...newJobs, ...sentJobs];
fs.writeFileSync(this.sentJobsPath, JSON.stringify(sentJobs));
}

Expand All @@ -180,14 +188,24 @@ export class InternJobProvider implements JobProvider {

public formatJobMessages(jobs: Job[]): string[] {
const messages: string[] = [];
for (let i = 0; i < jobs.length; i += this.config.jobsPerMessage) {
const jobGroup = jobs.slice(i, i + this.config.jobsPerMessage);
let message = '📢 New Job Opportunities for INTERN📢\n\n';
jobGroup.forEach((job) => {
message += this.formatJobMessage(job);
});
messages.push(message);
let currentMessage = `📢 New Job Opportunities for ${this.jobType} 📢\n\n`;
let jobCount = 0;

for (const job of jobs) {
currentMessage += this.formatJobMessage(job);
jobCount++;

if (jobCount === this.config.jobsPerMessage) {
messages.push(currentMessage);
currentMessage = `📢 New Job Opportunities for ${this.jobType} 📢\n\n`;
jobCount = 0;
}
}

if (jobCount > 0) {
messages.push(currentMessage);
}

return messages;
}

Expand Down
50 changes: 34 additions & 16 deletions src/providers/NGJobProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Job, JobProvider } from '../types';
* @source https://github.com/SimplifyJobs/New-Grad-Positions
*/
export class NGJobProvider implements JobProvider {
readonly jobType = 'New Graduate';

private sentJobsPath: string;
private config: Config;
private githubUrl: string =
Expand Down Expand Up @@ -136,16 +138,22 @@ export class NGJobProvider implements JobProvider {
'Dec',
];

return jobs.filter((job) => {
const [month, day] = job.datePosted.split(' ');
const jobDate = new Date(today.getFullYear(), months.indexOf(month), parseInt(day));
return jobs
.filter((job) => {
const [month, day] = job.datePosted.split(' ');
const jobDate = new Date(today.getFullYear(), months.indexOf(month), parseInt(day));

if (jobDate > today) {
jobDate.setFullYear(jobDate.getFullYear() - 1);
}
if (jobDate > today) {
jobDate.setFullYear(jobDate.getFullYear() - 1);
}

return jobDate >= cutoffDate;
});
return jobDate >= cutoffDate;
})
.sort((a, b) => {
const dateA = new Date(a.datePosted);
const dateB = new Date(b.datePosted);
return dateA.getTime() - dateB.getTime();
});
}

public async getNewJobs(): Promise<Job[]> {
Expand All @@ -171,7 +179,7 @@ export class NGJobProvider implements JobProvider {
);

if (newJobs.length > 0) {
sentJobs = [...sentJobs, ...newJobs];
sentJobs = [...newJobs, ...sentJobs];
fs.writeFileSync(this.sentJobsPath, JSON.stringify(sentJobs));
}

Expand All @@ -180,14 +188,24 @@ export class NGJobProvider implements JobProvider {

public formatJobMessages(jobs: Job[]): string[] {
const messages: string[] = [];
for (let i = 0; i < jobs.length; i += this.config.jobsPerMessage) {
const jobGroup = jobs.slice(i, i + this.config.jobsPerMessage);
let message = '📢 New Job Opportunities for New Graduates 📢\n\n';
jobGroup.forEach((job) => {
message += this.formatJobMessage(job);
});
messages.push(message);
let currentMessage = `📢 New Job Opportunities for ${this.jobType} 📢\n\n`;
let jobCount = 0;

for (const job of jobs) {
currentMessage += this.formatJobMessage(job);
jobCount++;

if (jobCount === this.config.jobsPerMessage) {
messages.push(currentMessage);
currentMessage = `📢 New Job Opportunities for ${this.jobType} 📢\n\n`;
jobCount = 0;
}
}

if (jobCount > 0) {
messages.push(currentMessage);
}

return messages;
}

Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface Job {
* Interface for job providers
*/
export interface JobProvider {
readonly jobType: string;

/**
* Fetches new jobs from the source
* @returns Promise<Job[]> An array of new job listings
Expand Down

0 comments on commit b270540

Please sign in to comment.