Sharkey/src/queue/processors/db/delete-drive-files.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 User from '../../../models/user';
import DriveFile from '../../../models/drive-file';
import deleteFile from '../../../services/drive/delete-file';
2019-02-20 18:30:21 +02:00
const logger = queueLogger.createSubLogger('delete-drive-files');
2019-03-07 16:07:21 +02:00
export async function deleteDriveFiles(job: Bull.Job, done: any): Promise<void> {
2019-02-20 18:30:21 +02:00
logger.info(`Deleting drive files 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 files = await DriveFile.find({
userId: user._id,
...(cursor ? { _id: { $gt: cursor } } : {})
}, {
limit: 100,
sort: {
_id: 1
}
});
if (files.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 = files[files.length - 1]._id;
for (const file of files) {
await deleteFile(file);
deletedCount++;
}
const total = await DriveFile.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 drive files (${deletedCount}) of ${user._id} has been deleted.`);
done();
}