fix: Newly created private collections do not return correct policies (#1188)

closes #1185
This commit is contained in:
Tom Moor
2020-02-24 23:16:24 -08:00
committed by GitHub
parent 3e7acc377e
commit 0b86714984
2 changed files with 28 additions and 4 deletions

View File

@@ -22,15 +22,16 @@ const router = new Router();
router.post('collections.create', auth(), async ctx => {
const { name, color, description, type } = ctx.body;
const isPrivate = ctx.body.private;
ctx.assertPresent(name, 'name is required');
if (color)
if (color) {
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
}
const user = ctx.state.user;
authorize(user, 'create', Collection);
const collection = await Collection.create({
let collection = await Collection.create({
name,
description,
color,
@@ -49,6 +50,13 @@ router.post('collections.create', auth(), async ctx => {
ip: ctx.request.ip,
});
// we must reload the collection to get memberships for policy presenter
if (isPrivate) {
collection = await Collection.scope({
method: ['withMembership', user.id],
}).findByPk(collection.id);
}
ctx.body = {
data: presentCollection(collection),
policies: presentPolicies(user, [collection]),