-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
59 lines (50 loc) · 1.52 KB
/
index.js
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
57
58
59
const appInsights = require('applicationinsights');
appInsights.setup();
const client = appInsights.defaultClient;
module.exports = async function (context, req) {
var operationIdOverride = {
'ai.operation.id': context.traceContext.traceparent,
};
if (!req.body || !('bpm' in req.body) || !('isCorrect' in req.body)) {
client.trackException({
exception: new Error('No required parameter!'),
tagOverrides: operationIdOverride,
});
context.res = { status: 404, body: 'No required parameter!' };
context.done();
}
let clientPrincipal = {};
try {
const header = req.headers['x-ms-client-principal'];
const encoded = Buffer.from(header, 'base64');
const decoded = encoded.toString('ascii');
clientPrincipal = JSON.parse(decoded);
} catch (err) {
context.log(`${err.name}: ${err.message}`);
}
const bpm = req.body.bpm;
const isCorrect = req.body.isCorrect;
const timestamp = Math.floor(Date.now() / 1);
context.bindings.outputDocument = JSON.stringify({
id: new Date().toISOString() + Math.random().toString().substr(2, 8),
bpm: bpm,
isCorrect: isCorrect,
timestamp: timestamp,
});
client.trackEvent({
name: 'feedback_save',
tagOverrides: operationIdOverride,
properties: {
id: new Date().toISOString() + Math.random().toString().substr(2, 8),
bpm: bpm,
isCorrect: isCorrect,
timestamp: timestamp,
},
});
context.res = {
body: {
message: 'Thank you!',
clientPrincipal: clientPrincipal,
},
};
};