Add calender support in server
This commit is contained in:
@@ -4,39 +4,41 @@ const path = require('path');
|
||||
// Initialize Sequelize with SQLite
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: path.join(__dirname, '../database.sqlite'),
|
||||
logging: false, // Set to console.log to see SQL queries
|
||||
storage: path.join(__dirname, '..', 'database.sqlite'),
|
||||
logging: process.env.NODE_ENV === 'development' ? console.log : false
|
||||
});
|
||||
|
||||
// Import models
|
||||
const ShoppingItem = require('./ShoppingItem')(sequelize);
|
||||
// Import and initialize models
|
||||
const createShoppingItem = require('./ShoppingItem');
|
||||
const createCalendarUser = require('./CalendarUser');
|
||||
|
||||
// Function to clean up checked items older than 2 hours
|
||||
const ShoppingItem = createShoppingItem(sequelize);
|
||||
const CalendarUser = createCalendarUser(sequelize);
|
||||
|
||||
// Function to clean up checked shopping items older than 24 hours
|
||||
const cleanupCheckedItems = async () => {
|
||||
try {
|
||||
const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000);
|
||||
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
const deletedCount = await ShoppingItem.destroy({
|
||||
where: {
|
||||
checked: true,
|
||||
checkedAt: {
|
||||
[Sequelize.Op.lt]: twoHoursAgo,
|
||||
},
|
||||
},
|
||||
updatedAt: {
|
||||
[Sequelize.Op.lt]: oneDayAgo
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (deletedCount > 0) {
|
||||
console.log(`Cleaned up ${deletedCount} checked items older than 2 hours`);
|
||||
console.log(`Cleaned up ${deletedCount} checked shopping items older than 24 hours`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error cleaning up checked items:', error);
|
||||
console.error('Error during cleanup:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Run cleanup every 30 minutes
|
||||
setInterval(cleanupCheckedItems, 30 * 60 * 1000);
|
||||
|
||||
module.exports = {
|
||||
sequelize,
|
||||
ShoppingItem,
|
||||
cleanupCheckedItems,
|
||||
CalendarUser,
|
||||
cleanupCheckedItems
|
||||
};
|
||||
|
Reference in New Issue
Block a user