I want to remove all un-complete accounts in my app where the user signed up, created an account but didn't finish the verification process by uploading certain images...
I am running a cron job that performs this task every 24 hours...
I want to remove all objects where imageUrlOne & imageUrlTwo == "" AND createdAt is older than 24 hours.
My current code isn't working, any idea what I am doing wrong?
THANK YOU
const User = require("../models/user-model")
const removeInactiveAccounts = async () => {
let twentyFourHours = miliseconds(24, 0, 0)
// hours, min, seconds
function miliseconds(hrs, min, sec) {
return (hrs * 60 * 60 min * 60 sec) * 1000
}
let startDate = Date.now() // todays date
let endDate = startDate - twentyFourHours // 24 hours ago from today
try {
await User.deleteMany({
imageUrlOne: "",
imageUrlTwo: "",
createdAt: {
$gte: startDate,
$lte: endDate,
},
})
} catch (err) {
console.log(err.message)
}
}
module.exports = removeInactiveAccounts
CodePudding user response:
You have your signs a little mixed up.
You're saying greater than "right now", and less than 24 hours ago.
For simplicity, let's say "right now" is 86400
. That would make "24 hours ago" 0
.
So, you have: createdAt >= 86400 && createdAt <= 0
.
Well, that is impossible. No number can be greater than 86,400 and less than 0 at the same time.
You don't need to include the start date at all in this case. Ditch it and just have createdAt less than endDate and you should be good.