Skip to content

Commit

Permalink
Merge pull request #47 from doxa-tech/v2/cards-update-check
Browse files Browse the repository at this point in the history
Adds email for cards update checks
  • Loading branch information
nkcr authored Dec 30, 2021
2 parents faa5606 + a3d5a47 commit 0452445
Show file tree
Hide file tree
Showing 23 changed files with 359 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .deploy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ WORKDIR /home/app/resj
RUN bundle install
RUN --mount=type=secret,id=master_key,uid=9999 RAILS_MASTER_KEY=$(cat /run/secrets/master_key) bundle exec rake assets:precompile

# run the cards update check daily
COPY --chmod=0755 cards-check.sh /etc/cron.daily/

USER root
# baseimage-docker's init process
CMD ["/sbin/my_init"]
CMD ["/sbin/my_init"]
8 changes: 8 additions & 0 deletions .deploy/cards-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# This script should be placed in /etc/cron.daily to check the cards status
# every day and send an email to the card's owner accordingly.

set -e

setuser app bundle exec rake cards_update:check
6 changes: 6 additions & 0 deletions app/assets/stylesheets/pages/users/cards/edit.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
padding: 0 $content-padding;
margin: auto;

.standard-popup {
p {
padding: 5px 0;
}
}

section.top-container {
.title {
display: flex;
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/users/cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ class Users::CardsController < ApplicationController
layout "admin"

def edit
@confirmed = params[:confirmed] == "true"
@card = card
end

def update
@card = card
@card.last_updated = Time.current
if @card.update(card_params)
redirect_to edit_users_card_path(@card), success: "Ton groupe a été mis à jour"
@card.assign_attributes(card_params)
was_outdated = !@card.maintained?
if @card.valid?
@card.last_updated = Time.current
@card.validity = :maintained
@card.save
redirect_to edit_users_card_path(@card, confirmed: was_outdated), success: "Ton groupe a été mis à jour"
else
render 'edit'
end
Expand Down
5 changes: 5 additions & 0 deletions app/mailers/admin/card_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ def submit(card)
mail(to: "nkcr.je@gmail.com", subject: "Un nouveau groupe a été créé")
end

def deactivated(card)
@card = card
mail(to: "nkcr.je@gmail.com", subject: "Un groupe est désormais désactivé")
end

end
8 changes: 8 additions & 0 deletions app/mailers/card_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def offline(card, message)
mail(to: card.user.email, subject: "Ton groupe sur Réseau Jeunesse n'est désormais plus visible !")
end

# step indicates the number of reminder sent. 0 is the first "normal" request.
# After the 4th reminder the group is marked as "Deactivated".
def update_check(card, step)
@card = card
@step = step
mail(to: card.user.email, subject: "Vérifie ton groupe sur Réseau Jeunesse")
end

def migration(user)
@user = user
mail(to: user.email, subject: "Du nouveau sur Réseau Jeunesse")
Expand Down
1 change: 1 addition & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Card < ApplicationRecord

enum card_type: [:youth, :adult, :activist, :organization, :network, :training]
enum status: [:pending, :online, :incomplete, :change]
enum validity: [:maintained, :solicited, :disabled]

belongs_to :location, optional: true # validation made manually according to the step
belongs_to :user
Expand Down
9 changes: 9 additions & 0 deletions app/views/admin/card_mailer/deactivated.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% content_for :prehead, "Un groupe est désactivé sur Réseau Jeunesse" %>

<h1>Le groupe: <%= @card.name %></h1>

<p>... est désormais désactivé.</p>

<%= render "mailers/button_center" do %>
<%= link_to "Voir le groupe", edit_admin_card_url(@card), target: "_blank" %>
<% end %>
29 changes: 29 additions & 0 deletions app/views/card_mailer/update_check.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h1>Ton groupe <%= @card.name %> est-il encore à jour ?</h1>

<p>Voilà <b><%= time_ago_in_words(@card.last_updated) %></b> que ton groupe sur Réseau Jeunesse n'a pas été mis à jour.
<% if @step >= 4 %>
<b>Ton groupe est maintenant désactivé, il faut impérativement le vérifier et/ou le mettre à jour</b>.
<% end %>
</p>

<p>Afin de garder des informations valides, nous te demandons de vérifier et mettre à jour si nécessaire les informations de ton groupe.
Cette étape est nécessaire pour garder ton groupe sur le site.</p>

<%= render "mailers/button_center" do %>
<%= link_to "Confirmer / Mettre à jour mon groupe", edit_users_card_url(@card, confirm:true), "_target"=>"_blank" %>
<% end %>

<p>Tu peux changer en tout temps les informations de ton groupe à partir de
<%= link_to "ton compte", profile_url, target: "_blank" %> sur le site. Tu recevras tous les 6 mois une invitation à contrôler les informations de ton groupe.</p>

<% if @step == 1 %>
<p>Ceci est le premier rappel.</p>
<% elsif @step > 1 && @step <= 2 %>
<p>Ceci est le <%= @step %>e rappel.</p>
<% elsif @step == 3 %>
<p>Ceci est le <%= @step %>e rappel.<br><b>Attention: le groupe sera désactivé dans 2 semaines s'il n'est pas mis à jour</b>.</p>
<% elsif @step >= 4 %>
<p>Ceci est le <%= @step %>e rappel. <b>Nous nous réservons le droit de supprimer ton groupe si aucune action n'est entreprise</b>.</p>
<% end %>

<p>Tu peux en savoir plus sur notre politque de mise à jour des groupes <a href="https://reseaujeunesse.ch/faq#update_groups" target="_blank">via notre FAQ</a>.</p>
16 changes: 16 additions & 0 deletions app/views/pages/faq.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@
Les demandes pour devenir orateur sont traitées manuellement. Une fois la demande acceptée tu recevras un lien pour te créer un compte orateur.
</p>
</details>

<details id="update_groups">
<summary><h2>Pourquoi est-ce que je reçois des mails pour mettre à jour mon groupe ?</h2></summary>
<p class="bigger">
Il est essentiel que les groupes restent à jour. C'est pour ça que nous avons besoin de toi.
</p>
<p>
Pour que les groupes restent à jour, chaque responsable de groupe reçoit tous les 6 mois une invitation à vérifier et mettre à jour si nécessaire son groupe.
Notre système vérifie tous les jours les groupes dont la dernière mise à jour remonte à 6 mois et notifie son responsable par email. Même si le groupe est à jour,
il est nécessaire, via la page d'édition du groupe, de cliquer sur le bouton "Sauvegarder et mettre à jour".
</p>
<p>
Si aucune action n'est effectuée après le premier email, 4 rappels sont envoyés à 2 semaines d'interval chacun. À partir du 4e rappel le groupe est marqué comme <b>"Désactivé"</b> sur le site. Un email est envoyé
tous les deux mois en guise d’ultime invitation à mettre à jour son groupe. L'équipe se réserve ensuite le droit de <b>supprimer définitivement le groupe</b>.
</p>
</details>
</div>

<div class="right">
Expand Down
12 changes: 12 additions & 0 deletions app/views/users/cards/_confirmed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div data-controller="popup">
<div class="standard-popup active" data-popup-target="content">
<div class="content">

<p>Merci d'avoir confirmé ton groupe. Aucune autre action de ta part n'est nécessaire.</p>

<div class="buttons">
<button class="cancel" type="button" data-action="popup#close">Fermer</button>
</div>
</div>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/users/cards/_outdated.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div data-controller="popup">
<div class="standard-popup active" data-popup-target="content">
<div class="content">

<p>Merci de vérifier (et mettre à jour si nécessaire) les informations de ton groupe.</p>
<p>Quand tu as fini, clique sur le bouton <b>"Sauvegarder et mettre à jour"</b>, même si tu n'a fait aucun changement.</p>

<div class="buttons">
<button class="cancel" type="button" data-action="popup#close">Fermer</button>
</div>
</div>
</div>
</div>
4 changes: 4 additions & 0 deletions app/views/users/cards/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<%= link_to "Voir le groupe", card_path(@card) %>
<% end %>

<%= render "outdated" if !@card.maintained? %>

<%= render "confirmed" if @confirmed %>

<section class="std-container top-container">
<div class="left">
<div class="title red">
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20211219170017_add_validity_to_cards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddValidityToCards < ActiveRecord::Migration[6.0]
def change
add_column :cards, :validity, :integer, default: 0
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_11_06_163807) do
ActiveRecord::Schema.define(version: 2021_12_19_170017) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -112,6 +112,7 @@
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.datetime "last_updated"
t.integer "validity", default: 0
t.index ["location_id"], name: "index_cards_on_location_id"
t.index ["user_id"], name: "index_cards_on_user_id"
end
Expand Down
3 changes: 2 additions & 1 deletion features/cards/edit.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: update a card
Feature: Update a card

So that the card's information are update
as the owner
Expand All @@ -13,6 +13,7 @@ Feature: update a card
When I visit the card's update page
And I update the card's name with "Spacestation"
Then I should see "Spacestation"
And I should not see "Merci d'avoir confirmé ton groupe"

Scenario: I update the card with an error
When I visit the card's update page
Expand Down
25 changes: 25 additions & 0 deletions features/cards/validity.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Confirm the validity of a card

So that my card is not disabled
as the owner
I want to update the card

Background:
Given I am a confirmed user
And I am signed in

Scenario: The card is up-to-date
Given I have a complete card
When I visit the card's update page
Then I should not see "Merci de vérifier"

Scenario: The owner received a request to confirm the card
Given I have a card that must be confirmed
When I visit the card's update page
Then I should see "Merci de vérifier"

Scenario: I update an card after a request
Given I have a card that must be confirmed
When I visit the card's update page
And I click the button "Sauvegarder et mettre à jour"
Then I should see "Merci d'avoir confirmé ton groupe"
4 changes: 4 additions & 0 deletions features/step_definitions/cards/confirmation_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@card = create(:card, user: @user)
end

Given('I have a card that must be confirmed') do
@card = create(:card, user: @user, validity: :solicited)
end

Given("I have an incomplete card") do
visit "/cards/wizards/new"
step "I complete the first step and submit it"
Expand Down
59 changes: 59 additions & 0 deletions lib/tasks/cards_update_check.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace :cards_update do

desc "Check if a card must be checked by its owner"
task check: :environment do
Card.all.each do |card|
# Here is the schedule reminder:
#
# | Step | Days |
# | ---- | ---- |
# | 0 | 180 |
# | 1 | 180 + 14 |
# | 2 | 180 + 14*2 |
# | 3 | 180 + 14*3 |
# | 4 | 180 + 14*4 | // deactivate the card
# | 5 | 180 + 14*4 + 60 |
# | 5 +i | 180 + 14*4 + 60 * (i+1) | // send every two months

# to be fair we cap the last_updated to the release of V2 - 6 months to be
# adjusted so that when we release the task we quickly get 180 days. To be
# removed once all cards have received their warnings.
release = Date.strptime("4/7/2021", "%d/%m/%Y")

if Rails.env == "test"
release = Date.strptime("1/1/1970", "%d/%m/%Y")
end

base = [release, card.last_updated.to_date].max

days_from_update = (Time.current.to_date - base).to_i

days2step = {180 => 0, 180+14 => 1, 180+14*2 => 2, 180+14*3 => 3, 180+14*4 => 4}

step = days2step[days_from_update] || -1

if step == -1 && days_from_update > 180 + 14*4 && (days_from_update - (180 + 14*4)) % 60 == 0
step = 4 + (days_from_update - (180 + 14*4)) / 60
end

# p "step: #{step}, days_from_update: #{days_from_update}"

# flag the card and notify the admin if not already done
if step >= 4 && !card.disabled?
card.update_attribute(:validity, :disabled)
Admin::CardMailer.deactivated(card).deliver_now
end

# flag the card if not already done
if step >= 0 && step <= 3 && !card.solicited?
card.update_attribute(:validity, :solicited)
end

# notify the card owner
if step >= 0
CardMailer.update_check(card, step).deliver_now
end
end
end

end
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
location { Location.find_by_official_name("Bulle") || create(:location) }
card_type { :youth }
status { :online }
validity { :maintained }
end

factory :location do
Expand Down
Loading

0 comments on commit 0452445

Please sign in to comment.