mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-20 14:54:15 +01:00
Merge branch 'spaces_multiprices' into spaces_multiprices_front
This commit is contained in:
commit
af4acc895c
@ -5,6 +5,19 @@
|
||||
class API::PricesController < API::ApiController
|
||||
before_action :authenticate_user!
|
||||
|
||||
def create
|
||||
@price = Price.new(price_params)
|
||||
@price.amount *= 100
|
||||
|
||||
authorize @price
|
||||
|
||||
if @price.save
|
||||
render json: @price, status: :created
|
||||
else
|
||||
render json: @price.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def index
|
||||
@prices = PriceService.list(params)
|
||||
end
|
||||
@ -29,6 +42,10 @@ class API::PricesController < API::ApiController
|
||||
|
||||
private
|
||||
|
||||
def price_create_params
|
||||
params.require(:price).permit(:amount, :duration, :group_id, :plan_id, :priceable_id, :priceable_type)
|
||||
end
|
||||
|
||||
def price_params
|
||||
params.require(:price).permit(:amount, :duration)
|
||||
end
|
||||
|
@ -12,7 +12,7 @@ export interface Price {
|
||||
priceable_type: string,
|
||||
priceable_id: number,
|
||||
amount: number,
|
||||
duration: number // in minutes
|
||||
duration?: number // in minutes
|
||||
}
|
||||
|
||||
export interface ComputePriceResult {
|
||||
|
15
app/frontend/src/javascript/models/space.ts
Normal file
15
app/frontend/src/javascript/models/space.ts
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
export interface Space {
|
||||
id: number,
|
||||
name: string,
|
||||
description: string,
|
||||
slug: string,
|
||||
default_places: number,
|
||||
disabled: boolean,
|
||||
space_image: string,
|
||||
space_file_attributes?: {
|
||||
id: number,
|
||||
attachment: string,
|
||||
attachement_url: string,
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ class CartItem::Reservation < CartItem::BaseItem
|
||||
end
|
||||
|
||||
def price
|
||||
base_amount = get_hourly_rate
|
||||
base_amount = hourly_rate
|
||||
is_privileged = @operator.privileged? && @operator.id != @customer.id
|
||||
prepaid = { minutes: PrepaidPackService.minutes_available(@customer, @reservable) }
|
||||
|
||||
@ -109,26 +109,24 @@ class CartItem::Reservation < CartItem::BaseItem
|
||||
# Eg. If the reservation is for 12 hours, and there are prices for 3 hours, 7 hours,
|
||||
# and the base price (1 hours), we use the 7 hours price, then 3 hours price, and finally the base price.
|
||||
# Then we divide the total price by the total duration to get the hourly rate.
|
||||
def get_hourly_rate
|
||||
def hourly_rate
|
||||
total_duration = @slots.map { |slot| (slot[:end_at].to_time - slot[:start_at].to_time) / SECONDS_PER_MINUTE }.reduce(:+)
|
||||
price = 0
|
||||
|
||||
remaining_duration = total_duration
|
||||
while remaining_duration > 60
|
||||
while remaining_duration.positive?
|
||||
max_duration = @reservable.prices.where(group_id: @customer.group_id, plan_id: @plan.try(:id))
|
||||
.where(Price.arel_table[:duration].lteq(remaining_duration))
|
||||
.maximum(:duration)
|
||||
max_duration = 60 if max_duration.nil?
|
||||
max_duration_amount = @reservable.prices.find_by(group_id: @customer.group_id, plan_id: @plan.try(:id), duration: max_duration)
|
||||
.amount
|
||||
|
||||
price += max_duration_amount
|
||||
current_duration = [remaining_duration, max_duration].min
|
||||
price += (max_duration_amount / max_duration) * current_duration
|
||||
remaining_duration -= max_duration
|
||||
end
|
||||
|
||||
# base price for the last hour or less
|
||||
base_amount = @reservable.prices.find_by(group_id: @customer.group_id, plan_id: @plan.try(:id), duration: 60).amount
|
||||
price += (base_amount / MINUTES_PER_HOUR) * remaining_duration
|
||||
|
||||
price / (total_duration / MINUTES_PER_HOUR)
|
||||
end
|
||||
|
||||
|
@ -2,6 +2,10 @@
|
||||
|
||||
# Check the access policies for API::PricesController
|
||||
class PricePolicy < ApplicationPolicy
|
||||
def create?
|
||||
user.admin? && record.duration != 60
|
||||
end
|
||||
|
||||
def update?
|
||||
user.admin?
|
||||
end
|
||||
|
3
app/views/api/prices/create.json.jbuilder
Normal file
3
app/views/api/prices/create.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
json.partial! 'api/prices/price', price: @price
|
@ -1 +1,3 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
json.partial! 'api/prices/price', price: @price
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
json.array!(@spaces) do |space|
|
||||
json.extract! space, :id, :name, :description, :slug, :default_places, :disabled
|
||||
json.space_image space.space_image.attachment.medium.url if space.space_image
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
json.extract! @space, :id, :name, :description, :characteristics, :created_at, :updated_at, :slug, :default_places, :disabled
|
||||
json.space_image @space.space_image.attachment.large.url if @space.space_image
|
||||
json.space_files_attributes @space.space_files do |f|
|
||||
@ -9,4 +11,4 @@ end
|
||||
# using the space in the space_show screen
|
||||
# json.space_projects @space.projects do |p|
|
||||
# json.extract! p, :slug, :name
|
||||
# end
|
||||
# end
|
||||
|
@ -75,7 +75,7 @@ Rails.application.routes.draw do
|
||||
get 'pricing' => 'pricing#index'
|
||||
put 'pricing' => 'pricing#update'
|
||||
|
||||
resources :prices, only: %i[index update] do
|
||||
resources :prices, only: %i[create index update] do
|
||||
post 'compute', on: :collection
|
||||
end
|
||||
resources :prepaid_packs
|
||||
|
36
test/fixtures/prices.yml
vendored
36
test/fixtures/prices.yml
vendored
@ -6,6 +6,7 @@ price_1:
|
||||
priceable_id: 1
|
||||
priceable_type: Machine
|
||||
amount: 2400
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.242608000 Z
|
||||
updated_at: 2016-04-04 14:11:34.242608000 Z
|
||||
|
||||
@ -16,6 +17,7 @@ price_2:
|
||||
priceable_id: 1
|
||||
priceable_type: Machine
|
||||
amount: 5300
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.247363000 Z
|
||||
updated_at: 2016-04-04 14:11:34.247363000 Z
|
||||
|
||||
@ -26,6 +28,7 @@ price_5:
|
||||
priceable_id: 2
|
||||
priceable_type: Machine
|
||||
amount: 4200
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.290427000 Z
|
||||
updated_at: 2016-04-04 14:11:34.290427000 Z
|
||||
|
||||
@ -36,6 +39,7 @@ price_6:
|
||||
priceable_id: 2
|
||||
priceable_type: Machine
|
||||
amount: 1100
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.293603000 Z
|
||||
updated_at: 2016-04-04 14:11:34.293603000 Z
|
||||
|
||||
@ -46,6 +50,7 @@ price_9:
|
||||
priceable_id: 3
|
||||
priceable_type: Machine
|
||||
amount: 4100
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.320809000 Z
|
||||
updated_at: 2016-04-04 14:11:34.320809000 Z
|
||||
|
||||
@ -56,6 +61,7 @@ price_10:
|
||||
priceable_id: 3
|
||||
priceable_type: Machine
|
||||
amount: 5300
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.325274000 Z
|
||||
updated_at: 2016-04-04 14:11:34.325274000 Z
|
||||
|
||||
@ -66,6 +72,7 @@ price_13:
|
||||
priceable_id: 4
|
||||
priceable_type: Machine
|
||||
amount: 900
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.362313000 Z
|
||||
updated_at: 2016-04-04 14:11:34.362313000 Z
|
||||
|
||||
@ -76,6 +83,7 @@ price_14:
|
||||
priceable_id: 4
|
||||
priceable_type: Machine
|
||||
amount: 5100
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.366049000 Z
|
||||
updated_at: 2016-04-04 14:11:34.366049000 Z
|
||||
|
||||
@ -86,6 +94,7 @@ price_17:
|
||||
priceable_id: 5
|
||||
priceable_type: Machine
|
||||
amount: 1600
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.398206000 Z
|
||||
updated_at: 2016-04-04 14:11:34.398206000 Z
|
||||
|
||||
@ -96,6 +105,7 @@ price_18:
|
||||
priceable_id: 5
|
||||
priceable_type: Machine
|
||||
amount: 2000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.407216000 Z
|
||||
updated_at: 2016-04-04 14:11:34.407216000 Z
|
||||
|
||||
@ -106,6 +116,7 @@ price_21:
|
||||
priceable_id: 6
|
||||
priceable_type: Machine
|
||||
amount: 3200
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.442054000 Z
|
||||
updated_at: 2016-04-04 14:11:34.442054000 Z
|
||||
|
||||
@ -116,6 +127,7 @@ price_22:
|
||||
priceable_id: 6
|
||||
priceable_type: Machine
|
||||
amount: 3400
|
||||
duration: 60
|
||||
created_at: 2016-04-04 14:11:34.445147000 Z
|
||||
updated_at: 2016-04-04 14:11:34.445147000 Z
|
||||
|
||||
@ -126,6 +138,7 @@ price_25:
|
||||
priceable_id: 1
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:15:21.038387000 Z
|
||||
updated_at: 2016-04-04 15:15:45.691674000 Z
|
||||
|
||||
@ -136,6 +149,7 @@ price_26:
|
||||
priceable_id: 2
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:15:21.048838000 Z
|
||||
updated_at: 2016-04-04 15:15:45.693896000 Z
|
||||
|
||||
@ -146,6 +160,7 @@ price_27:
|
||||
priceable_id: 3
|
||||
priceable_type: Machine
|
||||
amount: 2500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:15:21.053412000 Z
|
||||
updated_at: 2016-04-04 15:15:45.697794000 Z
|
||||
|
||||
@ -156,6 +171,7 @@ price_28:
|
||||
priceable_id: 4
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:15:21.057117000 Z
|
||||
updated_at: 2016-04-04 15:15:45.700657000 Z
|
||||
|
||||
@ -166,6 +182,7 @@ price_29:
|
||||
priceable_id: 5
|
||||
priceable_type: Machine
|
||||
amount: 1300
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:15:21.061171000 Z
|
||||
updated_at: 2016-04-04 15:15:45.707564000 Z
|
||||
|
||||
@ -176,6 +193,7 @@ price_30:
|
||||
priceable_id: 6
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:15:21.065166000 Z
|
||||
updated_at: 2016-04-04 15:15:45.710945000 Z
|
||||
|
||||
@ -186,6 +204,7 @@ price_31:
|
||||
priceable_id: 1
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:17:24.920457000 Z
|
||||
updated_at: 2016-04-04 15:17:34.255229000 Z
|
||||
|
||||
@ -196,6 +215,7 @@ price_32:
|
||||
priceable_id: 2
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:17:24.926967000 Z
|
||||
updated_at: 2016-04-04 15:17:34.257285000 Z
|
||||
|
||||
@ -206,6 +226,7 @@ price_33:
|
||||
priceable_id: 3
|
||||
priceable_type: Machine
|
||||
amount: 2500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:17:24.932723000 Z
|
||||
updated_at: 2016-04-04 15:17:34.258741000 Z
|
||||
|
||||
@ -216,6 +237,7 @@ price_34:
|
||||
priceable_id: 4
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:17:24.937168000 Z
|
||||
updated_at: 2016-04-04 15:17:34.260503000 Z
|
||||
|
||||
@ -226,6 +248,7 @@ price_35:
|
||||
priceable_id: 5
|
||||
priceable_type: Machine
|
||||
amount: 1300
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:17:24.940520000 Z
|
||||
updated_at: 2016-04-04 15:17:34.263627000 Z
|
||||
|
||||
@ -236,6 +259,7 @@ price_36:
|
||||
priceable_id: 6
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:17:24.944460000 Z
|
||||
updated_at: 2016-04-04 15:17:34.267328000 Z
|
||||
|
||||
@ -246,6 +270,7 @@ price_37:
|
||||
priceable_id: 1
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.836899000 Z
|
||||
updated_at: 2016-04-04 15:18:50.507019000 Z
|
||||
|
||||
@ -256,6 +281,7 @@ price_38:
|
||||
priceable_id: 2
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.842674000 Z
|
||||
updated_at: 2016-04-04 15:18:50.508799000 Z
|
||||
|
||||
@ -266,6 +292,7 @@ price_39:
|
||||
priceable_id: 3
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.847736000 Z
|
||||
updated_at: 2016-04-04 15:18:50.510437000 Z
|
||||
|
||||
@ -276,6 +303,7 @@ price_40:
|
||||
priceable_id: 4
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.852783000 Z
|
||||
updated_at: 2016-04-04 15:18:50.512239000 Z
|
||||
|
||||
@ -286,6 +314,7 @@ price_41:
|
||||
priceable_id: 5
|
||||
priceable_type: Machine
|
||||
amount: 800
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.856602000 Z
|
||||
updated_at: 2016-04-04 15:18:50.514062000 Z
|
||||
|
||||
@ -296,6 +325,7 @@ price_42:
|
||||
priceable_id: 6
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.860220000 Z
|
||||
updated_at: 2016-04-04 15:18:50.517702000 Z
|
||||
|
||||
@ -306,6 +336,7 @@ price_43:
|
||||
priceable_id: 1
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.836899000 Z
|
||||
updated_at: 2016-04-04 15:18:50.507019000 Z
|
||||
|
||||
@ -316,6 +347,7 @@ price_44:
|
||||
priceable_id: 2
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.842674000 Z
|
||||
updated_at: 2016-04-04 15:18:50.508799000 Z
|
||||
|
||||
@ -326,6 +358,7 @@ price_45:
|
||||
priceable_id: 3
|
||||
priceable_type: Machine
|
||||
amount: 1500
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.847736000 Z
|
||||
updated_at: 2016-04-04 15:18:50.510437000 Z
|
||||
|
||||
@ -336,6 +369,7 @@ price_46:
|
||||
priceable_id: 4
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.852783000 Z
|
||||
updated_at: 2016-04-04 15:18:50.512239000 Z
|
||||
|
||||
@ -346,6 +380,7 @@ price_47:
|
||||
priceable_id: 5
|
||||
priceable_type: Machine
|
||||
amount: 800
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.856602000 Z
|
||||
updated_at: 2016-04-04 15:18:50.514062000 Z
|
||||
|
||||
@ -356,5 +391,6 @@ price_48:
|
||||
priceable_id: 6
|
||||
priceable_type: Machine
|
||||
amount: 1000
|
||||
duration: 60
|
||||
created_at: 2016-04-04 15:18:28.860220000 Z
|
||||
updated_at: 2016-04-04 15:18:50.517702000 Z
|
||||
|
Loading…
x
Reference in New Issue
Block a user