Skip to content

Misleading function name in removeCollection controller #3806

@jjnawaaz

Description

@jjnawaaz

p5.js version

p5.js Web Editor (repository: p5.js-web-editor)

What is your operating system?

Mac OS

Web browser and version

Chrome / Firefox (any) — not browser-specific

Actual Behavior

In the backend collections controller, the file removeCollection.js
exports a function named createCollection.

However, the function:

  • Reads a collection ID from req.params
  • Finds an existing collection owned by the user
  • Calls deleteOne() on the collection

There is no collection creation logic in this function. The name
createCollection is misleading and does not reflect the actual behavior.

Expected Behavior

The controller function name should clearly reflect its behavior.
In this case, the function name should indicate that it removes (deletes)
a collection, matching the file name and route usage.

Steps to reproduce

Steps:

  1. Navigate to server/controllers/collection/collection.controller/removeCollection.js
  2. Observe that the exported function is named createCollection
  3. Review the function logic and note that it deletes an existing collection using deleteOne()

Snippet:

import Collection from '../../models/collection';

export default function createCollection(req, res) {
  const { id: collectionId } = req.params;
  const owner = req.user._id;

  function sendFailure({ code = 500, message = 'Something went wrong' }) {
    res.status(code).json({ success: false, message });
  }

  function sendSuccess() {
    res.status(200).json({ success: true });
  }

  function removeCollection(collection) {
    if (collection == null) {
      sendFailure({
        code: 404,
        message: 'Not found, or you user does not own this collection'
      });
      return null;
    }

    return collection.deleteOne();
  }

  function findCollection() {
    // Only returned if owner matches current user
    return Collection.findOne({ _id: collectionId, owner }).exec();
  }

  return findCollection()
    .then(removeCollection)
    .then(sendSuccess)
    .catch(sendFailure);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError or unexpected behaviors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions