diff --git a/app/controllers/api/children_controller.rb b/app/controllers/api/children_controller.rb index f42c13728..bbd1d8e04 100644 --- a/app/controllers/api/children_controller.rb +++ b/app/controllers/api/children_controller.rb @@ -47,7 +47,7 @@ class API::ChildrenController < API::APIController authorize @child cparams = params.require(:child).permit(:validated_at) - if @child.update(validated_at: cparams[:validated_at].present? ? Time.current : nil) + if ChildService.validate(@child, cparams[:validated_at].present?) render :show, status: :ok, location: child_path(@child) else render json: @child.errors, status: :unprocessable_entity diff --git a/app/services/child_service.rb b/app/services/child_service.rb index de4aea235..e589a9f54 100644 --- a/app/services/child_service.rb +++ b/app/services/child_service.rb @@ -15,4 +15,20 @@ class ChildService def self.update(child, child_params) child.update(child_params) end + + def self.validate(child, is_valid) + is_updated = child.update(validated_at: is_valid ? Time.current : nil) + if is_updated + if is_valid + NotificationCenter.call type: 'notify_user_child_is_validated', + receiver: child.user, + attached_object: child + else + NotificationCenter.call type: 'notify_user_child_is_invalidated', + receiver: child.user, + attached_object: child + end + end + is_updated + end end diff --git a/app/views/api/notifications/_notify_user_child_is_invalidated.json.jbuilder b/app/views/api/notifications/_notify_user_child_is_invalidated.json.jbuilder new file mode 100644 index 000000000..0d39d7100 --- /dev/null +++ b/app/views/api/notifications/_notify_user_child_is_invalidated.json.jbuilder @@ -0,0 +1,2 @@ +json.title notification.notification_type +json.description t('.child_invalidated') diff --git a/app/views/api/notifications/_notify_user_child_is_validated.json.jbuilder b/app/views/api/notifications/_notify_user_child_is_validated.json.jbuilder new file mode 100644 index 000000000..d53c23b3c --- /dev/null +++ b/app/views/api/notifications/_notify_user_child_is_validated.json.jbuilder @@ -0,0 +1,2 @@ +json.title notification.notification_type +json.description t('.child_validated') diff --git a/app/views/notifications_mailer/notify_user_child_is_invalidated.html.erb b/app/views/notifications_mailer/notify_user_child_is_invalidated.html.erb new file mode 100644 index 000000000..3a90e8daa --- /dev/null +++ b/app/views/notifications_mailer/notify_user_child_is_invalidated.html.erb @@ -0,0 +1,5 @@ +<%= render 'notifications_mailer/shared/hello', recipient: @recipient %> + +

+ <%= t('.body.child_invalidated') %> +

diff --git a/app/views/notifications_mailer/notify_user_child_is_validated.html.erb b/app/views/notifications_mailer/notify_user_child_is_validated.html.erb new file mode 100644 index 000000000..7d0ae40ae --- /dev/null +++ b/app/views/notifications_mailer/notify_user_child_is_validated.html.erb @@ -0,0 +1,5 @@ +<%= render 'notifications_mailer/shared/hello', recipient: @recipient %> + +

+ <%= t('.body.child_validated') %> +

diff --git a/config/locales/en.yml b/config/locales/en.yml index 4a588df09..bfa86abbe 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -447,6 +447,10 @@ en: account_validated: "Your account is valid." notify_user_is_invalidated: account_invalidated: "Your account is invalid." + notify_user_child_is_validated: + child_validated: "Your account child is valid." + notify_user_child_is_invalidated: + child_invalidated: "Your account child is invalid." notify_user_supporting_document_refusal: refusal: "Your supporting documents were refused" notify_admin_user_supporting_document_refusal: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 8effd40d2..8961745e6 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -447,6 +447,10 @@ fr: account_validated: "Votre compte est valide." notify_user_is_invalidated: account_invalidated: "Votre compte est invalide." + notify_user_child_is_validated: + child_validated: "Votre compte enfant est valide." + notify_user_child_is_invalidated: + child_invalidated: "Votre compte enfant est invalide." notify_user_supporting_document_refusal: refusal: "Vos pièces justificatives ont été refusées" notify_admin_user_supporting_document_refusal: diff --git a/config/locales/mails.en.yml b/config/locales/mails.en.yml index 3c759b770..bf2c74d02 100644 --- a/config/locales/mails.en.yml +++ b/config/locales/mails.en.yml @@ -401,6 +401,14 @@ en: subject: "Account invalidated" body: account_invalidated: "Your account was invalidated. You won't be able to book anymore, until your account is validated again." + notify_user_child_is_validated: + subject: "Child account validated" + body: + child_validated: "Your child account was validated. Now, you have access to event booking features." + notify_user_child_is_invalidated: + subject: "Child account invalidated" + body: + account_invalidated: "Your child account was invalidated. You won't be able to book event, until your child account is validated again." notify_user_supporting_document_refusal: subject: "Your supporting documents were refused" body: diff --git a/config/locales/mails.fr.yml b/config/locales/mails.fr.yml index f3607bb3a..69e10ab97 100644 --- a/config/locales/mails.fr.yml +++ b/config/locales/mails.fr.yml @@ -401,6 +401,14 @@ fr: subject: "Compte invalidé" body: account_invalidated: "Votre compte a été invalidé. Vous ne pourrez plus réserver, jusqu'à ce que votre compte soit à nouveau validé." + notify_user_child_is_validated: + subject: "Compte enfant validé" + body: + account_validated: "Votre compte enfant a été validé. Vous avez maintenant accès aux fonctionnalités de réservation de l'événement." + notify_user_child_is_invalidated: + subject: "Compte enfant invalidé" + body: + account_invalidated: "Votre compte enfant a été invalidé. Vous ne pourrez plus réserver l'événement, jusqu'à ce que votre compte enfant soit à nouveau validé." notify_user_supporting_document_refusal: subject: "Vos pièces justificatives ont été refusées" body: diff --git a/db/seeds/notification_types.rb b/db/seeds/notification_types.rb index 3c8240ae2..5cd95f461 100644 --- a/db/seeds/notification_types.rb +++ b/db/seeds/notification_types.rb @@ -55,3 +55,19 @@ unless NotificationType.find_by(name: 'notify_admin_child_created') is_configurable: true ) end + +unless NotificationType.find_by(name: 'notify_user_child_is_validated') + NotificationType.create!( + name: 'notify_user_child_is_validated', + category: 'users_accounts', + is_configurable: false + ) +end + +unless NotificationType.find_by(name: 'notify_user_child_is_invalidated') + NotificationType.create!( + name: 'notify_user_child_is_invalidated', + category: 'users_accounts', + is_configurable: false + ) +end