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

pulibrary / pdc_describe / 9dbcf7a4-1c56-4510-9614-74ad5a22cff6

31 Jul 2024 02:46PM UTC coverage: 1.08% (-95.1%) from 96.17%
9dbcf7a4-1c56-4510-9614-74ad5a22cff6

push

circleci

jrgriffiniii
wip

52 of 4814 relevant lines covered (1.08%)

0.01 hits per line

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

0.0
/app/models/work.rb
1
# frozen_string_literal: true
2

3
# rubocop:disable Metrics/ClassLength
4
class Work < ApplicationRecord
×
5
  # Errors for cases where there is no valid Group
6
  class InvalidGroupError < ::ArgumentError; end
×
7

8
  has_many :work_activity, -> { order(updated_at: :desc) }, dependent: :destroy
×
9
  has_many :user_work, -> { order(updated_at: :desc) }, dependent: :destroy
×
10
  has_many :upload_snapshots, -> { order(updated_at: :desc) }, dependent: :destroy
×
11

12
  belongs_to :group, class_name: "Group"
×
13
  belongs_to :curator, class_name: "User", foreign_key: "curator_user_id", optional: true
×
14

15
  attribute :work_type, :string, default: "DATASET"
×
16
  attribute :profile, :string, default: "DATACITE"
×
17

18
  attr_accessor :user_entered_doi
×
19

20
  alias state_history user_work
×
21

22
  delegate :valid_to_submit, :valid_to_draft, :valid_to_approve, :valid_to_complete, to: :work_validator
×
23

24
  include AASM
×
25

26
  aasm column: :state do
×
27
    state :none, initial: true
×
28
    state :draft, :awaiting_approval, :approved, :withdrawn, :deletion_marker
×
29

30
    event :draft, after: :draft_doi do
×
31
      transitions from: :none, to: :draft, guard: :valid_to_draft
×
32
    end
×
33

34
    event :complete_submission do
×
35
      transitions from: :draft, to: :awaiting_approval, guard: :valid_to_complete
×
36
    end
×
37

38
    event :request_changes do
×
39
      transitions from: :awaiting_approval, to: :awaiting_approval, guard: :valid_to_submit
×
40
    end
×
41

42
    event :approve do
×
43
      transitions from: :awaiting_approval, to: :approved, guard: :valid_to_approve, after: :publish
×
44
    end
×
45

46
    event :withdraw do
×
47
      transitions from: [:draft, :awaiting_approval, :approved], to: :withdrawn
×
48
    end
×
49

50
    event :resubmit do
×
51
      transitions from: :withdrawn, to: :draft
×
52
    end
×
53

54
    event :remove do
×
55
      transitions from: :withdrawn, to: :deletion_marker
×
56
    end
×
57

58
    after_all_events :track_state_change
×
59
  end
×
60

61
  def state=(new_state)
×
62
    new_state_sym = new_state.to_sym
×
63
    valid_states = self.class.aasm.states.map(&:name)
×
64
    raise(StandardError, "Invalid state '#{new_state}'") unless valid_states.include?(new_state_sym)
×
65
    aasm_write_state_without_persistence(new_state_sym)
×
66
  end
×
67

68
  ##
69
  # Is this work editable by a given user?
70
  # A work is editable when:
71
  # * it is being edited by the person who made it
72
  # * it is being edited by a group admin of the group where is resides
73
  # * it is being edited by a super admin
74
  # @param [User]
75
  # @return [Boolean]
76
  def editable_by?(user)
×
77
    submitted_by?(user) || administered_by?(user)
×
78
  end
×
79

80
  def editable_in_current_state?(user)
×
81
    # anyone with edit privleges can edit a work while it is in draft
82
    return editable_by?(user) if draft?
×
83

84
    # Only admisitrators can edit a work in other states
85
    administered_by?(user)
×
86
  end
×
87

88
  def submitted_by?(user)
×
89
    created_by_user_id == user.id
×
90
  end
×
91

92
  def administered_by?(user)
×
93
    user.has_role?(:group_admin, group)
×
94
  end
×
95

96
  class << self
×
97
    def find_by_doi(doi)
×
98
      prefix = "10.34770/"
×
99
      doi = "#{prefix}#{doi}" unless doi.blank? || doi.start_with?(prefix)
×
100
      Work.find_by!("metadata @> ?", JSON.dump(doi:))
×
101
    end
×
102

103
    def find_by_ark(ark)
×
104
      prefix = "ark:/"
×
105
      ark = "#{prefix}#{ark}" unless ark.blank? || ark.start_with?(prefix)
×
106
      Work.find_by!("metadata @> ?", JSON.dump(ark:))
×
107
    end
×
108

109
    delegate :resource_type_general_values, to: PDCMetadata::Resource
×
110
  end
×
111

112
  include Rails.application.routes.url_helpers
×
113

114
  before_save do |work|
×
115
    # Ensure that the metadata JSONB postgres field is persisted properly
116
    work.metadata = JSON.parse(work.resource.to_json)
×
117
  end
×
118

119
  after_save do |work|
×
120
    if work.approved?
×
121
      work.reload
×
122
    end
×
123
  end
×
124

125
  validate do |_work|
×
126
    work_validator.valid?
×
127
  end
×
128

129
  # Overload ActiveRecord.reload method
130
  # https://apidock.com/rails/ActiveRecord/Base/reload
131
  #
132
  # NOTE: Usually `after_save` is a better place to put this kind of code:
133
  #
134
  #   after_save do |work|
135
  #     work.resource = nil
136
  #   end
137
  #
138
  # but that does not work in this case because the block points to a different
139
  # memory object for `work` than the we want we want to reload.
140
  def reload(options = nil)
×
141
    super
×
142
    # Force `resource` to be reloaded
143
    @resource = nil
×
144
    self
×
145
  end
×
146

147
  def title
×
148
    resource.main_title
×
149
  end
×
150

151
  def uploads_attributes
×
152
    return [] if approved? # once approved we no longer allow the updating of uploads via the application
×
153
    uploads.map do |upload|
×
154
      {
×
155
        id: upload.id,
×
156
        key: upload.key,
×
157
        filename: upload.filename.to_s,
×
158
        created_at: upload.created_at,
×
159
        url: upload.url
×
160
      }
×
161
    end
×
162
  end
×
163

164
  def form_attributes
×
165
    {
×
166
      uploads: uploads_attributes
×
167
    }
×
168
  end
×
169

170
  def draft_doi
×
171
    return if resource.doi.present?
×
172
    resource.doi = datacite_service.draft_doi
×
173
    save!
×
174
  end
×
175

176
  # Return the DOI formatted as a URL, so it can be used as a link on display pages
177
  # @return [String] A url formatted version of the DOI
178
  def doi_url
×
179
    return "https://doi.org/#{doi}" unless doi.starts_with?("https://doi.org")
×
180
    doi
×
181
  end
×
182

183
  def created_by_user
×
184
    User.find(created_by_user_id)
×
185
  rescue ActiveRecord::RecordNotFound
×
186
    nil
×
187
  end
×
188

189
  def resource=(resource)
×
190
    @resource = resource
×
191
    # Ensure that the metadata JSONB postgres field is persisted properly
192
    self.metadata = JSON.parse(resource.to_json)
×
193
  end
×
194

195
  def resource
×
196
    @resource ||= PDCMetadata::Resource.new_from_jsonb(metadata)
×
197
  end
×
198

199
  def url
×
200
    return unless persisted?
×
201

202
    @url ||= url_for(self)
×
203
  end
×
204

205
  def files_location_upload?
×
206
    files_location.blank? || files_location == "file_upload"
×
207
  end
×
208

209
  def files_location_cluster?
×
210
    files_location == "file_cluster"
×
211
  end
×
212

213
  def files_location_other?
×
214
    files_location == "file_other"
×
215
  end
×
216

217
  def change_curator(curator_user_id, current_user)
×
218
    if curator_user_id == "no-one"
×
219
      clear_curator(current_user)
×
220
    else
×
221
      update_curator(curator_user_id, current_user)
×
222
    end
×
223
  end
×
224

225
  def clear_curator(current_user)
×
226
    # Update the curator on the Work
227
    self.curator_user_id = nil
×
228
    save!
×
229

230
    # ...and log the activity
231
    WorkActivity.add_work_activity(id, "Unassigned existing curator", current_user.id, activity_type: WorkActivity::SYSTEM)
×
232
  end
×
233

234
  def update_curator(curator_user_id, current_user)
×
235
    # Update the curator on the Work
236
    self.curator_user_id = curator_user_id
×
237
    save!
×
238

239
    # ...and log the activity
240
    new_curator = User.find(curator_user_id)
×
241

242
    work_url = "[#{title}](#{Rails.application.routes.url_helpers.work_url(self)})"
×
243

244
    # Troubleshooting https://github.com/pulibrary/pdc_describe/issues/1783
245
    if work_url.include?("/describe/describe/")
×
246
      Rails.logger.error("URL #{work_url} included /describe/describe/ and was fixed. See https://github.com/pulibrary/pdc_describe/issues/1783")
×
247
      work_url = work_url.gsub("/describe/describe/", "/describe/")
×
248
    end
×
249

250
    message = if curator_user_id.to_i == current_user.id
×
251
                "Self-assigned @#{current_user.uid} as curator for work #{work_url}"
×
252
              else
×
253
                "Set curator to @#{new_curator.uid} for work #{work_url}"
×
254
              end
×
255
    WorkActivity.add_work_activity(id, message, current_user.id, activity_type: WorkActivity::SYSTEM)
×
256
  end
×
257

258
  def add_message(message, current_user_id)
×
259
    WorkActivity.add_work_activity(id, message, current_user_id, activity_type: WorkActivity::MESSAGE)
×
260
  end
×
261

262
  def add_provenance_note(date, note, current_user_id, change_label = "")
×
263
    WorkActivity.add_work_activity(id, { note:, change_label: }.to_json, current_user_id, activity_type: WorkActivity::PROVENANCE_NOTES, created_at: date)
×
264
    # WorkActivity.add_work_activity(id, note, current_user_id, activity_type: WorkActivity::PROVENANCE_NOTES, created_at: date)
265
  end
×
266

267
  def log_changes(resource_compare, current_user_id)
×
268
    return if resource_compare.identical?
×
269
    WorkActivity.add_work_activity(id, resource_compare.differences.to_json, current_user_id, activity_type: WorkActivity::CHANGES)
×
270
  end
×
271

272
  def log_file_changes(current_user_id)
×
273
    return if changes.count == 0
×
274
    WorkActivity.add_work_activity(id, changes.to_json, current_user_id, activity_type: WorkActivity::FILE_CHANGES)
×
275
  end
×
276

277
  def activities
×
278
    WorkActivity.activities_for_work(id, WorkActivity::MESSAGE_ACTIVITY_TYPES + WorkActivity::CHANGE_LOG_ACTIVITY_TYPES)
×
279
  end
×
280

281
  def new_notification_count_for_user(user_id)
×
282
    WorkActivityNotification.joins(:work_activity)
×
283
                            .where(user_id:, read_at: nil)
×
284
                            .where(work_activity: { work_id: id })
×
285
                            .count
×
286
  end
×
287

288
  # Marks as read the notifications for the given user_id in this work.
289
  # In practice, the user_id is the id of the current user and therefore this method marks the current's user
290
  # notifications as read.
291
  def mark_new_notifications_as_read(user_id)
×
292
    activities.each do |activity|
×
293
      unread_notifications = WorkActivityNotification.where(user_id:, work_activity_id: activity.id, read_at: nil)
×
294
      unread_notifications.each do |notification|
×
295
        notification.read_at = Time.now.utc
×
296
        notification.save
×
297
      end
×
298
    end
×
299
  end
×
300

301
  def current_transition
×
302
    aasm.current_event.to_s.humanize.delete("!")
×
303
  end
×
304

305
  # Retrieve the S3 file uploads associated with the Work
306
  # @return [Array<S3File>]
307
  def uploads
×
308
    return post_curation_uploads if approved?
×
309

310
    pre_curation_uploads
×
311
  end
×
312

313
  # Retrieve the S3 file uploads named "README"
314
  # @return [Array<S3File>]
315
  def readme_uploads
×
316
    uploads.select { |s3_file| s3_file.filename.include?("README") }
×
317
  end
×
318

319
  # Retrieve the S3 file uploads which are research artifacts proper (not README or other files providing metadata/documentation)
320
  # @return [Array<S3File>]
321
  def artifact_uploads
×
322
    uploads.reject { |s3_file| s3_file.filename.include?("README") }
×
323
  end
×
324

325
  # Returns the list of files for the work with some basic information about each of them.
326
  # This method is much faster than `uploads` because it does not return the actual S3File
327
  # objects to the client, instead it returns just a few selected data elements.
328
  def file_list
×
329
    s3_files = approved? ? post_curation_uploads : pre_curation_uploads
×
330
    files_info = s3_files.map do |s3_file|
×
331
      {
×
332
        "safe_id": s3_file.safe_id,
×
333
        "filename": s3_file.filename,
×
334
        "filename_display": s3_file.filename_display,
×
335
        "last_modified": s3_file.last_modified,
×
336
        "last_modified_display": s3_file.last_modified_display,
×
337
        "size": s3_file.size,
×
338
        "display_size": s3_file.display_size,
×
339
        "url": s3_file.url,
×
340
        "is_folder": s3_file.is_folder
×
341
      }
×
342
    end
×
343
    files_info
×
344
  end
×
345

346
  def total_file_size
×
347
    @total_file_size ||= begin
×
348
      total_size = 0
×
349
      file_list.each do |file|
×
350
        total_size += file[:size]
×
351
      end
×
352
      total_size
×
353
    end
×
354
  end
×
355

356
  # Fetches the data from S3 directly bypassing ActiveStorage
357
  def pre_curation_uploads
×
358
    s3_query_service.client_s3_files.sort_by(&:filename)
×
359
  end
×
360

361
  # Accesses post-curation S3 Bucket Objects
362
  def post_curation_s3_resources
×
363
    if approved?
×
364
      s3_resources
×
365
    else
×
366
      []
×
367
    end
×
368
  end
×
369

370
  # Returns the files in post-curation for the work
371
  def post_curation_uploads(force_post_curation: false)
×
372
    if force_post_curation
×
373
      # Always use the post-curation data regardless of the work's status
374
      post_curation_s3_query_service = S3QueryService.new(self, "postcuration")
×
375
      post_curation_s3_query_service.data_profile.fetch(:objects, [])
×
376
    else
×
377
      # Return the list based of files honoring the work status
378
      post_curation_s3_resources
×
379
    end
×
380
  end
×
381

382
  def s3_files
×
383
    pre_curation_uploads
×
384
  end
×
385

386
  def s3_client
×
387
    s3_query_service.client
×
388
  end
×
389

390
  delegate :bucket_name, :prefix, to: :s3_query_service
×
391
  delegate :doi_attribute_url, :curator_or_current_uid, to: :datacite_service
×
392

393
  # Generates the S3 Object key
394
  # @return [String]
395
  def s3_object_key
×
396
    "#{doi}/#{id}"
×
397
  end
×
398

399
  # Transmit a HEAD request for the S3 Bucket directory for this Work
400
  # @param bucket_name location to be checked to be found
401
  # @return [Aws::S3::Types::HeadObjectOutput]
402
  def find_post_curation_s3_dir(bucket_name:)
×
403
    # TODO: Directories really do not exists in S3
404
    #      if we really need this check then we need to do something else to check the bucket
405
    s3_client.head_object({
×
406
                            bucket: bucket_name,
×
407
                            key: s3_object_key
×
408
                          })
×
409
    true
×
410
  rescue Aws::S3::Errors::NotFound
×
411
    nil
×
412
  end
×
413

414
  # Generates the JSON serialized expression of the Work
415
  # @param args [Array<Hash>]
416
  # @option args [Boolean] :force_post_curation Force the request of AWS S3
417
  #   Resources, clearing the in-memory cache
418
  # @return [String]
419
  def as_json(*args)
×
420
    files = files_as_json(*args)
×
421

422
    # to_json returns a string of serialized JSON.
423
    # as_json returns the corresponding hash.
424
    {
×
425
      "resource" => resource.as_json,
×
426
      "files" => files,
×
427
      "group" => group.as_json.except("id"),
×
428
      "embargo_date" => embargo_date_as_json,
×
429
      "created_at" => format_date_for_solr(created_at),
×
430
      "updated_at" => format_date_for_solr(updated_at)
×
431
    }
×
432
  end
×
433

434
  # Format the date for Apache Solr
435
  # @param date [ActiveSupport::TimeWithZone]
436
  # @return [String]
437
  def format_date_for_solr(date)
×
438
    date.strftime("%Y-%m-%dT%H:%M:%SZ")
×
439
  end
×
440

441
  def pre_curation_uploads_count
×
442
    s3_query_service.file_count
×
443
  end
×
444

445
  delegate :ark, :doi, :resource_type, :resource_type=, :resource_type_general, :resource_type_general=,
×
446
           :to_xml, to: :resource
×
447

448
  # S3QueryService object associated with this Work
449
  # @return [S3QueryService]
450
  def s3_query_service
×
451
    mode = approved? ? "postcuration" : "precuration"
×
452
    @s3_query_service ||= S3QueryService.new(self, mode)
×
453
  end
×
454

455
  def past_snapshots
×
456
    UploadSnapshot.where(work: self)
×
457
  end
×
458

459
  # Build or find persisted UploadSnapshot models for this Work
460
  # @param [integer] user_id optional user to assign the snapshot to
461
  # @return [UploadSnapshot]
462
  def reload_snapshots(user_id: nil)
×
463
    work_changes = []
×
464
    s3_files = pre_curation_uploads
×
465
    s3_filenames = s3_files.map(&:filename)
×
466

467
    upload_snapshot = latest_snapshot
×
468

469
    upload_snapshot.snapshot_deletions(work_changes, s3_filenames)
×
470

471
    upload_snapshot.snapshot_modifications(work_changes, s3_files)
×
472

473
    # Create WorkActivity models with the set of changes
474
    unless work_changes.empty?
×
475
      new_snapshot = UploadSnapshot.new(work: self, url: s3_query_service.prefix)
×
476
      new_snapshot.store_files(s3_files)
×
477
      new_snapshot.save!
×
478
      WorkActivity.add_work_activity(id, work_changes.to_json, user_id, activity_type: WorkActivity::FILE_CHANGES)
×
479
    end
×
480
  end
×
481

482
  def self.presenter_class
×
483
    WorkPresenter
×
484
  end
×
485

486
  def presenter
×
487
    self.class.presenter_class.new(work: self)
×
488
  end
×
489

490
  def changes
×
491
    @changes ||= []
×
492
  end
×
493

494
  def track_change(action, filename)
×
495
    changes << { action:, filename: }
×
496
  end
×
497

498
  # rubocop:disable Naming/PredicateName
499
  def has_rights?(rights_id)
×
500
    resource.rights_many.index { |rights| rights.identifier == rights_id } != nil
×
501
  end
×
502
  # rubocop:enable Naming/PredicateName
503

504
  # This is the solr id / work show page in PDC Discovery
505
  def pdc_discovery_url
×
506
    "https://datacommons.princeton.edu/discovery/catalog/doi-#{doi.tr('/', '-').tr('.', '-')}"
×
507
  end
×
508

509
  # Determine whether or not the Work is under active embargo
510
  # @return [Boolean]
511
  def embargoed?
×
512
    return false if embargo_date.blank?
×
513

514
    current_date = Time.zone.now
×
515
    embargo_date >= current_date
×
516
  end
×
517

518
  def upload_count
×
519
    @upload_count ||= s3_query_service.count_objects
×
520
  end
×
521

522
  protected
×
523

524
    def work_validator
×
525
      @work_validator ||= WorkValidator.new(self)
×
526
    end
×
527

528
    # This must be protected, NOT private for ActiveRecord to work properly with this attribute.
529
    #   Protected will still keep others from setting the metatdata, but allows ActiveRecord the access it needs
530
    def metadata=(metadata)
×
531
      super
×
532
      @resource = PDCMetadata::Resource.new_from_jsonb(metadata)
×
533
    end
×
534

535
  private
×
536

537
    def publish(user)
×
538
      datacite_service.publish_doi(user)
×
539
      update_ark_information
×
540
      publish_precurated_files(user)
×
541
      save!
×
542
    end
×
543

544
    # Update EZID (our provider of ARKs) with the new information for this work.
545
    def update_ark_information
×
546
      # We only want to update the ark url under certain conditions.
547
      # Set this value in config/update_ark_url.yml
548
      if Rails.configuration.update_ark_url
×
549
        if ark.present?
×
550
          Ark.update(ark, datacite_service.doi_attribute_url)
×
551
        end
×
552
      end
×
553
    end
×
554

555
    def track_state_change(user, state = aasm.to_state)
×
556
      uw = UserWork.new(user_id: user.id, work_id: id, state:)
×
557
      uw.save!
×
558
      WorkActivity.add_work_activity(id, "marked as #{state.to_s.titleize}", user.id, activity_type: WorkActivity::SYSTEM)
×
559
      WorkStateTransitionNotification.new(self, user.id).send
×
560
    end
×
561

562
    # Request S3 Bucket Objects associated with this Work
563
    # @return [Array<S3File>]
564
    def s3_resources
×
565
      data_profile = s3_query_service.data_profile
×
566
      data_profile.fetch(:objects, [])
×
567
    end
×
568
    alias pre_curation_s3_resources s3_resources
×
569

570
    def s3_object_persisted?(s3_file)
×
571
      uploads_keys = uploads.map(&:key)
×
572
      uploads_keys.include?(s3_file.key)
×
573
    end
×
574

575
    def publish_precurated_files(user)
×
576
      # We need to explicitly check the to post-curation bucket here.
577
      s3_post_curation_query_service = S3QueryService.new(self, "postcuration")
×
578

579
      s3_dir = find_post_curation_s3_dir(bucket_name: s3_post_curation_query_service.bucket_name)
×
580
      raise(StandardError, "Attempting to publish a Work with an existing S3 Bucket directory for: #{s3_object_key}") unless s3_dir.nil?
×
581

582
      # Copy the pre-curation S3 Objects to the post-curation S3 Bucket...
583
      s3_query_service.publish_files(user)
×
584
    end
×
585

586
    def latest_snapshot
×
587
      return upload_snapshots.first unless upload_snapshots.empty?
×
588

589
      UploadSnapshot.new(work: self, files: [])
×
590
    end
×
591

592
    def datacite_service
×
593
      @datacite_service ||= PULDatacite.new(self)
×
594
    end
×
595

596
    def files_as_json(*args)
×
597
      return [] if embargoed?
×
598

599
      force_post_curation = args.any? { |arg| arg[:force_post_curation] == true }
×
600

601
      # Pre-curation files are not accessible externally,
602
      # so we are not interested in listing them in JSON.
603
      post_curation_uploads(force_post_curation:).map do |upload|
×
604
        {
×
605
          "filename": upload.filename,
×
606
          "size": upload.size,
×
607
          "display_size": upload.display_size,
×
608
          "url": upload.globus_url
×
609
        }
×
610
      end
×
611
    end
×
612

613
    def embargo_date_as_json
×
614
      if embargo_date.present?
×
615
        embargo_datetime = embargo_date.to_datetime
×
616
        embargo_date_iso8601 = embargo_datetime.iso8601
×
617
        # Apache Solr timestamps require the following format:
618
        # 1972-05-20T17:33:18Z
619
        # https://solr.apache.org/guide/solr/latest/indexing-guide/date-formatting-math.html
620
        embargo_date_iso8601.gsub(/\+.+$/, "Z")
×
621
      end
×
622
    end
×
623
end
×
624
# rubocop:enable Metrics/ClassLength
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