Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#161847] Rake tasks for adding new PriceGroups to ScheduleRules #3594

Merged
merged 11 commits into from
Jul 26, 2023
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@ It is possible to track deprecation warnings locally with [deprecation_toolkit](

`deprecation_toolkit` is configured in [`spec/deprecation_toolkit_env.rb`](spec/deprecation_toolkit_env.rb).

## Creating Global Price Groups

A global price group is a price group that's accessible across all facilities, and it must be created via the Rails console.

```
pg = PriceGroup.new(name: "New Price Group Name", display_order: PriceGroup.globals.count + 1, is_internal: false)
pg.save(validate: false)
```

Once the price group is created, the `schedule_rule:add_new_price_groups` rake task should be run to add the price group to all existing schedule rules. If you would like the discount to be something other than 0%, the task accepts the discount as a parameter. This example will set a 20% discount for all the price group it will add to all schedule rules

```
rake schedule_rule:add_new_price_groups[20]
```
jossim marked this conversation as resolved.
Show resolved Hide resolved


## Optional Modules

The following modules are provided as optional features via
Expand Down
67 changes: 67 additions & 0 deletions app/models/price_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def self.external
globals.find_by(name: Settings.price_group.name.external)
end

# Create a global price group, if it does not exist, and setup all the
# schedule rules with price group discounts for the price group.
def self.setup_global(name:, is_internal: false, admin_editable: true, discount_percent: 0)
jossim marked this conversation as resolved.
Show resolved Hide resolved
jossim marked this conversation as resolved.
Show resolved Hide resolved
price_group = find_or_create_global(name:, is_internal:, admin_editable:)
price_group.setup_schedule_rules(discount_percent:)
price_group
end

def is_not_global?
!global?
end
Expand Down Expand Up @@ -78,4 +86,63 @@ def external?
!is_internal?
end

private

# Find a global price group by name, or create it if it does not exist
def self.find_or_create_global(name:, is_internal: false, admin_editable: true)
global_price_groups = PriceGroup.globals
found_price_group = global_price_groups.find_by(name:)

if found_price_group
log_string = "Global price group '#{name}' already exists."

puts log_string
logger.info log_string
jossim marked this conversation as resolved.
Show resolved Hide resolved

found_price_group
else
pg = PriceGroup.new(
name:,
is_internal:,
admin_editable:,
facility_id: nil,
display_order: global_price_groups.count + 1
)

pg.save(validate: false)
log_string = "Created global price group '#{name}'"

puts log_string
logger.info log_string

pg
end
end

# Creates price group discounts for every schedule rule that does not have a
# price group discount for this price group
jossim marked this conversation as resolved.
Show resolved Hide resolved
def setup_schedule_rules(discount_percent: 0)
ScheduleRule.all.each do |schedule_rule|
schedule_price_groups = schedule_rule.price_group_discounts.map(&:price_group)

if schedule_price_groups.include? self
log_string = "price_group_discount for #{self} already exists for schedule rule #{schedule_rule.id}"

puts log_string
logger.info log_string

next
end

schedule_rule.price_group_discounts.create(
price_group: self,
discount_percent:
)

log_string = "Created price_group_discount for #{self} and schedule rule #{schedule_rule.id}"

puts log_string
logger.info log_string
end
end
end
19 changes: 19 additions & 0 deletions lib/tasks/pirce_groups.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

namespace :price_group do
# This creates a new global price group, if it does not exist, and adds
# a PriceGroupDiscount for it to every ScheduleRule. If the price group
# already exists, this will just add PriceGroupDiscounts for it to ScheduleRules
# that are missing then.
#
# rake price_group:add_global_price_group["new group",true,false,15]
desc "Create a new global price group and/or setup schedule rules for it"
task :add_global_price_group, [:name, :internal, :admin_editable, :discount] => :environment do |_t, args|
name = args[:name]
is_internal = args[:internal].casecmp("true").zero?
giladshanan marked this conversation as resolved.
Show resolved Hide resolved
admin_editable = args[:admin_editable].casecmp("true").zero?
discount_percent = args[:discount] || 0

PriceGroup.setup_global(name:, is_internal:, admin_editable:, discount_percent:)
end
end
5 changes: 4 additions & 1 deletion lib/tasks/schedule_rules.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

namespace :schedule_rule do
desc "Adds PriceGroupDiscounts for global price groups to every schedule rule"
# This task is useful for switching a school over to PriceGroupDiscounts. If a
# school is already using price group discounts on all its schedule rules, this
# task will do nothing
desc "Adds PriceGroupDiscounts for existing global price groups to every schedule rule"
task add_price_group_discounts: :environment do |_t, _args|
price_groups = PriceGroup.globals

Expand Down