Sha256: ef2cb422d4d6ffcf78ca598824db10bbf227c3c5832df08a951956cf0ad30527

Contents?: true

Size: 1.54 KB

Versions: 97

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/hd"
require "renalware/week_period"
##
# Returns a hospital unit's diary for the requested week (passed to #new as a WeekPeriod
# value object).
#
module Renalware
  module HD
    class FindOrCreateDiaryByWeekQuery
      attr_reader :unit_id, :period, :by, :relation

      def initialize(unit_id:, week_period:, by:, relation: WeeklyDiary.all)
        @relation = relation
        @period = week_period
        @unit_id = unit_id
        @by = by
      end

      def call
        find_or_create_diary
      end

      private

      # Find the diary for the current unit/week/year, or, if for example if a user wants to fill
      # next week's diary and it does not yet exist, create it
      # NB we _always_ return a diary from this method - whether it is found or built just-in-time,
      def find_or_create_diary
        attrs = {
          hospital_unit_id: unit_id,
          week_number: period.week_number,
          year: period.year,
          master: false
        }
        relation.find_by(**attrs) || build_diary(**attrs)
      end

      # Create a new Weekly diary for the current week/year/unit
      # Add in DiaryPeriods for each currently defined diurnal period eg am pm eve
      def build_diary(attrs)
        master_diary = FindOrCreateMasterDiary.for_unit(unit_id, by)
        diary = WeeklyDiary.create!(**attrs, by: by, master_diary: master_diary)
        # Reload the diary using the supplied relation (might be egager_loads etc)
        relation.find(diary.id)
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 1 rubygems

Version Path
renalware-core-2.0.113 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.112 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.111 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.110 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.109 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.108 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.106 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.105 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.104 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.103 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.102 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.101 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.100 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.99 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.98 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.97 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.96 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.95 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.94 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.93 app/models/renalware/hd/find_or_create_diary_by_week_query.rb