Skip to content

Conversation

@rhian-cs
Copy link
Contributor

@rhian-cs rhian-cs commented Mar 23, 2022

Closes: #69

What?

  • Add a new attribute to the users table: self_registered
  • Add a way for unauthenticated users to register themselves in the platform
  • When a user registers themselves, an email is sent to all admins notifying about the new user and their email address
  • Self registered users have, by default, self_registered: true and active: false and need to be enabled manually by an admin

Why?

With these changes, admins no longer need to manually add each user to the platform, they only need to approve their entry.

How to test it?

Open up the application and create a new user via the JSON request POST /signup with the body:

{
  "user": {
    "full_name": "John Doe",
    "email": "[email protected]",
    "password": "12345678",
    "address": "Test Street, 123",
    "cpf": "12345670088",
    "cellphone": "123456789012345"
  }
}

Example cURL request:

curl -v -X POST http://localhost:3000/signup.json \
  -H 'Content-Type: application/json' \
  -d '{
    "user": {
      "full_name": "John Doe",
      "email": "[email protected]",
      "password": "12345678",
      "address": "Test Street, 123",
      "cpf": "12345670088",
      "cellphone": "123456789012345"
    }
  }'

It should return status 201 Created and a body containing the created user:

{
  "id": 5,
  "full_name": "John Doe",
  "email": "[email protected]",
  "cpf": "12345670088",
  "address": "Test Street, 123",
  "cellphone": "123456789012345",
  "picture_url": null,
  "admin": false,
  "active": false,
  "self_registered": true,
  "created_at": "2022-03-23T17:10:05.768Z",
  "updated_at": "2022-03-23T17:10:05.768Z",
  "companies": []
}

If something is wrong, it will return 422 Unprocessable Entity with the errors inside the body:

{
  "errors": {
    "email": [
      "já está em uso"
    ],
    "cpf": [
      "já está em uso"
    ]
  }
}

Mailer Preview Screenshot

oss-comarev-69-email

@@ -0,0 +1,9 @@
# Preview all emails at http://localhost:3000/rails/mailers/admin_mailer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment here needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was autogenerated by rails generate, I thought it would be nice to keep it.

But thinking about it now, it may be better to include this information in the README.md or in the setup docs instead.

def send_user_self_registration_notification_email
return unless resource.persisted?

AdminMailer.user_self_registration_notification(admins: User.admins.to_a, user: resource).deliver_later

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you don't need to call .to_a on User.admins

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, for this case it is necessary to specify the to_a option for the admins. If this option is omitted, ActiveJob raises an exception:

ActiveJob::SerializationError:
       Unsupported argument type: ActiveRecord::Relation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about sending only the emails themselves? It seems to me you don't need the entire admin and by doing that you'll simplify the serialization and make the mailer know only what it needs to know to complete its job.

@rhian-cs rhian-cs force-pushed the 69-add-approval-flow-for-customers branch from 6a0a7e9 to 7cae99d Compare March 28, 2022 13:07
Copy link

@RodrigoVitiello RodrigoVitiello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Copy link
Collaborator

@mauricio-prl mauricio-prl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks awesome, thanks for this pr! 🥇
Just left a comment telling you about our request helpers, but it will be merged very soon! 🚀
Also, it has a few rubocop offenses, can you please take a look?

describe 'POST /signup' do
subject(:request) { post signup_path, params: params, as: :json }

let(:response_body) { JSON.parse(response.body).deep_symbolize_keys }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PERMITTED_PARAMS = %i[email full_name cellphone cpf address].freeze

before_action :configure_permitted_parameters
after_action :send_user_self_registration_notification_email, only: %i[create] # rubocop:disable Rails/LexicallyScopedActionFilter
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-pereira I disabled this Rubocop rule for this case, is this ok?

The reason is that, at first Rubocop pointed out that create is not defined in the class, but if I explicitly add the method, like so:

def create
  super
end

Rubocop complains about an useless method:

app/controllers/registrations_controller.rb:10:3: W: [Correctable] Lint/UselessMethodDefinition: Useless method definition detected.

Copy link
Collaborator

@mauricio-prl mauricio-prl Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's ok commenting as you did, in this case, the method is inherited from the Devise gem. And it's easy to find why, on the rubocop docs

@rhian-cs rhian-cs force-pushed the 69-add-approval-flow-for-customers branch from 7ee29f9 to b17de60 Compare April 1, 2022 16:28
Copy link
Collaborator

@mauricio-prl mauricio-prl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks @rhian-cs 🚀

@mauricio-prl mauricio-prl merged commit b4dc209 into comarev:master Apr 1, 2022
@mauricio-prl
Copy link
Collaborator

@allcontributors add @rhian-cs for code

@allcontributors
Copy link
Contributor

@m-pereira

I've put up a pull request to add @rhian-cs! 🎉

@mauricio-prl
Copy link
Collaborator

@allcontributors add @wenderjean and @RodrigoVitiello for review

@allcontributors
Copy link
Contributor

@m-pereira

I've put up a pull request to add @wenderjean! 🎉

@mauricio-prl
Copy link
Collaborator

@allcontributors add @RodrigoVitiello for review

@allcontributors
Copy link
Contributor

@m-pereira

I've put up a pull request to add @RodrigoVitiello! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Approval flow for new customers

4 participants