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

Fix #269, adminSettings.json dependent on compilation order #280

Open
wants to merge 1 commit into
base: preview
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 48 additions & 26 deletions server/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const models = require('../models');
const sockets = require('./sockets');
const slack = require('./slack');
const home = require('./home');
const path = require('path');

// Global admin settings
// FIXME: some default values are set to simplify testing;
Expand All @@ -23,48 +24,69 @@ const defaultAdminSettings = {
dayDictionary: {},
};

const adminSettingsPath = path.join(
__dirname,
'..',
'..',
'adminSettings.json'
);

let adminSettings = defaultAdminSettings;

// whenever starting server, check if adminSettings.json is up to date
updateAdminSettingsJSON();

function haveSameKeys(obj1, obj2) {
const obj1Keys = Object.keys(obj1).sort();
const obj2Keys = Object.keys(obj2).sort();
return JSON.stringify(obj1Keys) === JSON.stringify(obj2Keys);
}

// If no admin setting have been generated, use the above default values
if (!fs.existsSync('../adminSettings.json')) {
var json = JSON.stringify(adminSettings);
fs.writeFile('../adminSettings.json', json, 'utf8', function () {
console.log('Created admin settings JSON');
});
function _createAdminSettingsJSON() {
if (!fs.existsSync(adminSettingsPath)) {
var json = JSON.stringify(adminSettings);
fs.writeFileSync(adminSettingsPath, json, 'utf8');
}
}

// If admin settings have been generated, but the keys don't match, update the missing keys
else if (
fs.existsSync('../adminSettings.json') &&
!haveSameKeys(
adminSettings,
JSON.parse(fs.readFileSync('../adminSettings.json', 'utf8'))
)
) {
let currAdminSettings = fs.readFileSync(
'../adminSettings.json',
'utf8',
(flag = 'r+')
);
let newAdminSettings = JSON.parse(currAdminSettings);
for (let key in adminSettings) {
if (!newAdminSettings.hasOwnProperty(key)) {
newAdminSettings[key] = adminSettings[key];
function updateAdminSettingsJSON() {
if (!fs.existsSync(adminSettingsPath)) {
_createAdminSettingsJSON();
}

if (
!haveSameKeys(
adminSettings,
JSON.parse(fs.readFileSync(adminSettingsPath, 'utf8'))
)
) {
let currAdminSettings = fs.readFileSync(
adminSettingsPath,
'utf8',
(flag = 'r+')
);
let newAdminSettings = JSON.parse(currAdminSettings);
for (let key in adminSettings) {
if (!newAdminSettings.hasOwnProperty(key)) {
newAdminSettings[key] = adminSettings[key];
}
}
var json = JSON.stringify(newAdminSettings);
fs.writeFileSync(adminSettingsPath, json, 'utf8', function () {
console.log('Updated admin settings JSON');
});
}
var json = JSON.stringify(newAdminSettings);
fs.writeFileSync('../adminSettings.json', json, 'utf8', function () {
console.log('Updated admin settings JSON');
});
}

exports.get_admin_settings = function () {
let data = fs.readFileSync('../adminSettings.json', 'utf8', (flag = 'r+'));
// if file doesn't exist, create it
if (!fs.existsSync(adminSettingsPath)) {
updateAdminSettingsJSON();
}

let data = fs.readFileSync(adminSettingsPath, 'utf8', (flag = 'r+'));
if (data) {
adminSettings = JSON.parse(data);
} else {
Expand Down