diff --git a/server/controllers/collection.controller/listCollections.js b/server/controllers/collection.controller/listCollections.js index fb5c70cff8..6920bcf2e1 100644 --- a/server/controllers/collection.controller/listCollections.js +++ b/server/controllers/collection.controller/listCollections.js @@ -14,21 +14,23 @@ async function getOwnerUserId(req) { return null; } -export default function listCollections(req, res) { - function sendFailure({ code = 500, message = 'Something went wrong' }) { +export default async function listCollections(req, res) { + const sendFailure = ({ code = 500, message = 'Something went wrong' }) => { res.status(code).json({ success: false, message }); - } + }; - function sendSuccess(collections) { + const sendSuccess = (collections) => { res.status(200).json(collections); - } + }; + + try { + const owner = await getOwnerUserId(req); - function findCollections(owner) { - if (owner == null) { - sendFailure({ code: 404, message: 'User not found' }); + if (!owner) { + sendFailure('404', 'User not found'); } - return Collection.find({ owner }).populate([ + const collections = await Collection.find({ owner }).populate([ { path: 'owner', select: ['id', 'username'] }, { path: 'items.project', @@ -39,10 +41,9 @@ export default function listCollections(req, res) { } } ]); - } - return getOwnerUserId(req) - .then(findCollections) - .then(sendSuccess) - .catch(sendFailure); + sendSuccess(collections); + } catch (error) { + sendFailure(error.code || 500, error.message || 'Something went wrong'); + } } diff --git a/server/controllers/collection.controller/removeProjectFromCollection.js b/server/controllers/collection.controller/removeProjectFromCollection.js index 56a83df297..f652d1b76c 100644 --- a/server/controllers/collection.controller/removeProjectFromCollection.js +++ b/server/controllers/collection.controller/removeProjectFromCollection.js @@ -1,6 +1,6 @@ import Collection from '../../models/collection'; -export default function addProjectToCollection(req, res) { +export default function removeProjectFromCollection(req, res) { const owner = req.user._id; const { id: collectionId, projectId } = req.params; diff --git a/server/models/collection.js b/server/models/collection.js index c7fc0d1ee7..1633c03847 100644 --- a/server/models/collection.js +++ b/server/models/collection.js @@ -6,9 +6,9 @@ const { Schema } = mongoose; const collectedProjectSchema = new Schema( { - project: { type: Schema.Types.ObjectId, ref: 'Project' } + project: { type: Schema.Types.String, ref: 'Project' } }, - { timestamps: true, _id: true, usePushEach: true } + { timestamps: true } ); collectedProjectSchema.virtual('id').get(function getId() { @@ -36,7 +36,7 @@ const collectionSchema = new Schema( owner: { type: Schema.Types.ObjectId, ref: 'User' }, items: { type: [collectedProjectSchema] } }, - { timestamps: true, usePushEach: true } + { timestamps: true } ); collectionSchema.virtual('id').get(function getId() { @@ -48,9 +48,8 @@ collectionSchema.set('toJSON', { }); collectionSchema.pre('save', function generateSlug(next) { - const collection = this; - collection.slug = slugify(collection.name, '_'); - return next(); + this.slug = slugify(this.name, { lower: true, strict: true }); + next(); }); export default mongoose.models.Collection || diff --git a/server/models/project.js b/server/models/project.js index 89fc8bed50..30b506fa4d 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -16,7 +16,7 @@ const fileSchema = new Schema( fileType: { type: String, default: 'file' }, isSelectedFile: { type: Boolean } }, - { timestamps: true, _id: true, usePushEach: true } + { timestamps: true } ); fileSchema.virtual('id').get(function getFileId() { @@ -40,7 +40,7 @@ const projectSchema = new Schema( _id: { type: String, default: shortid.generate }, slug: { type: String } }, - { timestamps: true, usePushEach: true } + { timestamps: true } ); projectSchema.virtual('id').get(function getProjectId() { @@ -52,13 +52,10 @@ projectSchema.set('toJSON', { }); projectSchema.pre('save', function generateSlug(next) { - const project = this; - - if (!project.slug) { - project.slug = slugify(project.name, '_'); + if (!this.slug) { + this.slug = slugify(this.name, { lower: true, strict: true }); } - - return next(); + next(); }); /**