Skip to content

Commit

Permalink
validation to avoid duplicated reports
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Oct 29, 2018
1 parent 45d44b5 commit 7718ce7
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/controllers/concerns/helpeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def self.permit_recursive_params(params)
end
end

def get_month date
Date.strptime(date,"%Y-%m-%d").month.to_s
end

def get_year date
Date.strptime(date,"%Y-%m-%d").year.to_s
end

def self.get_em(h)
h.each_with_object([]) do |(k,v),keys|
keys << k
Expand Down
14 changes: 13 additions & 1 deletion app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ReportsController < ApplicationController
prepend_before_action :authenticate_user_from_token!
before_action :set_report, only: [:show, :destroy]
before_action :set_user_hash, only: [:create, :update, :destroy]
before_action :validate_monthly_report, only: [:create, :update]
authorize_resource :except => [:index, :show]


Expand Down Expand Up @@ -90,7 +91,15 @@ def update
end

def create
@report = Report.new(safe_params.merge(@user_hash))

@report = Report.where(created_by: safe_params[:created_by])
.where(month: get_month(safe_params.dig("reporting_period","begin_date")))
.where(year: get_year(safe_params.dig("reporting_period","begin_date")))
.where(client_id: safe_params.merge(@user_hash)[:client_id])
.first
exists = @report.present?

@report = Report.new(safe_params.merge(@user_hash)) unless @report.present?
authorize! :create, @report

if @report.save
Expand All @@ -114,6 +123,9 @@ def set_user_hash
@user_hash = { client_id: current_user.client_id, provider_id: current_user.provider_id }
end

def validate_monthly_report
fail JSON::ParserError, "Reports are monthly. reporting dates are within the same month" if get_month(safe_params.dig("reporting_period","begin_date")) != get_month(safe_params.dig("reporting_period","end_date"))
end

private

Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/files/report_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"created": "2128-04-09",
"created-by": "dash",
"reporting-period": {
"begin-date": "2128-04-09",
"end-date": "2128-04-09"
"begin-date": "2128-04-01",
"end-date": "2128-04-02"
},
"report-attributes": [],
"report-filters": [
Expand Down
98 changes: 98 additions & 0 deletions spec/fixtures/files/report_repeat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"report-header": {
"report-name": "dataset report",
"report-id": "DSR",
"release": "rd1",
"created": "2128-04-13",
"created-by": "dash",
"reporting-period": {
"begin-date": "2128-04-09",
"end-date": "2128-04-13"
},
"report-attributes": [],
"report-filters": [
{
"name": "begin-date",
"value": "2018-03-01"
},
{
"name": "end-date",
"value": "2018-03-31"
}
],
"exceptions": [
{
"code": 3040,
"severity": "warning",
"message": "partial data returned",
"help-url": "string",
"data": "usage data has not been processed for the entire reporting period"
}
]
},
"report-datasets": [
{
"dataset-title": "12345chemical shift-based methods in nmr structure determination",
"dataset-id": [
{
"type": "doi",
"value": "10.7291/d1q94r"
}
],
"dataset-contributors": [
{
"type": "name",
"value": "santrupti nerli"
},
{
"type": "name",
"value": "andrew c. mcshan"
},
{
"type": "name",
"value": "nikolaos g. sgourakis"
}
],
"dataset-dates": [
{
"type": "pub-date",
"value": "2018-05-01"
}
],
"platform": "dash",
"publisher": "uc santa cruz",
"publisher-id": [
{
"type": "orcid",
"value": "2059-4343-4343-4343"
}
],
"data-type": "dataset",
"yop": "2018",
"uri": "https://dash.library.ucsc.edu/stash/dataset/doi:10.7291/d1q94r",
"performance": [
{
"period": {
"begin-date": "2018-03-01",
"end-date": "2018-03-31"
},
"instance": [
{
"access-method": "regular",
"metric-type": "total-dataset-investigations",
"count": 3
},
{
"access-method": "regular",
"metric-type": "unique-dataset-investigations",
"count": 3
}
]
}
]
}]

}



13 changes: 13 additions & 0 deletions spec/requests/reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@
# end
# end

context 'when the request is valid but another report from the same month exist' do
let(:params) {file_fixture('report_3.json').read}
let(:params_repeat) {file_fixture('report_repeat.json').read}

before { post '/reports', params: params, headers: headers}
before { post '/reports', params: params_repeat, headers: headers}

it 'fails to create a report' do
expect(json.dig("report", "report-header", "created")).to eq("2128-04-09")
expect(json.dig("report", "report-datasets", 0, "dataset-title")).to eq("chemical shift-based methods in nmr structure determination")
expect(response).to have_http_status(201)
end
end

context 'index filter by client_id' do
let!(:bearer_ext) { User.generate_token(client_id: "datacite.demo", provider_id: "datacite", role_id: "staff_admin") }
Expand Down

0 comments on commit 7718ce7

Please sign in to comment.