|
|
0.0% |
17 |
1 |
71 |
2
1
def initialize(product, pharmacy = nil)
8
0
base = @product.price_cents
9
0
base = apply_pharmacy_markup(base) if @pharmacy
10
0
base = apply_volume_discount(base)
11
0
base = apply_seasonal_adjustment(base)
12
0
{ final_price: base, currency: "EUR" }
17
1
def apply_pharmacy_markup(price)
18
0
markup = @pharmacy.auto_accept_orders? ? 1.05 : 1.08
19
0
(price * markup).round
22
1
def apply_volume_discount(price)
23
0
return price if @product.stock.to_i < 100
24
0
(price * 0.96).round
27
1
def apply_seasonal_adjustment(price)
28
0
month = Time.current.month
29
0
if [11, 12, 1].include?(month)
30
0
(price * 1.08).round # winter markup
31
0
elsif [6, 7, 8].include?(month)
32
0
(price * 0.95).round # summer discount
|
|
|
37.5% |
6 |
2 |
43 |
1
1
class Order < ApplicationRecord
2
1
has_many :order_items, dependent: :destroy
4
1
validates :customer_email, presence: true
5
1
validates :status, inclusion: { in: %w[pending confirmed shipped delivered cancelled] }
7
1
scope :pending, -> { where(status: "pending") }
8
1
scope :active, -> { where.not(status: "cancelled") }
11
1
update!(status: "confirmed")
15
2
raise "Cannot ship unconfirmed order" unless status == "confirmed"
16
1
update!(status: "shipped", shipped_at: Time.current)
20
1
raise "Cannot deliver unshipped order" unless status == "shipped"
21
1
update!(status: "delivered")
24
1
def cancel!(reason: nil)
25
0
update!(status: "cancelled", notes: reason)
29
0
payment_method == "cod"
32
1
def recalculate_total!
33
25
update!(total_cents: order_items.sum { |item| item.subtotal_cents })
36
1
def awaiting_cod_confirmation?
37
0
cod? && status == "pending"
40
1
def handle_cod_timeout
41
0
return unless payment_method == "cod"
42
0
cancel!(reason: "cod_timeout") if awaiting_cod_confirmation?
43
0
notify_cs_team(:cod_timeout_alert)
48
1
def notify_cs_team(alert_type)
|
|
|
35.7% |
10 |
1 |
40 |
2
1
class PaymentError < StandardError; end
4
1
def initialize(order)
9
2
raise PaymentError, "Order already paid" if @order.status == "confirmed"
10
1
raise PaymentError, "Invalid order" unless @order.total_cents&.positive?
11
1
{ status: :authorized, amount: @order.total_cents }
15
1
raise PaymentError, "Must authorize first" unless @order.status == "confirmed"
16
1
{ status: :captured, amount: @order.total_cents }
20
0
raise PaymentError, "Cannot refund pending order" if @order.status == "pending"
21
0
{ status: :refunded, amount: @order.total_cents }
26
0
@order.cancel!(reason: "payment_timeout")
35
0
3.times do |attempt|
37
0
return result if result[:status] == :captured
41
0
@order.cancel!(reason: "capture_failed_after_retries")
|
|
|
33.3% |
8 |
1 |
29 |
2
1
class CartOptimizationError < StandardError; end
4
1
def initialize(products, pharmacies)
6
2
@pharmacies = pharmacies
10
2
available = @pharmacies.select(&:accepts_orders?)
11
2
raise CartOptimizationError, "no pharmacies available" if available.empty?
13
3
best = available.min_by { |pharmacy| total_cost(pharmacy) }
14
1
{ pharmacy: best, total_cost: total_cost(best), items: build_items(best) }
19
1
def total_cost(pharmacy)
20
12
@products.sum { |p| p.price_cents }
23
1
def build_items(pharmacy)
24
1
@products.map do |product|
25
3
{ product: product, pharmacy: pharmacy, price: product.price_cents }
29
1
def calculate_absorption(pharmacy, product)
30
0
ratio = pharmacy.id.to_f / (product.price_cents + 1)
31
0
ratio > max_absorption ? max_absorption : ratio
38
1
def apply_discounts(items)
40
0
if item[:price] > 4000
41
0
item.merge(discount: (item[:price] * 0.05).round)
48
1
def validate_availability(pharmacy, products)
49
0
products.all? { |p| p.in_stock? }
|
|
|
— |
0 |
1 |
0 |
1
1
class ApplicationController < ActionController::API
|
|
|
— |
0 |
1 |
0 |
1
1
class ApplicationJob < ActiveJob::Base
2
# Automatically retry jobs that encountered a deadlock
3
# retry_on ActiveRecord::Deadlocked
5
# Most jobs are safe to ignore if the underlying records are no longer available
6
# discard_on ActiveJob::DeserializationError
|
|
|
100.0% |
0 |
1 |
0 |
1
1
class InventorySyncJob < ApplicationJob
4
1
def perform(pharmacy_id)
5
2
pharmacy = Pharmacy.find(pharmacy_id)
6
2
return unless pharmacy.active?
8
1
products = Product.active.in_stock
9
1
products.each do |product|
10
5
sync_product(pharmacy, product)
16
1
def sync_product(pharmacy, product)
17
# Simulate external API sync
18
5
{ pharmacy: pharmacy.code, product: product.minsan_code, stock: product.stock }
|
|
|
— |
0 |
1 |
0 |
1
1
class ApplicationRecord < ActiveRecord::Base
2
1
primary_abstract_class
|
|
|
— |
0 |
2 |
0 |
1
1
class OrderItem < ApplicationRecord
5
1
validates :quantity, presence: true, numericality: { greater_than: 0 }
6
1
validates :unit_price_cents, presence: true, numericality: { greater_than: 0 }
8
1
after_save :recalculate_order_total
11
14
quantity * unit_price_cents
16
1
def recalculate_order_total
17
11
order.recalculate_total!
|
|
|
— |
0 |
1 |
0 |
1
1
class Pharmacy < ApplicationRecord
2
1
validates :name, presence: true
3
1
validates :code, presence: true, uniqueness: true
5
1
scope :active, -> { where(active: true) }
6
1
scope :auto_accept, -> { where(auto_accept_orders: true) }
9
6
active? && auto_accept_orders?
|
|
|
100.0% |
0 |
1 |
0 |
1
1
class Product < ApplicationRecord
2
1
validates :name, presence: true
3
1
validates :minsan_code, presence: true, uniqueness: true
4
1
validates :price_cents, presence: true, numericality: { greater_than: 0 }
5
1
validates :stock, numericality: { greater_than_or_equal_to: 0 }
7
3
scope :active, -> { where(active: true) }
8
3
scope :in_stock, -> { where("stock > 0") }
9
2
scope :by_category, ->(cat) { where(category: cat) }
12
1
format("%.2f", price_cents / 100.0)
19
1
def reserve!(quantity)
20
2
raise "Insufficient stock" if stock < quantity
21
1
update!(stock: stock - quantity)
|
|
|
81.8% |
0 |
1 |
0 |
1
1
class ShippingCalculator
2
1
STANDARD_RATE = 499 # cents
4
1
FREE_THRESHOLD = 4900 # free shipping above 49 EUR
6
1
def initialize(order)
10
1
def calculate(method: :standard)
11
5
return { cost: 0, method: :free } if qualifies_for_free_shipping?
14
2
when :standard then calculate_standard
15
1
when :express then calculate_express
16
1
else raise "Unknown shipping method: #{method}"
19
3
{ cost: cost, method: method }
24
1
def qualifies_for_free_shipping?
25
5
@order.total_cents.to_i >= FREE_THRESHOLD
28
1
def calculate_standard
29
# Simulate API call latency
30
2
sleep(0.01) if ENV["SIMULATE_LATENCY"]
31
2
STANDARD_RATE + weight_surcharge
34
1
def calculate_express
35
1
sleep(0.01) if ENV["SIMULATE_LATENCY"]
36
1
EXPRESS_RATE + weight_surcharge
39
1
def weight_surcharge
40
3
item_count = @order.order_items.count
41
3
item_count > 5 ? (item_count - 5) * 100 : 0
|