-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from DigitalCurationCentre/xsrust/bugfixes
Xsrust/bugfixes
- Loading branch information
Showing
17 changed files
with
169 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,48 @@ | ||
module Api | ||
module V0 | ||
class ProjectsController < Api::V0::BaseController | ||
class PlansController < Api::V0::BaseController | ||
before_action :authenticate | ||
|
||
swagger_controller :projects, 'Plans' | ||
|
||
swagger_api :create do |api| | ||
summary 'Returns a single guidance group item' | ||
notes 'Notes...' | ||
param :header, 'Authentication-Token', :string, :required, 'Authentication-Token' | ||
response :unauthorized | ||
response :not_found | ||
end | ||
|
||
## | ||
# Creates a new project based on the information passed in JSON to the API | ||
# Creates a new plan based on the information passed in JSON to the API | ||
def create | ||
# find the user's api_token permissions | ||
# then ensure that they have the permission associated with creating plans | ||
if has_auth(constant("api_endpoint_types.plans")) | ||
#params[:organization_id] = Org.where(name: params[:template][:organization]) | ||
# find_by returns nil if none found, find_by! raises an ActiveRecord error | ||
org = Org.find_by name: params[:template][:organisation] | ||
|
||
# if organization exists | ||
if !org.nil? | ||
# if organization is funder | ||
if org.funder? | ||
# if organization has only 1 template | ||
if org.templates.length == 1 | ||
# set template id | ||
template = org.templates.first | ||
# else if params.template.name specified && params.template.name == one of organization's tempates | ||
elsif !org.templates.find_by title: params[:template][:name].nil? | ||
# set template id | ||
template = org.templates.find_by title: params[:template][:name] | ||
# else error: organization has more than one template and template name unspecified | ||
else | ||
render json: _('{"Error":"Organisation has more than one template and template name unspecified or invalid"}'), status: 400 and return | ||
end | ||
# else error: organization specified is not a funder | ||
else | ||
render json: _('{"Error":"Organisation specified is not a funder"}'), status: 400 and return | ||
end | ||
# else error: organization does not exist | ||
else | ||
render json: _('{"Error":"Organisation does not exist"}'), status: 400 and return | ||
end | ||
|
||
all_groups = [] | ||
# Check to see if the user specified guidances | ||
if !params[:guidance].nil? | ||
# for each specified guidance, see if it exists | ||
params[:guidance][:name].each do |guidance_name| | ||
group = GuidanceGroup.find_by(name: guidance_name) | ||
# if it exists, add it to the guidances for the new project | ||
if !group.nil? | ||
all_groups = all_groups + [group] | ||
end | ||
end | ||
end | ||
|
||
# cant invite a user without having a current user because of devise :ivitable | ||
# after we have auth, will be able to assign an :invited_by_id | ||
user = User.find_by email: params[:project][:email] | ||
# if user does not exist | ||
if user.nil? | ||
# invite user to DMPRoadmap | ||
User.invite!({email: params[:project][:email]}, ( @user)) | ||
# set project owner to user associated w/email | ||
user = (User.find_by email: params[:project][:email]) | ||
end | ||
|
||
# create new project with specified parameters | ||
@project = Plan.new | ||
@project.title = params[:project][:title] | ||
@project.template = template | ||
@project.slug = params[:project][:title] | ||
#@project.organisation = @user.organisations.first | ||
@project.assign_creator(user.id) | ||
@project.guidance_groups = all_groups | ||
|
||
# if save successful, render success, otherwise show error | ||
if @project.save | ||
#render json: @project ,status: :created | ||
render :show, status: :created | ||
else | ||
render json: get_resource.errors, status: :unprocessable_entity | ||
end | ||
@template = Template.live(params[:template_id]) | ||
raise Pundit::NotAuthorizedError unless Api::V0::PlansPolicy.new(@user, @template).create? | ||
|
||
plan_user = User.find_by(email: params[:plan][:email]) | ||
# ensure user exists | ||
if plan_user.blank? | ||
User.invite!({email: params[:plan][:email]}, ( @user)) | ||
plan_user = User.find_by(email: params[:plan][:email]) | ||
plan_user.org = @user.org | ||
plan_user.save | ||
end | ||
# ensure user's organisation is the same as api user's | ||
raise Pundit::NotAuthorizedError, _("user must be in your organisation") unless plan_user.org == @user.org | ||
|
||
# initialize the plan | ||
@plan = Plan.new | ||
@plan.principal_investigator = plan_user.surname.blank? ? nil : "#{plan_user.firstname} #{plan_user.surname}" | ||
@plan.data_contact = plan_user.email | ||
# set funder name to template's org, or original template's org | ||
if @template.customization_of.nil? | ||
@plan.funder_name = @template.org.name | ||
else | ||
|
||
render json: _('{"Error":"You do not have authorisation to view this endpoint"}'), status: 400 and return | ||
@plan.funder_name = Template.where(dmptemplate_id: @template.customization_of).first.org.name | ||
end | ||
@plan.template = @template | ||
@plan.title = params[:plan][:title] | ||
if @plan.save | ||
@plan.assign_creator(plan_user) | ||
respond_with @plan | ||
else | ||
# the plan did not save | ||
self.headers['WWW-Authenticate'] = "Token realm=\"\"" | ||
render json: _("Bad Parameters"), status: 400 | ||
end | ||
end | ||
|
||
# private | ||
# def project_params | ||
# params.require(:template).permit(:organisation, :name) | ||
# params.require(:project).permit(:title, :email) | ||
# end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Api | ||
module V0 | ||
class PlansPolicy < ApplicationPolicy | ||
attr_reader :user | ||
attr_reader :template | ||
|
||
def initialize(user, template) | ||
raise Pundit::NotAuthorizedError, _("must be logged in") unless user | ||
unless user.org.token_permission_types.include? TokenPermissionType::PLANS | ||
raise Pundit::NotAuthorizedError, _("must have access to plans api") | ||
end | ||
@user = user | ||
@template = template | ||
end | ||
|
||
## | ||
# users can create a plan if their template exists | ||
def create? | ||
@template.present? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 2 additions & 15 deletions
17
app/views/api/v0/statistics/plans_by_template.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,7 @@ | ||
json.prettify! | ||
templates = {} | ||
@org_projects.each do |plan| | ||
# if hash exists | ||
if templates[plan.template.title].blank? | ||
templates[plan.template.title] = {} | ||
templates[plan.template.title][:title] = plan.template.title | ||
templates[plan.template.title][:id] = plan.template.id | ||
templates[plan.template.title][:uses] = 1 | ||
else | ||
templates[plan.template.title][:uses] += 1 | ||
end | ||
end | ||
|
||
json.templates templates.each do |template, info| | ||
json.templates @templates.each do |template, info| | ||
json.template_name info[:title] | ||
json.template_id info[:id] | ||
json.template_uses info[:uses] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
json.prettify! | ||
|
||
json.plans_using_template @template_count | ||
json.templates @templates.each do |template, info| | ||
json.template_name info[:title] | ||
json.template_id info[:id] | ||
json.template_uses info[:uses] | ||
end |
Oops, something went wrong.