-
Notifications
You must be signed in to change notification settings - Fork 110
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
Conversation
…ier to manage and to provide better errors to clients
… from local machine time
There was a problem hiding this 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
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 |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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:)
…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
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 afind_or_create
. The entire process is wrapped inside a transaction so that if any failures are encountered the entire transaction is aborted.{ contact: { email: '[email protected]' }
and{ contributor: { email: '[email protected]', name: 'John Doe' }
are combined into one single entryI 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.