-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportApplicationContactsData.js
More file actions
38 lines (32 loc) · 1.46 KB
/
exportApplicationContactsData.js
File metadata and controls
38 lines (32 loc) · 1.46 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
const functions = require('firebase-functions');
const { firebase, db } = require('../shared/admin.js');
const { exportApplicationContactsData } = require('../actions/exercises/exportApplicationContactsData')(firebase, db);
const { getDocument } = require('../shared/helpers');
const { logEvent } = require('../actions/logs/logEvent')(firebase, db);
module.exports = functions.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 (!(typeof data.exerciseId === 'string') || data.exerciseId.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'Please specify an "exerciseId"');
}
if (!(typeof data.status === 'string') || data.status.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'Please specify a "status"');
}
// log an event
const exercise = await getDocument(db.collection('exercises').doc(data.exerciseId));
let details = {
exerciseId: exercise.id,
exerciseRef: exercise.referenceNumber,
status: data.status,
};
let user = {
id: context.auth.token.user_id,
name: context.auth.token.name,
};
await logEvent('info', 'Application contacts exported', details, user);
// return the requested data
return await exportApplicationContactsData(data.exerciseId, data.status);
});