2018-12-10 12:20:54 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
2019-11-05 13:07:30 +03:00
|
|
|
const uuid = require('uuid');
|
2020-02-14 12:33:10 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const sequence = require('../lib/promise/sequence');
|
2018-12-10 12:20:54 +03:00
|
|
|
|
|
|
|
const Member = ghostBookshelf.Model.extend({
|
2019-10-11 10:15:39 +03:00
|
|
|
tableName: 'members',
|
|
|
|
|
2019-11-05 13:07:30 +03:00
|
|
|
defaults() {
|
|
|
|
return {
|
|
|
|
subscribed: true,
|
|
|
|
uuid: uuid.v4()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
relationships: ['labels'],
|
|
|
|
|
|
|
|
relationshipBelongsTo: {
|
|
|
|
labels: 'labels'
|
|
|
|
},
|
|
|
|
|
|
|
|
labels: function labels() {
|
|
|
|
return this.belongsToMany('Label', 'members_labels', 'member_id', 'label_id')
|
|
|
|
.withPivot('sort_order')
|
|
|
|
.query('orderBy', 'sort_order', 'ASC');
|
|
|
|
},
|
|
|
|
|
2019-10-11 10:15:39 +03:00
|
|
|
emitChange: function emitChange(event, options) {
|
|
|
|
const eventToTrigger = 'member' + '.' + event;
|
|
|
|
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
onCreated: function onCreated(model, attrs, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('added', options);
|
|
|
|
},
|
|
|
|
|
2019-10-31 17:34:50 +03:00
|
|
|
onUpdated: function onUpdated(model, attrs, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('edited', options);
|
|
|
|
},
|
|
|
|
|
2019-10-11 10:15:39 +03:00
|
|
|
onDestroyed: function onDestroyed(model, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('deleted', options);
|
2020-02-14 12:33:10 +03:00
|
|
|
},
|
|
|
|
onDestroying: function onDestroyed(model) {
|
|
|
|
ghostBookshelf.Model.prototype.onDestroying.apply(this, arguments);
|
|
|
|
|
|
|
|
this.handleAttachedModels(model);
|
|
|
|
},
|
|
|
|
onSaving: function onSaving(model, attr, options) {
|
|
|
|
let labelsToSave;
|
|
|
|
let ops = [];
|
|
|
|
|
|
|
|
// CASE: detect lowercase/uppercase label slugs
|
|
|
|
if (!_.isUndefined(this.get('labels')) && !_.isNull(this.get('labels'))) {
|
|
|
|
labelsToSave = [];
|
|
|
|
|
|
|
|
// and deduplicate upper/lowercase tags
|
|
|
|
_.each(this.get('labels'), function each(item) {
|
|
|
|
item.name = item.name && item.name.trim();
|
|
|
|
for (let i = 0; i < labelsToSave.length; i = i + 1) {
|
|
|
|
if (labelsToSave[i].name && item.name && labelsToSave[i].name.toLocaleLowerCase() === item.name.toLocaleLowerCase()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labelsToSave.push(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('labels', labelsToSave);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: Detect existing labels with same case-insensitive name and replace
|
|
|
|
ops.push(function updateLabels() {
|
|
|
|
return ghostBookshelf.model('Label')
|
|
|
|
.findAll(Object.assign({
|
|
|
|
columns: ['id', 'name']
|
|
|
|
}, _.pick(options, 'transacting')))
|
|
|
|
.then((labels) => {
|
|
|
|
labelsToSave.forEach((label) => {
|
|
|
|
let existingLabel = labels.find((lab) => {
|
|
|
|
return label.name.toLowerCase() === lab.get('name').toLowerCase();
|
|
|
|
});
|
|
|
|
label.name = (existingLabel && existingLabel.get('name')) || label.name;
|
|
|
|
});
|
|
|
|
|
|
|
|
model.set('labels', labelsToSave);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.handleAttachedModels(model);
|
|
|
|
return sequence(ops);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleAttachedModels: function handleAttachedModels(model) {
|
|
|
|
/**
|
|
|
|
* @NOTE:
|
|
|
|
* Bookshelf only exposes the object that is being detached on `detaching`.
|
|
|
|
* For the reason above, `detached` handler is using the scope of `detaching`
|
|
|
|
* to access the models that are not present in `detached`.
|
|
|
|
*/
|
|
|
|
model.related('labels').once('detaching', function onDetached(collection, label) {
|
|
|
|
model.related('labels').once('detached', function onDetached(detachedCollection, response, options) {
|
|
|
|
label.emitChange('detached', options);
|
|
|
|
model.emitChange('label.detached', options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
model.related('labels').once('attaching', function onDetached(collection, labels) {
|
|
|
|
model.related('labels').once('attached', function onDetached(detachedCollection, response, options) {
|
|
|
|
labels.forEach((label) => {
|
|
|
|
label.emitChange('attached', options);
|
|
|
|
model.emitChange('label.attached', options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base model keeps only the columns, which are defined in the schema.
|
|
|
|
* We have to add the relations on top, otherwise bookshelf-relations
|
|
|
|
* has no access to the nested relations, which should be updated.
|
|
|
|
*/
|
|
|
|
permittedAttributes: function permittedAttributes() {
|
|
|
|
let filteredKeys = ghostBookshelf.Model.prototype.permittedAttributes.apply(this, arguments);
|
|
|
|
|
|
|
|
this.relationships.forEach((key) => {
|
|
|
|
filteredKeys.push(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
return filteredKeys;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We have to ensure consistency. If you listen on model events (e.g. `member.added`), you can expect that you always
|
|
|
|
* receive all fields including relations. Otherwise you can't rely on a consistent flow. And we want to avoid
|
|
|
|
* that event listeners have to re-fetch a resource. This function is used in the context of inserting
|
|
|
|
* and updating resources. We won't return the relations by default for now.
|
|
|
|
*/
|
|
|
|
defaultRelations: function defaultRelations(methodName, options) {
|
|
|
|
if (['edit', 'add', 'destroy'].indexOf(methodName) !== -1) {
|
|
|
|
options.withRelated = _.union(['labels'], options.withRelated || []);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2019-10-11 10:15:39 +03:00
|
|
|
}
|
2018-12-10 12:20:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const Members = ghostBookshelf.Collection.extend({
|
|
|
|
model: Member
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Member: ghostBookshelf.model('Member', Member),
|
|
|
|
Members: ghostBookshelf.collection('Members', Members)
|
|
|
|
};
|