2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
module InsertDuplicate
|
|
|
|
def self.campaign(campaign_id, profile_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ campaign_id: campaign_id, profile_id: profile_id },
|
|
|
|
campaign_id: { required: true, is_integer: true },
|
|
|
|
profile_id: { required: true, is_integer: true })
|
|
|
|
campaign = Campaign.where('id = ?', campaign_id).first
|
2018-03-25 17:30:42 +00:00
|
|
|
unless campaign
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ParamValidation::ValidationError.new("#{campaign_id} is not a valid campaign", key: :campaign_id)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
profile = Profile.where('id = ?', profile_id).first
|
2018-03-25 17:30:42 +00:00
|
|
|
unless profile
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ParamValidation::ValidationError.new("#{profile_id} is not a valid profile", key: :profile_id)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Qx.transaction do
|
|
|
|
dupe = campaign.dup
|
|
|
|
|
|
|
|
dupe.slug = SlugCopyNamingAlgorithm.new(Campaign, dupe.nonprofit.id).create_copy_name(dupe.slug)
|
|
|
|
dupe.name = NameCopyNamingAlgorithm.new(Campaign, dupe.nonprofit.id).create_copy_name(dupe.name)
|
2019-07-30 21:29:24 +00:00
|
|
|
if dupe.end_datetime && dupe.end_datetime.ago(7.days) < DateTime.now
|
2018-03-25 17:30:42 +00:00
|
|
|
dupe.end_datetime = DateTime.now.since(7.days)
|
|
|
|
end
|
|
|
|
|
|
|
|
dupe.published = false
|
|
|
|
|
|
|
|
dupe.save!
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
dupe.main_image.attach(campaign.main_image.blob) if campaign.main_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
dupe.background_image.attach(campaign.background_image.blob) if campaign.background_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
InsertDuplicate.campaign_gift_options(campaign_id, dupe.id)
|
|
|
|
|
|
|
|
return dupe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.event(event_id, profile_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ event_id: event_id, profile_id: profile_id },
|
|
|
|
event_id: { required: true, is_integer: true },
|
|
|
|
profile_id: { required: true, is_integer: true })
|
|
|
|
event = Event.where('id = ?', event_id).first
|
2018-03-25 17:30:42 +00:00
|
|
|
unless event
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ParamValidation::ValidationError.new("#{event_id} is not a valid event", key: :event_id)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
profile = Profile.where('id = ?', profile_id).first
|
2018-03-25 17:30:42 +00:00
|
|
|
unless profile
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ParamValidation::ValidationError.new("#{profile_id} is not a valid profile", key: :profile_id)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Qx.transaction do
|
|
|
|
dupe = event.dup
|
|
|
|
|
|
|
|
dupe.slug = SlugCopyNamingAlgorithm.new(Event, dupe.nonprofit.id).create_copy_name(dupe.slug)
|
|
|
|
dupe.name = NameCopyNamingAlgorithm.new(Event, dupe.nonprofit.id).create_copy_name(dupe.name)
|
|
|
|
|
|
|
|
we_changed_start_time = false
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
length_of_event = dupe.end_datetime - dupe.start_datetime
|
|
|
|
if dupe.start_datetime.ago(7.days) < DateTime.now
|
2018-03-25 17:30:42 +00:00
|
|
|
dupe.start_datetime = DateTime.now.since(7.days)
|
|
|
|
we_changed_start_time = true
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
if we_changed_start_time && dupe.end_datetime
|
2018-03-25 17:30:42 +00:00
|
|
|
dupe.end_datetime = dupe.start_datetime.since(length_of_event)
|
|
|
|
end
|
|
|
|
|
|
|
|
dupe.published = false
|
|
|
|
|
|
|
|
dupe.save!
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
dupe.main_image.attach(event.main_image.blob) if event.main_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
begin
|
2020-05-18 21:05:25 +00:00
|
|
|
dupe.background_image.attach( event.background_image.blob) if event.background_image.attached?
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError
|
|
|
|
AWS::S3::Errors::NoSuchKey
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
InsertDuplicate.ticket_levels(event_id, dupe.id)
|
|
|
|
InsertDuplicate.event_discounts(event_id, dupe.id)
|
|
|
|
|
|
|
|
return dupe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# selects all gift options associated with old campaign
|
|
|
|
# and inserts them and creates associations with a new campaign
|
|
|
|
def self.campaign_gift_options(old_campaign_id, new_campaign_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
cgos = Qx.select('*')
|
|
|
|
.from('campaign_gift_options')
|
|
|
|
.where(campaign_id: old_campaign_id)
|
|
|
|
.execute
|
|
|
|
.map { |c| c.except('id', 'created_at', 'updated_at', 'campaign_id') }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
if cgos.any?
|
2019-07-30 21:29:24 +00:00
|
|
|
return Qx.insert_into('campaign_gift_options')
|
|
|
|
.values(cgos)
|
|
|
|
.common_values(campaign_id: new_campaign_id)
|
|
|
|
.ts
|
|
|
|
.returning('*')
|
|
|
|
.execute
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# selects all ticket levels associated with old event
|
|
|
|
# and inserts them and creates associations with a new event
|
|
|
|
def self.ticket_levels(old_event_id, new_event_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
tls = Qx.select('*')
|
|
|
|
.from('ticket_levels')
|
|
|
|
.where(event_id: old_event_id)
|
|
|
|
.execute
|
|
|
|
.map { |t| t.except('id', 'created_at', 'updated_at', 'event_id') }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
if tls.any?
|
2019-07-30 21:29:24 +00:00
|
|
|
return Qx.insert_into('ticket_levels')
|
|
|
|
.values(tls)
|
|
|
|
.common_values(event_id: new_event_id)
|
|
|
|
.ts
|
|
|
|
.returning('*')
|
|
|
|
.execute
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# selects all discounts associated with old event
|
|
|
|
# and inserts them and creates associations with a new event
|
|
|
|
def self.event_discounts(old_event_id, new_event_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
eds = Qx.select('*')
|
|
|
|
.from('event_discounts')
|
|
|
|
.where(event_id: old_event_id)
|
|
|
|
.execute
|
|
|
|
.map { |t| t.except('id', 'created_at', 'updated_at', 'event_id') }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
if eds.any?
|
2019-07-30 21:29:24 +00:00
|
|
|
return Qx.insert_into('event_discounts')
|
|
|
|
.values(eds)
|
|
|
|
.common_values(event_id: new_event_id)
|
|
|
|
.ts
|
|
|
|
.returning('*')
|
|
|
|
.execute
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|