diff --git a/app/assets/javascripts/controllers/admin/calendar.js.erb b/app/assets/javascripts/controllers/admin/calendar.js.erb index 983369b7b..6e35d5a91 100644 --- a/app/assets/javascripts/controllers/admin/calendar.js.erb +++ b/app/assets/javascripts/controllers/admin/calendar.js.erb @@ -264,8 +264,8 @@ Application.Controllers.controller('AdminCalendarController', ['$scope', '$state * @return {string} 'male' or 'female' */ var getGender = function (user) { - if (user.profile) { - if (user.profile.gender === 'true') { return 'male'; } else { return 'female'; } + if (user.statistic_profile) { + if (user.statistic_profile.gender === 'true') { return 'male'; } else { return 'female'; } } else { return 'other'; } }; diff --git a/app/assets/javascripts/controllers/admin/members.js.erb b/app/assets/javascripts/controllers/admin/members.js.erb index 72dde78c8..fad467431 100644 --- a/app/assets/javascripts/controllers/admin/members.js.erb +++ b/app/assets/javascripts/controllers/admin/members.js.erb @@ -536,7 +536,7 @@ Application.Controllers.controller('EditMemberController', ['$scope', '$state', CSRF.setMetaTags(); // init the birth date to JS object - $scope.user.profile.birthday = moment($scope.user.profile.birthday).toDate(); + $scope.user.statistic_profile.birthday = moment($scope.user.statistic_profile.birthday).toDate(); // the user subscription if (($scope.user.subscribed_plan != null) && ($scope.user.subscription != null)) { @@ -604,9 +604,10 @@ Application.Controllers.controller('NewAdminController', ['$state', '$scope', 'A // default admin profile let getGender; $scope.admin = { - profile_attributes: { + statistic_profile_attributes: { gender: true }, + profile_attributes: {}, invoicing_profile_attributes: {} }; @@ -650,8 +651,8 @@ Application.Controllers.controller('NewAdminController', ['$state', '$scope', 'A * @return {string} 'male' or 'female' */ return getGender = function (user) { - if (user.profile_attributes) { - if (user.profile_attributes.gender) { return 'male'; } else { return 'female'; } + if (user.statistic_profile_attributes) { + if (user.statistic_profile_attributes.gender) { return 'male'; } else { return 'female'; } } else { return 'other'; } }; } diff --git a/app/assets/javascripts/controllers/application.js.erb b/app/assets/javascripts/controllers/application.js.erb index 7e89ca850..7dde087de 100644 --- a/app/assets/javascripts/controllers/application.js.erb +++ b/app/assets/javascripts/controllers/application.js.erb @@ -86,7 +86,7 @@ Application.Controllers.controller('ApplicationController', ['$rootScope', '$sco return $uibModal.open({ templateUrl: '<%= asset_path "shared/signupModal.html" %>', size: 'md', - controller: ['$scope', '$uibModalInstance', 'Group', 'CustomAsset', function ($scope, $uibModalInstance, Group, CustomAsset) { + controller: ['$scope', '$uibModalInstance', 'Group', 'CustomAsset', 'growl', '_t', function ($scope, $uibModalInstance, Group, CustomAsset, growl, _t) { // default parameters for the date picker in the account creation modal $scope.datePicker = { format: Fablab.uibDateFormat, @@ -134,8 +134,13 @@ Application.Controllers.controller('ApplicationController', ['$rootScope', '$sco delete $scope.user.organization; // register on server return Auth.register($scope.user).then(function (user) { - // creation successful - $uibModalInstance.close(user); + if (user.id) { + // creation successful + $uibModalInstance.close(user); + } else { + // the user was not saved in database, something wrong occurred + growl.error(_t('unexpected_error_occurred')); + } }, function (error) { // creation failed... // restore organization param diff --git a/app/assets/javascripts/controllers/members.js b/app/assets/javascripts/controllers/members.js index 5d19e1167..6a5d44698 100644 --- a/app/assets/javascripts/controllers/members.js +++ b/app/assets/javascripts/controllers/members.js @@ -258,7 +258,7 @@ Application.Controllers.controller('EditProfileController', ['$scope', '$rootSco CSRF.setMetaTags(); // init the birth date to JS object - $scope.user.profile.birthday = moment($scope.user.profile.birthday).toDate(); + $scope.user.statistic_profile.birthday = moment($scope.user.statistic_profile.birthday).toDate(); if ($scope.activeProvider.providable_type !== 'DatabaseProvider') { $scope.preventPassword = true; diff --git a/app/assets/javascripts/controllers/plans.js.erb b/app/assets/javascripts/controllers/plans.js.erb index 37400e920..e14cdaefa 100644 --- a/app/assets/javascripts/controllers/plans.js.erb +++ b/app/assets/javascripts/controllers/plans.js.erb @@ -150,8 +150,8 @@ Application.Controllers.controller('PlansIndexController', ['$scope', '$rootScop * @return {string} 'male' or 'female' */ $scope.getGender = function (user) { - if (user && user.profile) { - if (user.profile.gender === 'true') { return 'male'; } else { return 'female'; } + if (user && user.statistic_profile) { + if (user.statistic_profile.gender === 'true') { return 'male'; } else { return 'female'; } } else { return 'other'; } }; diff --git a/app/assets/javascripts/controllers/profile.js.erb b/app/assets/javascripts/controllers/profile.js.erb index e0c9a3602..4c7e11048 100644 --- a/app/assets/javascripts/controllers/profile.js.erb +++ b/app/assets/javascripts/controllers/profile.js.erb @@ -206,7 +206,7 @@ Application.Controllers.controller('CompleteProfileController', ['$scope', '$roo CSRF.setMetaTags(); // init the birth date to JS object - $scope.user.profile.birthday = moment($scope.user.profile.birthday).toDate(); + $scope.user.statistic_profile.birthday = moment($scope.user.statistic_profile.birthday).toDate(); // bind fields protection with sso fields angular.forEach(activeProviderPromise.mapping, function (map) { $scope.preventField[map] = true; }); diff --git a/app/assets/templates/shared/_admin_form.html b/app/assets/templates/shared/_admin_form.html index df886e807..dae346c14 100644 --- a/app/assets/templates/shared/_admin_form.html +++ b/app/assets/templates/shared/_admin_form.html @@ -1,18 +1,18 @@
-
+
@@ -73,13 +73,13 @@ {{ 'email_is_required' }}
-
+
+ name="admin[statistic_profile_attributes][birthday]" + value="{{admin.statistic_profile_attributes.birthday | toIsoDate}}" />
diff --git a/app/assets/templates/shared/_member_form.html.erb b/app/assets/templates/shared/_member_form.html.erb index faf26868b..f83b1a50f 100644 --- a/app/assets/templates/shared/_member_form.html.erb +++ b/app/assets/templates/shared/_member_form.html.erb @@ -38,27 +38,27 @@
-
+
- {{ 'gender_is_required' }} + {{ 'gender_is_required' }}
@@ -200,25 +200,25 @@ {{ 'organization_address_is_required' }}
-
+
+ name="user[statistic_profile_attributes][birthday]" + value="{{user.statistic_profile.birthday | toIsoDate}}" />
- {{ 'date_of_birth_is_required' }} + {{ 'date_of_birth_is_required' }}
diff --git a/app/assets/templates/shared/signupModal.html.erb b/app/assets/templates/shared/signupModal.html.erb index eecde8559..e8433a206 100644 --- a/app/assets/templates/shared/signupModal.html.erb +++ b/app/assets/templates/shared/signupModal.html.erb @@ -12,14 +12,14 @@ @@ -182,7 +182,7 @@ User#tap) @@ -186,8 +184,8 @@ class User < ActiveRecord::Base end def need_completion? - profile.gender.nil? || profile.first_name.blank? || profile.last_name.blank? || username.blank? || - email.blank? || encrypted_password.blank? || group_id.nil? || profile.birthday.blank? || profile.phone.blank? + statistic_profile.gender.nil? || profile.first_name.blank? || profile.last_name.blank? || username.blank? || + email.blank? || encrypted_password.blank? || group_id.nil? || statistic_profile.birthday.blank? || profile.phone.blank? end ## Retrieve the requested data in the User and user's Profile tables @@ -248,9 +246,7 @@ class User < ActiveRecord::Base ## and remove the auth_token to mark his account as "migrated" def link_with_omniauth_provider(auth) active_provider = AuthProvider.active - if active_provider.strategy_name != auth.provider - raise SecurityError, 'The identity provider does not match the activated one' - end + raise SecurityError, 'The identity provider does not match the activated one' if active_provider.strategy_name != auth.provider if User.where(provider: auth.provider, uid: auth.uid).size.positive? raise DuplicateIndexError, "This #{active_provider.name} account is already linked to an existing user" @@ -374,7 +370,6 @@ class User < ActiveRecord::Base group_id_changed? or new_record? end - def update_invoicing_profile if invoicing_profile.nil? InvoicingProfile.create!( diff --git a/app/services/statistic_service.rb b/app/services/statistic_service.rb index 5b9c1c750..95bafb739 100644 --- a/app/services/statistic_service.rb +++ b/app/services/statistic_service.rb @@ -356,8 +356,8 @@ class StatisticService def user_info(user) { user_id: user.id, - gender: user.profile.str_gender, - age: user.profile.age, + gender: user.statistic_profile.str_gender, + age: user.statistic_profile.age, group: user.group ? user.group.slug : nil, email: user.email } diff --git a/app/services/user_service.rb b/app/services/user_service.rb new file mode 100644 index 000000000..3293c57c6 --- /dev/null +++ b/app/services/user_service.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +# helpers for managing users with special roles +class UserService + def self.create_partner(params) + generated_password = Devise.friendly_token.first(8) + group_id = Group.first.id + user = User.new( + email: params[:email], + username: "#{params[:first_name]}#{params[:last_name]}".parameterize, + password: generated_password, + password_confirmation: generated_password, + group_id: group_id + ) + user.build_profile( + first_name: params[:first_name], + last_name: params[:last_name], + phone: '0000000000' + ) + user.build_statistic_profile( + gender: true, + birthday: Time.now + ) + + saved = user.save + if saved + user.remove_role :member + user.add_role :partner + end + { saved: saved, user: user } + end + + def self.create_admin(params) + generated_password = Devise.friendly_token.first(8) + admin = User.new(params.merge(password: generated_password)) + admin.send :set_slug + + # we associate the admin group to prevent linking any other 'normal' group (which won't be deletable afterwards) + admin.group = Group.find_by(slug: 'admins') + + # if the authentication is made through an SSO, generate a migration token + admin.generate_auth_migration_token unless AuthProvider.active.providable_type == DatabaseProvider.name + + saved = admin.save(validate: false) + if saved + admin.send_confirmation_instructions + admin.add_role(:admin) + admin.remove_role(:member) + UsersMailer.delay.notify_user_account_created(admin, generated_password) + end + { saved: saved, user: admin } + end +end diff --git a/app/views/api/admins/_admin.json.jbuilder b/app/views/api/admins/_admin.json.jbuilder index 5f3f8bda9..250b3df84 100644 --- a/app/views/api/admins/_admin.json.jbuilder +++ b/app/views/api/admins/_admin.json.jbuilder @@ -3,8 +3,8 @@ json.profile_attributes do json.id admin.profile.id json.first_name admin.profile.first_name json.last_name admin.profile.last_name - json.gender admin.profile.gender - json.birthday admin.profile.birthday if admin.profile.birthday + json.gender admin.statistic_profile.gender + json.birthday admin.statistic_profile.birthday if admin.statistic_profile.birthday json.phone admin.profile.phone if admin.profile.user_avatar json.user_avatar do diff --git a/app/views/api/members/_member.json.jbuilder b/app/views/api/members/_member.json.jbuilder index 61f25cc0d..f3970073a 100644 --- a/app/views/api/members/_member.json.jbuilder +++ b/app/views/api/members/_member.json.jbuilder @@ -1,3 +1,5 @@ +# frozen_string_literal: true + json.extract! member, :id, :username, :email, :group_id json.role member.roles.first.name json.name member.profile.full_name @@ -13,8 +15,6 @@ json.profile do end json.first_name member.profile.first_name json.last_name member.profile.last_name - json.gender member.profile.gender.to_s - json.birthday member.profile.birthday.to_date.iso8601 if member.profile.birthday json.interest member.profile.interest json.software_mastered member.profile.software_mastered json.phone member.profile.phone @@ -46,6 +46,11 @@ json.invoicing_profile do end end +json.statistic_profile do + json.gender member.statistic_profile.gender.to_s + json.birthday member.statistic_profile&.birthday&.to_date&.iso8601 +end + if member.subscribed_plan json.subscribed_plan do json.partial! 'api/shared/plan', plan: member.subscribed_plan diff --git a/app/views/api/members/index.json.jbuilder b/app/views/api/members/index.json.jbuilder index f3da831aa..86fedaa75 100644 --- a/app/views/api/members/index.json.jbuilder +++ b/app/views/api/members/index.json.jbuilder @@ -1,4 +1,6 @@ -user_is_admin = (current_user and current_user.admin?) +# frozen_string_literal: true + +user_is_admin = current_user&.admin? max_members = @query.except(:offset, :limit, :order).count json.array!(@members) do |member| @@ -10,55 +12,77 @@ json.array!(@members) do |member| json.email member.email if current_user json.first_name member.profile.first_name json.last_name member.profile.last_name - json.profile do - json.user_avatar do - json.id member.profile.user_avatar.id - json.attachment_url member.profile.user_avatar.attachment_url - end if member.profile.user_avatar - json.first_name member.profile.first_name - json.last_name member.profile.last_name - json.gender member.profile.gender.to_s - if user_is_admin - json.phone member.profile.phone - json.birthday member.profile.birthday.iso8601 if member.profile.birthday - end - end if attribute_requested?(@requested_attributes, 'profile') json.need_completion member.need_completion? json.group_id member.group_id - json.group do - json.id member.group.id - json.name member.group.name - end if attribute_requested?(@requested_attributes, 'group') and member.group - if user_is_admin - json.subscribed_plan do - json.partial! 'api/shared/plan', plan: member.subscribed_plan - end if member.subscribed_plan - json.subscription do - json.id member.subscription.id - json.expired_at member.subscription.expired_at.iso8601 - json.canceled_at member.subscription.canceled_at.iso8601 if member.subscription.canceled_at - json.stripe member.subscription.stp_subscription_id.present? - json.plan do - json.id member.subscription.plan.id - json.name member.subscription.plan.name - json.interval member.subscription.plan.interval - json.amount member.subscription.plan.amount ? (member.subscription.plan.amount / 100.0) : 0 + if attribute_requested?(@requested_attributes, 'profile') + json.profile do + if member.profile.user_avatar + json.user_avatar do + json.id member.profile.user_avatar.id + json.attachment_url member.profile.user_avatar.attachment_url + end end - end if member.subscription - end if attribute_requested?(@requested_attributes, 'subscription') + json.first_name member.profile.first_name + json.last_name member.profile.last_name + json.gender member.statistic_profile.gender.to_s + end + if user_is_admin + json.statistic_profile do + json.phone member.statistic_profile.phone + json.birthday member.statistic_profile&.birthday&.iso8601 + end + end + end - json.training_credits member.training_credits do |tc| - json.training_id tc.creditable_id - end if attribute_requested?(@requested_attributes, 'credits') or attribute_requested?(@requested_attributes, 'training_credits') + if attribute_requested?(@requested_attributes, 'group') && member.group + json.group do + json.id member.group.id + json.name member.group.name + end + end - json.machine_credits member.machine_credits do |mc| - json.machine_id mc.creditable_id - json.hours_used mc.users_credits.find_by(user_id: member.id).hours_used - end if attribute_requested?(@requested_attributes, 'credits') or attribute_requested?(@requested_attributes, 'machine_credits') + if attribute_requested?(@requested_attributes, 'subscription') + if user_is_admin + if member.subscribed_plan + json.subscribed_plan do + json.partial! 'api/shared/plan', plan: member.subscribed_plan + end + end + if member.subscription + json.subscription do + json.id member.subscription.id + json.expired_at member.subscription.expired_at.iso8601 + json.canceled_at member.subscription&.canceled_at&.iso8601 + json.stripe member.subscription.stp_subscription_id.present? + json.plan do + json.id member.subscription.plan.id + json.name member.subscription.plan.name + json.interval member.subscription.plan.interval + json.amount member.subscription.plan.amount ? (member.subscription.plan.amount / 100.0) : 0 + end + end + end + end + end - json.tags member.tags do |t| - json.id t.id - json.name t.name - end if attribute_requested?(@requested_attributes, 'tags') + if attribute_requested?(@requested_attributes, 'credits') || attribute_requested?(@requested_attributes, 'training_credits') + json.training_credits member.training_credits do |tc| + json.training_id tc.creditable_id + end + end + + if attribute_requested?(@requested_attributes, 'credits') || attribute_requested?(@requested_attributes, 'machine_credits') + json.machine_credits member.machine_credits do |mc| + json.machine_id mc.creditable_id + json.hours_used mc.users_credits.find_by(user_id: member.id).hours_used + end + end + + if attribute_requested?(@requested_attributes, 'tags') + json.tags member.tags do |t| + json.id t.id + json.name t.name + end + end end diff --git a/app/views/api/members/show.json.jbuilder b/app/views/api/members/show.json.jbuilder index 403c99abd..9aa41c8db 100644 --- a/app/views/api/members/show.json.jbuilder +++ b/app/views/api/members/show.json.jbuilder @@ -39,13 +39,15 @@ json.all_projects @member.all_projects do |project| json.first_name pu.user.profile.first_name json.last_name pu.user.profile.last_name json.full_name pu.user.profile.full_name - json.user_avatar do - json.id pu.user.profile.user_avatar.id - json.attachment_url pu.user.profile.user_avatar.attachment_url - end if pu.user.profile.user_avatar + if pu.user.profile.user_avatar + json.user_avatar do + json.id pu.user.profile.user_avatar.id + json.attachment_url pu.user.profile.user_avatar.attachment_url + end + end json.username pu.user.username json.is_valid pu.is_valid - json.valid_token pu.valid_token if !pu.is_valid and @member == pu.user + json.valid_token pu.valid_token if !pu.is_valid && @member == pu.user end end end diff --git a/app/views/api/users/create.json.jbuilder b/app/views/api/users/create.json.jbuilder index be7c585e1..2e9ed138b 100644 --- a/app/views/api/users/create.json.jbuilder +++ b/app/views/api/users/create.json.jbuilder @@ -1,2 +1,4 @@ - json.extract! @user, :id, :email, :first_name, :last_name - json.name "#{@user.first_name} #{@user.last_name}" +# frozen_string_literal: true + +json.extract! @user, :id, :email, :first_name, :last_name +json.name @user.profile.full_name diff --git a/app/views/api/users/index.json.jbuilder b/app/views/api/users/index.json.jbuilder index 76cb8c963..d40b43c30 100644 --- a/app/views/api/users/index.json.jbuilder +++ b/app/views/api/users/index.json.jbuilder @@ -1,4 +1,4 @@ json.users @users do |user| json.extract! user, :id, :email, :first_name, :last_name - json.name "#{user.first_name} #{user.last_name}" + json.name user.profile.full_name end diff --git a/app/views/archive/_accounting.json.jbuilder b/app/views/archive/_accounting.json.jbuilder index a5435ee4b..f0315858c 100644 --- a/app/views/archive/_accounting.json.jbuilder +++ b/app/views/archive/_accounting.json.jbuilder @@ -14,10 +14,14 @@ json.invoices do end end json.user do - json.extract! invoice[:invoice].user, :id, :email, :created_at - json.profile do - json.extract! invoice[:invoice].user.profile, :id, :first_name, :last_name, :birthday, :phone - json.gender invoice[:invoice].user.profile.gender ? 'male' : 'female' + json.extract! invoice[:invoice].invoicing_profile, :user_id, :email, :first_name, :last_name + json.address invoice[:invoice].invoicing_profile&.address&.address + json.invoicing_profile_id invoice[:invoice].invoicing_profile.id + if invoice[:invoice].invoicing_profile.organization + json.organization do + json.extract! invoice[:invoice].invoicing_profile.organization, :name, :id + json.address invoice[:invoice].invoicing_profile.organization&.address&.address + end end end json.invoice_items invoice[:invoice].invoice_items do |item| diff --git a/config/locales/app.public.en.yml b/config/locales/app.public.en.yml index 12516262e..938ceb2d6 100644 --- a/config/locales/app.public.en.yml +++ b/config/locales/app.public.en.yml @@ -86,6 +86,7 @@ en: i_ve_read_and_i_accept_: "I've read and I accept" _the_fablab_policy: "the FabLab policy" field_required: "Field required" + unexpected_error_occurred: "An unexpected error occurred. Please try again later." # password modification modal change_your_password: "Change your password" diff --git a/config/locales/app.public.es.yml b/config/locales/app.public.es.yml index c0230e2c0..19868ee56 100644 --- a/config/locales/app.public.es.yml +++ b/config/locales/app.public.es.yml @@ -84,7 +84,8 @@ es: i_accept_to_receive_information_from_the_fablab: "Acepto recibir información del FabLab" i_ve_read_and_i_accept_: "He leido y acepto" _the_fablab_policy: "la política de FabLab" - field_required: "Field required" #translation_missing + field_required: "Field required" # translation missing + unexpected_error_occurred: "An unexpected error occurred. Please try again later." # translation missing # password modification modal change_your_password: "Cambiar contraseña" diff --git a/config/locales/app.public.fr.yml b/config/locales/app.public.fr.yml index 3a423ba83..4b5cb1b5f 100644 --- a/config/locales/app.public.fr.yml +++ b/config/locales/app.public.fr.yml @@ -86,6 +86,7 @@ fr: i_ve_read_and_i_accept_: "J'ai lu et j'accepte" _the_fablab_policy: "la charte d'utilisation du Fab Lab" field_required: "Champ requis" + unexpected_error_occurred: "Une erreur inattendue s'est produite. Veuillez réessayer ultérieurement." # fenêtre de changement de mot de passe change_your_password: "Modifier votre mot de passe" diff --git a/config/locales/app.public.pt.yml b/config/locales/app.public.pt.yml index d408e4e84..811da46b6 100755 --- a/config/locales/app.public.pt.yml +++ b/config/locales/app.public.pt.yml @@ -85,7 +85,8 @@ pt: i_accept_to_receive_information_from_the_fablab: "Eu aceito receber informações do FabLab" i_ve_read_and_i_accept_: "Eu li e aceito" _the_fablab_policy: "a política do FabLab" - field_required: "Field required" #translation_missing + field_required: "Field required" # translation missing + unexpected_error_occurred: "An unexpected error occurred. Please try again later." # translation missing # password modification modal change_your_password: "Mudar sua senha" diff --git a/config/routes.rb b/config/routes.rb index 73d0b2eb5..e036624e5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,12 +7,14 @@ Rails.application.routes.draw do if AuthProvider.active.providable_type == DatabaseProvider.name # with local authentification we do not use omniAuth so we must differentiate the config - devise_for :users, controllers: {registrations: 'registrations', sessions: 'sessions', - confirmations: 'confirmations', passwords: 'passwords'} + devise_for :users, controllers: { + registrations: 'registrations', sessions: 'sessions', confirmations: 'confirmations', passwords: 'passwords' + } else - devise_for :users, controllers: {registrations: 'registrations', sessions: 'sessions', - confirmations: 'confirmations', passwords: 'passwords', - :omniauth_callbacks => 'users/omniauth_callbacks'} + devise_for :users, controllers: { + registrations: 'registrations', sessions: 'sessions', confirmations: 'confirmations', passwords: 'passwords', + omniauth_callbacks: 'users/omniauth_callbacks' + } end diff --git a/db/migrate/20190603151142_feed_statistic_profile.rb b/db/migrate/20190603151142_feed_statistic_profile.rb deleted file mode 100644 index 71ceafac0..000000000 --- a/db/migrate/20190603151142_feed_statistic_profile.rb +++ /dev/null @@ -1,15 +0,0 @@ -class FeedStatisticProfile < ActiveRecord::Migration - def change - User.all.each do |u| - p = u.profile - puts "WARNING: User #{u.id} has no profile" and next unless p - - StatisticProfile.create!( - user: u, - group: u.group, - gender: p.gender, - birthday: p.birthday - ) - end - end -end diff --git a/db/migrate/20190603151142_migrate_profile_to_statistic_profile.rb b/db/migrate/20190603151142_migrate_profile_to_statistic_profile.rb new file mode 100644 index 000000000..f08cb22be --- /dev/null +++ b/db/migrate/20190603151142_migrate_profile_to_statistic_profile.rb @@ -0,0 +1,27 @@ +class MigrateProfileToStatisticProfile < ActiveRecord::Migration + def up + User.all.each do |u| + p = u.profile + puts "WARNING: User #{u.id} has no profile" and next unless p + + StatisticProfile.create!( + user: u, + group: u.group, + gender: p.gender, + birthday: p.birthday + ) + end + end + + def down + StatisticProfile.all.each do |sp| + p = sp.user.profile + puts "WARNING: User #{sp.user_id} has no profile" and next unless p + + p.update_attributes( + gender: sp.gender, + birthday: sp.birthday + ) + end + end +end diff --git a/db/migrate/20190604075717_remove_gender_birthday_from_profile.rb b/db/migrate/20190604075717_remove_gender_birthday_from_profile.rb new file mode 100644 index 000000000..2e673b311 --- /dev/null +++ b/db/migrate/20190604075717_remove_gender_birthday_from_profile.rb @@ -0,0 +1,6 @@ +class RemoveGenderBirthdayFromProfile < ActiveRecord::Migration + def change + remove_column :profiles, :gender, :boolean + remove_column :profiles, :birthday, :date + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e2c1d040..e6602899f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,12 +11,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190603141109) do +ActiveRecord::Schema.define(version: 20190604075717) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - enable_extension "pg_trgm" enable_extension "unaccent" + enable_extension "pg_trgm" create_table "abuses", force: :cascade do |t| t.integer "signaled_id" @@ -44,14 +44,14 @@ ActiveRecord::Schema.define(version: 20190603141109) do end create_table "addresses", force: :cascade do |t| - t.string "address", limit: 255 - t.string "street_number", limit: 255 - t.string "route", limit: 255 - t.string "locality", limit: 255 - t.string "country", limit: 255 - t.string "postal_code", limit: 255 + t.string "address" + t.string "street_number" + t.string "route" + t.string "locality" + t.string "country" + t.string "postal_code" t.integer "placeable_id" - t.string "placeable_type", limit: 255 + t.string "placeable_type" t.datetime "created_at" t.datetime "updated_at" end @@ -67,9 +67,9 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "assets", force: :cascade do |t| t.integer "viewable_id" - t.string "viewable_type", limit: 255 - t.string "attachment", limit: 255 - t.string "type", limit: 255 + t.string "viewable_type" + t.string "attachment" + t.string "type" t.datetime "created_at" t.datetime "updated_at" end @@ -86,12 +86,12 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "availabilities", force: :cascade do |t| t.datetime "start_at" t.datetime "end_at" - t.string "available_type", limit: 255 + t.string "available_type" t.datetime "created_at" t.datetime "updated_at" t.integer "nb_total_places" - t.boolean "destroying", default: false - t.boolean "lock", default: false + t.boolean "destroying", default: false + t.boolean "lock", default: false end create_table "availability_tags", force: :cascade do |t| @@ -105,7 +105,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "availability_tags", ["tag_id"], name: "index_availability_tags_on_tag_id", using: :btree create_table "categories", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name" t.datetime "created_at" t.datetime "updated_at" t.string "slug" @@ -114,7 +114,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "categories", ["slug"], name: "index_categories_on_slug", unique: true, using: :btree create_table "components", force: :cascade do |t| - t.string "name", limit: 255, null: false + t.string "name", null: false end create_table "coupons", force: :cascade do |t| @@ -132,7 +132,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "credits", force: :cascade do |t| t.integer "creditable_id" - t.string "creditable_type", limit: 255 + t.string "creditable_type" t.integer "plan_id" t.integer "hours" t.datetime "created_at" @@ -173,7 +173,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "event_themes", ["slug"], name: "index_event_themes_on_slug", unique: true, using: :btree create_table "events", force: :cascade do |t| - t.string "title", limit: 255 + t.string "title" t.text "description" t.datetime "created_at" t.datetime "updated_at" @@ -211,10 +211,10 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "exports", ["user_id"], name: "index_exports_on_user_id", using: :btree create_table "friendly_id_slugs", force: :cascade do |t| - t.string "slug", limit: 255, null: false - t.integer "sluggable_id", null: false + t.string "slug", null: false + t.integer "sluggable_id", null: false t.string "sluggable_type", limit: 50 - t.string "scope", limit: 255 + t.string "scope" t.datetime "created_at" end @@ -224,10 +224,10 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree create_table "groups", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name" t.datetime "created_at" t.datetime "updated_at" - t.string "slug", limit: 255 + t.string "slug" t.boolean "disabled" end @@ -247,7 +247,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "invoice_items", force: :cascade do |t| t.integer "invoice_id" - t.string "stp_invoice_item_id", limit: 255 + t.string "stp_invoice_item_id" t.integer "amount" t.datetime "created_at" t.datetime "updated_at" @@ -261,16 +261,16 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "invoices", force: :cascade do |t| t.integer "invoiced_id" - t.string "invoiced_type", limit: 255 - t.string "stp_invoice_id", limit: 255 + t.string "invoiced_type" + t.string "stp_invoice_id" t.integer "total" t.datetime "created_at" t.datetime "updated_at" - t.string "reference", limit: 255 - t.string "avoir_mode", limit: 255 + t.string "reference" + t.string "avoir_mode" t.datetime "avoir_date" t.integer "invoice_id" - t.string "type", limit: 255 + t.string "type" t.boolean "subscription_to_expire" t.text "description" t.integer "wallet_amount" @@ -299,17 +299,17 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "invoicing_profiles", ["user_id"], name: "index_invoicing_profiles_on_user_id", using: :btree create_table "licences", force: :cascade do |t| - t.string "name", limit: 255, null: false + t.string "name", null: false t.text "description" end create_table "machines", force: :cascade do |t| - t.string "name", limit: 255, null: false + t.string "name", null: false t.text "description" t.text "spec" t.datetime "created_at" t.datetime "updated_at" - t.string "slug", limit: 255 + t.string "slug" t.boolean "disabled" end @@ -326,14 +326,14 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "notifications", force: :cascade do |t| t.integer "receiver_id" t.integer "attached_object_id" - t.string "attached_object_type", limit: 255 + t.string "attached_object_type" t.integer "notification_type_id" - t.boolean "is_read", default: false + t.boolean "is_read", default: false t.datetime "created_at" t.datetime "updated_at" t.string "receiver_type" - t.boolean "is_send", default: false - t.jsonb "meta_data", default: {} + t.boolean "is_send", default: false + t.jsonb "meta_data", default: {} end add_index "notifications", ["notification_type_id"], name: "index_notifications_on_notification_type_id", using: :btree @@ -402,20 +402,20 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "organizations", ["invoicing_profile_id"], name: "index_organizations_on_invoicing_profile_id", using: :btree create_table "plans", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name" t.integer "amount" - t.string "interval", limit: 255 + t.string "interval" t.integer "group_id" - t.string "stp_plan_id", limit: 255 + t.string "stp_plan_id" t.datetime "created_at" t.datetime "updated_at" - t.integer "training_credit_nb", default: 0 - t.boolean "is_rolling", default: true + t.integer "training_credit_nb", default: 0 + t.boolean "is_rolling", default: true t.text "description" t.string "type" t.string "base_name" - t.integer "ui_weight", default: 0 - t.integer "interval_count", default: 1 + t.integer "ui_weight", default: 0 + t.integer "interval_count", default: 1 t.string "slug" t.boolean "disabled" end @@ -445,11 +445,9 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "profiles", force: :cascade do |t| t.integer "user_id" - t.string "first_name", limit: 255 - t.string "last_name", limit: 255 - t.boolean "gender" - t.date "birthday" - t.string "phone", limit: 255 + t.string "first_name" + t.string "last_name" + t.string "phone" t.text "interest" t.text "software_mastered" t.datetime "created_at" @@ -479,7 +477,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do t.integer "project_id" t.datetime "created_at" t.datetime "updated_at" - t.string "title", limit: 255 + t.string "title" t.integer "step_nb" end @@ -490,27 +488,27 @@ ActiveRecord::Schema.define(version: 20190603141109) do t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" - t.boolean "is_valid", default: false - t.string "valid_token", limit: 255 + t.boolean "is_valid", default: false + t.string "valid_token" end add_index "project_users", ["project_id"], name: "index_project_users_on_project_id", using: :btree add_index "project_users", ["user_id"], name: "index_project_users_on_user_id", using: :btree create_table "projects", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name" t.text "description" t.datetime "created_at" t.datetime "updated_at" t.integer "author_id" t.text "tags" t.integer "licence_id" - t.string "state", limit: 255 - t.string "slug", limit: 255 + t.string "state" + t.string "slug" t.datetime "published_at" end - add_index "projects", ["slug"], name: "index_projects_on_slug", using: :btree + add_index "projects", ["slug"], name: "index_projects_on_slug", unique: true, using: :btree create_table "projects_components", force: :cascade do |t| t.integer "project_id" @@ -550,19 +548,19 @@ ActiveRecord::Schema.define(version: 20190603141109) do t.datetime "created_at" t.datetime "updated_at" t.integer "reservable_id" - t.string "reservable_type", limit: 255 - t.string "stp_invoice_id", limit: 255 + t.string "reservable_type" + t.string "stp_invoice_id" t.integer "nb_reserve_places" end - add_index "reservations", ["reservable_id", "reservable_type"], name: "index_reservations_on_reservable_id_and_reservable_type", using: :btree + add_index "reservations", ["reservable_type", "reservable_id"], name: "index_reservations_on_reservable_type_and_reservable_id", using: :btree add_index "reservations", ["stp_invoice_id"], name: "index_reservations_on_stp_invoice_id", using: :btree add_index "reservations", ["user_id"], name: "index_reservations_on_user_id", using: :btree create_table "roles", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name" t.integer "resource_id" - t.string "resource_type", limit: 255 + t.string "resource_type" t.datetime "created_at" t.datetime "updated_at" end @@ -636,18 +634,18 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "statistic_fields", force: :cascade do |t| t.integer "statistic_index_id" - t.string "key", limit: 255 - t.string "label", limit: 255 + t.string "key" + t.string "label" t.datetime "created_at" t.datetime "updated_at" - t.string "data_type", limit: 255 + t.string "data_type" end add_index "statistic_fields", ["statistic_index_id"], name: "index_statistic_fields_on_statistic_index_id", using: :btree create_table "statistic_graphs", force: :cascade do |t| t.integer "statistic_index_id" - t.string "chart_type", limit: 255 + t.string "chart_type" t.integer "limit" t.datetime "created_at" t.datetime "updated_at" @@ -656,17 +654,27 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "statistic_graphs", ["statistic_index_id"], name: "index_statistic_graphs_on_statistic_index_id", using: :btree create_table "statistic_indices", force: :cascade do |t| - t.string "es_type_key", limit: 255 - t.string "label", limit: 255 + t.string "es_type_key" + t.string "label" t.datetime "created_at" t.datetime "updated_at" - t.boolean "table", default: true - t.boolean "ca", default: true + t.boolean "table", default: true + t.boolean "ca", default: true end + create_table "statistic_profiles", force: :cascade do |t| + t.boolean "gender" + t.date "birthday" + t.integer "group_id" + t.integer "user_id" + end + + add_index "statistic_profiles", ["group_id"], name: "index_statistic_profiles_on_group_id", using: :btree + add_index "statistic_profiles", ["user_id"], name: "index_statistic_profiles_on_user_id", using: :btree + create_table "statistic_sub_types", force: :cascade do |t| - t.string "key", limit: 255 - t.string "label", limit: 255 + t.string "key" + t.string "label" t.datetime "created_at" t.datetime "updated_at" end @@ -683,8 +691,8 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "statistic_types", force: :cascade do |t| t.integer "statistic_index_id" - t.string "key", limit: 255 - t.string "label", limit: 255 + t.string "key" + t.string "label" t.boolean "graph" t.datetime "created_at" t.datetime "updated_at" @@ -702,7 +710,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do create_table "subscriptions", force: :cascade do |t| t.integer "plan_id" t.integer "user_id" - t.string "stp_subscription_id", limit: 255 + t.string "stp_subscription_id" t.datetime "created_at" t.datetime "updated_at" t.datetime "expiration_date" @@ -721,7 +729,7 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree create_table "themes", force: :cascade do |t| - t.string "name", limit: 255, null: false + t.string "name", null: false end create_table "tickets", force: :cascade do |t| @@ -736,13 +744,13 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "tickets", ["reservation_id"], name: "index_tickets_on_reservation_id", using: :btree create_table "trainings", force: :cascade do |t| - t.string "name", limit: 255 + t.string "name" t.datetime "created_at" t.datetime "updated_at" t.integer "nb_total_places" - t.string "slug", limit: 255 + t.string "slug" t.text "description" - t.boolean "public_page", default: true + t.boolean "public_page", default: true t.boolean "disabled" end @@ -798,31 +806,31 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_index "user_trainings", ["user_id"], name: "index_user_trainings_on_user_id", using: :btree create_table "users", force: :cascade do |t| - t.string "email", limit: 255, default: "", null: false - t.string "encrypted_password", limit: 255, default: "", null: false - t.string "reset_password_token", limit: 255 + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" - t.string "current_sign_in_ip", limit: 255 - t.string "last_sign_in_ip", limit: 255 - t.string "confirmation_token", limit: 255 + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" - t.string "unconfirmed_email", limit: 255 - t.integer "failed_attempts", default: 0, null: false - t.string "unlock_token", limit: 255 + t.string "unconfirmed_email" + t.integer "failed_attempts", default: 0, null: false + t.string "unlock_token" t.datetime "locked_at" t.datetime "created_at" t.datetime "updated_at" - t.boolean "is_allow_contact", default: true + t.boolean "is_allow_contact", default: true t.integer "group_id" - t.string "stp_customer_id", limit: 255 - t.string "username", limit: 255 - t.string "slug", limit: 255 - t.boolean "is_active", default: true + t.string "stp_customer_id" + t.string "username" + t.string "slug" + t.boolean "is_active", default: true t.string "provider" t.string "uid" t.string "auth_token" @@ -911,6 +919,8 @@ ActiveRecord::Schema.define(version: 20190603141109) do add_foreign_key "spaces_availabilities", "availabilities" add_foreign_key "spaces_availabilities", "spaces" add_foreign_key "statistic_custom_aggregations", "statistic_types" + add_foreign_key "statistic_profiles", "groups" + add_foreign_key "statistic_profiles", "users" add_foreign_key "tickets", "event_price_categories" add_foreign_key "tickets", "reservations" add_foreign_key "user_tags", "tags" diff --git a/db/seeds.rb b/db/seeds.rb index 39bf8bc32..7656142a6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -89,7 +89,8 @@ Group.create! name: I18n.t('group.admins'), slug: 'admins' unless Group.find_by( if Role.where(name: 'admin').joins(:users).count.zero? admin = User.new(username: 'admin', email: ENV['ADMIN_EMAIL'], password: ENV['ADMIN_PASSWORD'], password_confirmation: Rails.application.secrets.admin_password, group_id: Group.find_by(slug: 'admins').id, - profile_attributes: { first_name: 'admin', last_name: 'admin', gender: true, phone: '0123456789', birthday: Time.now }) + profile_attributes: { first_name: 'admin', last_name: 'admin', phone: '0123456789' }, + statistic_profile_attributes: { gender: true, birthday: Time.now }) admin.add_role 'admin' admin.save! end diff --git a/test/fixtures/profiles.yml b/test/fixtures/profiles.yml index 610e272d5..b63c21534 100644 --- a/test/fixtures/profiles.yml +++ b/test/fixtures/profiles.yml @@ -4,8 +4,6 @@ profile_1: user_id: 1 first_name: admin last_name: admin - gender: true - birthday: 2016-04-04 phone: 0123456789 interest: software_mastered: @@ -17,8 +15,6 @@ profile_2: user_id: 2 first_name: Jean last_name: Dupont - gender: true - birthday: 1975-06-07 phone: '0214321420' interest: 3D printers software_mastered: autocad @@ -30,8 +26,6 @@ profile_4: user_id: 4 first_name: Kevin last_name: Dumas - gender: true - birthday: 1998-06-05 phone: 0446124793 interest: '' software_mastered: solidworks @@ -43,8 +37,6 @@ profile_5: user_id: 5 first_name: Vanessa last_name: Lonchamp - gender: false - birthday: 1994-05-03 phone: 0233412482 interest: vélo, natation software_mastered: '' @@ -56,8 +48,6 @@ profile_6: user_id: 6 first_name: Gilbert last_name: Partenaire - gender: true - birthday: 2016-04-04 phone: '0000000000' interest: software_mastered: @@ -69,8 +59,6 @@ profile_3: user_id: 3 first_name: Paulette last_name: Durand - gender: false - birthday: 1949-06-18 phone: '0474264261' interest: '' software_mastered: '' @@ -82,8 +70,6 @@ profile_7: user_id: 7 first_name: Lucile last_name: Seguin - gender: false - birthday: 1969-02-03 phone: '0241853679' interest: software_mastered: diff --git a/test/fixtures/statistic_profiles.yml b/test/fixtures/statistic_profiles.yml new file mode 100644 index 000000000..f1d09da75 --- /dev/null +++ b/test/fixtures/statistic_profiles.yml @@ -0,0 +1,48 @@ +admin: + id: 1 + user_id: 1 + gender: true + birthday: 2016-04-04 + group_id: 3 + +jdupont: + id: 2 + user_id: 2 + gender: true + birthday: 1975-06-07 + group_id: 1 + +kdumas: + id: 4 + user_id: 4 + gender: true + birthday: 1998-06-05 + group_id: 2 + +vlonchamp: + id: 5 + user_id: 5 + gender: false + birthday: 1994-05-03 + group_id: 2 + +gpartenaire: + id: 6 + user_id: 6 + gender: true + birthday: 2016-04-04 + group_id: 1 + +pdurand: + id: 3 + user_id: 3 + gender: false + birthday: 1949-06-18 + group_id: 1 + +lseguin: + id: 7 + user_id: 7 + gender: false + birthday: 1969-02-03 + group_id: 1 diff --git a/test/integration/admins_test.rb b/test/integration/admins_test.rb index 458f56f3a..27896002a 100644 --- a/test/integration/admins_test.rb +++ b/test/integration/admins_test.rb @@ -23,14 +23,16 @@ class AdminsTest < ActionDispatch::IntegrationTest profile_attributes: { first_name: 'Gérard', last_name: 'Lepower', - gender: true, - birthday: '1999-09-19', - phone: '0547124852', + phone: '0547124852' }, invoicing_profile_attributes: { address_attributes: { address: '6 Avenue Henri de Bournazel, 19000 Tulle' } + }, + statistic_profile_attributes: { + gender: true, + birthday: '1999-09-19' } } }.to_json, diff --git a/test/integration/members/as_admin_test.rb b/test/integration/members/as_admin_test.rb index 01ac9f914..86935f572 100644 --- a/test/integration/members/as_admin_test.rb +++ b/test/integration/members/as_admin_test.rb @@ -20,16 +20,18 @@ class MembersTest < ActionDispatch::IntegrationTest email: email, group_id: group_id, profile_attributes: { - gender: true, last_name: 'Dubois', first_name: 'Robert', - birthday: '2018-02-08', phone: '0485232145' }, invoicing_profile_attributes: { address_attributes: { address: '21 grand rue, 73110 Bourget-en-Huile' } + }, + statistic_profile_attributes: { + gender: true, + birthday: '2018-02-08' } } }.to_json, default_headers end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 7426e4f3f..88f34b1a7 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,9 +1,15 @@ +# frozen_string_literal: true + require 'test_helper' class UserTest < ActiveSupport::TestCase - test "must create a wallet after create user" do + test 'must create wallet and profiles after create user' do u = User.create(username: 'user', email: 'userwallet@fabmanager.com', password: 'testpassword', password_confirmation: 'testpassword', - profile_attributes: {first_name: 'user', last_name: 'wallet', gender: true, birthday: 18.years.ago, phone: '0123456789'} ) + profile_attributes: { first_name: 'user', last_name: 'wallet', phone: '0123456789' }, + statistic_profile_attributes: { gender: true, birthday: 18.years.ago }) assert u.wallet.present? + assert u.profile.present? + assert u.invoicing_profile.present? + assert u.statistic_profile.present? end end