-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch-server.ts
executable file
·70 lines (36 loc) · 1.37 KB
/
batch-server.ts
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
55
56
import {firebaseConfig} from "environments/firebase.config";
import {initializeApp, auth,database} from 'firebase';
var Queue = require('firebase-queue');
console.log('Running batch server ...');
initializeApp(firebaseConfig);
auth()
.signInWithEmailAndPassword('[email protected]', 'test123')
.then(runConsumer)
.catch(onError);
function onError(err) {
console.error("Could not login", err);
process.exit();
}
function runConsumer() {
console.log("Running consumer ...");
const lessonsRef = database().ref("lessons");
const lessonsPerCourseRef = database().ref("lessonsPerCourse");
const queueRef = database().ref('queue');
const queue = new Queue(queueRef, function(data, progress, resolve, reject) {
console.log('received delete request ...',data);
const deleteLessonPromise = lessonsRef.child(data.lessonId).remove();
const deleteLessonPerCoursePromise =
lessonsPerCourseRef.child(`${data.courseId}/${data.lessonId}`).remove();
Promise.all([deleteLessonPromise, deleteLessonPerCoursePromise])
.then(
() => {
console.log("lesson deleted");
resolve();
}
)
.catch(() => {
console.log("lesson deletion in error");
reject();
});
});
}