Added new admin API for members (#10435)

no issue

- Added read and browse admin API for members
This commit is contained in:
Rishabh Garg 2019-01-30 17:06:09 +05:30 committed by GitHub
parent 5bd509c873
commit 8fd4b3f09f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 105 additions and 7 deletions

View File

@ -63,6 +63,10 @@ module.exports = {
return shared.pipeline(require('./subscribers'), localUtils);
},
get members() {
return shared.pipeline(require('./members'), localUtils);
},
get upload() {
return shared.pipeline(require('./upload'), localUtils);
},

View File

@ -0,0 +1,35 @@
const memberUserObject = require('../../services/members').api.memberUserObject;
const members = {
docName: 'members',
browse: {
options: [
'limit',
'fields',
'filter',
'order',
'debug',
'page'
],
permissions: true,
validation: {},
query(frame) {
return memberUserObject.list(frame.options);
}
},
read: {
headers: {},
data: [
'id',
'email'
],
validation: {},
permissions: true,
query(frame) {
return memberUserObject.get(frame.data, frame.options);
}
}
};
module.exports = members;

View File

@ -55,6 +55,10 @@ module.exports = {
return require('./subscribers');
},
get members() {
return require('./members');
},
get upload() {
return require('./upload');
},

View File

@ -0,0 +1,24 @@
const common = require('../../../../../lib/common');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:members');
module.exports = {
browse(data, apiConfig, frame) {
debug('browse');
frame.response = data;
},
read(data, apiConfig, frame) {
debug('read');
if (!data) {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.members.memberNotFound')
}));
}
frame.response = {
members: [data]
};
}
};

View File

@ -20,6 +20,7 @@ module.exports = function MembersApi({
validateMember,
updateMember,
getMember,
listMembers,
sendEmail
}) {
const {encodeToken, decodeToken, getPublicKeys} = Tokens({privateKey, publicKey, issuer});
@ -31,6 +32,7 @@ module.exports = function MembersApi({
validateMember,
sendEmail,
encodeToken,
listMembers,
decodeToken
});

View File

@ -2,13 +2,14 @@ module.exports = function ({
createMember,
updateMember,
getMember,
listMembers,
validateMember,
sendEmail,
encodeToken,
decodeToken
}) {
function requestPasswordReset({email}) {
return getMember({email}).then((member) => {
return getMember({email}, {require: true}).then((member) => {
return encodeToken({
sub: member.id
}).then((token) => {
@ -30,6 +31,7 @@ module.exports = function ({
resetPassword,
create: createMember,
validate: validateMember,
get: getMember
get: getMember,
list: listMembers
};
};

View File

@ -25,11 +25,22 @@ function updateMember(member, newData) {
});
}
function getMember(member) {
return models.Member.findOne(member, {
require: true
}).then((member) => {
return member.toJSON();
function getMember(data, options) {
options = options || {};
return models.Member.findOne(data, options).then((model) => {
if (!model) {
return null;
}
return model.toJSON(options);
});
}
function listMembers(options) {
return models.Member.findPage(options).then((models) => {
return {
members: models.data.map(model => model.toJSON(options)),
meta: models.meta
};
});
}
@ -104,6 +115,7 @@ const api = MembersApi({
validateAudience,
createMember,
getMember,
listMembers,
validateMember,
updateMember,
sendEmail

View File

@ -403,6 +403,10 @@
"subscriberNotFound": "Subscriber not found.",
"subscriberAlreadyExists": "Email address is already subscribed."
},
"members": {
"memberNotFound": "Member not found.",
"memberAlreadyExists": "Email address is already member."
},
"tags": {
"tagNotFound": "Tag not found."
},

View File

@ -94,6 +94,10 @@ module.exports = function apiRoutes() {
router.del('/subscribers/:id', shared.middlewares.labs.subscribers, mw.authAdminApi, apiv2.http(apiv2.subscribers.destroy));
router.del('/subscribers/email/:email', shared.middlewares.labs.subscribers, mw.authAdminApi, apiv2.http(apiv2.subscribers.destroy));
// ## Members
router.get('/members', shared.middlewares.labs.members, mw.authAdminApi, apiv2.http(apiv2.members.browse));
router.get('/members/:id', shared.middlewares.labs.members, mw.authAdminApi, apiv2.http(apiv2.members.read));
// ## Roles
router.get('/roles/', mw.authAdminApi, apiv2.http(apiv2.roles.browse));

View File

@ -8,6 +8,13 @@ const labs = {
} else {
return next(new common.errors.NotFoundError());
}
},
members(req, res, next) {
if (labsUtil.isSet('members') === true) {
return next();
} else {
return next(new common.errors.NotFoundError());
}
}
};