Skip to content

Commit

Permalink
Merge pull request #595 from CDLUC3/drafts-in-api
Browse files Browse the repository at this point in the history
Add plans from DMP Upload pages to API v2
  • Loading branch information
briri committed May 22, 2024
2 parents c43efcf + 24d27b6 commit 60579ee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/controllers/api/v2/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def index
@scope = 'mine'
@scope = params[:scope].to_s.downcase if %w[mine public both].include?(params[:scope].to_s.downcase)

# If the User is part of the pilot project then include their DMP Upload Drafts
plans = @client&.user&.org&.v5_pilot? ? Draft.by_org(org_id: @client&.user&.org_id) : []

# See the Policy for details on what Plans are returned to the Caller based on the AccessToken
plans = Api::V2::PlansPolicy::Scope.new(@client, @resource_owner, @scope).resolve
plans += Api::V2::PlansPolicy::Scope.new(@client, @resource_owner, @scope).resolve

if plans.present? && plans.any?
plans = plans.sort { |a, b| b.updated_at <=> a.updated_at }
Expand All @@ -45,6 +48,9 @@ def show
@plan = Api::V2::PlansPolicy::Scope.new(@client, @resource_owner, 'both').resolve
.find { |plan| plan.id.to_s == params[:id] }

# See if it's a Draft if the Plan was not found
@plan = Draft.find_by(id: params[:id].gsub('d_', '')) if @plan.nil? &&
@client&.user&.org&.v5_pilot?
if @plan.present?
respond_to do |format|
format.pdf do
Expand Down
6 changes: 6 additions & 0 deletions app/models/draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Draft < ApplicationRecord
validates :narrative, size: { less_than: 250.kilobytes , message: 'PDF too large, must be less than 350KB' },
content_type: { in: ['application/pdf'], message: 'must be a PDF document' }

def self.by_org(org_id:)
return [] if org_id.nil?

includes(:user).joins(:user).where(user: { org_id: org_id })
end

# Support for filtering and search
def self.search(user:, params: {})
return [] unless user.is_a?(User) && !user.org_id.nil?
Expand Down
29 changes: 29 additions & 0 deletions app/views/branded/api/v2/plans/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

json.partial! 'api/v2/standard_response', total_items: @total_items

json.items @items do |item|
if item.is_a?(Plan)
json.dmp do
json.partial! 'api/v2/plans/show', plan: item
end
elsif item.is_a?(Draft)
draft = item.dmp_id.nil? ? item.metadata : DmpIdService.fetch_dmp_id(dmp_id: item.dmp_id)
dmp = draft.is_a?(Hash) ? draft['dmp'] : {}

url = api_v3_draft_url(item.id).gsub('/v3/drafts/', '/v2/plans/')
.gsub(item.id.to_s, "d_#{item.id}")

if dmp['dmp_id'].nil?
dmp['dmp_id'] = JSON.parse({ type: 'url', identifier: url }.to_json)
end

dmp.delete('draft_data')

links = { get: url }
links[:download] = item.send(:safe_narrative_url) if item.narrative.attached?
dmp['dmproadmap_links'] = JSON.parse(links.to_json)

json.dmp item.metadata['dmp']
end
end

0 comments on commit 60579ee

Please sign in to comment.