Removes status field for Asset. Now closedAt and closedMessage are in the settings field.

This commit is contained in:
gaba
2016-12-12 13:02:39 -08:00
parent d228b30f36
commit 3b131ce315
5 changed files with 36 additions and 60 deletions
+13 -8
View File
@@ -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()}));
}
};
+4 -40
View File
@@ -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.
+11 -7
View File
@@ -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;
+1 -2
View File
@@ -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;
+7 -3
View File
@@ -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)