Skip to content

Commit

Permalink
Merge pull request #86 from Amsterdam/feature/auto-update-siteconfig
Browse files Browse the repository at this point in the history
Refresh site config in CMS after update in API
  • Loading branch information
ToshKoevoets committed Oct 21, 2020
2 parents 74e2d71 + 3a2228d commit 3f9ed3b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
* If extraData.images is set then the new value ovreplaces the old value instead of being merged (it waas impossible to delete images)
* Add min/max number of to budgeting voting
* Make sure postcode validation doesn't fail on an empty string
* Add Delete route for vote

## 0.7.6 (2020-10-07)
* Votes were viewable when they should not be
Expand Down
4 changes: 1 addition & 3 deletions src/middleware/oauth-clients.js
Expand Up @@ -62,13 +62,11 @@ exports.withAllForSite = (req, res, next) => {
return userClientApi
.fetch(authServerUrl,apiCredentials, oauthClientId)
.then((client) => {
console.log('==>> err', client);

req.siteOAuthClients.push(client);
resolve();
})
.catch((err) => {
console.log('==>> err', oauthClientId, err.message);
console.log('==>> err oauthClientId', oauthClientId, err.message);
resolve();
});
})
Expand Down
4 changes: 2 additions & 2 deletions src/models/Vote.js
Expand Up @@ -109,11 +109,11 @@ module.exports = function( db, sequelize, DataTypes ) {
viewableBy: 'all',
createableBy: 'member',
updateableBy: ['editor', 'owner'],
deleteableBy: 'admin',
deleteableBy: ['editor', 'owner'],
canToggle: function(user, self) {
return userHasRole(user, 'editor', self.userId);
}
}

return Vote;
};
25 changes: 21 additions & 4 deletions src/routes/api/site.js
Expand Up @@ -99,6 +99,7 @@ router.route('/:siteIdOrDomain') //(\\d+)
.put(function(req, res, next) {
const site = req.results;
if (!( site && site.can && site.can('update') )) return next( new Error('You cannot update this site') );

req.results
.authorizeData(req.body, 'update')
.update(req.body)
Expand All @@ -113,6 +114,7 @@ router.route('/:siteIdOrDomain') //(\\d+)
next();
});
})

// update certain parts of config to the oauth client
// mainly styling settings are synched so in line with the CMS
.put(function (req, res, next) {
Expand Down Expand Up @@ -143,20 +145,35 @@ router.route('/:siteIdOrDomain') //(\\d+)
body: JSON.stringify(Object.assign(apiCredentials, oauthClient))
}


updates.push(fetch(authUpdateUrl, options));
});

Promise.all(updates)
.then(() => {
// when succesfull return site JSON
res.json(req.site);
next()
})
.catch((e) => {
console.log('errr', e);
console.log('errr oauth', e);
next(e)
});
})
// call the site, to let the site know a refresh of the siteConfig is needed
.put(function (req, res, next) {
const site = req.results;
const cmsUrl = site.config.cms.url;

if (!cmsUrl) {
next();
}

return fetch(cmsUrl + '/modules/openstad-api/refresh')
.then(function () { next(); })
.catch(function (err) { console.log('errrr', err); next(); });
})
.put(function (req, res, next) {
// when succesfull return site JSON
res.json(req.results);
})
// delete site
// ---------
.delete(auth.can('Site', 'delete'))
Expand Down
29 changes: 29 additions & 0 deletions src/routes/api/vote.js
Expand Up @@ -415,6 +415,35 @@ router.route('/*')
.catch(next)
})

router.route('/:voteId(\\d+)')
.all(( req, res, next ) => {
var voteId = req.params.voteId;

db.Vote
.findOne({
where: { id: voteId }
})
.then(function( vote ) {
if( vote ) {
req.results = vote;
}
next();
})
.catch(next);
})
.delete(auth.useReqUser)
.delete(function(req, res, next) {
const vote = req.results;
if (!( vote && vote.can && vote.can('delete') )) return next( new Error('You cannot delete this vote') );

vote
.destroy()
.then(() => {
res.json({ "vote": "deleted" });
})
.catch(next);
})

router.route('/:voteId(\\d+)/toggle')
.all(( req, res, next ) => {
var voteId = req.params.voteId;
Expand Down

0 comments on commit 3f9ed3b

Please sign in to comment.