diff --git a/lib/services/demoCleanup.js b/lib/services/demoCleanup.js index 4386dd4..5879d57 100644 --- a/lib/services/demoCleanup.js +++ b/lib/services/demoCleanup.js @@ -1,29 +1,38 @@ import { setInterval } from 'node:timers'; -import {removeJobsByUserName} from './storage/jobStorage.js'; -import {config} from '../utils.js'; +import { removeJobsByUserName } from './storage/jobStorage.js'; +import { config } from '../utils.js'; +import { getUsers } from './storage/userStorage.js'; /** * if we are running in demo environment, we have to cleanup the db files (specifically the jobs table) */ export function cleanupDemoAtMidnight() { - const now = new Date(); - const millisUntilMidnightUTC = (24 - now.getUTCHours()) * 60 * 60 * 1000 - - now.getUTCMinutes() * 60 * 1000 - - now.getUTCSeconds() * 1000 - - now.getUTCMilliseconds(); + const now = new Date(); + const millisUntilMidnightUTC = + (24 - now.getUTCHours()) * 60 * 60 * 1000 - + now.getUTCMinutes() * 60 * 1000 - + now.getUTCSeconds() * 1000 - + now.getUTCMilliseconds(); - setTimeout(() => { + setTimeout(() => { + cleanup(); + + setInterval( + () => { cleanup(); - - setInterval(() => { - cleanup(); - }, 24 * 60 * 60 * 1000); - - }, millisUntilMidnightUTC); + }, + 24 * 60 * 60 * 1000, + ); + }, millisUntilMidnightUTC); } -function cleanup(){ - if(config.demoMode){ - removeJobsByUserName('demo'); +function cleanup() { + if (config.demoMode) { + const demoUser = getUsers(false).find((user) => user.username === 'demo'); + if (demoUser == null) { + console.error('Demo user not found, cannot remove Jobs'); + return; } -} \ No newline at end of file + removeJobsByUserName(demoUser.id); + } +} diff --git a/lib/services/storage/jobStorage.js b/lib/services/storage/jobStorage.js index 655ada2..c3a94a9 100644 --- a/lib/services/storage/jobStorage.js +++ b/lib/services/storage/jobStorage.js @@ -11,7 +11,6 @@ const db = new LowdashAdapter(adapter, { jobs: [] }); db.read(); - export const upsertJob = ({ jobId, name, blacklist = [], enabled = true, provider, notificationAdapter, userId }) => { const currentJob = jobId == null @@ -77,15 +76,15 @@ export const removeJobsByUserId = (userId) => { .value(); db.write(); }; -export const removeJobsByUserName = (userName) => { +export const removeJobsByUserName = (userId) => { db.chain - .get('jobs') - .filter((job) => job.username === userName) - .forEach((job) => listingStorage.removeListings(job.id)); + .get('jobs') + .filter((job) => job.userId === userId) + .forEach((job) => listingStorage.removeListings(job.id)); db.chain - .get('jobs') - .remove((job) => job.username === userName) - .value(); + .get('jobs') + .remove((job) => job.userId === userId) + .value(); db.write(); }; export const getJobs = () => {