Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.create = function (req, res) {
- dbConnection.getSystemDatabaseConnection(req).then(function(db) {
- if (db) {
- var Site = db.model('Site');
- var SystemPackage = db.model('SystemPackage');
- var site = new Site({
- code: req.body.code,
- name: req.body.name,
- });
- var systemPackageName = 'Basic';
- if (req.body.systemPackage) {
- systemPackageName = req.body.systemPackage;
- }
- SystemPackage.findOne({ name: systemPackageName }).select('_id').exec(function (err, systemPackageId) {
- if (err) {
- return res.status(400).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- console.log(chalk.bold.red('Find system package to assign to site: ' + systemPackageName));
- site.systemPackage = systemPackageId._id;
- site.user = req.user;
- site.save(function (err) {
- if (err) {
- return res.status(400).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- console.log(chalk.bold.red('New site created successfully'));
- // Ok, so we have saved the site, but now we need to create the default user in the 6PM user service
- // and then attach this user to the administrator role.
- // We also need to save the auto-generated siteId property in the users product_specific_attributes property
- // otherwise, we will not be able to create any users or see any data for the site.
- var adminAccount = {
- username: req.body.admin_email,
- password: req.body.admin_password,
- siteId: site.siteId
- };
- console.log(chalk.bold.red('Adding default user to site'));
- seedTheUser(adminAccount, req, res)
- .then(function () {
- console.log(chalk.bold.red('Default user added to new site'));
- req.user.site = site;
- userProfile.create(req,res).then(function (success) {
- }, function (error) {
- });
- res.json(site);
- })
- .catch(function(err){
- console.log(chalk.bold.red(err));
- res.status(400).send(err);
- });
- }
- });
- }
- });
- } else {
- return res.status(400).send({
- message: 'Database for site could not be found'
- });
- }
- }, function (errorResponse) {
- return res.status(400).send({
- message: errorHandler.getErrorMessage(errorResponse)
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement