Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: not allowed state transitions returns error 400 #122

Merged
merged 10 commits into from
Sep 11, 2023

Conversation

ThisIsDemetrio
Copy link
Collaborator

@ThisIsDemetrio ThisIsDemetrio commented Jul 6, 2023

Pull Request Type

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Description

Solves #53

The method {id}/state (POST) execute __STATE__ updates in a different way, following the rules of the document state management.

Before:

  • the crudService executes a findOneAndUpdate with the query parameters included and a $in operator based on the possible states the document can start from (like explained in the link above but in reverse) and returns one of the following;
    • if a document is found (and, therefore, updated), return 204 No Content;
    • if a document is not found (no result with given query or non allowed __STATE__ to start with): return 404 Not Found;
    • any other error (Mongo connection, other shenanigans) are not handled, returning the exception as received;

Now:

  • the crudService executes a findOne based on the id (and the query parameters, if included);
    • if a document is not found (no result with given query or non allowed __STATE__ to start with): return 404 Not Found;
    • if a document is found, a check is performed to verify we can move to that __STATE__;
      • if not possible, returns a 400 Bad Request with a message explaining the not allowed state transition;
      • if possible, an updateOne is executed returning 204 No Content (or any other 500 in case of other problems);

Breaking changes:

  • from now on, a non-allowed state transition returns 400 (and not anymore a 404);
  • requesting a state update to the same state (e.g. from PUBLIC to PUBLIC) updates the document anyway (updatedAt and updaterId properties are updated);

More:

  • unit tests have been refactored a little (to separate different cases), and now includes more cases of non allowed transitions;

PR Checklist

  • The commit message follows our guidelines included in the CONTRIBUTING.md
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@danibix95 danibix95 added enhancement New feature or request breaking For changes that might break service behavior labels Jul 17, 2023
@danibix95 danibix95 force-pushed the edit/state-transition-not-allowed-to-400 branch from fe4cf67 to 5f9bb80 Compare September 11, 2023 09:30
@danibix95 danibix95 merged commit 4fa2f96 into main Sep 11, 2023
@danibix95 danibix95 deleted the edit/state-transition-not-allowed-to-400 branch September 11, 2023 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking For changes that might break service behavior enhancement New feature or request next_major
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants