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

Admin newsletter email refactor #2474

Merged
merged 9 commits into from
Feb 21, 2018
Merged

Conversation

bertocq
Copy link
Collaborator

@bertocq bertocq commented Feb 21, 2018

Where

What

This issue is a backport of a refactor on Newsletters & Email downloads

How & Screenshots & Test

Best check the PR at madrid's fork AyuntamientoMadrid#1285

Deployment

As usual

Warnings

None

Why:

UserSegments are not only used for Newsletters or Email downloads, but
also for internal Global Notifications. We don't want to have that scope
hardcoded inside UserSegments as users that have opted-out from the
newsletter should still be recipients of global notifications.

How:

Removing the scope from the UserSegments `all_users` method that acts as
base for all the other segments. Including that `newsletter` scope only
on the places that is relevant:
* When listing recipients for a newsletter
* When downloading a listing emails that can be newsletter recipients

Also updated relevant tests
Why:

Newsletter attribute `segment_recipient` is an integer to be used as
enum. There's no advantage to store a number instead of an string if the
ammount of elements in the table is not going to be huge, or we can take
advantage of using an enum.

Also maintaining both Newsletters enum paired with UserSegments::SEGMENTS
would be a maintenance burden.

How:

* Migration to change segment_recipient column from integer to string
* Removing enumeration from Newsletter model class
* Using UserSegments::SEGMENTS instead of Newsletter.segment_recipients
or integer values
Why:

A Newsletter can only be sent if the are available user recipient emails
and that means the `segment_recipient` value actually corresponds to a
function on the UserSegments class.

We could rely on the UserSegments::SEGMENTS constant as the list of
possible user segments functions that a Newsletter can use to gather
emails, so any value not included in that hash would not be valid.

But to be 100% sure the newsletter can get a recipients_list we should
just check if the UserSegments class has a method with same name as the
`segment_recipient` value.

How:

* Adding an validation method that checks if UserSegment has a method
with same name as the `segment_recipient` value.
* Adding an scenario to the Newsletter model spec to check this
Newsletters with an invalid user segment should display a warning on
show and index. Also those newsletters should not be sent.
Instead of the name of the User Segment we'll display an error message
Why:

Newsletters without a valid user segment can't be sent since there is no
recipient user email list.

How:

* Hiding the send button at the form
* Preventing an invalid newsletter from being delivered at the controller
When a newsletter doesn't have a valid user segment for the recipients
list, the number of users should be 0.
Simple refactor to avoid creating unnecessary variables and make it easier
to read.
@bertocq bertocq merged commit df16194 into master Feb 21, 2018
@bertocq bertocq deleted the admin_newsletter_email_refactor branch February 21, 2018 12:03
clairezed pushed a commit to CDJ11/CDJ that referenced this pull request Jun 26, 2018
…email_refactor

Admin newsletter email refactor
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.

None yet

1 participant