-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteFile.js
More file actions
54 lines (52 loc) · 2.07 KB
/
deleteFile.js
File metadata and controls
54 lines (52 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
export async function deleteFile(file) {
let bucket;
let updated;
let arr;
try {
switch (file.bucket) {
case "course":
const { courseMaterialsBucket } = require("./buckets");
bucket = courseMaterialsBucket;
let course = await Course.findById(file.parentInstance);
arr = course.materials;
arr.remove(args.id);
updated = await Course.findByIdAndUpdate({ _id: course._id }, { materials: arr }, {
returnOriginal: false
});
break;
case "lesson":
const { lessonMaterialsBucket } = require("./buckets");
bucket = lessonMaterialsBucket;
let lesson = await Lesson.findById(file.parentInstance);
arr = lesson.materials;
arr.remove(args.id);
updated = await Lesson.findByIdAndUpdate({ _id: lesson._id }, { materials: arr }, {
returnOriginal: false
});
break;
case "pic":
const { profilePicsBucket } = require("./buckets");
bucket = profilePicsBucket;
updated = await Person.findByIdAndUpdate({ _id: file.parentInstance }, { photo: null }, {
returnOriginal: false
});
break;
case "answer":
const { answerMaterialsBucket } = require("./buckets");
bucket = answerMaterialsBucket;
let answer = await Answer.findById(file.parentInstance);
arr = answer.materials;
arr.remove(args.id);
updated = await Answer.findByIdAndUpdate({ _id: answer._id }, { materials: arr }, {
returnOriginal: false
});
break;
}
} catch (err) {
throw err;
}
bucket.delete(file.fileId, function (error) {
});
const res = await File.remove({ _id: file._id });
return { affectedRows: res.deletedCount };
}