• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / tigerdata-app / f746ef9f-93ec-4b82-81e5-05180385661c

11 Aug 2025 04:23PM UTC coverage: 76.534% (+0.02%) from 76.512%
f746ef9f-93ec-4b82-81e5-05180385661c

push

circleci

web-flow
Updating the create project operation to allow for success and failure (#1712)

* Updating the create project operation to allow for succes and failure

checking the class when getting the result of the operation

* Fixed a typo

---------

Co-authored-by: Hector Correa <hector_correa@princeton.edu>

14 of 33 new or added lines in 3 files covered. (42.42%)

2270 of 2966 relevant lines covered (76.53%)

176.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.14
/app/operations/project_create.rb
1
# frozen_string_literal: true
2
class ProjectCreate < Dry::Operation
1✔
3
  class ProjectCreateError < StandardError; end
1✔
4

5
  # TODO: Remove the optional project parameter once https://github.com/pulibrary/tigerdata-app/issues/1707
6
  # has been completed. In practice the project is always created from the request.
7
  def call(request:, approver:, project: nil)
1✔
8
    if project.nil?
1✔
9
      project = step create_project_from_request(request)
1✔
10
    end
11
    mediaflux_id = step persist_in_mediaflux(project, approver)
1✔
NEW
12
    step update_project_with_mediaflux_info(mediaflux_id:, project:)
×
13
    step persist_users_in_mediaflux(project, approver)
×
14
  end
15

16
  private
1✔
17

18
    def create_project_from_request(request)
1✔
19
      # Create the project in the Rails database
20
      project_metadata_json = RequestProjectMetadata.convert(request)
1✔
21
      project = Project.create!({ metadata_json: project_metadata_json })
1✔
22
      project.draft_doi
1✔
23
      project.save!
1✔
24

25
      # Return Success(attrs) or Failure(error)
26
      Success project
1✔
27
    rescue Exception => ex
NEW
28
      Failure("Error creating the project: #{ex}")
×
29
    end
30

31
    def persist_in_mediaflux(project, current_user)
1✔
32
      # Create the project in Mediaflux
33
      mediaflux_request = Mediaflux::ProjectCreateServiceRequest.new(session_token: current_user.mediaflux_session, project: project)
1✔
34
      mediaflux_request.resolve
1✔
35

36
      mediaflux_id = mediaflux_request.mediaflux_id
1✔
37

38
      if mediaflux_id.to_i == 0
1✔
39
        debug_output = "Error saving project #{project.id} to Mediaflux: #{mediaflux_request.response_error}. Debug output: #{mediaflux_request.debug_output}"
1✔
40
        Rails.logger.error debug_output
1✔
41

42
        # we do not want unsaved projects just hanging around
43
        project.destroy!
1✔
44
        Failure debug_output
1✔
45
      else
NEW
46
        ProvenanceEvent.generate_approval_events(project: project, user: current_user, debug_output: mediaflux_request.debug_output.to_s)
×
NEW
47
        Success(mediaflux_id)
×
48
      end
49
    end
50

51
    def update_project_with_mediaflux_info(mediaflux_id:, project:)
1✔
NEW
52
      project.mediaflux_id = mediaflux_id
×
NEW
53
      project.metadata_model.status = Project::APPROVED_STATUS
×
NEW
54
      project.save!
×
NEW
55
      Success(project)
×
56
    rescue Exception => ex
57
      # TODO: It was saved in mediaflux, so maybe a retry here?  I don't want to destroy the project
NEW
58
      Failure("Setting the mediaflux id the project(#{project.id}) : #{ex}")
×
59
    end
60

61
    def persist_users_in_mediaflux(project, current_user)
1✔
62
      # Do nothing if there are no users to persist
NEW
63
      return Success(project) if (project.metadata_model.ro_users + project.metadata_model.rw_users).empty?
×
64

65
      # Add the data users to the project in Mediaflux
66
      add_users_request = Mediaflux::ProjectUserAddRequest.new(session_token: current_user.mediaflux_session, project: project)
×
67
      add_users_request.resolve
×
68

NEW
69
      if add_users_request.error?
×
70
        # TODO: what should we do with the project in this case?  It got all the way to mediaflux, but the users were not added
NEW
71
        Failure("Error adding users to mediaflux project #{project.mediaflux_id} #{add_users_request.response_error}")
×
72
      else
NEW
73
        user_debug = add_users_request.debug_output.to_s
×
NEW
74
        Rails.logger.debug { "Project #{project.id} users have been added to MediaFlux: #{user_debug}" }
×
NEW
75
        Success project
×
76
      end
77
    end
78
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc