-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into HER-67-Update-Order-Form-to-Use-Address-and-…
…Organization
- Loading branch information
Showing
13 changed files
with
351 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Api::V1::AddressesController < ApplicationController | ||
before_action :set_addressable | ||
|
||
def create | ||
@address = @addressable.addresses.build(address_params) | ||
if @address.save | ||
render json: @address, status: :created | ||
else | ||
render json: @address.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
@address = @addressable.addresses.find(params[:id]) | ||
if @address.update(address_params) | ||
render json: @address, status: :ok | ||
else | ||
render json: @address.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@address = @addressable.addresses.find(params[:id]) | ||
@address.destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def set_addressable | ||
@addressable = if params[:user_id] | ||
User.find(params[:user_id]) | ||
elsif params[:organization_id] | ||
Organization.find(params[:organization_id]) | ||
end | ||
end | ||
|
||
def address_params | ||
params.require(:address).permit(:street_address, :city, :state, :postal_code, :save_to_user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class RecurringAvailabilityJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(viewing_month, viewing_year) | ||
start_date = DateTime.new(viewing_year, viewing_month, 1) | ||
end_date = start_date.end_of_month | ||
|
||
# Wrap the entire logic in a rescue block to handle unexpected errors | ||
begin | ||
# Check if availabilities for the specified month already exist | ||
existing_availabilities = Availability.where(start_time: start_date..end_date) | ||
return if existing_availabilities.exists? | ||
|
||
# Find all recurring availabilities that need to be created for the next month | ||
recurring_availabilities = RecurringAvailability.where("end_date IS NULL OR end_date >= ?", start_date) | ||
|
||
# Use a transaction to ensure atomicity | ||
Availability.transaction do | ||
recurring_availabilities.each do |recurring| | ||
(start_date.to_date..end_date.to_date).each do |date| # Use Date instead of DateTime | ||
if recurring.recurs_on?(date) | ||
Availability.create!( | ||
start_time: DateTime.new(date.year, date.month, date.day, recurring.start_time.hour, recurring.start_time.min, recurring.start_time.sec), | ||
end_time: DateTime.new(date.year, date.month, date.day, recurring.end_time.hour, recurring.end_time.min, recurring.end_time.sec), | ||
speaker_id: recurring.speaker_id, | ||
recurring_availability_id: recurring.id | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
rescue StandardError => e | ||
Rails.logger.error("RecurringAvailabilityJob failed for month #{viewing_month}, year #{viewing_year}: #{e.message}") | ||
Rails.logger.error(e.backtrace.join("\n")) | ||
raise e # Re-raise the error to ensure the job retries or fails properly | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddSaveToUserToAddresses < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :addresses, :save_to_user, :boolean | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe RecurringAvailabilityJob, type: :job do | ||
# describe '#perform' do | ||
# let(:viewing_month) { 1 } # January | ||
# let(:viewing_year) { 2025 } | ||
# let(:start_date) { Date.new(viewing_year, viewing_month, 1) } | ||
# let(:end_date) { start_date.end_of_month } | ||
|
||
# before do | ||
# # Create a recurring availability rule | ||
# @recurring_availability = create( | ||
# :recurring_availability, | ||
# start_time: "09:00", | ||
# end_time: "10:00", | ||
# speaker_id: 1, | ||
# recurs_on: ->(date) { date.wday == 1 } # Recur every Monday | ||
# ) | ||
# end | ||
|
||
# it 'creates new availabilities for recurring rules' do | ||
# expect { | ||
# RecurringAvailabilityJob.new.perform(viewing_month, viewing_year) | ||
# }.to change(Availability, :count).by(4) # Assuming 4 Mondays in January 2025 | ||
# end | ||
|
||
# it 'does not create duplicates if availabilities already exist' do | ||
# # First execution | ||
# RecurringAvailabilityJob.new.perform(viewing_month, viewing_year) | ||
|
||
# # Second execution | ||
# expect { | ||
# RecurringAvailabilityJob.new.perform(viewing_month, viewing_year) | ||
# }.not_to change(Availability, :count) | ||
# end | ||
# end | ||
end |
Oops, something went wrong.