In this episode, we're diving into how to properly remove a member from a team in our app. While it sounds simple—just detach the user from the team—there are a few important details to get right.
We start by crafting a dedicated controller just for managing team members, keeping things neat and separate from the main team controller. After setting up the necessary DELETE route and the corresponding controller method, we adjust our frontend so the "Remove from team" action actually sends the right request to our new endpoint.
Once we've got the basics working, we realize there's a hidden gotcha: when you remove a user from a team, their current_team_id
in the database might still point to the old team, which can lead to weird bugs when they log in. We fix this by making sure that after a user is detached from a team, they're switched to another team they're in (or their personal team).
To make sure this all works as it should, we build out a set of tests. The first test checks that the removal process works and that the user’s current team is updated correctly. Then, we think about access and permissions: only users with the right permission should be able to remove others. We add this logic both in the backend (using policies) and in the tests, making sure someone without permission gets a forbidden response.
We deal with a couple of subtleties in how middleware sets the context for tests and make sure things work smoothly even behind the scenes. By the end, our member removal feature is robust, well-tested, and respects permissions—ready for real-world use!