In this episode, we're tackling one of the trickier parts of building a team system: handling team invitations! We start with the basics by creating a simple form where you can enter an email to invite someone to your team. But before we get to the form, we make sure the 'invite to team' permission is set up and seeded in our database, so only admins can invite new members.
Next, we create the 'TeamInvite' model, along with the necessary migration, to actually store invite records. The invite keeps track of the team it's for, the invited user's email, and a unique token so we can find and verify invitations later. We also make sure invites expire and are removed after being used.
Then, we build the controller and route to handle submitting the invite form. We use a relationship to let teams have many invites and set everything up so when you submit the invite form, an invite is actually created in the database. Nice!
Testing gets a bit interesting here, since we're generating random tokens for invites. We show how to make predictable fake tokens in your tests so you can assert things like "the invite is really in the database with this token." Plus, we add a simple email validation both in the controller and the tests to make sure we're not saving blank emails.
By the end, you've got a working invite system: an invite form, backed by a model and database, with solid validation and a proper test suite. The focus here is on getting the core mechanics right so you can build on it in future episodes (like actually sending emails).