diff --git a/core/server/models/index.js b/core/server/models/index.js index 8d3451585e..4e2cbabe55 100644 --- a/core/server/models/index.js +++ b/core/server/models/index.js @@ -38,6 +38,7 @@ const models = [ 'member-email-change-event', 'member-payment-event', 'member-status-event', + 'member-product-event', 'posts-meta', 'member-stripe-customer', 'stripe-customer-subscription', diff --git a/core/server/models/member-product-event.js b/core/server/models/member-product-event.js new file mode 100644 index 0000000000..95b2d225e4 --- /dev/null +++ b/core/server/models/member-product-event.js @@ -0,0 +1,41 @@ +const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); +const ghostBookshelf = require('./base'); + +const messages = { + cannotPerformAction: 'Cannot {action} MemberProductEvent' +}; + +const MemberProductEvent = ghostBookshelf.Model.extend({ + tableName: 'members_product_events', + + member() { + return this.belongsTo('Member', 'member_id', 'id'); + }, + + product() { + return this.belongsTo('Product', 'product_id', 'id'); + } + +}, { + async edit() { + throw new errors.IncorrectUsageError( + tpl(messages.cannotPerformAction, 'edit') + ); + }, + + async destroy() { + throw new errors.IncorrectUsageError( + tpl(messages.cannotPerformAction, 'destroy') + ); + } +}); + +const MemberProductEvents = ghostBookshelf.Collection.extend({ + model: MemberProductEvent +}); + +module.exports = { + MemberProductEvent: ghostBookshelf.model('MemberProductEvent', MemberProductEvent), + MemberProductEvents: ghostBookshelf.collection('MemberProductEvents', MemberProductEvents) +}; diff --git a/core/server/models/member.js b/core/server/models/member.js index 9540c0881b..ad92ba776b 100644 --- a/core/server/models/member.js +++ b/core/server/models/member.js @@ -78,6 +78,11 @@ const Member = ghostBookshelf.Model.extend({ email_recipients: 'email_recipients' }, + productEvents() { + return this.hasMany('MemberProductEvent', 'member_id', 'id') + .query('orderBy', 'created_at', 'DESC'); + }, + products() { return this.belongsToMany('Product', 'members_products', 'member_id', 'product_id') .withPivot('sort_order')