diff --git a/classes/Config.js b/classes/Config.js index 06fe96099..827c814d4 100644 --- a/classes/Config.js +++ b/classes/Config.js @@ -54,14 +54,14 @@ class Config { "sha": sha } - await axios.put(endpoint, params, { + await axios.put(endpoint, params, { headers: { Authorization: `token ${this.accessToken}`, "Content-Type": "application/json" } }) } catch (err) { - throw err + console.log(err) } } } diff --git a/classes/File.js b/classes/File.js index 07a9d8eb3..e5abd7703 100644 --- a/classes/File.js +++ b/classes/File.js @@ -228,5 +228,4 @@ class HomepageType { } } - module.exports = { File, PageType, CollectionPageType, ResourcePageType, ResourceType, ImageType, DocumentType, DataType, HomepageType } diff --git a/classes/Settings.js b/classes/Settings.js new file mode 100644 index 000000000..8630f59b9 --- /dev/null +++ b/classes/Settings.js @@ -0,0 +1,89 @@ +const { Base64 } = require('js-base64') +const yaml = require('js-yaml') + +// import classes +const { Config } = require('../classes/Config.js') +const { File, DataType } = require('../classes/File.js') + +class Settings { + constructor(accessToken, siteName) { + this.accessToken = accessToken + this.siteName = siteName + } + + async get() { + try { + // retrieve _config.yml and social-media.yml + const configResp = new Config(this.accessToken, this.siteName) + const IsomerDataFile = new File(this.accessToken, this.siteName) + const dataType = new DataType() + IsomerDataFile.setFileType(dataType) + + const { content: config } = await configResp.read() + const socialMediaResp = IsomerDataFile.read('social-media.yml').catch((err) => { + // social-media.yml doesn't exist so we create a social-media.yml + const content = { + facebook: '', + linkedin: '', + twitter: '', + youtube: '', + instagram: '', + } + const socialMediaYml = Base64.encode(yaml.safeDump(content)) + const { sha } = IsomerDataFile.create('social-media.yml', socialMediaYml) + return { content, sha } + }) + const { content: socialMedia, sha: socialMediaSha } = await socialMediaResp + + // convert data to object form + const configContent = yaml.safeLoad(Base64.decode(config)); + const socialMediaContent = yaml.safeLoad(Base64.decode(socialMedia)); + + // retrieve only the relevant config fields + const configFieldsRequired = { + title: configContent.title, + favicon: configContent.favicon, + resources_name: configContent.resources_name, + colors: configContent.colors, + + } + + return ({ configFieldsRequired, socialMediaContent, socialMediaSha }) + } catch (err) { + console.log(err) + } + } + + async post(payload) { + try { + // setup + const configResp = new Config(this.accessToken, this.siteName) + const config = await configResp.read() + const IsomerDataFile = new File(this.accessToken, this.siteName) + const dataType = new DataType() + IsomerDataFile.setFileType(dataType) + + // extract data + const { + socialMediaSettings, + configSettings, + socialMediaSha, + } = payload + + // update config object + const configContent = yaml.safeLoad(Base64.decode(config.content)); + Object.keys(configSettings).forEach((setting) => (configContent[setting] = configSettings[setting])); + + // update files + const newConfigContent = Base64.encode(yaml.safeDump(configContent)) + const newSocialMediaContent = Base64.encode(yaml.safeDump(socialMediaSettings)) + await configResp.update(newConfigContent, config.sha) + await IsomerDataFile.update('social-media.yml', newSocialMediaContent, socialMediaSha) + return + } catch (err) { + console.log(err) + } + } +} + +module.exports = { Settings } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 07cdd47fd..8bd409b4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -364,6 +364,11 @@ "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz", "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==" }, + "js-base64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-2.5.1.tgz", + "integrity": "sha512-M7kLczedRMYX4L8Mdh4MzyAMM9O5osx+4FcOQuTvr3A9F2D9S5JXheN0ewNbrvK2UatkTRhL5ejGmGSjNMiZuw==" + }, "js-yaml": { "version": "3.13.1", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", diff --git a/package.json b/package.json index aec7c6ca2..364f35380 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "dotenv": "^8.1.0", "express": "~4.16.1", "http-errors": "~1.6.3", + "js-base64": "^2.5.1", "js-yaml": "^3.13.1", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.15", diff --git a/routes/settings.js b/routes/settings.js new file mode 100644 index 000000000..5af2d61c3 --- /dev/null +++ b/routes/settings.js @@ -0,0 +1,38 @@ +const express = require('express'); +const router = express.Router(); +const _ = require('lodash') +const jwtUtils = require('../utils/jwt-utils') + +// Import Classes +const { Settings } = require('../classes/Settings.js') + +router.get('/:siteName/settings', async function(req, res, next) { + try { + const { oauthtoken } = req.cookies + let { access_token } = jwtUtils.verifyToken(oauthtoken) + const { siteName } = req.params + + const settingsFile = new Settings(access_token, siteName) + const settings = await settingsFile.get() + res.status(200).json({ settings }) + } catch (err) { + console.log(err) + } +}) + +router.post('/:siteName/settings', async function(req, res, next) { + try { + const { oauthtoken } = req.cookies + let { access_token } = jwtUtils.verifyToken(oauthtoken) + const { siteName } = req.params + + const settings = new Settings(access_token, siteName) + await settings.post(req.body) + res.status(200).send('OK') + } catch (err) { + console.log(err) + } +}) + + +module.exports = router; diff --git a/server.js b/server.js index 5a0d7a297..42c60e3f7 100644 --- a/server.js +++ b/server.js @@ -23,6 +23,7 @@ const documentsRouter = require('./routes/documents') const menuRouter = require('./routes/menus') const homepageRouter = require('./routes/homepage') const menuDirectoryRouter = require('./routes/menuDirectory') +const settingsRouter = require('./routes/settings') const app = express(); @@ -52,6 +53,7 @@ app.use('/sites', documentsRouter) app.use('/sites', menuRouter) app.use('/sites', homepageRouter) app.use('/sites', menuDirectoryRouter) +app.use('/sites', settingsRouter) // catch 404 and forward to error handler app.use(function(req, res, next) {