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.27 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.26 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.25 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.24 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.23 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.22 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.21 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.20 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.18 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.17 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.16 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.15 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.14 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.13 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.12 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.11 app/models/renalware/hd/find_or_create_diary_by_week_query.rb
renalware-core-2.0.9 app/models/renalware/hd/find_or_create_diary_by_week_query.rb