Skip to content

Instantly share code, notes, and snippets.

@ezesundayeze
Last active December 25, 2023 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezesundayeze/ded17ddba0b9d22c7f0d6ac62643a43e to your computer and use it in GitHub Desktop.
Save ezesundayeze/ded17ddba0b9d22c7f0d6ac62643a43e to your computer and use it in GitHub Desktop.
import schedule from 'node-schedule';
const publishPost = (postId: string, isScheduled: boolean, dateTime: string) => {
if (isScheduled) {
let date = extractDateParam(new Date(dateTime));
const { year, month, hours, minutes, seconds } = date;
schedule.scheduleJob(new Date(year, month, hours, minutes, seconds), function () {
console.log("Scheduled job running at:", new Date());
// call the function that updates the publishing status and pass pass the id to it.
updatePostStatus(postId)
})
} else {
// Implement instant posting -- simply changing the draft status to published
updatePostStatus(postId)
}
};
const updatePostStatus = (postId: string) => {
console.log("Post with id:", postId, " has been published");
}
function extractDateParam(date: Date) {
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return { year, month, day, hours, minutes, seconds };
}
publishPost("post-id1", true, "2023-12-25T06:03:24+00:00");
publishPost("post-id3", false, "2023-12-25T02:26:22+00:00");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment