Project json exporter adaptions for step improvements [SCI-6898] (#4177)

* Adapt project export to step improvements [SCI-6898]

* Add tests [SCI-6898]
This commit is contained in:
ajugo 2022-07-06 11:45:50 +02:00 committed by GitHub
parent 9323c5e99a
commit 8e7307c5c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 212 additions and 44 deletions

View file

@ -59,43 +59,22 @@ class ProjectsJsonExportService
private
def protocol_json(protocol)
protocol_json = protocol.as_json(only: %i(id description))
protocol_json = protocol.as_json(only: %i(id name))
t_m_files = protocol.tiny_mce_assets.map { |asset| tiny_mce_file_json(asset) }
protocol_json['tiny_mce_files'] = t_m_files if t_m_files.any?
protocol_json
end
def step_json(step)
step_json = step.as_json(only: %i(id position name description completed))
checklists = []
step.checklists.find_each do |cl|
checklist = cl.as_json(only: %i(id name))
items = []
cl.checklist_items.find_each do |cli|
item = cli.as_json(only: %i(id position text checked))
items << item
step_json = step.as_json(only: %i(id position name completed))
if step.step_orderable_elements.any?
step_elements = step.step_orderable_elements.order(position: :asc).map do |step_orderable_element|
step_element_json(step_orderable_element)
end
checklist['items'] = items if items.any?
checklists << checklist
end
step_json['checklists'] = checklists if checklists.any?
files = []
step.assets.find_each do |asset|
files << asset_file_json(asset)
step_json['elements'] = step_elements if step_elements.any?
end
files = asset_file_json(step)
step_json['files'] = files if files.any?
t_m_files = []
step.tiny_mce_assets.find_each do |asset|
t_m_files << tiny_mce_file_json(asset)
end
step_json['tiny_mce_files'] = t_m_files if t_m_files.any?
tables = []
step.tables.find_each do |tbl|
table = tbl.as_json(only: %i(id name))
table['contents'] = JSON.parse(tbl.contents)['data']
tables << table
end
step_json['tables'] = tables if tables.any?
step_json
end
@ -111,7 +90,6 @@ class ProjectsJsonExportService
end
task_json['steps'] = steps if steps.any?
results = []
Rails.logger.info(task.results.to_yaml.to_s)
task.results
.where(archived: false)
.order(created_at: :desc)
@ -142,22 +120,48 @@ class ProjectsJsonExportService
result_json
end
def asset_file_json(asset)
if ENV['ACTIVESTORAGE_SERVICE'] && ENV['S3_BUCKET']
{
'storage' => ENV['ACTIVESTORAGE_SERVICE'],
'id' => asset.id,
'bucket' => ENV['S3_BUCKET'],
'key' => ActiveStorage::Blob.service.path_for(asset.file.key),
'url' => asset_url(asset)
}
else
{
'storage' => 'local',
'id' => asset.id,
'url' => asset_url(asset)
}
def step_element_json(step_orderable_element)
element = step_orderable_element.orderable
element_json = step_orderable_element.as_json(only: %i(id position orderable_type))
case element
when StepText
text = { text: element.text }
t_m_files = element.tiny_mce_assets.map { |asset| tiny_mce_file_json(asset) }
text['tiny_mce_files'] = t_m_files if t_m_files.any?
element_json['data'] = text
when StepTable
table = element.table.as_json(only: %i(id name))
table['contents'] = JSON.parse(element.table.contents)['data']
element_json['data'] = table
when Checklist
checklist = element.as_json(only: %i(id name))
items = element.checklist_items.map { |ci| ci.as_json(only: %i(id position text checked)) }
checklist['items'] = items if items.any?
element_json['data'] = checklist
end
element_json
end
def asset_file_json(step)
files = []
step.assets.find_each do |asset|
files << if ENV.fetch('ACTIVESTORAGE_SERVICE') && ENV.fetch('S3_BUCKET')
{
'storage' => ENV.fetch('ACTIVESTORAGE_SERVICE'),
'id' => asset.id,
'bucket' => ENV.fetch('S3_BUCKET'),
'key' => ActiveStorage::Blob.service.path_for(asset.file.key),
'url' => asset_url(asset)
}
else
{
'storage' => 'local',
'id' => asset.id,
'url' => asset_url(asset)
}
end
end
files
end
def tiny_mce_file_json(asset)

View file

@ -0,0 +1,77 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Api::Service::ProjectsJsonExportController", type: :request do
before :all do
@user = create(:user)
@team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: 1)
@accessible_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@accessible_experiment = create(:experiment, created_by: @user,
last_modified_by: @user, project: @accessible_project)
@unaccessible_experiment = create(:experiment, created_by: @user,
last_modified_by: @user, project: @accessible_project)
@accessible_task = create(:my_module, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @accessible_experiment)
@unaccessible_task = create(:my_module, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @unaccessible_experiment)
@unaccessible_experiment.user_assignments.destroy_all
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
end
describe 'POST get projects json export, #projects_json_export' do
before :all do
@valid_headers['Content-Type'] = 'application/json'
end
let(:request_body) do
{
data: {
"callback_url": Faker::Internet.url,
"task_ids": [@accessible_task.id, @unaccessible_task.id]
}
}
end
let(:action) do
post(
api_service_projects_json_export_path,
params: request_body.to_json,
headers: @valid_headers
)
end
context 'when has valid params' do
it 'returns status 202' do
action
expect(response).to have_http_status 202
end
end
context 'when has invalid params' do
it 'Missing task_ids parameter' do
request_body[:data].delete(:task_ids)
action
expect(response).to have_http_status 400
end
it 'Missing callback_url parameter' do
request_body[:data].delete(:callback_url)
action
expect(response).to have_http_status 400
end
it 'Invalid callback url' do
request_body[:data][:callback_url] = Faker::Name.unique.name
action
expect(response).to have_http_status 400
end
end
end
end

View file

@ -0,0 +1,87 @@
require 'rails_helper'
describe ProjectsJsonExportService do
before :all do
@user = create(:user)
@team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: 1)
@accessible_project_1 = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@accessible_project_2 = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@accessible_experiment_1 = create(:experiment, created_by: @user,
last_modified_by: @user, project: @accessible_project_1)
@accessible_experiment_2 = create(:experiment, created_by: @user,
last_modified_by: @user, project: @accessible_project_2)
@unaccessible_experiment = create(:experiment, created_by: @user,
last_modified_by: @user, project: @unaccessible_project)
@accessible_task_1 = create(:my_module, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @accessible_experiment_1)
@accessible_task_2 = create(:my_module, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @accessible_experiment_2)
@unaccessible_task = create(:my_module, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @unaccessible_experiment)
@unaccessible_project.user_assignments.destroy_all
end
describe 'Generate project json' do
let(:action) do
ProjectsJsonExportService.new(task_ids, Faker::Internet.url, @user).generate_data
end
let(:one_project) do
[@accessible_task_1.id]
end
let(:multiple_projects) do
[@accessible_task_1.id, @accessible_task_2.id, @unaccessible_task.id]
end
let(:no_accessible_projects) do
[@unaccessible_task.id]
end
context 'One project' do
let(:task_ids) do
one_project
end
it 'Get response one project' do
response = action
expect(response.length).to eq 1
expect(response[0].keys).to contain_exactly('id', 'name', 'experiments')
expect(response[0]['experiments'].length).to eq 1
expect(response[0]['experiments'][0].keys).to contain_exactly('id', 'name', 'description', 'tasks')
expect(response[0]['experiments'][0]['tasks'].length).to eq 1
end
end
context 'Multiple projects' do
let(:task_ids) do
multiple_projects
end
it 'Get response multiple projects' do
response = action
expect(response.length).to eq 2
expect(response[0].keys).to contain_exactly('id', 'name', 'experiments')
expect(response[0]['experiments'].length).to eq 1
expect(response[0]['experiments'][0].keys).to contain_exactly('id', 'name', 'description', 'tasks')
expect(response[0]['experiments'][0]['tasks'].length).to eq 1
end
end
context 'Not accessible project' do
let(:task_ids) do
no_accessible_projects
end
it 'Get response for not accessible project' do
response = action
expect(response.length).to eq 0
end
end
end
end