/**
* @module server/setServer
*/
const {registerTheme} = require('./themesManager')
const {registerDocument, getDocumentAsync} = require('./documentsManager/documentsManager')
const generatePresentationAsync = require('./generatePresentation')
/**
* @description The function sets the exporting endpoint, as well as registers the documents and themes
* @param {Object} app - express application instance
* @param {string} routePrefix - endpoint's route prefix path. e.g: 'agen' would be used for /agen/export
* @param {Array} documents - array of all the documents
* @param {Array} themes - array of all the themes
*/
module.exports = function setServer (app, routePrefix, documents, themes = []) {
// register documents and themes
documents.forEach(doc => registerDocument(doc.documentName, doc.documentCreator))
themes.forEach(theme => registerTheme(theme.themeId, theme.apply))
// expose endpoint for exporting
app.get(`/${routePrefix}/export/:documentName`, function (req, res) {
let {documentName} = req.params
if (!documentName) {
return res.send({
status: 'error',
statusCode: 400,
message: `Error: argument 'documentName' is missing. use path: /export/:documentName`
})
}
let format = req.query.format || 'pptx'
let supportedFormats = ['pptx']
if (!supportedFormats.includes(format)) {
return res.send({
status: 'error',
statusCode: 400,
message: `Error: format ${format} is not supported. supported formats: ${supportedFormats.join(', ')}`
})
}
let authorizationHeader = req.headers['authorization']
getDocumentAsync(authorizationHeader, documentName)
.then(documentMeta => generatePresentationAsync(documentMeta))
.then(fileBase64 => {
res.json({
message: 'SUCCESS',
data: {
contentType: `application/pptx`,
fileName: `report.pptx`,
content: fileBase64
}
})
})
.catch(err => {
console.error(err)
return res.send({
status: 'error',
statusCode: 400,
message: `Error: failed to create presentation document`,
appDebug: err
})
})
})
}