From a53c6e80e7af2bc893d04b58317d9b843a25a5d0 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 12 Jun 2019 12:22:38 +0200 Subject: [PATCH] move invoice->operator to invoicing_profile + minor fixes to handle user deletion --- CHANGELOG.md | 2 ++ .../controllers/admin/invoices.js.erb | 16 +++++++++++---- .../trainings/validTrainingModal.html.erb | 7 ++++--- app/assets/templates/projects/show.html.erb | 7 ++++++- .../api/reservations_controller.rb | 2 +- .../api/subscriptions_controller.rb | 4 ++-- app/models/invoice.rb | 2 +- app/models/invoicing_profile.rb | 2 ++ app/models/project/openlab_sync.rb | 2 +- app/models/reservation.rb | 16 +++++++++------ app/models/subscription.rb | 20 +++++++++---------- app/models/user.rb | 14 +++++++++++-- app/services/availabilities/status_service.rb | 2 +- app/services/reservations/reserve.rb | 10 +++++----- app/services/subscriptions/subscribe.rb | 14 ++++++------- ...in_member_create_reservation.json.jbuilder | 2 +- ...notify_admin_subscribed_plan.json.jbuilder | 2 +- config/locales/app.admin.en.yml | 1 + config/locales/app.admin.es.yml | 1 + config/locales/app.admin.fr.yml | 3 ++- config/locales/app.admin.pt.yml | 1 + config/locales/app.public.en.yml | 1 + config/locales/app.public.es.yml | 1 + config/locales/app.public.fr.yml | 1 + config/locales/app.public.pt.yml | 1 + .../20140703231208_add_username_to_user.rb | 4 +--- ...0190521122429_create_invoicing_profiles.rb | 3 +++ ...0_migrate_invoice_to_invoicing_profile.rb} | 7 ++++++- .../20190528140012_remove_user_id_columns.rb | 1 + db/schema.rb | 4 ++-- lib/tasks/fablab/setup.rake | 10 ++++++---- test/fixtures/invoices.yml | 20 +++++++++---------- 32 files changed, 116 insertions(+), 67 deletions(-) rename db/migrate/{20190522115230_migrate_user_to_invoicing_profile.rb => 20190522115230_migrate_invoice_to_invoicing_profile.rb} (90%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82eacbf1a..0b7474335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - Improved translations syntax according to YML specifications - Refactored some Ruby code to match style guide - [TODO DEPLOY] `rake fablab:fix:users_group_ids` +- [TODO DEPLOY] `rake db:migrate` +- [TODO DEPLOY] `rake fablab:setup:migrate_pdf_invoices_folders` ## v3.1.2 2019 May 27 diff --git a/app/assets/javascripts/controllers/admin/invoices.js.erb b/app/assets/javascripts/controllers/admin/invoices.js.erb index 6ce3aa390..6e87bc394 100644 --- a/app/assets/javascripts/controllers/admin/invoices.js.erb +++ b/app/assets/javascripts/controllers/admin/invoices.js.erb @@ -397,8 +397,11 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I * Full reload the results list */ $scope.handleFilterChange = function () { - resetSearchInvoice(); - return invoiceSearch(); + if (searchTimeout) clearTimeout(searchTimeout); + searchTimeout = setTimeout(function() { + resetSearchInvoice(); + invoiceSearch(); + }, 300); }; /** @@ -407,7 +410,7 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I */ $scope.showNextInvoices = function () { $scope.page += 1; - return invoiceSearch(true); + invoiceSearch(true); }; /** @@ -484,6 +487,11 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I }); }; + /** + * Will temporize the search query to prevent overloading the API + */ + var searchTimeout = null; + /** * Output the given integer with leading zeros. If the given value is longer than the given * length, it will be truncated. @@ -519,7 +527,7 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I /** * Run a search query with the current parameters set concerning invoices, then affect or concat the results * to $scope.invoices - * @param concat {boolean} if true, the result will be append to $scope.invoices instead of being affected + * @param [concat] {boolean} if true, the result will be append to $scope.invoices instead of being affected */ var invoiceSearch = function (concat) { Invoice.list({ diff --git a/app/assets/templates/admin/trainings/validTrainingModal.html.erb b/app/assets/templates/admin/trainings/validTrainingModal.html.erb index 99608fef3..9cb819f6b 100644 --- a/app/assets/templates/admin/trainings/validTrainingModal.html.erb +++ b/app/assets/templates/admin/trainings/validTrainingModal.html.erb @@ -6,8 +6,9 @@ {{ 'you_can_validate_the_training_of_the_following_members' | translate }}

{{ 'no_reservation' }}

@@ -15,4 +16,4 @@ \ No newline at end of file + diff --git a/app/assets/templates/projects/show.html.erb b/app/assets/templates/projects/show.html.erb index aacdfaabb..0918d3a98 100644 --- a/app/assets/templates/projects/show.html.erb +++ b/app/assets/templates/projects/show.html.erb @@ -76,7 +76,12 @@
-
{{ 'by_name' | translate:{NAME:project.author.first_name} }}
+
+ + {{ 'by_name' | translate:{NAME:project.author.first_name} }} + + {{ 'deleted_user' }} +
{{ 'posted_on_' | translate }} {{project.created_at | amDateFormat: 'LL'}} diff --git a/app/controllers/api/reservations_controller.rb b/app/controllers/api/reservations_controller.rb index 3e1d709c5..08313deb5 100644 --- a/app/controllers/api/reservations_controller.rb +++ b/app/controllers/api/reservations_controller.rb @@ -26,7 +26,7 @@ class API::ReservationsController < API::ApiController user_id = current_user.admin? ? params[:reservation][:user_id] : current_user.id @reservation = Reservation.new(reservation_params) - is_reserve = Reservations::Reserve.new(user_id, current_user.id) + is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) .pay_and_save(@reservation, method, coupon_params[:coupon_code]) if is_reserve diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index ea84c30ed..08a0d5218 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -19,7 +19,7 @@ class API::SubscriptionsController < API::ApiController user_id = current_user.admin? ? params[:subscription][:user_id] : current_user.id @subscription = Subscription.new(subscription_params) - is_subscribe = Subscriptions::Subscribe.new(current_user.id, user_id) + is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id) .pay_and_save(@subscription, method, coupon_params[:coupon_code], true) if is_subscribe @@ -35,7 +35,7 @@ class API::SubscriptionsController < API::ApiController free_days = params[:subscription][:free] || false - res = Subscriptions::Subscribe.new(current_user.id) + res = Subscriptions::Subscribe.new(current_user.invoicing_profile.id) .extend_subscription(@subscription, subscription_update_params[:expired_at], free_days) if res.is_a?(Subscription) @subscription = res diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 0e8de8dbe..b052253ad 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -22,7 +22,7 @@ class Invoice < ActiveRecord::Base belongs_to :offer_day, foreign_type: 'OfferDay', foreign_key: 'invoiced_id' has_one :avoir, class_name: 'Invoice', foreign_key: :invoice_id, dependent: :destroy - belongs_to :operator, foreign_key: :operator_id, class_name: 'User' + belongs_to :operator_profile, foreign_key: :operator_profile_id, class_name: 'InvoicingProfile' before_create :add_environment after_create :update_reference, :chain_record diff --git a/app/models/invoicing_profile.rb b/app/models/invoicing_profile.rb index d8c8fa2ee..3cd8c6b5e 100644 --- a/app/models/invoicing_profile.rb +++ b/app/models/invoicing_profile.rb @@ -16,6 +16,8 @@ class InvoicingProfile < ActiveRecord::Base has_many :history_values, dependent: :nullify + has_many :operated_invoices, foreign_key: :operator_profile_id, class_name: 'Invoice', dependent: :nullify + def full_name # if first_name or last_name is nil, the empty string will be used as a temporary replacement (first_name || '').humanize.titleize + ' ' + (last_name || '').humanize.titleize diff --git a/app/models/project/openlab_sync.rb b/app/models/project/openlab_sync.rb index 932e5ead9..a7972eb10 100644 --- a/app/models/project/openlab_sync.rb +++ b/app/models/project/openlab_sync.rb @@ -33,7 +33,7 @@ module Project::OpenlabSync components: components.map(&:name), themes: themes.map(&:name), author: author&.profile&.full_name, - collaborators: users.map { |u| u.profile.full_name }, + collaborators: users.map { |u| u&.profile&.full_name }, steps_body: steps_body, image_path: project_image&.attachment&.medium&.url, project_path: "/#!/projects/#{slug}", diff --git a/app/models/reservation.rb b/app/models/reservation.rb index dbab3aeb8..0b2c42834 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -224,10 +224,10 @@ class Reservation < ActiveRecord::Base invoice_items end - def save_with_payment(operator_id, coupon_code = nil) + def save_with_payment(operator_profile_id, coupon_code = nil) begin clean_pending_strip_invoice_items - build_invoice(invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, operator_id: operator_id) + build_invoice(invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, operator_profile_id: operator_profile_id) invoice_items = generate_invoice_items(false, coupon_code) rescue StandardError => e logger.error e @@ -242,7 +242,7 @@ class Reservation < ActiveRecord::Base if plan_id self.subscription = Subscription.find_or_initialize_by(statistic_profile_id: statistic_profile_id) subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, card_token: card_token, expiration_date: nil } - if subscription.save_with_payment(operator_id, false) + if subscription.save_with_payment(operator_profile_id, false) self.stp_invoice_id = invoice_items.first.refresh.invoice invoice.stp_invoice_id = invoice_items.first.refresh.invoice invoice.invoice_items.push InvoiceItem.new( @@ -368,8 +368,12 @@ class Reservation < ActiveRecord::Base pending_invoice_items.each(&:delete) end - def save_with_local_payment(operator_id, coupon_code = nil) - build_invoice(invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, operator_id: operator_id) + def save_with_local_payment(operator_profile_id, coupon_code = nil) + build_invoice( + invoicing_profile: user.invoicing_profile, + statistic_profile: user.statistic_profile, + operator_profile_id: operator_profile_id + ) generate_invoice_items(true, coupon_code) return false unless valid? @@ -377,7 +381,7 @@ class Reservation < ActiveRecord::Base if plan_id self.subscription = Subscription.find_or_initialize_by(statistic_profile_id: statistic_profile_id) subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, expiration_date: nil } - if subscription.save_with_local_payment(operator_id, false) + if subscription.save_with_local_payment(operator_profile_id, false) invoice.invoice_items.push InvoiceItem.new( amount: subscription.plan.amount, description: subscription.plan.name, diff --git a/app/models/subscription.rb b/app/models/subscription.rb index aba6e8b97..1a8d193e3 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -20,7 +20,7 @@ class Subscription < ActiveRecord::Base # Stripe subscription payment # @param invoice if true then subscription pay itself, dont pay with reservation # if false then subscription pay with reservation - def save_with_payment(operator_id, invoice = true, coupon_code = nil) + def save_with_payment(operator_profile_id, invoice = true, coupon_code = nil) return unless valid? begin @@ -75,7 +75,7 @@ class Subscription < ActiveRecord::Base # generate invoice stp_invoice = Stripe::Invoice.all(customer: user.stp_customer_id, limit: 1).data.first if invoice - db_invoice = generate_invoice(operator_id, stp_invoice.id, coupon_code) + db_invoice = generate_invoice(operator_profile_id, stp_invoice.id, coupon_code) # debit wallet wallet_transaction = debit_user_wallet if wallet_transaction @@ -129,7 +129,7 @@ class Subscription < ActiveRecord::Base # @param invoice if true then only the subscription is payed, without reservation # if false then the subscription is payed with reservation - def save_with_local_payment(operator_id, invoice = true, coupon_code = nil) + def save_with_local_payment(operator_profile_id, invoice = true, coupon_code = nil) return false unless valid? set_expiration_date @@ -142,7 +142,7 @@ class Subscription < ActiveRecord::Base # debit wallet wallet_transaction = debit_user_wallet - invoc = generate_invoice(operator_id, nil, coupon_code) + invoc = generate_invoice(operator_profile_id, nil, coupon_code) if wallet_transaction invoc.wallet_amount = @wallet_amount_debit invoc.wallet_transaction_id = wallet_transaction.id @@ -152,7 +152,7 @@ class Subscription < ActiveRecord::Base true end - def generate_invoice(operator_id, stp_invoice_id = nil, coupon_code = nil) + def generate_invoice(operator_profile_id, stp_invoice_id = nil, coupon_code = nil) coupon_id = nil total = plan.amount @@ -173,7 +173,7 @@ class Subscription < ActiveRecord::Base total: total, stp_invoice_id: stp_invoice_id, coupon_id: coupon_id, - operator_id: operator_id + operator_profile_id: operator_profile_id ) invoice.invoice_items.push InvoiceItem.new( amount: plan.amount, @@ -184,8 +184,8 @@ class Subscription < ActiveRecord::Base invoice end - def generate_and_save_invoice(operator_id, stp_invoice_id = nil) - generate_invoice(operator_id, stp_invoice_id).save + def generate_and_save_invoice(operator_profile_id, stp_invoice_id = nil) + generate_invoice(operator_profile_id, stp_invoice_id).save end def cancel @@ -222,7 +222,7 @@ class Subscription < ActiveRecord::Base expiration_date end - def free_extend(expiration, operator_id) + def free_extend(expiration, operator_profile_id) return false if expiration <= expired_at od = offer_days.create(start_at: expired_at, end_at: expiration) @@ -231,7 +231,7 @@ class Subscription < ActiveRecord::Base invoiced_type: 'OfferDay', invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, - operator_id: operator_id, + operator_profile_id: operator_profile_id, total: 0 ) invoice.invoice_items.push InvoiceItem.new(amount: 0, description: plan.name, subscription_id: id) diff --git a/app/models/user.rb b/app/models/user.rb index f40dc1996..95b5f8f6d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -56,6 +56,7 @@ class User < ActiveRecord::Base after_update :notify_group_changed, if: :group_id_changed? after_update :update_invoicing_profile, if: :invoicing_data_was_modified? after_update :update_statistic_profile, if: :statistic_data_was_modified? + before_destroy :remove_orphan_drafts attr_accessor :cgu delegate :first_name, to: :profile @@ -127,10 +128,10 @@ class User < ActiveRecord::Base my_projects.to_a.concat projects end - def generate_subscription_invoice(operator_id) + def generate_subscription_invoice(operator_profile_id) return unless subscription - subscription.generate_and_save_invoice(operator_id) + subscription.generate_and_save_invoice(operator_profile_id) end def stripe_customer @@ -276,6 +277,15 @@ class User < ActiveRecord::Base protected + # remove projets drafts that are not linked to another user + def remove_orphan_drafts + orphans = my_projects + .joins('LEFT JOIN project_users ON projects.id = project_users.project_id') + .where('project_users.project_id IS NULL') + .where(state: 'draft') + orphans.map(&:destroy!) + end + def confirmation_required? false end diff --git a/app/services/availabilities/status_service.rb b/app/services/availabilities/status_service.rb index 438be1d0b..c52329ab3 100644 --- a/app/services/availabilities/status_service.rb +++ b/app/services/availabilities/status_service.rb @@ -18,7 +18,7 @@ class Availabilities::StatusService slot.id = s.id slot.is_reserved = true - slot.title = "#{slot.machine.name} - #{@show_name ? r.user.profile.full_name : I18n.t('availabilities.not_available')}" + slot.title = "#{slot.machine.name} - #{@show_name ? r.user&.profile&.full_name : I18n.t('availabilities.not_available')}" slot.can_modify = true if @current_user_role == 'admin' slot.reservations.push r diff --git a/app/services/reservations/reserve.rb b/app/services/reservations/reserve.rb index d55a376c7..41303f8d9 100644 --- a/app/services/reservations/reserve.rb +++ b/app/services/reservations/reserve.rb @@ -2,19 +2,19 @@ # Provides helper methods for Reservation actions class Reservations::Reserve - attr_accessor :user_id, :operator_id + attr_accessor :user_id, :operator_profile_id - def initialize(user_id, operator_id) + def initialize(user_id, operator_profile_id) @user_id = user_id - @operator_id = operator_id + @operator_profile_id = operator_profile_id end def pay_and_save(reservation, payment_method, coupon) reservation.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id if payment_method == :local - reservation.save_with_local_payment(operator_id, coupon) + reservation.save_with_local_payment(operator_profile_id, coupon) elsif payment_method == :stripe - reservation.save_with_payment(operator_id, coupon) + reservation.save_with_payment(operator_profile_id, coupon) end end end diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index 776e0c8e6..9fd9dc972 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -2,11 +2,11 @@ # Provides helper methods for Subscription actions class Subscriptions::Subscribe - attr_accessor :user_id, :operator_id + attr_accessor :user_id, :operator_profile_id - def initialize(operator_id, user_id = nil) + def initialize(operator_profile_id, user_id = nil) @user_id = user_id - @operator_id = operator_id + @operator_profile_id = operator_profile_id end def pay_and_save(subscription, payment_method, coupon, invoice) @@ -14,14 +14,14 @@ class Subscriptions::Subscribe subscription.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id if payment_method == :local - subscription.save_with_local_payment(operator_id, invoice, coupon) + subscription.save_with_local_payment(operator_profile_id, invoice, coupon) elsif payment_method == :stripe - subscription.save_with_payment(operator_id, invoice, coupon) + subscription.save_with_payment(operator_profile_id, invoice, coupon) end end def extend_subscription(subscription, new_expiration_date, free_days) - return subscription.free_extend(new_expiration_date, @operator_id) if free_days + return subscription.free_extend(new_expiration_date, @operator_profile_id) if free_days new_sub = Subscription.create( plan_id: subscription.plan_id, @@ -29,7 +29,7 @@ class Subscriptions::Subscribe expiration_date: new_expiration_date ) if new_sub.save - new_sub.user.generate_subscription_invoice(operator_id) + new_sub.user.generate_subscription_invoice(operator_profile_id) return new_sub end false diff --git a/app/views/api/notifications/_notify_admin_member_create_reservation.json.jbuilder b/app/views/api/notifications/_notify_admin_member_create_reservation.json.jbuilder index 13ec07a04..45bd53c8b 100644 --- a/app/views/api/notifications/_notify_admin_member_create_reservation.json.jbuilder +++ b/app/views/api/notifications/_notify_admin_member_create_reservation.json.jbuilder @@ -1,5 +1,5 @@ json.title notification.notification_type json.description t('.a_RESERVABLE_reservation_was_made_by_USER_html', RESERVABLE: notification.attached_object.reservable.name, - USER: notification.attached_object.user.profile.full_name) + USER: notification.attached_object.user&.profile&.full_name) json.url notification_url(notification, format: :json) diff --git a/app/views/api/notifications/_notify_admin_subscribed_plan.json.jbuilder b/app/views/api/notifications/_notify_admin_subscribed_plan.json.jbuilder index 14cf26c78..cebfa20f6 100644 --- a/app/views/api/notifications/_notify_admin_subscribed_plan.json.jbuilder +++ b/app/views/api/notifications/_notify_admin_subscribed_plan.json.jbuilder @@ -1,5 +1,5 @@ json.title notification.notification_type json.description t('.subscription_PLAN_has_been_subscribed_by_USER_html', PLAN: notification.attached_object.plan.name, - USER: notification.attached_object.user.profile.full_name) + USER: notification.attached_object.user&.profile&.full_name) json.url notification_url(notification, format: :json) diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index 5523c9970..356e743a4 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -85,6 +85,7 @@ en: training_validation: "Training validation" training_of_the_ : "Training of the " # context: date. eg. "training of the september 1st 2012" you_can_validate_the_training_of_the_following_members: "You can validate the training of the following members:" + deleted_user: "Deleted user" no_reservation: "No reservation" validate_the_trainings: "Validate the trainings" edition_of_the_description_tooltip: "Edition of the description tooltip" diff --git a/config/locales/app.admin.es.yml b/config/locales/app.admin.es.yml index b985122ab..9702f1954 100644 --- a/config/locales/app.admin.es.yml +++ b/config/locales/app.admin.es.yml @@ -85,6 +85,7 @@ es: training_validation: "Validación de la formación" training_of_the_ : "Training of the " # context: fecha. p.ej. "entrenamiento del 1 de septiembre 2012" you_can_validate_the_training_of_the_following_members: "Puede validar la formación de los siguientes miembros:" + deleted_user: "Deleted user" # translation_missing no_reservation: "Sin reserva" validate_the_trainings: "Validar los entrenamientos" edition_of_the_description_tooltip: "Edición de la descripción tooltip" diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index af3c76f9c..57e301863 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -85,6 +85,7 @@ fr: training_validation: "Validation formation" training_of_the_ : "Formation du " # context: date. eg. "training of the september 1st 2012" you_can_validate_the_training_of_the_following_members: "Vous pouvez valider la formation des membres suivants :" + deleted_user: "Utilisateur supprimé" no_reservation: "Aucune réservation" validate_the_trainings: "Valider les formations" edition_of_the_description_tooltip: "Édition de l'infobulle de description" @@ -768,4 +769,4 @@ fr: confirmation_required: "Confirmez le traitement du signalement" report_will_be_destroyed: "Une fois le signalement traité, le rapport sera supprimé. Cette action est irréversible, continuer ?" report_removed: "Le rapport a bien été supprimé" - failed_to_remove: "Une erreur est survenue, impossible de supprimer le rapport" \ No newline at end of file + failed_to_remove: "Une erreur est survenue, impossible de supprimer le rapport" diff --git a/config/locales/app.admin.pt.yml b/config/locales/app.admin.pt.yml index 2acd63c15..41accb500 100755 --- a/config/locales/app.admin.pt.yml +++ b/config/locales/app.admin.pt.yml @@ -85,6 +85,7 @@ pt: training_validation: "Validação de treinamentos" training_of_the_ : "Treinamento de " # context: date. eg. "training of the september 1st 2012" you_can_validate_the_training_of_the_following_members: "Você pode validar um treinamento dos seguintes membros:" + deleted_user: "Deleted user" # translation_missing no_reservation: "Sem reservas" validate_the_trainings: "Validação de treinamentos" edition_of_the_description_tooltip: "Edição da descrição tooltip" diff --git a/config/locales/app.public.en.yml b/config/locales/app.public.en.yml index 938ceb2d6..764ae1366 100644 --- a/config/locales/app.public.en.yml +++ b/config/locales/app.public.en.yml @@ -171,6 +171,7 @@ en: # details of a projet project_description: "Project description" by_name: "By {{NAME}}" # angular interpolation + deleted_user: "Deleted user" posted_on_: "Posted on" CAD_file_to_download: "{COUNT, plural, =0{No CAD files} =1{CAD file to download} other{CAD files to download}}" # messageFormat interpolation licence: "Licence" diff --git a/config/locales/app.public.es.yml b/config/locales/app.public.es.yml index 19868ee56..a8bba4339 100644 --- a/config/locales/app.public.es.yml +++ b/config/locales/app.public.es.yml @@ -170,6 +170,7 @@ es: # details of a projet project_description: "Descripción de proyecto" by_name: "Por {{NAME}}" # angular interpolation + deleted_user: "Deleted user" #translation_missing posted_on_: "Subido el" CAD_file_to_download: "{COUNT, plural, =0{No CAD files} =1{CAD file to download} other{CAD files to download}}" # messageFormat interpolation licence: "Licencia" diff --git a/config/locales/app.public.fr.yml b/config/locales/app.public.fr.yml index 4b5cb1b5f..17beb74f9 100644 --- a/config/locales/app.public.fr.yml +++ b/config/locales/app.public.fr.yml @@ -171,6 +171,7 @@ fr: # détails d'un projet project_description: "Description du projet" by_name: "Par {{NAME}}" # angular interpolation + deleted_user: "Utilisateur supprimé" posted_on_: "posté le" CAD_file_to_download: "{COUNT, plural, =0{Aucun fichier CAO} =1{Fichier CAO à télécharger} other{Fichiers CAO à télécharger}}" # messageFormat interpolation licence: "Licence" diff --git a/config/locales/app.public.pt.yml b/config/locales/app.public.pt.yml index 811da46b6..9550020ba 100755 --- a/config/locales/app.public.pt.yml +++ b/config/locales/app.public.pt.yml @@ -171,6 +171,7 @@ pt: # details of a projet project_description: "Descrição do projeto" by_name: "Por {{NAME}}" # angular interpolation + deleted_user: "Deleted user" #translation_missing posted_on_: "Criado em" CAD_file_to_download: "{COUNT, plural, =0{No CAD files} =1{CAD file to download} other{CAD files to download}}" # messageFormat interpolation licence: "Licença" diff --git a/db/migrate/20140703231208_add_username_to_user.rb b/db/migrate/20140703231208_add_username_to_user.rb index 14ea52e97..4ebd934d3 100644 --- a/db/migrate/20140703231208_add_username_to_user.rb +++ b/db/migrate/20140703231208_add_username_to_user.rb @@ -3,9 +3,7 @@ class AddUsernameToUser < ActiveRecord::Migration add_column :users, :username, :string, after: :id User.includes(:profile).each do |u| - if u.respond_to? :username and !u.username? - u.update_columns(username: u.profile.username) - end + u.update_columns(username: u.profile.username) if u.respond_to?(:username) && !u.username? end end diff --git a/db/migrate/20190521122429_create_invoicing_profiles.rb b/db/migrate/20190521122429_create_invoicing_profiles.rb index 0d1c423b0..3e6e665d1 100644 --- a/db/migrate/20190521122429_create_invoicing_profiles.rb +++ b/db/migrate/20190521122429_create_invoicing_profiles.rb @@ -14,5 +14,8 @@ class CreateInvoicingProfiles < ActiveRecord::Migration add_reference :wallets, :invoicing_profile, index: true, foreign_key: true add_reference :wallet_transactions, :invoicing_profile, index: true, foreign_key: true add_reference :history_values, :invoicing_profile, index: true, foreign_key: true + + add_column :invoices, :operator_profile_id, :integer + add_foreign_key :invoices, :invoicing_profiles, column: :operator_profile_id, primary_key: :id end end diff --git a/db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb b/db/migrate/20190522115230_migrate_invoice_to_invoicing_profile.rb similarity index 90% rename from db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb rename to db/migrate/20190522115230_migrate_invoice_to_invoicing_profile.rb index 3688ebfb5..cbee7cfad 100644 --- a/db/migrate/20190522115230_migrate_user_to_invoicing_profile.rb +++ b/db/migrate/20190522115230_migrate_invoice_to_invoicing_profile.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # migrate the invoices from being attached to a user to invoicing_profiles which are GDPR compliant -class MigrateUserToInvoicingProfile < ActiveRecord::Migration +class MigrateInvoiceToInvoicingProfile < ActiveRecord::Migration def up # first, check the footprints check_footprints @@ -13,9 +13,12 @@ class MigrateUserToInvoicingProfile < ActiveRecord::Migration puts 'Migrating invoices. This may take a while...' Invoice.order(:id).all.each do |i| user = User.find(i.user_id) + operator = User.find_by(id: i.operator_id) i.update_column('invoicing_profile_id', user.invoicing_profile.id) i.update_column('statistic_profile_id', user.statistic_profile.id) + i.update_column('operator_profile_id', operator&.invoicing_profile&.id) i.update_column('user_id', nil) + i.update_column('operator_id', nil) end # chain all records InvoiceItem.order(:id).all.each(&:chain_record) @@ -32,8 +35,10 @@ class MigrateUserToInvoicingProfile < ActiveRecord::Migration # reset invoices Invoice.order(:created_at).all.each do |i| i.update_column('user_id', i.invoicing_profile.user_id) + i.update_column('operator_id', i.operator_profile.user_id) i.update_column('invoicing_profile_id', nil) i.update_column('statistic_profile_id', nil) + i.update_column('operator_profile_id', nil) end # chain all records InvoiceItem.order(:id).all.each(&:chain_record) diff --git a/db/migrate/20190528140012_remove_user_id_columns.rb b/db/migrate/20190528140012_remove_user_id_columns.rb index 84621123d..5e232ec63 100644 --- a/db/migrate/20190528140012_remove_user_id_columns.rb +++ b/db/migrate/20190528140012_remove_user_id_columns.rb @@ -5,5 +5,6 @@ class RemoveUserIdColumns < ActiveRecord::Migration remove_reference :wallets, :user, index: true, foreign_key: true remove_reference :wallet_transactions, :user, index: true, foreign_key: true remove_reference :history_values, :user, index: true, foreign_key: true + remove_reference :invoices, :operator, index: true, foreign_key: true end end diff --git a/db/schema.rb b/db/schema.rb index c29be35d2..a3e980e10 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -278,8 +278,8 @@ ActiveRecord::Schema.define(version: 20190606074801) do t.integer "coupon_id" t.string "footprint" t.string "environment" - t.integer "operator_id" t.integer "invoicing_profile_id" + t.integer "operator_profile_id" t.integer "statistic_profile_id" end @@ -910,8 +910,8 @@ ActiveRecord::Schema.define(version: 20190606074801) do add_foreign_key "history_values", "settings" add_foreign_key "invoices", "coupons" add_foreign_key "invoices", "invoicing_profiles" + add_foreign_key "invoices", "invoicing_profiles", column: "operator_profile_id" add_foreign_key "invoices", "statistic_profiles" - add_foreign_key "invoices", "users", column: "operator_id" add_foreign_key "invoices", "wallet_transactions" add_foreign_key "invoicing_profiles", "users" add_foreign_key "o_auth2_mappings", "o_auth2_providers" diff --git a/lib/tasks/fablab/setup.rake b/lib/tasks/fablab/setup.rake index 74363edca..2a96ce485 100644 --- a/lib/tasks/fablab/setup.rake +++ b/lib/tasks/fablab/setup.rake @@ -80,8 +80,8 @@ namespace :fablab do end end - desc 'migrate invoices PDF to folders by invoicing_profile' - task migrate_invoices_pdf_folders: :environment do + desc 'migrate PDF invoices to folders numbered by invoicing_profile' + task migrate_pdf_invoices_folders: :environment do require 'fileutils' Invoice.all.each do |i| invoicing_profile = i.invoicing_profile @@ -90,8 +90,10 @@ namespace :fablab do src = "invoices/#{user_id}/#{i.filename}" dest = "tmp/invoices/#{invoicing_profile.id}" - FileUtils.mkdir_p dest - FileUtils.mv src, "dest/#{i.filename}", force: true if FileTest.exist?(src) + if FileTest.exist?(src) + FileUtils.mkdir_p dest + FileUtils.mv src, "#{dest}/#{i.filename}", force: true + end end FileUtils.rm_rf 'invoices' FileUtils.mv 'tmp/invoices', 'invoices' diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index d1dc69c78..a959ef801 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -16,9 +16,9 @@ invoice_1: type: subscription_to_expire: description: - footprint: e7fffd325cacbb76218626ea2a35a7a9f052c208c41aac13f70c31eae9f81bc7 + footprint: d5fc7141c282279134320a4b1db74c540a82f105a0b1f4eeff6602730ed3e745 environment: test - operator_id: + operator_profile_id: 3 invoice_2: id: 2 @@ -37,9 +37,9 @@ invoice_2: type: subscription_to_expire: description: - footprint: bd0b739c211b40abed7ddb07bc054281513acab4a0adde6c416dc1715dd9f005 + footprint: 1c156831c7cd1fcc1c6be8b83d60fbb9b89f84285638e87131582eb54b8a377d environment: test - operator_id: + operator_profile_id: 1 invoice_3: id: 3 @@ -58,9 +58,9 @@ invoice_3: type: subscription_to_expire: description: - footprint: ab00a9318314b75d29ec220e00c96e738d608d64423b1c5abb25786551f12f8b + footprint: 5fa3ac2e2684f6df1440c63969b78ac24daa9207682b359f61c655d41d4c4f03 environment: test - operator_id: + operator_profile_id: 1 invoice_4: @@ -80,9 +80,9 @@ invoice_4: type: subscription_to_expire: description: - footprint: 6c70f2bbbb3fd02a1ad7437ccd14456d1281d5a1f8666cce2e9d0b64701a837d + footprint: 6de8d985d82f2146aaa60e79882237b3bc8812521643b14498e4d498aec78613 environment: test - operator_id: + operator_profile_id: 1 invoice_5: id: 5 @@ -101,6 +101,6 @@ invoice_5: type: subscription_to_expire: description: - footprint: cccd4f290d900cb7004baa63896191a6938305e75589d137655cb91a0e7dede2 + footprint: a78aa3f991718f023f1c6b85b6f30aeda3f3d6b20f2d64504d29a23141db9edb environment: test - operator_id: + operator_profile_id: 1