Good evening. Im Using JSJoda to calculate the difference between two dates. In the database there is a row where by default, the date is inserted automatically. I need to create a normal function (without Req, Res object) where each row will be deleted after 60 days.
const cron = require('node-cron');
const JSJoda = require('js-joda');
const {deleteRecord, getAllMessages} = require('../connection/db.js');
exports.deleteMessage = async () => {
/**
* Get all the messages and delete statement
*/
const deleteStatement = 'DELETE FROM ?? WHERE ?? = ?';
const selectStatement = 'SELECT * FROM message';
try
{
const messageList = await getAllMessages(selectStatement);
/**
* Loop over the array of messages and find the date
*/
for (message of messageList) {
let dateTarget = message.dateAdded;
/**
* Convert the date to ISOString() and leave only the dates
*/
let messageDates = dateTarget.toISOString().split('T')[0];
let today = new Date().toISOString().split('T')[0];
let difference = JSJoda.ChronoUnit.DAYS.between(JSJoda.LocalDate.parse(messageDates), JSJoda.LocalDate.parse(today));
while (difference === 0) {
// Problem is here I dont know what should I do
await deleteRecord(deleteStatement, 'message', message.messageID, value);
}
}
}
catch (error)
{
console.log(error);
}
};
In this function I get all the records, and parsed it to the required format by the library. The variable bellow apparently shows the right days, But now I realised that I am stuck.
let difference = JSJoda.ChronoUnit.DAYS.between(JSJoda.LocalDate.parse(messageDates), JSJoda.LocalDate.parse(today));
I dont know how should I proceed with this function.... Where to place it, and the most important, how to select the right messageID that must be deleted.