From c29ae0036d770ed46ba022d57720da6de976a227 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Tue, 7 Jan 2020 23:12:07 +0000 Subject: [PATCH 01/11] initial comments --- stash/stash_engine/lib/tasks/stash_engine_tasks.rake | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index b2c4f277a8..a09de1379a 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -171,6 +171,18 @@ namespace :identifiers do end end + desc 'Generate a report of items that have been published in a given month' + task shopping_cart_report: :environment , [:year_month] do |task, args| + p "processing #{args}" + p "Shopping Cart Report for #{year_month}" + # find the right list of items + ## take argument for the year-month + ## if none, default to the previously completed month + # for each item + ## get the variables associated with publication status + ## output a CSV of the metadata + end + desc 'populate publicationName' task load_publication_names: :environment do p "Searching CrossRef and the Journal API for publication names: #{Time.now.utc}" From 404ee2c012da91e6e40ea9d978a328339f2cea6b Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Fri, 10 Jan 2020 18:41:13 +0000 Subject: [PATCH 02/11] add approval_date to identifier --- .../app/models/stash_engine/identifier.rb | 16 ++++++++++++++++ .../lib/tasks/stash_engine_tasks.rake | 19 +++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/stash/stash_engine/app/models/stash_engine/identifier.rb b/stash/stash_engine/app/models/stash_engine/identifier.rb index c9d5bd80bf..3057c7ce3f 100644 --- a/stash/stash_engine/app/models/stash_engine/identifier.rb +++ b/stash/stash_engine/app/models/stash_engine/identifier.rb @@ -318,6 +318,22 @@ def embargoed_until_article_appears? found_article_appears end + # returns the date on which this identifier was approved for publication + # (i.e., the date on which it entered the status 'published' or 'embargoed' + def approval_date + return nil unless %w[published embargoed].include?(pub_state) + + found_approval_date = nil + resources.each do |res| + res.curation_activities.each do |ca| + next unless %w[published embargoed].include?(ca.status) + found_approval_date = ca.created_at + break + end + end + found_approval_date + end + # returns the publication state based on history # finds the latest applicable state from terminal states for each resource/version. # We only really care about whether it's some form of published, embargoed or withdrawn diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index a09de1379a..7dc09b9e17 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -172,15 +172,22 @@ namespace :identifiers do end desc 'Generate a report of items that have been published in a given month' - task shopping_cart_report: :environment , [:year_month] do |task, args| - p "processing #{args}" - p "Shopping Cart Report for #{year_month}" - # find the right list of items - ## take argument for the year-month - ## if none, default to the previously completed month + task shopping_cart_report: :environment do + # Get the year-month specified in YEAR_MONTH environment variable. + # If none, default to the previously completed month. + if ENV['YEAR_MONTH'].blank? + puts 'No month specified, assuming last month.' + year_month = 1.month.ago.strftime("%Y-%m") + else + year_month = ENV['YEAR_MONTH'] + end + p "Shopping Cart Report for #{year_month}" + # - any identifier that became published or embargoed during the target month # for each item ## get the variables associated with publication status ## output a CSV of the metadata + # Exit cleanly (don't let rake assume that an extra argument is another task to process) + exit end desc 'populate publicationName' From 78b094369e8c367b4c32d0411880699fac224193 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Fri, 10 Jan 2020 19:26:23 +0000 Subject: [PATCH 03/11] rubocop --- stash/stash_engine/app/models/stash_engine/identifier.rb | 2 +- stash/stash_engine/lib/tasks/stash_engine_tasks.rake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stash/stash_engine/app/models/stash_engine/identifier.rb b/stash/stash_engine/app/models/stash_engine/identifier.rb index 3057c7ce3f..c0bb2a73ed 100644 --- a/stash/stash_engine/app/models/stash_engine/identifier.rb +++ b/stash/stash_engine/app/models/stash_engine/identifier.rb @@ -333,7 +333,7 @@ def approval_date end found_approval_date end - + # returns the publication state based on history # finds the latest applicable state from terminal states for each resource/version. # We only really care about whether it's some form of published, embargoed or withdrawn diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index 7dc09b9e17..323c76f356 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -177,7 +177,7 @@ namespace :identifiers do # If none, default to the previously completed month. if ENV['YEAR_MONTH'].blank? puts 'No month specified, assuming last month.' - year_month = 1.month.ago.strftime("%Y-%m") + year_month = 1.month.ago.strftime('%Y-%m') else year_month = ENV['YEAR_MONTH'] end From 7b52b418575f05a4db5e6ccc0a7919b4fbe5c8b1 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Sat, 11 Jan 2020 04:04:01 +0000 Subject: [PATCH 04/11] change invoice_id to payment_type and payment_id --- .../stash_api/datasets_controller.rb | 8 ++--- .../app/models/stash_api/dataset_parser.rb | 3 +- .../models/stash_api/dataset_parser_spec.rb | 8 +++-- .../models/stash_engine/curation_activity.rb | 2 +- .../20200111013011_add_payment_type.rb | 33 +++++++++++++++++++ .../lib/stash/payments/invoicer.rb | 10 +++--- .../lib/tasks/stash_engine_tasks.rake | 8 ++--- .../spec/unit/stash/payments/invoicer_spec.rb | 3 +- 8 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb diff --git a/stash/stash_api/app/controllers/stash_api/datasets_controller.rb b/stash/stash_api/app/controllers/stash_api/datasets_controller.rb index c0866c85e9..6b6b18896a 100644 --- a/stash/stash_api/app/controllers/stash_api/datasets_controller.rb +++ b/stash/stash_api/app/controllers/stash_api/datasets_controller.rb @@ -182,7 +182,7 @@ def lock_down_admin_only_params # all this bogus return false stuff is to prevent double render errors in some circumstances return if check_superuser_restricted_params == false return if check_may_set_user_id == false - return if check_may_set_invoice_id == false + return if check_may_set_payment_id == false end def check_superuser_restricted_params @@ -211,10 +211,10 @@ def check_may_set_user_id false end - def check_may_set_invoice_id - return if params['invoiceId'].nil? + def check_may_set_payment_id + return if params['paymentId'].nil? unless @user.role == 'superuser' - render json: { error: 'Unauthorized: only superuser roles may set an invoiceId' }.to_json, status: 401 + render json: { error: 'Unauthorized: only superuser roles may set a paymentId' }.to_json, status: 401 return false end false diff --git a/stash/stash_api/app/models/stash_api/dataset_parser.rb b/stash/stash_api/app/models/stash_api/dataset_parser.rb index b89c08f982..bbfbef0308 100644 --- a/stash/stash_api/app/models/stash_api/dataset_parser.rb +++ b/stash/stash_api/app/models/stash_api/dataset_parser.rb @@ -37,7 +37,8 @@ def parse @hash[:authors]&.each { |author| add_author(json_author: author) } StashDatacite::Description.create(description: @hash[:abstract], description_type: 'abstract', resource_id: @resource.id) TO_PARSE.each { |item| dynamic_parse(my_class: item) } - @resource.identifier.invoice_id = @hash['invoiceId'] + @resource.identifier.payment_type = @hash['paymentType'] + @resource.identifier.payment_id = @hash['paymentId'] @resource.identifier.save @resource.identifier end diff --git a/stash/stash_api/spec/unit/models/stash_api/dataset_parser_spec.rb b/stash/stash_api/spec/unit/models/stash_api/dataset_parser_spec.rb index 08d4e2c831..755a9f090a 100644 --- a/stash/stash_api/spec/unit/models/stash_api/dataset_parser_spec.rb +++ b/stash/stash_api/spec/unit/models/stash_api/dataset_parser_spec.rb @@ -46,7 +46,8 @@ module StashApi 'abstract' => 'Cyberneticists agree that concurrent models are an interesting new topic in the field of machine learning.', 'userId' => @user2.id, - 'invoiceId' => 'invoice-123' + 'paymentId' => 'invoice-123', + 'paymentType' => 'stripe' }.with_indifferent_access @update_metadata = { @@ -135,8 +136,9 @@ module StashApi expect(resource.current_editor_id).to eq(@user2.id) end - it 'puts the invoiceId on the identifier' do - expect(@stash_identifier.invoice_id). to eq('invoice-123') + it 'puts the paymentId on the identifier' do + expect(@stash_identifier.payment_id). to eq('invoice-123') + expect(@stash_identifier.payment_type). to eq('stripe') end end diff --git a/stash/stash_engine/app/models/stash_engine/curation_activity.rb b/stash/stash_engine/app/models/stash_engine/curation_activity.rb index 9537bc329c..5267dd8253 100644 --- a/stash/stash_engine/app/models/stash_engine/curation_activity.rb +++ b/stash/stash_engine/app/models/stash_engine/curation_activity.rb @@ -242,7 +242,7 @@ def update_publication_flags def ready_for_payment? resource&.identifier&.reload StashEngine.app&.payments&.service == 'stripe' && - resource&.identifier&.invoice_id.nil? && + resource&.identifier&.payment_id.nil? && (status == 'published' || status == 'embargoed') end diff --git a/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb b/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb new file mode 100644 index 0000000000..ef34bc08bd --- /dev/null +++ b/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb @@ -0,0 +1,33 @@ +class AddPaymentType < ActiveRecord::Migration + def up + rename_column :stash_engine_identifiers, :invoice_id, :payment_id + add_column :stash_engine_identifiers, :payment_type, :string, after: :search_words + StashEngine::Identifier.reset_column_information + StashEngine::Identifier.all.each do |i| + if i.payment_id + if i.payment_id.start_with?('in_') + i.update_column(:payment_type, 'stripe') + else + # else, separate on the colon, set payment_type to part before colon, and payment_id to rest + found_type, found_id = i.payment_id.match(/(.+):(.+)/).captures + next unless found_type && found_id + i.update_column(:payment_id, found_id) + i.update_column(:payment_type, found_type) + end + end + end + end + + def down + StashEngine::Identifier.all.each do |i| + if i.payment_id + if i.payment_type != 'stripe' + new_id = "#{i.payment_type}:#{i.payment_id}" + i.update_column(:payment_id, new_id) + end + end + end + rename_column :stash_engine_identifiers, :payment_id, :invoice_id + remove_column :stash_engine_identifiers, :payment_type + end +end diff --git a/stash/stash_engine/lib/stash/payments/invoicer.rb b/stash/stash_engine/lib/stash/payments/invoicer.rb index 57e05f010e..fa47b748e1 100644 --- a/stash/stash_engine/lib/stash/payments/invoicer.rb +++ b/stash/stash_engine/lib/stash/payments/invoicer.rb @@ -26,7 +26,8 @@ def charge_user_via_invoice return unless customer_id.present? create_invoice_items_for_dpc(customer_id) invoice = create_invoice(customer_id) - resource.identifier.invoice_id = invoice.id + resource.identifier.payment_id = invoice.id + resource.identifier.payment_type = 'stripe' resource.identifier.save invoice.send_invoice end @@ -40,15 +41,16 @@ def charge_journal_via_invoice return unless customer_id.present? # the following line was creating invoice_item from return value, but didn't seem used anywhere create_invoice_items_for_dpc(customer_id) - resource.identifier.invoice_id = invoice_item&.id + resource.identifier.payment_id = "#{resource.identifier&.publication_issn}-#{invoice_item&.id}" + resource.identifier.payment_type = 'journal' resource.identifier.save end def external_service_online? set_api_key - latest = StashEngine::Identifier.where.not(invoice_id: nil).order(updated_at: :desc).first + latest = StashEngine::Identifier.where.not(payment_id: nil).order(updated_at: :desc).first return false unless latest.present? - Stripe::Charge.retrieve(latest.invoice_id).present? + Stripe::Charge.retrieve(latest.payment_id).present? end # takes a size and returns overage charges in cents diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index 323c76f356..36798ae46d 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -182,10 +182,10 @@ namespace :identifiers do year_month = ENV['YEAR_MONTH'] end p "Shopping Cart Report for #{year_month}" - # - any identifier that became published or embargoed during the target month - # for each item - ## get the variables associated with publication status - ## output a CSV of the metadata + StashEngine::Identifier.publicly_viewable.each do |i| + ## get the variables associated with publication status + ## output a CSV of the metadata + end # Exit cleanly (don't let rake assume that an extra argument is another task to process) exit end diff --git a/stash/stash_engine/spec/unit/stash/payments/invoicer_spec.rb b/stash/stash_engine/spec/unit/stash/payments/invoicer_spec.rb index 03ad082a9d..404a4532e2 100644 --- a/stash/stash_engine/spec/unit/stash/payments/invoicer_spec.rb +++ b/stash/stash_engine/spec/unit/stash/payments/invoicer_spec.rb @@ -10,7 +10,8 @@ module Payments before(:each) do @identifier = double(StashEngine::Identifier) allow(@identifier).to receive(:to_s).and_return('doi:10.123/a1b.c2d3') - allow(@identifier).to receive(:invoice_id=) + allow(@identifier).to receive(:payment_id=) + allow(@identifier).to receive(:payment_type=) allow(@identifier).to receive(:save).and_return(true) allow(@identifier).to receive(:storage_size).and_return(5.01e+10.to_i) From bd2980b995be0ac12215362700f1fbcb9cef0059 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Sat, 11 Jan 2020 04:05:02 +0000 Subject: [PATCH 05/11] rubocop --- .../db/migrate/20200111013011_add_payment_type.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb b/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb index ef34bc08bd..cbb9d2bf2a 100644 --- a/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb +++ b/stash/stash_engine/db/migrate/20200111013011_add_payment_type.rb @@ -7,7 +7,7 @@ def up if i.payment_id if i.payment_id.start_with?('in_') i.update_column(:payment_type, 'stripe') - else + else # else, separate on the colon, set payment_type to part before colon, and payment_id to rest found_type, found_id = i.payment_id.match(/(.+):(.+)/).captures next unless found_type && found_id @@ -20,11 +20,10 @@ def up def down StashEngine::Identifier.all.each do |i| - if i.payment_id - if i.payment_type != 'stripe' - new_id = "#{i.payment_type}:#{i.payment_id}" - i.update_column(:payment_id, new_id) - end + next unless i.payment_id + if i.payment_type != 'stripe' + new_id = "#{i.payment_type}:#{i.payment_id}" + i.update_column(:payment_id, new_id) end end rename_column :stash_engine_identifiers, :payment_id, :invoice_id From 51ec2fc7a5d79c32a71f90b87b06887756478d0c Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Mon, 13 Jan 2020 04:32:09 +0000 Subject: [PATCH 06/11] Record payment_type and payment_id in database --- .../stash_engine/curation_activities.rb | 2 +- spec/mocks/curation_activity.rb | 2 +- .../app/models/stash_engine/curation_activity.rb | 16 ++++++++++++---- .../app/models/stash_engine/identifier.rb | 16 ++++++++++++++++ .../spec/db/stash_engine/identifier_spec.rb | 10 +++++----- .../spec/db/stash_engine/resource_spec.rb | 2 +- stash/stash_engine/spec/factories.rb | 2 +- .../spec/unit/models/curation_activity_spec.rb | 6 +++--- 8 files changed, 40 insertions(+), 16 deletions(-) diff --git a/spec/factories/stash_engine/curation_activities.rb b/spec/factories/stash_engine/curation_activities.rb index 63ff13b32e..62c2b6218e 100644 --- a/spec/factories/stash_engine/curation_activities.rb +++ b/spec/factories/stash_engine/curation_activities.rb @@ -45,7 +45,7 @@ # https://stackoverflow.com/questions/8751175/skip-callbacks-on-factory-girl-and-rspec ca.define_singleton_method(:submit_to_datacite) {} ca.define_singleton_method(:update_solr) {} - ca.define_singleton_method(:submit_to_stripe) {} + ca.define_singleton_method(:process_payment) {} ca.define_singleton_method(:email_status_change_notices) {} ca.define_singleton_method(:email_orcid_invitations) {} end diff --git a/spec/mocks/curation_activity.rb b/spec/mocks/curation_activity.rb index 259764dc1f..ba819a85b3 100644 --- a/spec/mocks/curation_activity.rb +++ b/spec/mocks/curation_activity.rb @@ -7,7 +7,7 @@ def neuter_curation_callbacks! # callback explicitly. allow_any_instance_of(StashEngine::CurationActivity).to receive(:submit_to_datacite).and_return(true) allow_any_instance_of(StashEngine::CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(StashEngine::CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(StashEngine::CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(StashEngine::CurationActivity).to receive(:email_status_change_notices).and_return(true) allow_any_instance_of(StashEngine::CurationActivity).to receive(:email_orcid_invitations).and_return(true) end diff --git a/stash/stash_engine/app/models/stash_engine/curation_activity.rb b/stash/stash_engine/app/models/stash_engine/curation_activity.rb index 5267dd8253..c120228ebd 100644 --- a/stash/stash_engine/app/models/stash_engine/curation_activity.rb +++ b/stash/stash_engine/app/models/stash_engine/curation_activity.rb @@ -51,7 +51,7 @@ class CurationActivity < ActiveRecord::Base # rubocop:disable Metrics/ClassLengt # ------------------------------------------ # When the status is published/embargoed send to Stripe and DataCite - after_create :submit_to_datacite, :update_solr, :submit_to_stripe, :remove_peer_review, + after_create :submit_to_datacite, :update_solr, :process_payment, :remove_peer_review, if: proc { |ca| !ca.resource.skip_datacite_update && (ca.published? || ca.embargoed?) && latest_curation_status_changed? @@ -118,10 +118,18 @@ def latest_curation_status_changed? # Callbacks # ------------------------------------------ - def submit_to_stripe - return unless ready_for_payment? && - resource.identifier&.user_must_pay? + def process_payment + return unless ready_for_payment? + + if resource.identifier&.user_must_pay? + submit_to_stripe + else + resource.identifier&.record_payment + end + end + def submit_to_stripe + return unless ready_for_payment? inv = Stash::Payments::Invoicer.new(resource: resource, curator: user) inv.charge_user_via_invoice end diff --git a/stash/stash_engine/app/models/stash_engine/identifier.rb b/stash/stash_engine/app/models/stash_engine/identifier.rb index c0bb2a73ed..3e13142d84 100644 --- a/stash/stash_engine/app/models/stash_engine/identifier.rb +++ b/stash/stash_engine/app/models/stash_engine/identifier.rb @@ -222,6 +222,22 @@ def user_must_pay? (!submitter_affiliation.present? || !submitter_affiliation.fee_waivered?) end + def record_payment + return if payment_type.present? + if submitter_affiliation&.fee_waivered? + self.payment_type = 'waiver' + self.payment_id = submitter_affiliation.country_name + elsif institution_will_pay? + self.payment_type = 'institution' + self.payment_id = latest_resource&.tenant&.tenant_id + elsif journal_will_pay? + self.payment_type = 'journal-' + publication_data('paymentPlanType') + self.payment_id = publication_issn + else + self.payment_type = 'unknown' + end + end + def publication_data(field_name) return nil if publication_issn.nil? url = APP_CONFIG.old_dryad_url + '/api/v1/journals/' + publication_issn diff --git a/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb b/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb index ba9fd780d0..b2d8a98635 100644 --- a/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb +++ b/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb @@ -162,7 +162,7 @@ module StashEngine describe 'curation activity setup' do before(:each) do allow_any_instance_of(CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) end @@ -493,7 +493,7 @@ module StashEngine before(:each) do # a way to neuter all the callback activity allow_any_instance_of(CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) end @@ -539,7 +539,7 @@ module StashEngine before(:each) do # a way to neuter all the callback activity allow_any_instance_of(CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) end @@ -570,7 +570,7 @@ module StashEngine before(:each) do # a way to neuter all the callback activity allow_any_instance_of(CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) end @@ -663,7 +663,7 @@ module StashEngine before(:each) do # a way to neuter all the callback activity allow_any_instance_of(CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) end diff --git a/stash/stash_engine/spec/db/stash_engine/resource_spec.rb b/stash/stash_engine/spec/db/stash_engine/resource_spec.rb index a76a68601f..39d95b69e7 100644 --- a/stash/stash_engine/spec/db/stash_engine/resource_spec.rb +++ b/stash/stash_engine/spec/db/stash_engine/resource_spec.rb @@ -23,7 +23,7 @@ module StashEngine tenant_id: 'ucop' ) allow_any_instance_of(CurationActivity).to receive(:update_solr).and_return(true) - allow_any_instance_of(CurationActivity).to receive(:submit_to_stripe).and_return(true) + allow_any_instance_of(CurationActivity).to receive(:process_payment).and_return(true) allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) # Mock all the mailers fired by callbacks because these tests don't load everything we need diff --git a/stash/stash_engine/spec/factories.rb b/stash/stash_engine/spec/factories.rb index e773f13890..f3f2cb77a5 100644 --- a/stash/stash_engine/spec/factories.rb +++ b/stash/stash_engine/spec/factories.rb @@ -31,7 +31,7 @@ # https://stackoverflow.com/questions/8751175/skip-callbacks-on-factory-girl-and-rspec ca.define_singleton_method(:submit_to_datacite) {} ca.define_singleton_method(:update_solr) {} - ca.define_singleton_method(:submit_to_stripe) {} + ca.define_singleton_method(:process_payment) {} ca.define_singleton_method(:email_status_change_notices) {} ca.define_singleton_method(:email_orcid_invitations) {} end diff --git a/stash/stash_engine/spec/unit/models/curation_activity_spec.rb b/stash/stash_engine/spec/unit/models/curation_activity_spec.rb index 0b58ef3db5..cd103b8337 100644 --- a/stash/stash_engine/spec/unit/models/curation_activity_spec.rb +++ b/stash/stash_engine/spec/unit/models/curation_activity_spec.rb @@ -53,7 +53,7 @@ module StashEngine # get rid of callbacks for adding one and testing CurationActivity.skip_callback(:create, :after, :submit_to_datacite) CurationActivity.skip_callback(:create, :after, :update_solr) - CurationActivity.skip_callback(:save, :after, :submit_to_stripe) + CurationActivity.skip_callback(:save, :after, :process_payment) @curation_activity1 = create(:curation_activity, resource: @resource) CurationActivity.set_callback(:create, :after, :submit_to_datacite) # these two never need to fire to test this example @@ -67,7 +67,7 @@ module StashEngine end it "doesn't submit when status isn't changed" do - CurationActivity.skip_callback(:create, :after, :submit_to_datacite, :submit_to_stripe) + CurationActivity.skip_callback(:create, :after, :submit_to_datacite, :process_payment) @curation_activity2 = create(:curation_activity, resource: @resource, status: 'published') CurationActivity.set_callback(:create, :after, :submit_to_datacite) @@ -105,7 +105,7 @@ module StashEngine @resource = create(:resource, identifier_id: @identifier.id, user: @user) CurationActivity.skip_callback(:create, :after, :update_solr) - CurationActivity.skip_callback(:save, :after, :submit_to_stripe) + CurationActivity.skip_callback(:save, :after, :process_payment) allow_any_instance_of(CurationActivity).to receive(:should_update_doi?).and_return(true) logger = double(ActiveSupport::Logger) From 597ba58ba5af8537000a36c378baf37554787455 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Mon, 13 Jan 2020 16:34:19 +0000 Subject: [PATCH 07/11] force saving --- stash/stash_engine/app/models/stash_engine/identifier.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/stash/stash_engine/app/models/stash_engine/identifier.rb b/stash/stash_engine/app/models/stash_engine/identifier.rb index 3e13142d84..10beabb8c4 100644 --- a/stash/stash_engine/app/models/stash_engine/identifier.rb +++ b/stash/stash_engine/app/models/stash_engine/identifier.rb @@ -236,6 +236,7 @@ def record_payment else self.payment_type = 'unknown' end + self.save end def publication_data(field_name) From a6f6d39e6d77bb02a394bd916309c56cf971eb0d Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Mon, 13 Jan 2020 21:35:22 +0000 Subject: [PATCH 08/11] tests and population script --- .../app/models/stash_engine/identifier.rb | 2 +- .../lib/tasks/stash_engine_tasks.rake | 9 ++++ .../db/stash_engine/curation_activity_spec.rb | 12 ++++- .../spec/db/stash_engine/identifier_spec.rb | 54 +++++++++++++++++-- .../unit/models/curation_activity_spec.rb | 6 --- 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/stash/stash_engine/app/models/stash_engine/identifier.rb b/stash/stash_engine/app/models/stash_engine/identifier.rb index 10beabb8c4..f8e0f5af2a 100644 --- a/stash/stash_engine/app/models/stash_engine/identifier.rb +++ b/stash/stash_engine/app/models/stash_engine/identifier.rb @@ -236,7 +236,7 @@ def record_payment else self.payment_type = 'unknown' end - self.save + save end def publication_data(field_name) diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index 36798ae46d..3f9f5e3238 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -190,6 +190,15 @@ namespace :identifiers do exit end + desc 'populate payment info' + task load_payment_info: :environment do + p 'Populating payment information for published/embargoed items' + StashEngine::Identifier.publicly_viewable.where(payment_type: nil).each do |i| + i.record_payment + p "#{i.id} #{i.identifier} #{i.payment_type} #{i.payment_id}" + end + end + desc 'populate publicationName' task load_publication_names: :environment do p "Searching CrossRef and the Journal API for publication names: #{Time.now.utc}" diff --git a/stash/stash_engine/spec/db/stash_engine/curation_activity_spec.rb b/stash/stash_engine/spec/db/stash_engine/curation_activity_spec.rb index 5e41e4ea75..3461e29bcd 100644 --- a/stash/stash_engine/spec/db/stash_engine/curation_activity_spec.rb +++ b/stash/stash_engine/spec/db/stash_engine/curation_activity_spec.rb @@ -72,7 +72,6 @@ module StashEngine allow_any_instance_of(Stash::Doi::IdGen).to receive(:update_identifier_metadata).and_return(true) allow_any_instance_of(Stash::Payments::Invoicer).to receive(:new).and_return(true) allow_any_instance_of(Stash::Payments::Invoicer).to receive(:charge_user_via_invoice).and_return(true) - # allow_any_instance_of(StashEngine::CurationActivity).to receive(:update_publication_flags).and_return(true) end context :update_solr do @@ -129,7 +128,7 @@ module StashEngine end - context :submit_to_stripe do + context :process_payment do before(:each) do allow_any_instance_of(StashEngine::CurationActivity).to receive(:email_status_change_notices).and_return(true) @@ -164,6 +163,15 @@ module StashEngine ca.save end + it 'does not call submit_to_stripe when user is not responsible for payment' do + allow_any_instance_of(StashEngine::CurationActivity).to receive(:ready_for_payment?).and_return(true) + allow_any_instance_of(StashEngine::Identifier).to receive(:user_must_pay?).and_return(false) + ca = CurationActivity.new(resource_id: @resource.id, status: 'embargoed') + expect(ca).to receive(:process_payment) + expect(ca).not_to receive(:submit_to_stripe) + ca.save + end + end context :email_status_change_notices do diff --git a/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb b/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb index b2d8a98635..c50162dc39 100644 --- a/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb +++ b/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb @@ -166,6 +166,21 @@ module StashEngine allow_any_instance_of(CurationActivity).to receive(:submit_to_datacite).and_return(true) end + describe '#approval_date' do + it 'selects the correct approval_date' do + target_date = Date.new(2010, 2, 3) + @res1.curation_activities << CurationActivity.create(status: 'curation', user: @user, created_at: '2000-01-01') + @res1.curation_activities << CurationActivity.create(status: 'published', user: @user, created_at: target_date) + @res1.curation_activities << CurationActivity.create(status: 'published', user: @user, created_at: '2020-01-01') + expect(@identifier.approval_date).to eq(target_date) + end + + it 'gives no approval_date for unpublished items' do + @res1.curation_activities << CurationActivity.create(status: 'curation', user: @user, created_at: '2000-01-01') + expect(@identifier.approval_date).to eq(nil) + end + end + describe '#latest_resource_with_public_metadata' do it 'finds the last published resource' do @@ -313,8 +328,8 @@ module StashEngine describe '#user_must_pay?' do before(:each) do - allow(@identifier).to receive(:'journal_will_pay?').and_return(false) - allow(@identifier).to receive(:'institution_will_pay?').and_return(false) + allow(@identifier).to receive(:journal_will_pay?).and_return(false) + allow(@identifier).to receive(:institution_will_pay?).and_return(false) end it 'returns true if no one else will pay' do @@ -322,16 +337,47 @@ module StashEngine end it 'returns false if journal will pay' do - allow(@identifier).to receive(:'journal_will_pay?').and_return(true) + allow(@identifier).to receive(:journal_will_pay?).and_return(true) expect(@identifier.user_must_pay?).to eq(false) end it 'returns false if institution will pay' do - allow(@identifier).to receive(:'institution_will_pay?').and_return(true) + allow(@identifier).to receive(:institution_will_pay?).and_return(true) expect(@identifier.user_must_pay?).to eq(false) end end + describe '#record_payment' do + it 'does nothing when a payment has already been recorded' do + @identifier.payment_type = 'bogus_payment_type' + expect(@identifier.record_payment).to eq(nil) + expect(@identifier.payment_type).to eq('bogus_payment_type') + end + + it 'records a journal payment' do + allow(@identifier).to receive(:journal_will_pay?).and_return(true) + allow(@identifier).to receive(:publication_data).and_return('bogus_journal_data') + @identifier.record_payment + expect(@identifier.payment_type).to match(/journal/) + end + + it 'records an institution payment' do + allow(@identifier).to receive(:institution_will_pay?).and_return(true) + @identifier.record_payment + expect(@identifier.payment_type).to eq('institution') + end + + it 'records an a country-based fee waiver' do + affil = double(StashDatacite::Affiliation) + allow(affil).to receive(:fee_waivered?).and_return(true) + allow(affil).to receive(:country_name).and_return('Bogusland') + allow(@identifier).to receive(:submitter_affiliation).and_return(affil) + allow(@identifier).to receive(:publication_data).and_return('bogus_journal_data') + @identifier.record_payment + expect(@identifier.payment_type).to eq('waiver') + end + end + describe '#publication_data' do it 'reads the value correctly from json body' do stub_request(:any, %r{/journals/#{@fake_issn}}) diff --git a/stash/stash_engine/spec/unit/models/curation_activity_spec.rb b/stash/stash_engine/spec/unit/models/curation_activity_spec.rb index cd103b8337..d2cede2552 100644 --- a/stash/stash_engine/spec/unit/models/curation_activity_spec.rb +++ b/stash/stash_engine/spec/unit/models/curation_activity_spec.rb @@ -43,10 +43,7 @@ module StashEngine @resource.update(current_resource_state_id: @resource_state.id) @version = create(:version, resource_id: @resource.id) - # @curation_activity = create(:curation_activity) - # @curation_activity.update(identifier_id: @identifier.id) @mock_idgen = spy('idgen') - # allow(@mock_idgen).to receive('update_identifier_metadata!'.intern).and_raise('submitted DOI') allow(@mock_idgen).to receive('update_identifier_metadata!'.intern) # .and_return('called make metadata') allow(Stash::Doi::IdGen).to receive(:make_instance).and_return(@mock_idgen) @@ -56,9 +53,6 @@ module StashEngine CurationActivity.skip_callback(:save, :after, :process_payment) @curation_activity1 = create(:curation_activity, resource: @resource) CurationActivity.set_callback(:create, :after, :submit_to_datacite) - # these two never need to fire to test this example - # CurationActivity.set_callback(:create, :after, :update_solr) - # CurationActivity.set_callback(:save, :after, :submit_to_stripe) end it "doesn't submit when a status besides Embargoed or Published is set" do From e069b88646bb84391e8a67e51f95aec942fd361d Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Mon, 13 Jan 2020 22:53:16 +0000 Subject: [PATCH 09/11] writing to CSV --- .../lib/tasks/stash_engine_tasks.rake | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index 3f9f5e3238..bbcd7aeb01 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -176,15 +176,20 @@ namespace :identifiers do # Get the year-month specified in YEAR_MONTH environment variable. # If none, default to the previously completed month. if ENV['YEAR_MONTH'].blank? - puts 'No month specified, assuming last month.' + p 'No month specified, assuming last month.' year_month = 1.month.ago.strftime('%Y-%m') else year_month = ENV['YEAR_MONTH'] end - p "Shopping Cart Report for #{year_month}" - StashEngine::Identifier.publicly_viewable.each do |i| - ## get the variables associated with publication status - ## output a CSV of the metadata + p "Writing Shopping Cart Report for #{year_month} to file..." + CSV.open("shopping_cart_report_#{year_month}.csv", "w") do |csv| + csv << ["DOI", "Approval Date", "Payment Type", "Payment ID", "Journal Name", "Sponsor Name"] + StashEngine::Identifier.publicly_viewable.each do |i| + approval_date_str = i.approval_date&.strftime('%Y-%m-%d') + if approval_date_str&.start_with?(year_month) + csv << [ i.identifier, approval_date_str, i.payment_type, i.payment_id, i.publication_name ] + end + end end # Exit cleanly (don't let rake assume that an extra argument is another task to process) exit From 5f3cd2b48af667d3ba098d176b2ebfe83fdcc6e2 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Tue, 14 Jan 2020 01:43:51 +0000 Subject: [PATCH 10/11] rubocop --- stash/stash_engine/lib/tasks/stash_engine_tasks.rake | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index bbcd7aeb01..ff482a2d01 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -182,13 +182,11 @@ namespace :identifiers do year_month = ENV['YEAR_MONTH'] end p "Writing Shopping Cart Report for #{year_month} to file..." - CSV.open("shopping_cart_report_#{year_month}.csv", "w") do |csv| - csv << ["DOI", "Approval Date", "Payment Type", "Payment ID", "Journal Name", "Sponsor Name"] + CSV.open("shopping_cart_report_#{year_month}.csv", 'w') do |csv| + csv << ['DOI', 'Approval Date', 'Payment Type', 'Payment ID', 'Journal Name', 'Sponsor Name'] StashEngine::Identifier.publicly_viewable.each do |i| approval_date_str = i.approval_date&.strftime('%Y-%m-%d') - if approval_date_str&.start_with?(year_month) - csv << [ i.identifier, approval_date_str, i.payment_type, i.payment_id, i.publication_name ] - end + csv << [i.identifier, approval_date_str, i.payment_type, i.payment_id, i.publication_name] if approval_date_str&.start_with?(year_month) end end # Exit cleanly (don't let rake assume that an extra argument is another task to process) From c159e2bd0e3f8bf139f4f6840fc9b196eb5d5332 Mon Sep 17 00:00:00 2001 From: Ryan Scherle Date: Tue, 14 Jan 2020 15:50:33 +0000 Subject: [PATCH 11/11] add names for journal sponsors --- .../app/models/stash_engine/identifier.rb | 4 ++++ .../lib/tasks/stash_engine_tasks.rake | 4 +++- .../spec/db/stash_engine/identifier_spec.rb | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/stash/stash_engine/app/models/stash_engine/identifier.rb b/stash/stash_engine/app/models/stash_engine/identifier.rb index f8e0f5af2a..2b2053f76e 100644 --- a/stash/stash_engine/app/models/stash_engine/identifier.rb +++ b/stash/stash_engine/app/models/stash_engine/identifier.rb @@ -281,6 +281,10 @@ def journal_customer_id publication_data('stripeCustomerID') end + def journal_sponsor_name + publication_data('sponsorName') + end + def journal_notify_contacts publication_data('notifyContacts') end diff --git a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake index ff482a2d01..ef323ede42 100644 --- a/stash/stash_engine/lib/tasks/stash_engine_tasks.rake +++ b/stash/stash_engine/lib/tasks/stash_engine_tasks.rake @@ -186,7 +186,9 @@ namespace :identifiers do csv << ['DOI', 'Approval Date', 'Payment Type', 'Payment ID', 'Journal Name', 'Sponsor Name'] StashEngine::Identifier.publicly_viewable.each do |i| approval_date_str = i.approval_date&.strftime('%Y-%m-%d') - csv << [i.identifier, approval_date_str, i.payment_type, i.payment_id, i.publication_name] if approval_date_str&.start_with?(year_month) + if approval_date_str&.start_with?(year_month) + csv << [i.identifier, approval_date_str, i.payment_type, i.payment_id, i.publication_name, i.journal_sponsor_name] + end end end # Exit cleanly (don't let rake assume that an extra argument is another task to process) diff --git a/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb b/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb index c50162dc39..057b8c7654 100644 --- a/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb +++ b/stash/stash_engine/spec/db/stash_engine/identifier_spec.rb @@ -440,6 +440,22 @@ module StashEngine end end + describe '#journal_sponsor_name' do + it 'retrieves the journal sponsorName' do + @fake_journal_name = 'Fake Journal' + @fake_sponsor_name = 'sponsor_abc123' + stub_request(:any, %r{/journals/#{@fake_issn}}) + .to_return(body: '{"fullName":"' + @fake_journal_name + '", + "issn":"' + @fake_issn + '", + "website":"http://onlinelibrary.wiley.com/journal/10.1111/(ISSN)1365-294X", + "description":"Molecular Ecology publishes papers that utilize molecular genetic techniques...", + "sponsorName":"' + @fake_sponsor_name + '"}', + status: 200, + headers: { 'Content-Type' => 'application/json' }) + expect(identifier.journal_sponsor_name).to eq(@fake_sponsor_name) + end + end + describe '#journal_allows' do it 'allows review when there is no journal' do allow(@identifier).to receive('publication_name').and_return(nil)