-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportExerciseData.js
More file actions
51 lines (44 loc) · 1.63 KB
/
exportExerciseData.js
File metadata and controls
51 lines (44 loc) · 1.63 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
const functions = require('firebase-functions');
const { checkArguments } = require('../shared/helpers.js');
const config = require('../shared/config');
const { firebase, db } = require('../shared/admin.js');
const { exportExerciseData } = require('../actions/exercises/exportExerciseData')(config, firebase, db);
const { getAllDocuments } = require('../shared/helpers');
const { logEvent } = require('../actions/logs/logEvent')(firebase, db);
const runtimeOptions = {
timeoutSeconds: 300,
memory: '1GB',
};
module.exports = functions.runWith(runtimeOptions).region('europe-west2').https.onCall(async (data, context) => {
// authenticate the request
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
// validate input parameters
if (!checkArguments({
exerciseIds: { required: true },
}, data)) {
throw new functions.https.HttpsError('invalid-argument', 'Please provide valid arguments');
}
// generate the export
const result = await exportExerciseData(data.exerciseIds);
// log an event
const exerciseRefs = data.exerciseIds.map(id => db.collection('exercises').doc(id));
const exercises = await getAllDocuments(db, exerciseRefs);
let details = {
exercises: [],
};
exercises.forEach(exercise => {
details.exercises.push({
exerciseId: exercise.id,
exerciseRef: exercise.referenceNumber,
});
});
let user = {
id: context.auth.token.user_id,
name: context.auth.token.name,
};
await logEvent('info', 'Exercises exported', details, user);
// return the report to the caller
return result;
});