availability_scheduler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b691938d809bc5e817e33032cb4f122dae5469a2ed9caf2db996ce5803d1581a
4
+ data.tar.gz: 478e41afa5112ca380c81e6506dd49d0a6b2c7bc42bf23683ddcb862ec18cb7d
5
+ SHA512:
6
+ metadata.gz: 1a7c69a44b77f459c7d79804b56d9f8d4e6a2fb28bad04d088f9b480a20939a4f0590465bcfee46de50d113137905c8213b545e52e24d78998a80661ac313b12
7
+ data.tar.gz: 7ffc31e84d9835f5a41ac87b1511f467506e02e7fce026a9dd21d2b59bf4805ce7789ef9067d796b13cd08dc54df4c4f14c99081be69ca1bdbaa69b64638c554
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ - rubocop-factory_bot
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.0
8
+ NewCops: enable
9
+ Exclude:
10
+ - 'lib/generators/**/*'
11
+ - 'vendor/**/*'
12
+ - 'availability_scheduler.gemspec'
13
+
14
+ Style/FrozenStringLiteralComment:
15
+ Enabled: false
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ RSpec/MultipleMemoizedHelpers:
21
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-09-01
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Paweł Strącała
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,139 @@
1
+
2
+ # AvailabilityScheduler
3
+
4
+ `AvailabilityScheduler` is a Ruby on Rails gem for managing user availability and scheduling appointments. It ensures appointments are booked within the user's available time slots and prevents overlapping bookings. The gem supports both recurring weekly schedules and one-off special schedules.
5
+
6
+ ## Features
7
+
8
+ - Manage weekly recurring schedules and one-off special schedules for users.
9
+ - Book appointments within a user’s availability.
10
+ - Prevent overlapping appointments.
11
+ - Validate availability before confirming an appointment.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's `Gemfile`:
16
+
17
+ ```ruby
18
+ gem 'availability_scheduler'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```bash
24
+ bundle install
25
+ ```
26
+
27
+ Next, run the generator to create the necessary migration files:
28
+
29
+ ```bash
30
+ rails generate availability_scheduler:install
31
+ ```
32
+
33
+ Finally, migrate your database:
34
+
35
+ ```bash
36
+ rails db:migrate
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Setting Weekly Schedules
42
+
43
+ Define recurring weekly schedules for users.
44
+
45
+ ```ruby
46
+ # Define a weekly schedule for a user
47
+ AvailabilityScheduler::WeeklySchedule.create!(
48
+ user_id: 1,
49
+ day_of_week: 1, # Monday
50
+ start_time: '09:00',
51
+ end_time: '17:00'
52
+ )
53
+ ```
54
+
55
+ ### Setting Special Schedules
56
+
57
+ Define one-off special schedules for specific dates.
58
+
59
+ ```ruby
60
+ # Define a special schedule for a specific date
61
+ AvailabilityScheduler::SpecialSchedule.create!(
62
+ user_id: 1,
63
+ date: '2024-09-10',
64
+ start_time: '10:00',
65
+ end_time: '14:00'
66
+ )
67
+ ```
68
+
69
+ ### Booking an Appointment
70
+
71
+ Book an appointment with validation to ensure it’s within the user’s availability.
72
+
73
+ ```ruby
74
+ AvailabilityScheduler::Appointment.create!(
75
+ booker_id: 2, # User making the booking
76
+ booked_user_id: 1, # User being booked
77
+ date: '2024-09-10',
78
+ start_time: '11:00',
79
+ end_time: '12:00'
80
+ )
81
+ ```
82
+
83
+ ### Fetching Availability for a User
84
+
85
+ You can fetch the availability of a user for a given month using the `AvailabilityScheduler::Availability::FetchForUser` service. This service returns a hash where each day of the month is mapped to an array of availability time periods.
86
+
87
+ #### Example
88
+
89
+ ```ruby
90
+ availability_service = AvailabilityScheduler::Availability::FetchForUser.new(user_id: 1, date: '05-2024')
91
+ availability = availability_service.call
92
+
93
+ availability.each do |date, periods|
94
+ if periods.any?
95
+ periods.each do |period|
96
+ puts "#{date}: Available from #{period[:start_time]} to #{period[:end_time]}"
97
+ end
98
+ else
99
+ puts "#{date}: Not available"
100
+ end
101
+ end
102
+ ```
103
+ Example output:
104
+ ```ruby
105
+ {
106
+ "2024-05-01" => [{ start_time: "09:00", end_time: "10:00" }, { start_time: "11:00", end_time: "16:00" }],
107
+ "2024-05-02" => [],
108
+ "2024-05-03" => [{ start_time: "10:00", end_time: "14:00" }],
109
+ # ...
110
+ }
111
+ ```
112
+
113
+ ### Validation Example
114
+
115
+ The gem will automatically validate whether the appointment is within the availability and if it overlaps with any other appointments.
116
+
117
+ ```ruby
118
+ appointment = AvailabilityScheduler::Appointment.new(
119
+ booker_id: 2,
120
+ booked_user_id: 1,
121
+ date: '2024-09-10',
122
+ start_time: '11:00',
123
+ end_time: '12:00'
124
+ )
125
+
126
+ if appointment.save
127
+ puts 'Appointment successfully booked!'
128
+ else
129
+ puts appointment.errors.full_messages
130
+ end
131
+ ```
132
+
133
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/yourusername/availability_scheduler](https://github.com/yourusername/availability_scheduler).
136
+
137
+ ## License
138
+
139
+ This project is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ module AvailabilityScheduler
2
+ class Base < ActiveRecord::Base
3
+ self.abstract_class = true # primary_abstract_class
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ module AvailabilityScheduler
2
+ class Appointment < Base
3
+ self.table_name = 'appointments'
4
+
5
+ validates :booker_id, :booked_user_id, :date, :start_time, :end_time, presence: true
6
+ validate :user_has_schedule
7
+ validate :within_availability, if: lambda {
8
+ start_time.present? && end_time.present? && date.present? && errors[:base].blank?
9
+ }
10
+ validate :no_overlap, if: -> { errors[:base].blank? }
11
+
12
+ private
13
+
14
+ def user_has_schedule
15
+ return if SpecialSchedule.exists?(user_id: booked_user_id) || WeeklySchedule.exists?(user_id: booked_user_id)
16
+
17
+ errors.add(:base, 'The booked user does not have any available schedules.')
18
+ end
19
+
20
+ def within_availability
21
+ return if special_schedule_exists?
22
+ return if weekly_schedule_exists?
23
+
24
+ errors.add(:base, 'Appointment is not within the availability of the booked user.')
25
+ end
26
+
27
+ def no_overlap
28
+ overlapping_appointments = Appointment.where(booked_user_id: booked_user_id, date: date)
29
+ .where.not(id: id)
30
+ .where('start_time < ? AND end_time > ?', end_time, start_time)
31
+
32
+ return unless overlapping_appointments.exists?
33
+
34
+ errors.add(:base, 'Appointment overlaps with another appointment.')
35
+ end
36
+
37
+ def special_schedule_exists?
38
+ SpecialSchedule.where(user_id: booked_user_id, date: date)
39
+ .where('start_time <= ? AND end_time >= ?', start_time, end_time)
40
+ .exists?
41
+ end
42
+
43
+ def weekly_schedule_exists?
44
+ WeeklySchedule.where(user_id: booked_user_id, day_of_week: date.wday)
45
+ .where('start_time <= ? AND end_time >= ?', start_time, end_time)
46
+ .exists?
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ module AvailabilityScheduler
2
+ class SpecialSchedule < Base
3
+ self.table_name = 'special_schedules'
4
+
5
+ validates :user_id, presence: true
6
+ validates :date, :start_time, :end_time, presence: true
7
+ validate :no_time_overlap
8
+
9
+ private
10
+
11
+ def no_time_overlap
12
+ overlapping_schedule = SpecialSchedule.where(user_id: user_id, date: date)
13
+ .where.not(id: id)
14
+ .where('start_time < ? AND end_time > ?', end_time, start_time)
15
+
16
+ return unless overlapping_schedule.exists?
17
+
18
+ errors.add(:base, 'Special schedule overlaps with another special schedule.')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module AvailabilityScheduler
2
+ class WeeklySchedule < Base
3
+ self.table_name = 'weekly_schedules'
4
+
5
+ validates :user_id, presence: true
6
+ validates :day_of_week, inclusion: { in: 0..6 } # monday 0 .. sunday 6
7
+ validates :start_time, :end_time, presence: true
8
+
9
+ validate :no_time_overlap
10
+
11
+ private
12
+
13
+ def no_time_overlap
14
+ overlapping_schedules = WeeklySchedule.where(user_id: user_id, day_of_week: day_of_week)
15
+ .where.not(id: id)
16
+ .where('start_time < ? AND end_time > ?', end_time, start_time)
17
+
18
+ return unless overlapping_schedules.exists?
19
+
20
+ errors.add(:base, 'Weekly schedule overlaps with another weekly schedule.')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails/railtie'
2
+
3
+ module AvailabilityScheduler
4
+ class Railtie < Rails::Railtie
5
+ # Kod, który ma być uruchomiony po załadowaniu Rails (np. dodanie generatorów)
6
+ end
7
+ end
@@ -0,0 +1,50 @@
1
+ module AvailabilityScheduler
2
+ module Availability
3
+ class FetchForUser
4
+ def initialize(user_id:, date:)
5
+ @user_id = user_id
6
+ @start_date = Date.strptime(date, '%m-%Y')
7
+ @end_date = @start_date.end_of_month
8
+ end
9
+
10
+ def call
11
+ availability = {}
12
+
13
+ special_schedules = SpecialSchedule.where(user_id: @user_id, date: @start_date..@end_date)
14
+
15
+ weekly_schedules = WeeklySchedule.where(user_id: @user_id)
16
+
17
+ (@start_date..@end_date).each do |date|
18
+ availability[date.strftime('%d-%m-%Y')] = daily_availability(date, special_schedules, weekly_schedules)
19
+ end
20
+
21
+ availability
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :user_id, :start_date, :end_date
27
+
28
+ def daily_availability(date, special_schedules, weekly_schedules)
29
+ special_schedules_for_day = special_schedules.select { |schedule| schedule.date == date }
30
+ availability = special_schedules_for_day.map do |schedule|
31
+ single_time(schedule)
32
+ end
33
+
34
+ return availability if availability.present?
35
+
36
+ weekly_schedules_for_day = weekly_schedules.select { |schedule| schedule.day_of_week == date.wday }
37
+ weekly_schedules_for_day.map do |schedule|
38
+ single_time(schedule)
39
+ end
40
+ end
41
+
42
+ def single_time(schedule)
43
+ {
44
+ start_time: schedule.start_time.strftime('%H:%M'),
45
+ end_time: schedule.end_time.strftime('%H:%M')
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module AvailabilityScheduler
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'availability_scheduler/railtie' if defined?(Rails)
2
+
3
+ module AvailabilityScheduler
4
+ class Error < StandardError; end
5
+
6
+ autoload :Base, 'availability_scheduler/base'
7
+ autoload :WeeklySchedule, 'availability_scheduler/models/weekly_schedule'
8
+ autoload :SpecialSchedule, 'availability_scheduler/models/special_schedule'
9
+ autoload :Appointment, 'availability_scheduler/models/appointment'
10
+ autoload :Availability, 'availability_scheduler/services/availability/fetch_for_user'
11
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+
4
+ module AvailabilityScheduler
5
+ module Generators
6
+ class InstallGenerator < ActiveRecord::Generators::Base
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ class_option :force, type: :boolean, default: true, desc: "Overwrite existing migrations"
10
+
11
+ argument :name, type: :string, default: 'default_value', optional: true
12
+
13
+ def create_migrations
14
+ migration_template 'create_weekly_schedules.rb', 'db/migrate/create_weekly_schedules.rb'
15
+ migration_template 'create_special_schedules.rb', 'db/migrate/create_special_schedules.rb'
16
+ migration_template 'create_appointments.rb', 'db/migrate/create_appointments.rb'
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ class CreateAppointments < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :appointments do |t|
4
+ t.integer :booker_id, null: false
5
+ t.integer :booked_user_id, null: false
6
+ t.date :date, null: false
7
+ t.time :start_time, null: false
8
+ t.time :end_time, null: false
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :appointments,
14
+ %i[booked_user_id date start_time end_time],
15
+ unique: true, name: 'index_appointments_on_booked_user_id_and_time'
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ class CreateSpecialSchedules < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :special_schedules do |t|
4
+ t.integer :user_id, null: false
5
+ t.date :date, null: false
6
+ t.time :start_time, null: false
7
+ t.time :end_time, null: false
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :special_schedules, %i[user_id date]
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ class CreateWeeklySchedules < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :weekly_schedules do |t|
4
+ t.integer :user_id, null: false
5
+ t.integer :day_of_week, null: false
6
+ t.time :start_time, null: false
7
+ t.time :end_time, null: false
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :weekly_schedules, :user_id
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module AvailabilityScheduler
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: availability_scheduler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Strącała
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem allows scheduling availability for resources, including weekly
14
+ schedules and special one-day exceptions.
15
+ email:
16
+ - pawel.stracala@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/availability_scheduler.rb
28
+ - lib/availability_scheduler/base.rb
29
+ - lib/availability_scheduler/models/appointment.rb
30
+ - lib/availability_scheduler/models/special_schedule.rb
31
+ - lib/availability_scheduler/models/weekly_schedule.rb
32
+ - lib/availability_scheduler/railtie.rb
33
+ - lib/availability_scheduler/services/availability/fetch_for_user.rb
34
+ - lib/availability_scheduler/version.rb
35
+ - lib/generators/availability_scheduler/install_generator.rb
36
+ - lib/generators/availability_scheduler/templates/create_appointments.rb
37
+ - lib/generators/availability_scheduler/templates/create_special_schedules.rb
38
+ - lib/generators/availability_scheduler/templates/create_weekly_schedules.rb
39
+ - sig/availability_scheduler.rbs
40
+ homepage: https://github.com/Ununuk/availability_scheduler
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/Ununuk/availability_scheduler
45
+ source_code_uri: https://github.com/Ununuk/availability_scheduler
46
+ changelog_uri: https://github.com/Ununuk/availability_scheduler/CHANGELOG.md
47
+ rubygems_mfa_required: 'true'
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.5.11
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A gem for scheduling availability for resources.
67
+ test_files: []