Hi everyone,
I have a client who needs a job listings board on his site. I have a dynamic page set up to support this, linked to a ‘jobs’ collection, which is working well. However, I would like to be able to automatically remove items from the database (or change a status column to Expired, whichever is easiest) after the closing date for applications (date & time field: closingDate).
I have tried using backend .jsw code & the job scheduler to do this as per this previous forum post, but I’m not having any luck: https://www.wix.com/velo/forum/tips-tutorials-examples/video-how-to-execute-timed-tasks-with-the-job-scheduler
I would like this to run at 1 minute past midnight UTC each night to remove any jobs that have a closing date of the previous day.
My code is below - I have set it up on a demo site here: https://finnpattinson.wixsite.com/mysite/jobs
I’m new to Velo, so any help would be greatly appreciated!
.jsw code - removeExpiredJobs.jsw
import wixData from 'wix-data';
export function clearExpiredJobs() {
// Querying all the items in the collection.
return wixData.query("jobs").find().then(results => {
// Declaring a variable for an expiration date to test by initiating it as a date object.
let expirationDate = new Date();
// Setting the expiration date for 3 months prior.
expirationDate.setDate(expirationDate.getDate() - 1);
// Using the forEach method to create a filtered array with listings older than 3 months.
let ExpiredJobs = results.items.filter(item => item.closingDate < expirationDate);
ExpiredJobs.forEach(item => {
// Removing each of this items from the collection.
wixData.remove("jobs", item._id);
})
})
}
Job scheduler
// /backend/jobs.config
{
"jobs": [ // Define up to 20 different jobs
// Choose one of the options below to define the job execution interval:
{
// Option 1 - define execution interval using a cron expression
"functionLocation": "/removeExpiredJobs/clearExpiredJobs.jsw", // Relatively to Backend folder, started by slash
"functionName": "Remove Expired Jobs",
"description": "This Job Runs the code to set expired jobs to Expired", // Optional
"executionConfig": {
"time": "00:02", // "hh:mm" 24h format, UTC timezone (e.g. 13:00)
// Optional - uncomment section below for more complex intervals
//"dayOfWeek": "Monday", // Day of week
//"dateInMonth": 1 // Number between 1-31
}
},
{
// Add more jobs. Up to 20 jobs supported.
}
]
}