Added email relation to post and allowed include for /posts endpoint

This commit is contained in:
Nazar Gargol 2019-11-06 16:30:11 +07:00 committed by Naz Gargol
parent 97e2af9a06
commit 4e1caa8b08
4 changed files with 12 additions and 4 deletions

View File

@ -1,7 +1,7 @@
const models = require('../../models');
const common = require('../../lib/common');
const urlUtils = require('../../lib/url-utils');
const allowedIncludes = ['tags', 'authors', 'authors.roles'];
const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email'];
const unsafeAttrs = ['status', 'authors', 'visibility'];
module.exports = {

View File

@ -33,6 +33,10 @@ const Email = ghostBookshelf.Model.extend({
model.emitChange('deleted', options);
}
}, {
post() {
return this.belongsTo('Post');
}
});
const Emails = ghostBookshelf.Collection.extend({

View File

@ -576,6 +576,10 @@ Post = ghostBookshelf.Model.extend({
return this.hasOne('PostsMeta', 'post_id');
},
email: function postsMeta() {
return this.hasOne('Email', 'post_id');
},
/**
* @NOTE:
* If you are requesting models with `columns`, you try to only receive some fields of the model/s.

View File

@ -20,7 +20,7 @@ describe('Posts API', function () {
request = supertest.agent(config.get('url'));
})
.then(function () {
return localUtils.doAuth(request, 'users:extra', 'posts');
return localUtils.doAuth(request, 'users:extra', 'posts', 'emails');
})
.then(function (cookie) {
ownerCookie = cookie;
@ -60,7 +60,7 @@ describe('Posts API', function () {
});
it('combined fields, formats, include and non existing', function (done) {
request.get(localUtils.API.getApiQuery('posts/?formats=mobiledoc,html,plaintext&fields=id,title,primary_tag,doesnotexist&include=authors,tags'))
request.get(localUtils.API.getApiQuery('posts/?formats=mobiledoc,html,plaintext&fields=id,title,primary_tag,doesnotexist&include=authors,tags,email'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
@ -81,7 +81,7 @@ describe('Posts API', function () {
'post',
null,
null,
['mobiledoc', 'plaintext', 'id', 'title', 'html', 'authors', 'tags', 'primary_tag']
['mobiledoc', 'plaintext', 'id', 'title', 'html', 'authors', 'tags', 'primary_tag', 'email']
);
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');