-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
87 lines (74 loc) · 2.45 KB
/
handler.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { deviceLogin, hasValidToken, refreshToken } = require('./lib/auth')
const { getNote } = require('./lib/onenote')
const notify = require('./lib/notify')
const localStorage = require('./lib/store')
const { promises: fs } = require('fs')
const db = require('./db/persist')
const { snakeCase } = require("snake-case");
/**
* Lambda functions have ephemeral storage on the server in /tmp.
* Seed the MSAL Key Cache and localStorage with the latest from the database
*/
async function initCache(sectionHandle) {
// populate cache with db contents
const data = await db.getItem('cache')
await fs
.writeFile(process.env.CACHE_PATH, data)
.then(console.log('Restore Cache'))
// populate local storage with login contents
// coerced to json
localStorage.initStore();
const onenote = await db.getItem('onenote', true)
localStorage.setItem('onenote', onenote)
const count = await db.getItem(`${sectionHandle}_section_count`)
localStorage.setItem(`${sectionHandle}_section_count`, count)
const lastPage = await db.getItem(`${sectionHandle}_last_page`)
localStorage.setItem(`${sectionHandle}_last_page`, lastPage)
const recent = (await db.getItem(`recent_${sectionHandle}`, true)) || []
localStorage.setItem(`recent_${sectionHandle}`, recent)
console.log('Restore localStorage')
}
const app = async (event, context) => {
let { onenoteSettings, messageSettings } = event
onenoteSettings = {
sectionHandle: snakeCase(onenoteSettings.sectionName),
isSequential: false,
...onenoteSettings
}
const resp = await initCache(onenoteSettings.sectionHandle)
.then(() => refreshToken())
.then(tokenResponse => {
if (!tokenResponse || !hasValidToken()) {
console.log('Token still invalid after refresh, initiating device login');
return deviceLogin();
}
return tokenResponse;
})
.then(() => getNote(onenoteSettings))
.then(note => {
if (typeof note === 'undefined') {
throw new Error('Note is undefined');
}
return notify.withTelegram(note, messageSettings);
})
.catch(err => {
console.log(
'Ooops!',
`Can't seem to find any notes here. Please check if you created a section called '${onenoteSettings.sectionName}', add some notes.`
);
console.error('App: Check Logs', err);
return {
status: 400,
title: 'Error',
body: err
};
});
return {
status: resp.status,
title: resp.title,
body: resp.body
}
}
module.exports = {
app
}