2019-08-09 17:11:24 +03:00
|
|
|
// NOTE: We must not cache references to membersService.api
|
|
|
|
// as it is a getter and may change during runtime.
|
2019-10-03 20:59:19 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-05-26 12:38:42 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const errors = require('@tryghost/errors');
|
2020-12-09 19:15:53 +03:00
|
|
|
const GhostMailer = require('../../services/mail').GhostMailer;
|
2019-12-06 08:04:10 +03:00
|
|
|
const models = require('../../models');
|
2019-08-09 17:11:24 +03:00
|
|
|
const membersService = require('../../services/members');
|
2020-12-09 19:15:53 +03:00
|
|
|
const jobsService = require('../../services/jobs');
|
2020-05-26 12:38:42 +03:00
|
|
|
const settingsCache = require('../../services/settings/cache');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-12-09 19:15:53 +03:00
|
|
|
const ghostMailer = new GhostMailer();
|
2020-12-10 13:04:05 +03:00
|
|
|
const allowedIncludes = ['email_recipients'];
|
2020-02-04 08:51:24 +03:00
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
module.exports = {
|
2019-08-09 17:11:24 +03:00
|
|
|
docName: 'members',
|
2020-06-18 19:07:02 +03:00
|
|
|
|
|
|
|
hasActiveStripeSubscriptions: {
|
|
|
|
permissions: {
|
|
|
|
method: 'browse'
|
|
|
|
},
|
|
|
|
async query() {
|
|
|
|
const hasActiveStripeSubscriptions = await membersService.api.hasActiveStripeSubscriptions();
|
|
|
|
return {
|
|
|
|
hasActiveStripeSubscriptions
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'limit',
|
|
|
|
'fields',
|
|
|
|
'filter',
|
|
|
|
'order',
|
|
|
|
'debug',
|
2020-05-28 12:14:02 +03:00
|
|
|
'page',
|
2020-06-12 14:12:10 +03:00
|
|
|
'search',
|
|
|
|
'paid'
|
2019-08-09 17:11:24 +03:00
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
validation: {},
|
2020-01-15 13:52:47 +03:00
|
|
|
async query(frame) {
|
2020-08-12 16:17:44 +03:00
|
|
|
frame.options.withRelated = ['labels', 'stripeSubscriptions', 'stripeSubscriptions.customer'];
|
|
|
|
const page = await membersService.api.members.list(frame.options);
|
|
|
|
|
2020-09-30 12:22:22 +03:00
|
|
|
return page;
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
2020-12-10 13:04:05 +03:00
|
|
|
options: [
|
|
|
|
'include'
|
|
|
|
],
|
2019-08-09 17:11:24 +03:00
|
|
|
headers: {},
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'email'
|
|
|
|
],
|
2020-12-10 13:04:05 +03:00
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
permissions: true,
|
2019-09-03 07:10:32 +03:00
|
|
|
async query(frame) {
|
2020-12-10 13:04:05 +03:00
|
|
|
const defaultWithRelated = ['labels', 'stripeSubscriptions', 'stripeSubscriptions.customer'];
|
|
|
|
|
|
|
|
if (!frame.options.withRelated) {
|
|
|
|
frame.options.withRelated = defaultWithRelated;
|
|
|
|
} else {
|
|
|
|
frame.options.withRelated = frame.options.withRelated.concat(defaultWithRelated);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.options.withRelated.includes('email_recipients')) {
|
|
|
|
frame.options.withRelated.push('email_recipients.email');
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
let model = await membersService.api.members.get(frame.data, frame.options);
|
2020-01-15 13:52:47 +03:00
|
|
|
|
2020-01-28 07:25:00 +03:00
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.members.memberNotFound')
|
2019-09-03 07:10:32 +03:00
|
|
|
});
|
|
|
|
}
|
2020-01-15 13:52:47 +03:00
|
|
|
|
2020-09-30 12:22:22 +03:00
|
|
|
return model;
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-03 12:15:50 +03:00
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'send_email',
|
|
|
|
'email_type'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
data: {
|
|
|
|
email: {required: true}
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
email_type: {
|
|
|
|
values: ['signin', 'signup', 'subscribe']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2019-10-09 10:14:26 +03:00
|
|
|
async query(frame) {
|
2020-08-12 16:17:44 +03:00
|
|
|
let member;
|
|
|
|
frame.options.withRelated = ['stripeSubscriptions', 'stripeSubscriptions.customer'];
|
2019-10-09 10:14:26 +03:00
|
|
|
try {
|
2020-10-14 03:24:09 +03:00
|
|
|
if (!membersService.config.isStripeConnected()
|
|
|
|
&& (frame.data.members[0].stripe_customer_id || frame.data.members[0].comped)) {
|
|
|
|
const property = frame.data.members[0].comped ? 'comped' : 'stripe_customer_id';
|
|
|
|
|
|
|
|
throw new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.api.members.stripeNotConnected.message'),
|
|
|
|
context: i18n.t('errors.api.members.stripeNotConnected.context'),
|
|
|
|
help: i18n.t('errors.api.members.stripeNotConnected.help'),
|
|
|
|
property
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
member = await membersService.api.members.create(frame.data.members[0], frame.options);
|
2020-02-04 08:51:24 +03:00
|
|
|
|
|
|
|
if (frame.data.members[0].stripe_customer_id) {
|
|
|
|
await membersService.api.members.linkStripeCustomer(frame.data.members[0].stripe_customer_id, member);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.data.members[0].comped) {
|
|
|
|
await membersService.api.members.setComplimentarySubscription(member);
|
|
|
|
}
|
2020-01-15 13:52:47 +03:00
|
|
|
|
|
|
|
if (frame.options.send_email) {
|
2020-08-12 16:17:44 +03:00
|
|
|
await membersService.api.sendEmailWithMagicLink({email: member.get('email'), requestedType: frame.options.email_type});
|
2020-01-15 13:52:47 +03:00
|
|
|
}
|
|
|
|
|
2020-09-30 12:22:22 +03:00
|
|
|
return member;
|
2019-10-09 10:14:26 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
|
2020-06-29 15:22:52 +03:00
|
|
|
throw new errors.ValidationError({
|
2020-08-20 08:41:47 +03:00
|
|
|
message: i18n.t('errors.models.member.memberAlreadyExists.message'),
|
2020-09-10 07:03:57 +03:00
|
|
|
context: i18n.t('errors.models.member.memberAlreadyExists.context', {
|
|
|
|
action: 'add'
|
|
|
|
})
|
2020-06-29 15:22:52 +03:00
|
|
|
});
|
2019-10-09 10:14:26 +03:00
|
|
|
}
|
|
|
|
|
2020-07-01 10:03:12 +03:00
|
|
|
// NOTE: failed to link Stripe customer/plan/subscription or have thrown custom Stripe connection error.
|
|
|
|
// It's a bit ugly doing regex matching to detect errors, but it's the easiest way that works without
|
|
|
|
// introducing additional logic/data format into current error handling
|
2020-10-14 03:24:09 +03:00
|
|
|
const isStripeLinkingError = error.message && (error.message.match(/customer|plan|subscription/g));
|
2020-08-12 16:17:44 +03:00
|
|
|
if (member && isStripeLinkingError) {
|
2020-06-05 15:06:19 +03:00
|
|
|
if (error.message.indexOf('customer') && error.code === 'resource_missing') {
|
2020-06-12 07:33:45 +03:00
|
|
|
error.message = `Member not imported. ${error.message}`;
|
2020-06-05 15:06:19 +03:00
|
|
|
error.context = i18n.t('errors.api.members.stripeCustomerNotFound.context');
|
|
|
|
error.help = i18n.t('errors.api.members.stripeCustomerNotFound.help');
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
await membersService.api.members.destroy({
|
|
|
|
id: member.get('id')
|
|
|
|
}, frame.options);
|
2020-02-04 08:51:24 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 10:14:26 +03:00
|
|
|
throw error;
|
|
|
|
}
|
2019-10-03 12:15:50 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-03 14:38:22 +03:00
|
|
|
edit: {
|
|
|
|
statusCode: 200,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
async query(frame) {
|
2020-09-10 07:03:57 +03:00
|
|
|
try {
|
|
|
|
frame.options.withRelated = ['stripeSubscriptions'];
|
|
|
|
const member = await membersService.api.members.update(frame.data.members[0], frame.options);
|
2020-01-28 07:25:00 +03:00
|
|
|
|
2021-01-04 20:12:57 +03:00
|
|
|
const hasCompedSubscription = !!member.related('stripeSubscriptions').find(sub => sub.get('plan_nickname') === 'Complimentary' && sub.get('status') === 'active');
|
2020-01-28 07:25:00 +03:00
|
|
|
|
2020-09-10 07:03:57 +03:00
|
|
|
if (typeof frame.data.members[0].comped === 'boolean') {
|
|
|
|
if (frame.data.members[0].comped && !hasCompedSubscription) {
|
|
|
|
await membersService.api.members.setComplimentarySubscription(member);
|
|
|
|
} else if (!(frame.data.members[0].comped) && hasCompedSubscription) {
|
|
|
|
await membersService.api.members.cancelComplimentarySubscription(member);
|
|
|
|
}
|
|
|
|
|
|
|
|
await member.load(['stripeSubscriptions']);
|
2020-01-28 07:25:00 +03:00
|
|
|
}
|
2020-08-12 16:17:44 +03:00
|
|
|
|
2020-09-10 07:03:57 +03:00
|
|
|
await member.load(['stripeSubscriptions.customer']);
|
2020-01-15 13:52:47 +03:00
|
|
|
|
2020-09-30 12:22:22 +03:00
|
|
|
return member;
|
2020-09-10 07:03:57 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
|
|
|
|
throw new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.models.member.memberAlreadyExists.message'),
|
|
|
|
context: i18n.t('errors.models.member.memberAlreadyExists.context', {
|
|
|
|
action: 'edit'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2020-08-12 16:17:44 +03:00
|
|
|
|
2020-09-10 07:03:57 +03:00
|
|
|
throw error;
|
|
|
|
}
|
2019-10-03 14:38:22 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-20 14:58:11 +03:00
|
|
|
editSubscription: {
|
|
|
|
statusCode: 200,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id',
|
|
|
|
'subscription_id'
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'cancel_at_period_end'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
subscription_id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'edit'
|
|
|
|
},
|
|
|
|
async query(frame) {
|
|
|
|
await membersService.api.members.updateSubscription(frame.options.id, {
|
|
|
|
subscriptionId: frame.options.subscription_id,
|
|
|
|
cancelAtPeriodEnd: frame.data.cancel_at_period_end
|
|
|
|
});
|
|
|
|
let model = await membersService.api.members.get({id: frame.options.id}, {
|
|
|
|
withRelated: ['labels', 'stripeSubscriptions', 'stripeSubscriptions.customer']
|
|
|
|
});
|
|
|
|
if (!model) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.members.memberNotFound')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-30 12:22:22 +03:00
|
|
|
return model;
|
2020-08-20 14:58:11 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
2020-07-24 13:39:08 +03:00
|
|
|
'id',
|
|
|
|
'cancel'
|
2019-08-09 17:11:24 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2019-10-02 11:25:49 +03:00
|
|
|
async query(frame) {
|
2019-08-09 17:11:24 +03:00
|
|
|
frame.options.require = true;
|
2020-08-12 16:17:44 +03:00
|
|
|
frame.options.cancelStripeSubscriptions = frame.options.cancel;
|
2020-01-15 13:52:47 +03:00
|
|
|
|
2020-08-21 12:44:37 +03:00
|
|
|
await Promise.resolve(membersService.api.members.destroy({
|
|
|
|
id: frame.options.id
|
|
|
|
}, frame.options)).catch(models.Member.NotFoundError, () => {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
|
|
|
resource: 'Member'
|
|
|
|
})
|
2019-12-06 08:04:10 +03:00
|
|
|
});
|
2020-08-21 12:44:37 +03:00
|
|
|
});
|
2019-12-06 08:04:10 +03:00
|
|
|
|
2019-10-02 11:25:49 +03:00
|
|
|
return null;
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
2019-10-03 20:59:19 +03:00
|
|
|
},
|
|
|
|
|
2019-10-03 21:36:22 +03:00
|
|
|
exportCSV: {
|
2019-10-29 07:50:32 +03:00
|
|
|
options: [
|
2020-09-23 13:46:08 +03:00
|
|
|
'limit',
|
|
|
|
'filter',
|
|
|
|
'search',
|
|
|
|
'paid'
|
2019-10-29 07:50:32 +03:00
|
|
|
],
|
2019-10-03 21:36:22 +03:00
|
|
|
headers: {
|
|
|
|
disposition: {
|
|
|
|
type: 'csv',
|
|
|
|
value() {
|
|
|
|
const datetime = (new Date()).toJSON().substring(0, 10);
|
|
|
|
return `members.${datetime}.csv`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
format: 'plain'
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'browse'
|
|
|
|
},
|
|
|
|
validation: {},
|
2020-01-15 13:52:47 +03:00
|
|
|
async query(frame) {
|
2020-08-12 16:17:44 +03:00
|
|
|
frame.options.withRelated = ['labels', 'stripeSubscriptions', 'stripeSubscriptions.customer'];
|
|
|
|
const page = await membersService.api.members.list(frame.options);
|
|
|
|
|
2020-09-30 12:22:22 +03:00
|
|
|
return page;
|
2019-10-03 21:36:22 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-03 20:59:19 +03:00
|
|
|
importCSV: {
|
2020-12-09 19:15:53 +03:00
|
|
|
statusCode(result) {
|
|
|
|
if (result && result.meta && result.meta.stats && result.meta.stats.imported !== null) {
|
|
|
|
return 201;
|
|
|
|
} else {
|
|
|
|
return 202;
|
|
|
|
}
|
|
|
|
},
|
2019-10-03 20:59:19 +03:00
|
|
|
permissions: {
|
|
|
|
method: 'add'
|
|
|
|
},
|
|
|
|
async query(frame) {
|
2020-12-09 19:15:53 +03:00
|
|
|
const siteTimezone = settingsCache.get('timezone');
|
2020-08-06 04:58:32 +03:00
|
|
|
|
2020-12-09 19:15:53 +03:00
|
|
|
const importLabel = {
|
|
|
|
name: `Import ${moment().tz(siteTimezone).format('YYYY-MM-DD HH:mm')}`
|
|
|
|
};
|
2020-08-06 04:58:32 +03:00
|
|
|
|
2020-12-09 19:15:53 +03:00
|
|
|
const globalLabels = [importLabel].concat(frame.data.labels);
|
|
|
|
const pathToCSV = frame.file.path;
|
|
|
|
const headerMapping = frame.data.mapping;
|
|
|
|
const job = await membersService.importer.prepare(pathToCSV, headerMapping, globalLabels, {
|
|
|
|
createdBy: frame.user.id
|
|
|
|
});
|
2020-08-25 10:23:05 +03:00
|
|
|
|
2020-12-09 19:15:53 +03:00
|
|
|
if (job.batches <= 500 && !job.metadata.hasStripeData) {
|
|
|
|
const result = await membersService.importer.perform(job.id);
|
|
|
|
const importLabelModel = result.imported ? await models.Label.findOne(importLabel) : null;
|
2020-08-06 04:58:32 +03:00
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
stats: {
|
2020-12-09 19:15:53 +03:00
|
|
|
imported: result.imported,
|
|
|
|
invalid: result.errors
|
2020-08-06 04:58:32 +03:00
|
|
|
},
|
2020-12-09 19:15:53 +03:00
|
|
|
import_label: importLabelModel
|
2020-08-06 04:58:32 +03:00
|
|
|
}
|
|
|
|
};
|
2020-12-09 19:15:53 +03:00
|
|
|
} else {
|
|
|
|
const emailRecipient = frame.user.get('email');
|
2021-01-06 07:47:57 +03:00
|
|
|
jobsService.addJob({
|
|
|
|
job: async () => {
|
|
|
|
const result = await membersService.importer.perform(job.id);
|
|
|
|
const importLabelModel = result.imported ? await models.Label.findOne(importLabel) : null;
|
|
|
|
const emailContent = membersService.importer.generateCompletionEmail(result, {
|
|
|
|
emailRecipient,
|
|
|
|
importLabel: importLabelModel ? importLabelModel.toJSON() : null
|
|
|
|
});
|
|
|
|
const errorCSV = membersService.importer.generateErrorCSV(result);
|
|
|
|
const emailSubject = result.imported > 0 ? 'Your member import is complete' : 'Your member import was unsuccessful';
|
|
|
|
|
|
|
|
await ghostMailer.send({
|
|
|
|
to: emailRecipient,
|
|
|
|
subject: emailSubject,
|
|
|
|
html: emailContent,
|
|
|
|
forceTextContent: true,
|
|
|
|
attachments: [{
|
|
|
|
filename: `${importLabel.name} - Errors.csv`,
|
|
|
|
contents: errorCSV,
|
|
|
|
contentType: 'text/csv',
|
|
|
|
contentDisposition: 'attachment'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
offloaded: false
|
2020-12-09 19:15:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2020-08-06 04:58:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-26 12:38:42 +03:00
|
|
|
stats: {
|
|
|
|
options: [
|
|
|
|
'days'
|
|
|
|
],
|
|
|
|
permissions: {
|
|
|
|
method: 'browse'
|
|
|
|
},
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
days: {
|
|
|
|
values: ['30', '90', '365', 'all-time']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async query(frame) {
|
|
|
|
const days = frame.options.days === 'all-time' ? 'all-time' : Number(frame.options.days || 30);
|
|
|
|
|
2021-01-13 12:42:16 +03:00
|
|
|
return await membersService.stats.fetch(days);
|
2020-05-26 12:38:42 +03:00
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
};
|