Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Create settings route and class #48

Merged
merged 12 commits into from
Dec 18, 2019
4 changes: 2 additions & 2 deletions classes/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion classes/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,4 @@ class HomepageType {
}
}


module.exports = { File, PageType, CollectionPageType, ResourcePageType, ResourceType, ImageType, DocumentType, DataType, HomepageType }
89 changes: 89 additions & 0 deletions classes/Settings.js
Original file line number Diff line number Diff line change
@@ -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 }
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions routes/settings.js
Original file line number Diff line number Diff line change
@@ -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)
kwajiehao marked this conversation as resolved.
Show resolved Hide resolved
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;
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down