Files
talk/routes/api/setup/index.js
T
Wyatt Johnson f488b0e02d Added BE for setup
- Adjusted docs
- Fixed issues with process terminating wrong
2017-02-03 11:05:46 -07:00

60 lines
1.3 KiB
JavaScript

const express = require('express');
const errors = require('../../../errors');
const SetupService = require('../../../services/setup');
const SettingsService = require('../../../services/settings');
const router = express.Router();
router.post('/', (req, res, next) => {
// Check if we have an install lock present.
if (process.env.TALK_INSTALL_LOCK === 'TRUE') {
return next(errors.ErrInstallLock);
}
// Get the current settings, we are expecing an error here.
SettingsService
.retrieve()
.then(() => {
// We should NOT have gotten a settings object, this means that the
// application is already setup. Error out here.
return next(errors.ErrSettingsInit);
})
.catch((err) => {
// If the error is `not init`, then we're good, otherwise, it's something
// else.
if (err !== errors.ErrSettingsNotInit) {
return next(err);
}
// Allow the request to keep going here.
return next();
});
}, (req, res, next) => {
const {
settings,
user: {email, password, displayName}
} = req.body;
SetupService
.setup({settings, user: {email, password, displayName}})
.then(() => {
// We're setup!
res.status(204).end();
})
.catch((err) => {
return next(err);
});
});
module.exports = router;