diff --git a/client/coral-framework/actions/config.js b/client/coral-framework/actions/config.js index f85e609eb..eb03d63ed 100644 --- a/client/coral-framework/actions/config.js +++ b/client/coral-framework/actions/config.js @@ -6,14 +6,6 @@ import I18n from 'coral-framework/modules/i18n/i18n'; import translations from './../translations'; const lang = new I18n(translations); -export const updateOpenStatus = status => (dispatch, getState) => { - const assetId = getState().items.get('assets') - .keySeq() - .toArray()[0]; - return coralApi(`/asset/${assetId}/status?status=${status}`, {method: 'PUT'}) - .then(() => dispatch({type: status === 'open' ? actions.OPEN_COMMENTS : actions.CLOSE_COMMENTS})); -}; - const updateConfigRequest = () => ({type: actions.UPDATE_CONFIG_REQUEST}); const updateConfigSuccess = config => ({type: actions.UPDATE_CONFIG_SUCCESS, config}); const updateConfigFailure = () => ({type: actions.UPDATE_CONFIG_FAILURE}); @@ -31,3 +23,16 @@ export const updateConfiguration = newConfig => (dispatch, getState) => { }) .catch(error => dispatch(updateConfigFailure(error))); }; + +const openStream = () => ({type: actions.OPEN_COMMENTS}); +const closeStream = () => ({type: actions.CLOSE_COMMENTS}); + +export const updateOpenStatus = status => dispatch => { + if (status === 'open') { + dispatch(openStream()); + dispatch(updateConfiguration({closedAt: null})); + } else { + dispatch(closeStream()); + dispatch(updateConfiguration({closedAt: Date.getTime()})); + } +}; diff --git a/models/asset.js b/models/asset.js index c1fc59dc3..09a9f49e8 100644 --- a/models/asset.js +++ b/models/asset.js @@ -5,12 +5,6 @@ const Setting = require('./setting'); const uuid = require('uuid'); -// ASSET_STATUSES is the list of statuses that are permitted for the asset status. -const ASSET_STATUS = [ - 'open', - 'closed' -]; - const AssetSchema = new Schema({ id: { type: String, @@ -35,15 +29,6 @@ const AssetSchema = new Schema({ type: Schema.Types.Mixed, default: null }, - status: { - type: String, - default: 'open' - }, - statusChangedAt: { - type: Date, - default: null - }, - statusClosedMessage: String, title: String, description: String, image: String, @@ -71,31 +56,6 @@ AssetSchema.index({ background: true }); -/** - * Returns true if the asset is closed, false else. - */ -AssetSchema.virtual('isClosed').get(function() { - return (this.status === 'closed') && this.statusChangedAt && this.statusChangedAt.getTime() <= new Date().getTime(); -}); - -/** - * Close or Open the asset. - */ -AssetSchema.statics.changeOpenStatus = (id, status, closedMessage = '') => { - // Check to see if the user role is in the allowable set of roles. - if (ASSET_STATUS.indexOf(status) === -1) { - // Asset status is not supported! Error out here. - return Promise.reject(new Error(`status ${status} is not supported`)); - } - return Asset.update({id}, { - $set: { - status: status, - statusChangedAt: new Date().getTime(), - statusClosedMessage: closedMessage - } - }); -}; - /** * Finds an asset by its id. * @param {String} id identifier of the asset (uuid). @@ -108,6 +68,10 @@ AssetSchema.statics.findById = (id) => Asset.findOne({id}); */ AssetSchema.statics.findByUrl = (url) => Asset.findOne({url}); +AssetSchema.virtual('isClosed').get(function() { + return this.settings && this.settings.closedAt && this.settings.closedAt <= new Date().getTime(); +}); + /** * Retrieves the settings given an asset query and rectifies it against the * global settings. diff --git a/routes/api/asset/index.js b/routes/api/asset/index.js index ebdcf038d..f61851f0c 100644 --- a/routes/api/asset/index.js +++ b/routes/api/asset/index.js @@ -89,12 +89,16 @@ router.put('/:asset_id/settings', (req, res, next) => { .catch((err) => next(err)); }); -router.put('/:asset_id/status', (req, res, next) => { - - Asset - .changeOpenStatus(req.params.asset_id, req.query.status, req.query.closedMessage) - .then(() => res.status(204).end()) - .catch((err) => next(err)); -}); +// router.put('/:asset_id/status', (req, res, next) => { +// Asset +// .findOneAndUpdate(req.params.asset_id, { +// $set: { +// closedAt: req.query.closedAt, +// closedMessage: req.query.closedMessage +// } +// }) +// .then(() => res.status(204).end()) +// .catch((err) => next(err)); +// }); module.exports = router; diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index fad9083cc..bf741ed2d 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -87,9 +87,8 @@ router.post('/', wordlist.filter('body'), (req, res, next) => { // Check to see if the asset has closed commenting... if (asset.isClosed) { - // They have, ensure that we send back an error. - return Promise.reject(new Error(`asset has commenting closed because: ${asset.statusClosedMessage}`)); + return Promise.reject(new Error(`asset has commenting closed because: ${asset.settings.closedMessage}`)); } return asset; diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 4787e4d58..0f6e8442e 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -160,6 +160,9 @@ describe('/api/v1/comments', () => { .then((res) => { expect(res).to.have.status(201); expect(res.body).to.have.property('id'); + }) + .catch((err) => { + expect(err).to.be.null; }); }); @@ -199,9 +202,10 @@ describe('/api/v1/comments', () => { it('shouldn\'t create a comment when the asset has expired commenting', () => { return Asset.create({ - status: 'closed', - statusChangedAt: new Date().setDate(0), - statusClosedMessage: 'tests said expired!' + settings: { + closedAt: new Date().setDate(0), + closedMessage: 'tests said expired!' + } }) .then((asset) => { return chai.request(app)