Skip to content

Commit

Permalink
specs on purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Aug 18, 2023
1 parent 444ded0 commit 01c9da4
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 110 deletions.
136 changes: 66 additions & 70 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/chaskiq/plain.git
revision: 43396865abbf0c9afa604a5deb0e316ec2cdcc5b
revision: 9bb2deca3a7f49bd987ca90dbb23dab7fe2601ca
branch: main
specs:
plain (0.1.0)
Expand All @@ -11,7 +11,6 @@ GIT
rails (>= 7.0.6)
redcarpet (~> 2.3.0)
ruby-openai (~> 4.2)
tailwindcss-rails

GIT
remote: https://github.com/rspec/rspec-core.git
Expand Down Expand Up @@ -65,47 +64,47 @@ GEM
specs:
aasm (5.5.0)
concurrent-ruby (~> 1.0)
actioncable (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
actioncable (7.0.7)
actionpack (= 7.0.7)
activesupport (= 7.0.7)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.6)
actionpack (= 7.0.6)
activejob (= 7.0.6)
activerecord (= 7.0.6)
activestorage (= 7.0.6)
activesupport (= 7.0.6)
actionmailbox (7.0.7)
actionpack (= 7.0.7)
activejob (= 7.0.7)
activerecord (= 7.0.7)
activestorage (= 7.0.7)
activesupport (= 7.0.7)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.6)
actionpack (= 7.0.6)
actionview (= 7.0.6)
activejob (= 7.0.6)
activesupport (= 7.0.6)
actionmailer (7.0.7)
actionpack (= 7.0.7)
actionview (= 7.0.7)
activejob (= 7.0.7)
activesupport (= 7.0.7)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.6)
actionview (= 7.0.6)
activesupport (= 7.0.6)
actionpack (7.0.7)
actionview (= 7.0.7)
activesupport (= 7.0.7)
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.6)
actionpack (= 7.0.6)
activerecord (= 7.0.6)
activestorage (= 7.0.6)
activesupport (= 7.0.6)
actiontext (7.0.7)
actionpack (= 7.0.7)
activerecord (= 7.0.7)
activestorage (= 7.0.7)
activesupport (= 7.0.7)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.6)
activesupport (= 7.0.6)
actionview (7.0.7)
activesupport (= 7.0.7)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
Expand All @@ -115,29 +114,29 @@ GEM
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (7.0.6)
activesupport (= 7.0.6)
activejob (7.0.7)
activesupport (= 7.0.7)
globalid (>= 0.3.6)
activemodel (7.0.6)
activesupport (= 7.0.6)
activerecord (7.0.6)
activemodel (= 7.0.6)
activesupport (= 7.0.6)
activestorage (7.0.6)
actionpack (= 7.0.6)
activejob (= 7.0.6)
activerecord (= 7.0.6)
activesupport (= 7.0.6)
activemodel (7.0.7)
activesupport (= 7.0.7)
activerecord (7.0.7)
activemodel (= 7.0.7)
activesupport (= 7.0.7)
activestorage (7.0.7)
actionpack (= 7.0.7)
activejob (= 7.0.7)
activerecord (= 7.0.7)
activesupport (= 7.0.7)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.6)
activesupport (7.0.7)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
acts-as-taggable-on (9.0.1)
activerecord (>= 6.0, < 7.1)
addressable (2.8.4)
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
aws-eventstream (1.2.0)
Expand Down Expand Up @@ -296,11 +295,12 @@ GEM
activemodel (>= 6.0.0)
activesupport (>= 6.0.0)
redis (>= 4.2, < 6)
langchainrb (0.6.11)
langchainrb (0.6.12)
baran (~> 0.1.6)
colorize (~> 0.8.1)
json-schema (~> 4.0.0)
tiktoken_ruby (~> 0.0.5)
zeitwerk (= 2.6.11)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
llhttp-ffi (0.4.0)
Expand All @@ -323,8 +323,8 @@ GEM
actionpack (>= 3.2.0, < 7.1)
method_source (1.0.0)
mini_magick (4.12.0)
mini_mime (1.1.2)
minitest (5.18.1)
mini_mime (1.1.5)
minitest (5.19.0)
mrsk (0.15.1)
activesupport (>= 7.0)
bcrypt_pbkdf (~> 1.0)
Expand All @@ -337,7 +337,7 @@ GEM
msgpack (1.7.2)
multi_xml (0.6.0)
multipart-post (2.3.0)
net-imap (0.3.6)
net-imap (0.3.7)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -350,9 +350,9 @@ GEM
net-protocol
net-ssh (7.2.0)
nio4r (2.5.9)
nokogiri (1.15.3-arm64-darwin)
nokogiri (1.15.4-arm64-darwin)
racc (~> 1.4)
nokogiri (1.15.3-x86_64-linux)
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
oauth (1.1.0)
oauth-tty (~> 1.0, >= 1.0.1)
Expand Down Expand Up @@ -400,26 +400,26 @@ GEM
qdrant-ruby (0.9.3)
faraday (>= 2.0.1, < 3)
racc (1.7.1)
rack (2.2.7)
rack (2.2.8)
rack-protection (3.0.6)
rack
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.6)
actioncable (= 7.0.6)
actionmailbox (= 7.0.6)
actionmailer (= 7.0.6)
actionpack (= 7.0.6)
actiontext (= 7.0.6)
actionview (= 7.0.6)
activejob (= 7.0.6)
activemodel (= 7.0.6)
activerecord (= 7.0.6)
activestorage (= 7.0.6)
activesupport (= 7.0.6)
rails (7.0.7)
actioncable (= 7.0.7)
actionmailbox (= 7.0.7)
actionmailer (= 7.0.7)
actionpack (= 7.0.7)
actiontext (= 7.0.7)
actionview (= 7.0.7)
activejob (= 7.0.7)
activemodel (= 7.0.7)
activerecord (= 7.0.7)
activestorage (= 7.0.7)
activesupport (= 7.0.7)
bundler (>= 1.15.0)
railties (= 7.0.6)
rails-dom-testing (2.1.1)
railties (= 7.0.7)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
Expand All @@ -430,9 +430,9 @@ GEM
actionview (> 3.1)
activesupport (> 3.1)
railties (> 3.1)
railties (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
railties (7.0.7)
actionpack (= 7.0.7)
activesupport (= 7.0.7)
method_source
rake (>= 12.2)
thor (~> 1.0)
Expand Down Expand Up @@ -539,10 +539,6 @@ GEM
store_attribute (1.1.1)
activerecord (>= 6.0)
stripe (8.6.0)
tailwindcss-rails (2.0.30-arm64-darwin)
railties (>= 6.0.0)
tailwindcss-rails (2.0.30-x86_64-linux)
railties (>= 6.0.0)
thor (1.2.2)
tiktoken_ruby (0.0.5-arm64-darwin)
tiktoken_ruby (0.0.5-x86_64-linux)
Expand Down Expand Up @@ -572,12 +568,12 @@ GEM
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0)
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.8)
zeitwerk (2.6.11)

PLATFORMS
arm64-darwin-21
Expand Down
60 changes: 36 additions & 24 deletions app/models/purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Purchase < ApplicationRecord

attr_writer :virtual_purchased

validate :validate_purchased_items

def virtual_purchased
@virtual_purchased || []
end
Expand All @@ -16,39 +18,49 @@ def virtual_purchased_attributes=(attributes)
end

def validate_purchased_items

return if @virtual_purchased.blank?

@virtual_purchased.each do |purchased_item|
ticket = purchased.ticket
resource = purchased_item.resource

# Check if the ticket with the specified ID exists
if ticket.nil?
errors.add(:base, "Ticket with ID #{purchased_item.ticket_id} does not exist.")
next
case resource.class.to_s
when "EventTicket"
validate_ticket(resource, purchased_item)
# TODO: when "Track" # how to validate track? pricing
else

end

# Check if the ticket quantity is sufficient for purchase
if ticket.qty < purchased_item.quantity
errors.add(:base, "Insufficient quantity for Ticket ID #{ticket.id}. Available: #{ticket.qty}. Requested: #{purchased_item.quantity}.")
next
end
end
end

# Check if the total quantity purchased for this ticket in all previous purchases
# does not exceed the ticket's available quantity
total_purchased_quantity = PurchasedItem.where(ticket_id: purchased_item.ticket_id).sum(:quantity)
if ticket.qty < total_purchased_quantity + purchased_item.quantity
errors.add(:base, "Exceeded available quantity for Ticket ID #{ticket.id}. Already Purchased: #{total_purchased_quantity}. Requested: #{purchased_item.quantity}.")
end
def validate_ticket(ticket, purchased_item)
# Check if the ticket with the specified ID exists
if ticket.blank?
errors.add(:base, "Ticket with ID #{purchased_item.ticket_id} does not exist.")
end

# Check if the purchased quantity meets the :min_tickets_per_order setting
if ticket.settings.present? && ticket.settings['min_tickets_per_order'].to_i > purchased_item.quantity
errors.add(:base, "Ticket ID #{ticket.id} requires a minimum of #{ticket.settings['min_tickets_per_order']} tickets per order.")
end
# Check if the ticket quantity is sufficient for purchase
if purchased_item.quantity <= ticket.qty
errors.add(:base, "Insufficient quantity for Ticket ID #{ticket.id}. Available: #{ticket.qty}. Requested: #{purchased_item.quantity}.")
end

# Check if the purchased quantity exceeds the :max_tickets_per_order setting
if ticket.settings.present? && ticket.settings['max_tickets_per_order'].present? && ticket.settings['max_tickets_per_order'].to_i < purchased_item.quantity
errors.add(:base, "Ticket ID #{ticket.id} allows a maximum of #{ticket.settings['max_tickets_per_order']} tickets per order.")
end
# Check if the total quantity purchased for this ticket in all previous purchases
# does not exceed the ticket's available quantity
total_purchased_quantity = self.purchasable.purchased_event_tickets.where(id: ticket.id).size
if total_purchased_quantity + purchased_item.quantity > ticket.qty
errors.add(:base, "Exceeded available quantity for Ticket ID #{ticket.id}. Already Purchased: #{total_purchased_quantity}. Requested: #{purchased_item.quantity}.")
end

# Check if the purchased quantity meets the :min_tickets_per_order setting
if ticket.min_tickets_per_order.present? && purchased_item.quantity < ticket.min_tickets_per_order.to_i
errors.add(:base, "Ticket ID #{ticket.id} requires a minimum of #{ticket.settings['min_tickets_per_order']} tickets per order.")
end

# Check if the purchased quantity exceeds the :max_tickets_per_order setting
if ticket.max_tickets_per_order.present? && purchased_item.quantity > ticket.max_tickets_per_order.to_i
errors.add(:base, "Ticket ID #{ticket.id} allows a maximum of #{ticket.settings['max_tickets_per_order']} tickets per order.")
end
end

Expand Down
11 changes: 11 additions & 0 deletions app/models/purchased_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ class PurchasedItem < ApplicationRecord
belongs_to :purchase
belongs_to :purchased_item, polymorphic: true

include AASM

aasm column: :state do
state :pending, initial: true
state :paid

event :confirm do
transitions from: :pending, to: :paid
end
end

def qr
url = Rails.application.routes.url_helpers.event_event_ticket_url(purchase.purchasable, signed_id)
encoded_url = ERB::Util.url_encode(url)
Expand Down
1 change: 1 addition & 0 deletions app/services/dante/video_block_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def render
private

def render_embed_html(embed_data)
return nil if embed_data.blank?
embed_data[:media] ? embed_data[:media][:html] : embed_data[:html]
end

Expand Down
Loading

0 comments on commit 01c9da4

Please sign in to comment.