diff --git a/app/controllers/api/children_controller.rb b/app/controllers/api/children_controller.rb index 55b2437e6..f42c13728 100644 --- a/app/controllers/api/children_controller.rb +++ b/app/controllers/api/children_controller.rb @@ -20,7 +20,7 @@ class API::ChildrenController < API::APIController def create @child = Child.new(child_params) authorize @child - if @child.save + if ChildService.create(@child) render status: :created else render json: @child.errors.full_messages, status: :unprocessable_entity diff --git a/app/services/child_service.rb b/app/services/child_service.rb new file mode 100644 index 000000000..de4aea235 --- /dev/null +++ b/app/services/child_service.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# ChildService +class ChildService + def self.create(child) + if child.save + NotificationCenter.call type: 'notify_admin_child_created', + receiver: User.admins_and_managers, + attached_object: child + return true + end + false + end + + def self.update(child, child_params) + child.update(child_params) + end +end diff --git a/app/views/api/children/_child.json.jbuilder b/app/views/api/children/_child.json.jbuilder index 029921ecf..0765d67ad 100644 --- a/app/views/api/children/_child.json.jbuilder +++ b/app/views/api/children/_child.json.jbuilder @@ -6,7 +6,7 @@ json.supporting_document_files_attributes child.supporting_document_files do |f| json.supportable_id f.supportable_id json.supportable_type f.supportable_type json.supporting_document_type_id f.supporting_document_type_id - json.attachment f.attachment.file.filename + json.attachment f.attachment.file&.filename json.attachment_name f.attachment_identifier - json.attachment_url "/api/supporting_document_files/#{f.id}/download" + json.attachment_url f.attachment_identifier ? "/api/supporting_document_files/#{f.id}/download" : nil end diff --git a/app/views/api/notifications/_notify_admin_child_created.json.jbuilder b/app/views/api/notifications/_notify_admin_child_created.json.jbuilder new file mode 100644 index 000000000..d65f42b8e --- /dev/null +++ b/app/views/api/notifications/_notify_admin_child_created.json.jbuilder @@ -0,0 +1,4 @@ +json.title notification.notification_type +json.description t('.a_new_child_has_been_created_NAME_html', + NAME: notification.attached_object&.full_name || t('api.notifications.deleted_user')) + diff --git a/app/views/notifications_mailer/notify_admin_child_created.html.erb b/app/views/notifications_mailer/notify_admin_child_created.html.erb new file mode 100644 index 000000000..77225fef8 --- /dev/null +++ b/app/views/notifications_mailer/notify_admin_child_created.html.erb @@ -0,0 +1,6 @@ +<%= render 'notifications_mailer/shared/hello', recipient: @recipient %> + +
+ <%= t('.body.new_child_created') %> + "<%= @attached_object&.full_name || t('api.notifications.deleted_user') %> <<%= @attached_object.email%>>" +
diff --git a/config/locales/app.logged.en.yml b/config/locales/app.logged.en.yml index 325bce3a2..69afbfbd7 100644 --- a/config/locales/app.logged.en.yml +++ b/config/locales/app.logged.en.yml @@ -284,6 +284,7 @@ en: app_management: "Concerning app management notifications" notification_form: notify_admin_when_user_is_created: "A user account has been created" + notify_admin_child_created: "A child has been created" notify_admin_when_user_is_imported: "A user account has been imported" notify_admin_profile_complete: "An imported account has completed its profile" notify_admin_user_merged: "An imported account has been merged with an existing account" diff --git a/config/locales/app.logged.fr.yml b/config/locales/app.logged.fr.yml index 1e3a76683..0dc9c03f0 100644 --- a/config/locales/app.logged.fr.yml +++ b/config/locales/app.logged.fr.yml @@ -284,6 +284,7 @@ fr: app_management: "Au sujet de la gestion de l'application" notification_form: notify_admin_when_user_is_created: "Un compte utilisateur a été créé" + notify_admin_child_created: "Un compte enfant a été créé" notify_admin_when_user_is_imported: "Un compte utilisateur a été importé" notify_admin_profile_complete: "Un compte importé a complété ses informations" notify_admin_user_merged: "Un compte importé a été fusionné avec un compte existant" diff --git a/config/locales/en.yml b/config/locales/en.yml index 4106c6df1..4a588df09 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -314,6 +314,8 @@ en: project_NAME_has_been_published_html: "Project %{NAME} has been published." notify_admin_when_user_is_created: a_new_user_account_has_been_created_NAME_EMAIL_html: "A new user account has been created: %{NAME} <%{EMAIL}>." + notify_admin_child_created: + a_new_child_has_been_created_NAME_html: "A new child has been created: %{NAME}." notify_admin_when_user_is_imported: a_new_user_account_has_been_imported_from_PROVIDER_UID_html: "A new user account has been imported from: %{PROVIDER} (%{UID})." notify_member_create_reservation: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index c85575406..8effd40d2 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -314,6 +314,8 @@ fr: project_NAME_has_been_published_html: "Le projet %{NAME} vient d'être publié." notify_admin_when_user_is_created: a_new_user_account_has_been_created_NAME_EMAIL_html: "Un nouveau compte utilisateur vient d'être créé : %{NAME} <%{EMAIL}>." + notify_admin_child_created: + a_new_child_has_been_created_NAME_html: "Un nouveau enfant vient d'être créé : %{NAME}." notify_admin_when_user_is_imported: a_new_user_account_has_been_imported_from_PROVIDER_UID_html: "Un nouveau compte utilisateur vient d'être importé depuis : %{PROVIDER} (%{UID})." notify_member_create_reservation: diff --git a/config/locales/mails.en.yml b/config/locales/mails.en.yml index 3b3ac8981..3c759b770 100644 --- a/config/locales/mails.en.yml +++ b/config/locales/mails.en.yml @@ -104,6 +104,10 @@ en: new_account_created: "A new user account has been created on the website:" user_of_group_html: "The user has registered in the group %{GROUP}" account_for_organization: "This account manage an organization:" + notify_admin_child_created: + subject: "A user's child has been created" + body: + new_child_created: "A new user's child has been created on the website" notify_admin_subscribed_plan: subject: "A subscription has been purchased" body: diff --git a/config/locales/mails.fr.yml b/config/locales/mails.fr.yml index 23b94146d..f3607bb3a 100644 --- a/config/locales/mails.fr.yml +++ b/config/locales/mails.fr.yml @@ -104,6 +104,10 @@ fr: new_account_created: "Un nouveau compte utilisateur vient d'être créé sur la plateforme :" user_of_group_html: "L'utilisateur s'est inscrit dans le groupe %{GROUP}" account_for_organization: "Ce compte gère une structure :" + notify_admin_child_created: + subject: "Un enfant a été créé" + body: + new_child_created: "Un nouveau enfant vient d'être créé sur la plateforme" notify_admin_subscribed_plan: subject: "Un abonnement a été souscrit" body: diff --git a/db/seeds/notification_types.rb b/db/seeds/notification_types.rb index d73b0be90..3c8240ae2 100644 --- a/db/seeds/notification_types.rb +++ b/db/seeds/notification_types.rb @@ -47,3 +47,11 @@ unless NotificationType.find_by(name: 'notify_user_child_supporting_document_ref is_configurable: false ) end + +unless NotificationType.find_by(name: 'notify_admin_child_created') + NotificationType.create!( + name: 'notify_admin_child_created', + category: 'users_accounts', + is_configurable: true + ) +end