Sharkey/src/queue/processors/db/delete-notes.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-03-07 16:07:21 +02:00
import * as Bull from 'bull';
2019-02-20 18:30:21 +02:00
import * as mongo from 'mongodb';
2019-03-07 16:07:21 +02:00
import { queueLogger } from '../../logger';
import Note from '../../../models/note';
import deleteNote from '../../../services/note/delete';
import User from '../../../models/user';
2019-02-20 18:30:21 +02:00
const logger = queueLogger.createSubLogger('delete-notes');
2019-03-07 16:07:21 +02:00
export async function deleteNotes(job: Bull.Job, done: any): Promise<void> {
2019-02-20 18:30:21 +02:00
logger.info(`Deleting notes of ${job.data.user._id} ...`);
const user = await User.findOne({
_id: new mongo.ObjectID(job.data.user._id.toString())
});
let deletedCount = 0;
let ended = false;
let cursor: any = null;
while (!ended) {
const notes = await Note.find({
userId: user._id,
...(cursor ? { _id: { $gt: cursor } } : {})
}, {
limit: 100,
sort: {
_id: 1
}
});
if (notes.length === 0) {
ended = true;
2019-03-07 16:07:21 +02:00
job.progress(100);
2019-02-20 18:30:21 +02:00
break;
}
cursor = notes[notes.length - 1]._id;
for (const note of notes) {
await deleteNote(user, note, true);
deletedCount++;
}
const total = await Note.count({
userId: user._id,
});
2019-03-07 16:07:21 +02:00
job.progress(deletedCount / total);
2019-02-20 18:30:21 +02:00
}
logger.succ(`All notes (${deletedCount}) of ${user._id} has been deleted.`);
done();
}