diff --git a/app/controllers/api/v0/base_controller.rb b/app/controllers/api/v0/base_controller.rb index 7e939fd..b36bfa8 100644 --- a/app/controllers/api/v0/base_controller.rb +++ b/app/controllers/api/v0/base_controller.rb @@ -131,7 +131,7 @@ # end # end # end - OrgTokenPermission.where(organisation_id: @user.organisation_id).find_each do |org_token_permission| + OrgTokenPermission.where(org_id: @user.org_id).find_each do |org_token_permission| logger.debug "#{org_token_permission.token_permission_type.token_type}" if org_token_permission.token_permission_type.token_type == auth_type auth= true diff --git a/app/controllers/api/v0/dmptemplates_controller.rb b/app/controllers/api/v0/dmptemplates_controller.rb deleted file mode 100644 index 0e0a256..0000000 --- a/app/controllers/api/v0/dmptemplates_controller.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Api - module V0 - class DmptemplatesController < Api::V0::BaseController - before_action :authenticate - - - ## - # GET - # @return a list of templates ordered by organisation - def index - # check if the user has permissions to use the templates API - if has_auth(constant("api_endpoint_types.templates")) - @all_templates = Dmptemplate.all - respond_with @all_templates - else - #render unauthorised - render json: I18n.t("api.no_auth_for_endpoint"), status: 401 - end - - end - end - end -end \ No newline at end of file diff --git a/app/controllers/api/v0/plans_controller.rb b/app/controllers/api/v0/plans_controller.rb new file mode 100644 index 0000000..ad76c3d --- /dev/null +++ b/app/controllers/api/v0/plans_controller.rb @@ -0,0 +1,104 @@ +module Api + module V0 + class ProjectsController < 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 + 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 + organization = Org.find_by name: params[:template][:organisation] + + # if organization exists + if !organization.nil? + # if organization is funder + if organization.organisation_type == (OrganisationType.find_by(name: constant("organisation_types.funder"))) + # if organization has only 1 template + if organization.dmptemplates.length == 1 + # set template id + dmptemplate = organization.dmptemplates.first + # else if params.template.name specified && params.template.name == one of organization's tempates + elsif !organization.dmptemplates.find_by title: params[:template][:name].nil? + # set template id + dmptemplate = organization.templates.find_by title: params[:template][:name] + # else error: organization has more than one template and template name unspecified + else + render json: I18n.t("api.org_multiple_templates"), status: 400 and return + end + # else error: organization specified is not a funder + else + render json: I18n.t("api.org_not_funder"), status: 400 and return + end + # else error: organization does not exist + else + render json: I18n.t("api.org_dosent_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 = Project.new + @project.title = params[:project][:title] + @project.dmptemplate = dmptemplate + @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 + else + + render json: I18n.t("api.no_auth_for_endpoint"), status: 400 and return + end + end + + # private + # def project_params + # params.require(:template).permit(:organisation, :name) + # params.require(:project).permit(:title, :email) + # end + end + end +end diff --git a/app/controllers/api/v0/projects_controller.rb b/app/controllers/api/v0/projects_controller.rb deleted file mode 100644 index ad76c3d..0000000 --- a/app/controllers/api/v0/projects_controller.rb +++ /dev/null @@ -1,104 +0,0 @@ -module Api - module V0 - class ProjectsController < 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 - 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 - organization = Org.find_by name: params[:template][:organisation] - - # if organization exists - if !organization.nil? - # if organization is funder - if organization.organisation_type == (OrganisationType.find_by(name: constant("organisation_types.funder"))) - # if organization has only 1 template - if organization.dmptemplates.length == 1 - # set template id - dmptemplate = organization.dmptemplates.first - # else if params.template.name specified && params.template.name == one of organization's tempates - elsif !organization.dmptemplates.find_by title: params[:template][:name].nil? - # set template id - dmptemplate = organization.templates.find_by title: params[:template][:name] - # else error: organization has more than one template and template name unspecified - else - render json: I18n.t("api.org_multiple_templates"), status: 400 and return - end - # else error: organization specified is not a funder - else - render json: I18n.t("api.org_not_funder"), status: 400 and return - end - # else error: organization does not exist - else - render json: I18n.t("api.org_dosent_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 = Project.new - @project.title = params[:project][:title] - @project.dmptemplate = dmptemplate - @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 - else - - render json: I18n.t("api.no_auth_for_endpoint"), status: 400 and return - end - end - - # private - # def project_params - # params.require(:template).permit(:organisation, :name) - # params.require(:project).permit(:title, :email) - # end - end - end -end diff --git a/app/controllers/api/v0/templates_controller.rb b/app/controllers/api/v0/templates_controller.rb new file mode 100644 index 0000000..3ab1207 --- /dev/null +++ b/app/controllers/api/v0/templates_controller.rb @@ -0,0 +1,34 @@ +module Api + module V0 + class TemplatesController < Api::V0::BaseController + before_action :authenticate + + + ## + # GET + # @return a list of templates ordered by organisation + def index + # check if the user has permissions to use the templates API + if has_auth(constant("api_endpoint_types.templates")) + @org_templates = {} + published_templates = Template.includes(:org).where(customization_of: nil, published: true).order(:org_id, :version) + published_templates.all.each do |temp| + if @org_templates[temp.org].present? + if @org_templates[temp.org][temp.dmptemplate_id].nil? + @org_templates[temp.org][temp.dmptemplate_id] = temp + end + else + @org_templates[temp.org] = {} + @org_templates[temp.org][temp.dmptemplate_id] = temp + end + end + respond_with @org_templates + else + #render unauthorised + render json: I18n.t("api.no_auth_for_endpoint"), status: 401 + end + + end + end + end +end \ No newline at end of file diff --git a/app/models/org.rb b/app/models/org.rb index 4ede001..b75f6d8 100644 --- a/app/models/org.rb +++ b/app/models/org.rb @@ -190,7 +190,7 @@ # # @return [Array] published dmptemplates def published_templates - return dmptemplates.where("published = ?", true) + return templates.where("published = ?", true) end def check_api_credentials diff --git a/app/models/user.rb b/app/models/user.rb index 4a915eb..6932106 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -226,7 +226,7 @@ end self.save! # send an email to the user to notify them of their new api token - UserMailer.api_token_granted_notification(self) + #UserMailer.api_token_granted_notification(self) end end end diff --git a/app/views/api/v0/dmptemplates/index.json.jbuilder b/app/views/api/v0/dmptemplates/index.json.jbuilder deleted file mode 100644 index 8ea25b3..0000000 --- a/app/views/api/v0/dmptemplates/index.json.jbuilder +++ /dev/null @@ -1,14 +0,0 @@ -# builds a json response to api query for a list of all dmptemplates -json.prettify! - -json.templates Org.all.each do |org| - unless org.published_templates.blank? - json.organisation_name org.name - json.organisation_id org.id - json.organisation_templates org.published_templates.each do |template| - json.title template.title - json.id template.id - json.description template.description - end - end -end \ No newline at end of file diff --git a/app/views/api/v0/plans/create.json.jbuilder b/app/views/api/v0/plans/create.json.jbuilder new file mode 100644 index 0000000..ebb8c58 --- /dev/null +++ b/app/views/api/v0/plans/create.json.jbuilder @@ -0,0 +1,15 @@ +# builds a json response to a successful project createtion + +json.prettify! + +json.project do + json.title @project.title + # TODO add after decision on user creation/identification + #json.created_by @project.owner.email + json.id @project.id + json.created_at @project.created_at +end + +# json.location do +# json.link (url_for action: 'show', controller: 'project') +# end diff --git a/app/views/api/v0/plans/show.json.jbuilder b/app/views/api/v0/plans/show.json.jbuilder new file mode 100644 index 0000000..2cd84bf --- /dev/null +++ b/app/views/api/v0/plans/show.json.jbuilder @@ -0,0 +1,13 @@ +# builds a json response to a successful project createtion + +json.prettify! + +json.project do + json.title @project.title + # TODO add after decision on user creation/identification + json.created_by @project.owner.email + json.id @project.id + json.created_at @project.created_at + #json.template @project.dmptemplate + json.dmptemplate @project.dmptemplate.title +end diff --git a/app/views/api/v0/projects/create.json.jbuilder b/app/views/api/v0/projects/create.json.jbuilder deleted file mode 100644 index ebb8c58..0000000 --- a/app/views/api/v0/projects/create.json.jbuilder +++ /dev/null @@ -1,15 +0,0 @@ -# builds a json response to a successful project createtion - -json.prettify! - -json.project do - json.title @project.title - # TODO add after decision on user creation/identification - #json.created_by @project.owner.email - json.id @project.id - json.created_at @project.created_at -end - -# json.location do -# json.link (url_for action: 'show', controller: 'project') -# end diff --git a/app/views/api/v0/projects/show.json.jbuilder b/app/views/api/v0/projects/show.json.jbuilder deleted file mode 100644 index 2cd84bf..0000000 --- a/app/views/api/v0/projects/show.json.jbuilder +++ /dev/null @@ -1,13 +0,0 @@ -# builds a json response to a successful project createtion - -json.prettify! - -json.project do - json.title @project.title - # TODO add after decision on user creation/identification - json.created_by @project.owner.email - json.id @project.id - json.created_at @project.created_at - #json.template @project.dmptemplate - json.dmptemplate @project.dmptemplate.title -end diff --git a/app/views/api/v0/templates/index.json.jbuilder b/app/views/api/v0/templates/index.json.jbuilder new file mode 100644 index 0000000..643d87d --- /dev/null +++ b/app/views/api/v0/templates/index.json.jbuilder @@ -0,0 +1,12 @@ +# builds a json response to api query for a list of all dmptemplates +json.prettify! + +json.templates @org_templates.each do |org, templates| + json.organisation_name org.name + json.organisation_id org.id + json.organisation_templates templates.each do |_, template| + json.title template.title + json.id template.id + json.description template.description + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 97252d6..8e6d1d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -156,8 +156,8 @@ namespace :api, defaults: {format: :json} do namespace :v0 do resources :guidance_groups, only: [:index, :show] - resources :plans, only: :create, controller: "projects", path: "plans" - resources :templates, only: :index, controller: "dmptemplates", path: "templates" + resources :plans, only: :create + resources :templates, only: :index resource :statistics, only: [], controller: "statistics", path: "statistics" do member do get :users_joined