From 3060e11a4ea1123b23c75d97457c2fb79ac95d0c Mon Sep 17 00:00:00 2001 From: Naz Gargol Date: Thu, 5 Dec 2019 18:16:18 +0700 Subject: [PATCH] Changed members-api constructor to accept Member model directly (#105) no issue - As members have become a part of Ghost core there is no need to proxy methods like this anymore and we can allow members-api to work on the model directly - Methods come from Ghost core: https://github.com/TryGhost/Ghost/blob/cc39786/core/server/services/members/api.js#L11-L110 --- ghost/members-api/index.js | 12 ++----- ghost/members-api/lib/users.js | 63 +++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index b9afa0d386..c7c6759922 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -27,11 +27,7 @@ module.exports = function MembersApi({ }, setMetadata, getMetadata, - createMember, - getMember, - updateMember, - deleteMember, - listMembers, + memberModel, logger }) { if (logger) { @@ -89,11 +85,7 @@ module.exports = function MembersApi({ const users = Users({ sendEmailWithMagicLink, stripe, - createMember, - getMember, - updateMember, - deleteMember, - listMembers + memberModel }); async function getMemberDataFromMagicLinkToken(token){ diff --git a/ghost/members-api/lib/users.js b/ghost/members-api/lib/users.js index 66917d3cf2..f5856b5229 100644 --- a/ghost/members-api/lib/users.js +++ b/ghost/members-api/lib/users.js @@ -1,13 +1,66 @@ const debug = require('ghost-ignition').debug('users'); + +let Member; + +async function createMember({email, name, note}, options = {}) { + const model = await Member.add({ + email, + name: name || null, + note: note || null + }); + const member = model.toJSON(options); + return member; +} + +async function getMember(data, options = {}) { + if (!data.email && !data.id && !data.uuid) { + return Promise.resolve(null); + } + const model = await Member.findOne(data, options); + if (!model) { + return null; + } + const member = model.toJSON(options); + return member; +} + +async function updateMember({name, note, subscribed}, options = {}) { + const attrs = { + name: name || null, + note: note || null + }; + + if (subscribed !== undefined) { + attrs.subscribed = subscribed; + } + + const model = await Member.edit(attrs, options); + + const member = model.toJSON(options); + return member; +} + +function deleteMember(options) { + options = options || {}; + return Member.destroy(options); +} + +function listMembers(options) { + return Member.findPage(options).then((models) => { + return { + members: models.data.map(model => model.toJSON(options)), + meta: models.meta + }; + }); +} + module.exports = function ({ sendEmailWithMagicLink, stripe, - createMember, - getMember, - updateMember, - listMembers, - deleteMember + memberModel }) { + Member = memberModel; + async function get(data, options) { debug(`get id:${data.id} email:${data.email}`); const member = await getMember(data, options);