Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add org_id to api_clients table and refactor of API v1 services #2725

Merged
merged 13 commits into from
Nov 23, 2020

Conversation

briri
Copy link
Contributor

@briri briri commented Nov 13, 2020

  • Added org_id as an optional FKey on the api_clients table
  • Updated UI to allow Org selection on 'Api Clients' screens
  • Added a scope to the API's plan controller policy
  • Refactored the API deserialization services:
    • Extracted validation logic to a separate service that can be called before actually processing the incoming JSON. It checks for the metadata we require specifically and will return specific issues to the caller
    • Extracted several common functions from the old deserialization services into a shared DeserializationService class
    • Added a ContextualErrorService that passes through the a Plan and all of its dependencies checking for errors. The resulting errors provide essential context for the caller (e.g. instead of getting 'name can't be blank' the caller would receive 'Funder name can't be blank')
    • API deserialization services no longer create/save records
    • Added a PersistenceService that allows for the 'safe' saving of the Plan and all of its dependencies. For example, calling plan.save would result in errors if it had 2 contributors belonging to the same org. The first org would get saved but the subsequent attempt would result in a duplicate record error. The methods in this class essentially just do a find_or_create. The entire process is wrapped inside a transaction so that if any failures are encountered the entire transaction is aborted.
    • Added logic to the PersistenceService that consolidates contributor/contact info so that { contact: { email: '[email protected]' } and { contributor: { email: '[email protected]', name: 'John Doe' } are combined into one single entry
    • Added tests for everything

I am considering adding a rake task that will take a json input and run it through these services to validate/provide feedback so that we can test the API more easily and also assist with debugging.

Copy link
Contributor

@xsrust xsrust left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good.

Thanks for the big refactor and for updating all the testing

Comment on lines +17 to +33
Plan.transaction do
plan.funder = safe_save_org(org: plan.funder)
plan.grant = safe_save_identifier(identifier: plan.grant)

plan.save

plan.identifiers.each do |id|
id.identifiable = plan.reload
safe_save_identifier(identifier: id)
end
plan.contributors.each do |contributor|
contributor.plan = plan.reload
safe_save_contributor(contributor: contributor)
end

plan.reload
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there's anywhere else in the application we generate multiple records like this (other than the Answers#create_or_update) but it would be good to be more consistent in using this Model.transaction do style save for any of those as well.

Comment on lines +37 to +51
def contextualize(errors:, context: "DMP")
errs = errors.is_a?(ActiveModel::Errors) ? errors.full_messages : []
errs = errors if errors.is_a?(Array) && errs.empty?
return errs unless errs.any?

errs.map do |msg|
msg.gsub("org name", "affiliation name")
.gsub("identifiers value", "identifier values")
.gsub("Contributors", "Contact/Contributor")
.gsub(/^Identifiers/, "#{context.capitalize} identifier")
.gsub(/^Name/, "#{context.capitalize} name")
.gsub(/^Title/, "#{context.capitalize} title")
.gsub(/^Value/, "#{context.capitalize} value")
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool approach to making the ActiveModel errors make sense to the API users without needing to re-invent the wheel:)

@xsrust xsrust merged commit 0abf99d into development Nov 23, 2020
@briri briri deleted the api-fix branch December 18, 2020 17:00
portagenetwork pushed a commit to portagenetwork/roadmap that referenced this pull request Feb 24, 2022
…oadmap#2725)

* updated contextualize_error_service

* added org_id to api_clients. Refactored API services to make them easier to manage and to provide better errors to clients

* updated test that fails randomly

* updated api to use new persistenceservice

* fixed rubocop issue

* remove rubocop disable that my machine wants butgithub actions does not

* remove rubocop disable that my machine wants butgithub actions does not

* remove rubocop disable that my machine wants butgithub actions does not

* fixed issue with safe_date tests because Travis instance time differs from local machine time

* fix for randomly failing test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants