Files
fredy/lib/services/storage/jobStorage.js
Christian Kellner 59e6d287fc adding similarity check (#29)
* adding similarity check

* adding paging

* fixing tests

* docu

* better error handling

* fixing tests

* adjusting page limit

* fixing login screen

* cleanup

* upgrade browser list

* prevent spamming the log

* fixing tests

* removing job listings when removing a job or the user
2021-06-28 08:52:09 +02:00

90 lines
2.0 KiB
JavaScript

const path = require('path');
const DB_PATH = path.dirname(require.main.filename) + '/db/jobs.json';
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync(DB_PATH);
const low = require('lowdb');
const db = low(adapter);
const { nanoid } = require('nanoid');
const listingStorage = require('./listingsStorage');
db.defaults({ jobs: [] }).write();
exports.upsertJob = ({ jobId, name, blacklist = [], enabled = true, provider, notificationAdapter, userId }) => {
const currentJob =
jobId == null
? null
: db
.get('jobs')
.find((job) => job.id === jobId)
.value();
const jobs = db
.get('jobs')
.value()
.filter((job) => job.id !== jobId);
jobs.push({
id: jobId || nanoid(),
//make sure to not overwrite the user id in case an admin changes the job
userId: currentJob == null ? userId : currentJob.userId,
enabled,
name,
blacklist,
provider,
notificationAdapter,
});
db.set('jobs', jobs).write();
};
exports.getJob = (jobId) => {
const job = db
.get('jobs')
.find((job) => job.id === jobId)
.value();
if (job == null) {
return null;
}
return {
...job,
numberOfFoundListings: listingStorage.getNumberOfAllKnownListings(job.id).length,
};
};
exports.setJobStatus = ({ jobId, status }) => {
db.get('jobs')
.find((job) => job.id === jobId)
.assign({ enabled: status })
.write();
};
exports.removeJob = (jobId) => {
listingStorage.removeListings(jobId);
db.get('jobs')
.remove((job) => job.id === jobId)
.write();
};
exports.removeJobsByUserId = (userId) => {
db.get('jobs')
.value()
.filter((job) => job.userId === userId)
.forEach((job) => listingStorage.removeListings(job.id));
db.get('jobs')
.remove((job) => job.userId === userId)
.write();
};
exports.getJobs = () => {
return db
.get('jobs')
.value()
.map((job) => ({
...job,
numberOfFoundListings: listingStorage.getNumberOfAllKnownListings(job.id),
}));
};