carvent/index.js

124 lines
4.3 KiB
JavaScript
Raw Normal View History

2023-02-24 23:09:16 +00:00
const SETTINGS = require("./settings.js")
const { churchtoolsClient, activateLogging, LOG_LEVEL_INFO, errorHelper } = require('@churchtools/churchtools-client');
const axiosCookieJarSupport = require('axios-cookiejar-support');
const tough = require('tough-cookie');
const fs = require('fs');
const cookieJar = new tough.CookieJar();
2023-02-24 23:09:16 +00:00
function initChurchToolsClient() {
churchtoolsClient.setCookieJar(axiosCookieJarSupport.wrapper, cookieJar);
2023-02-24 23:09:16 +00:00
churchtoolsClient.setBaseUrl(SETTINGS.BASEURL);
// Logging can be activated to either LOG_LEVEL_NONE (no logging at all, default),
// LOG_LEVEL_DEBUG (outputs every request and response including request/response data)
// LOG_LEVEL_INFO (outputs every request and response, but only method and URL) or
// LOG_LEVEL_ERROR (outputs only errors).
activateLogging(LOG_LEVEL_INFO);
}
function login() {
// if we have a login token, we use this
if (SETTINGS.TOKEN) {
churchtoolsClient.setUnauthorizedInterceptor(SETTINGS.TOKEN);
// we call /api/contactlabels here, as an example. Any api call will trigger an automatic login with TOKEN
return churchtoolsClient.get('/contactlabels')
.then(() => {
console.log('Login with token successful.');
return true;
});
}
// no login token => use username / password
return churchtoolsClient.post('/login', {
username: SETTINGS.USERNAME,
password: SETTINGS.PASSWORD
}).then(() => {
console.log('Login successful.');
return true;
});
}
async function mergeEventSongs(event) {
const dt = event.startDate
const date = dt.substring(0, dt.indexOf('T'))
try {
fs.mkdirSync(date)
} catch(e) { console.log(e); }
2023-07-27 15:06:56 +00:00
//console.debug(await cookieJar.getCookieStringSync(SETTINGS.BASEURL))
cookie = await cookieJar.getCookieStringSync(SETTINGS.BASEURL)
2023-02-24 23:09:16 +00:00
return churchtoolsClient.get(`/events/${event.id}/agenda`)
.then(agenda =>
agenda.items
.map(item => item.song)
.filter(Boolean)
).then(songs =>
songs.map(songItem =>
churchtoolsClient.get(`/songs/${songItem.songId}`)
.then(song => song.arrangements.find(a => a.id == songItem.arrangementId))
)
).then(result =>
Promise.allSettled(result)
).then(result =>
result.map(song => song.value.files.find(f => f.name.includes("Akkorde")))
2023-07-27 15:06:56 +00:00
.map(f => new Promise((resolve) => {
// https://github.com/churchtools/churchtools-js-client/issues/25
2023-07-27 15:06:56 +00:00
const filename = `${date}/${f.name}`
const url = f.fileUrl
const file = fs.createWriteStream(filename);
2023-07-27 15:06:56 +00:00
const https = require('https');
https.get(
url,
{
headers: {
2023-07-27 15:06:56 +00:00
Cookie: cookie
}
},
(response) => {
response.pipe(file);
file.on("finish", () => {
file.close();
2023-07-27 15:06:56 +00:00
resolve(filename)
});
}
2023-07-27 15:06:56 +00:00
)
}))
2023-02-24 23:09:16 +00:00
).then(result =>
Promise.allSettled(result)
).then(result => {
2023-07-27 15:06:56 +00:00
//console.debug(result)
2023-02-24 23:09:16 +00:00
const PDFMerger = require('pdf-merger-js');
const merger = new PDFMerger();
return new Promise(async (resolve) => {
for(const file of result) {
2023-07-27 15:06:56 +00:00
console.log('Adding', file)
2023-02-24 23:09:16 +00:00
await merger.add(file.value);
}
const merged = `${date}/${date}-songs-akkorde.pdf`
await merger.save(merged);
2023-07-27 15:06:56 +00:00
//const mergedPdfBuffer = await merger.saveAsBlob();
2023-02-24 23:09:16 +00:00
var data = new FormData();
2023-07-27 15:06:56 +00:00
data.append("files[]", fs.createReadStream(merged))
2023-02-24 23:09:16 +00:00
resolve(await churchtoolsClient.post(`/files/service/${event.id}`, data))
})
})
}
initChurchToolsClient();
login().then(() => {
churchtoolsClient.get('/whoami').then(whoAmI => {
console.log(`Hello ${whoAmI.firstName}!`);
});
return churchtoolsClient.get('/events')
2023-07-27 15:06:56 +00:00
}).then(events => {
// TODO select desired event id here or comment this block to see upcoming events
2023-07-27 14:02:13 +00:00
const event = events[1];
2023-07-27 15:06:56 +00:00
return mergeEventSongs(event);
2023-02-24 23:09:16 +00:00
}).then(result => {
console.log(result)
}).catch(error => {
// getTranslatedErrorMessage returns a human readable translated error message
// from either a full response object, response data or Exception or Error instances.
console.error(errorHelper.getTranslatedErrorMessage(error));
});