diff --git a/Procfile b/Procfile index 6fdee4e7..bb6ce271 100644 --- a/Procfile +++ b/Procfile @@ -6,5 +6,5 @@ # production: conters: bin/drb_counter.rb start #counters: drb_counter/drb_counter.rb development #faye: thin start -R faye/config.ru -p 9296 -web: rails s -p 3000 -b 0.0.0.0 +web: bundle exec rails s -p 3000 -b 0.0.0.0 #server: bundle exec rails s -e production diff --git a/app/controllers/supplier_controller.rb b/app/controllers/supplier_controller.rb index e2f9cf3b..6ca7fe31 100644 --- a/app/controllers/supplier_controller.rb +++ b/app/controllers/supplier_controller.rb @@ -1,5 +1,11 @@ class SupplierController < Suppliers::ApplicationController def home + if Rails.env.development? + binding.pry + redirecto_to "http://localhost:4202/supplier#{params[:other]}" + else + render html: File.read(Rails.root.join('public/supplier/index.html')) + end end =begin # GET /suppliers/1/active_orders diff --git a/app/controllers/suppliers/application_controller.rb b/app/controllers/suppliers/application_controller.rb index 6ab6b692..df1850e4 100644 --- a/app/controllers/suppliers/application_controller.rb +++ b/app/controllers/suppliers/application_controller.rb @@ -2,9 +2,6 @@ module Suppliers class ApplicationController < ::ApplicationController before_action :setup_employee_and_supplier! #load_and_authorize_resource - if Rails.env.development? - skip_before_action :setup_employee_and_supplier!, only: :employee_and_supplier - end attr_reader :current_supplier helper_method :current_supplier layout 'supplier/app' @@ -22,25 +19,21 @@ module Suppliers # GET #NOTE: temporary solution for development, if I am in production something is wrong def employee_and_supplier - employee = current_employee || Employee.find_by_email('bterkuile@gmail.com') - raise CanCan::AccessDenied unless employee.present? - supplier = current_supplier || employee.suppliers.first - employee.enrich_with_settings supplier.settings_for(employee) + # database optimization, preloading FlatKeys.as_nested_structure(Supplier::PRELOAD_INCLUDES).last.each do |relation_name, includes| - relation_result = supplier.public_send(relation_name) + relation_result = current_supplier.public_send(relation_name) relation_result.include_relations(includes) if relation_result.is_a?(Array) end render json: { - employee: JSONAPI::Serializer.serialize(employee, serializer: Suppliers::EmployeeSerializer), - supplier: JSONAPI::Serializer.serialize(supplier, serializer: Suppliers::SupplierSerializer, include: Supplier::PRELOAD_INCLUDES), - auth_token: employee.authentication_token, + employee: JSONAPI::Serializer.serialize(current_employee, serializer: Suppliers::EmployeeSerializer), + supplier: JSONAPI::Serializer.serialize(current_supplier, serializer: Suppliers::SupplierSerializer, include: Supplier::PRELOAD_INCLUDES), } end def setup_employee_and_supplier! authenticate_employee! find_current_supplier! - return unless current_supplier.present? + raise CanCan::AccessDenied unless current_supplier.present? current_employee.enrich_with_settings current_supplier.settings_for(current_employee) raise CanCan::AccessDenied unless current_employee.active? @current_ability = Suppliers::Ability.new( current_employee ) @@ -55,7 +48,7 @@ module Suppliers if supplier.employee_ids.include?(current_employee.id) @current_supplier = supplier else - render nothing: true, status: :unauthorized + session[:supplier_id] = nil end else @current_supplier = current_employee.suppliers.first diff --git a/app/controllers/suppliers/employee_shifts_controller.rb b/app/controllers/suppliers/employee_shifts_controller.rb index 322bd340..a1df69c8 100644 --- a/app/controllers/suppliers/employee_shifts_controller.rb +++ b/app/controllers/suppliers/employee_shifts_controller.rb @@ -25,6 +25,7 @@ module Suppliers end def destroy + @employee_shift = EmployeeShift.find_by_supplier_id_and_id!(current_supplier.id, params[:id]) head :forbidden and return unless @employee_shift.supplier_id == current_supplier.id @employee_shift= EmployeeShift.find_by_supplier_id_and_id!(current_supplier.id, params[:id]) @employee_shift.destroy @@ -34,7 +35,13 @@ module Suppliers private def employee_shift_params - params.require(:employee_shift).permit(:start_from, :end_on, :employee_id, :description) + permitted_attributes = [:start_from, :end_on, :employee_id, :description, :full_day] + # do not raise in development and test for json communication + result = if request.format.json? + params.require(:employee_shift).slice(*permitted_attributes).permit! + else + params.require(:employee_shift).permit permitted_attributes + end end end end diff --git a/app/controllers/suppliers/employees_controller.rb b/app/controllers/suppliers/employees_controller.rb index 787b6454..bb07f7c9 100644 --- a/app/controllers/suppliers/employees_controller.rb +++ b/app/controllers/suppliers/employees_controller.rb @@ -11,6 +11,11 @@ module Suppliers render json: @employees end + def suppliers + @suppliers = current_employee.suppliers + render json: @suppliers, serializer: Suppliers::SupplierSerializer, is_collection: true + end + # GET /employees/1 # GET /employees/1.json def show diff --git a/app/controllers/suppliers/sessions_controller.rb b/app/controllers/suppliers/sessions_controller.rb index 99e6d5c2..4146f7b7 100644 --- a/app/controllers/suppliers/sessions_controller.rb +++ b/app/controllers/suppliers/sessions_controller.rb @@ -1,6 +1,15 @@ class Suppliers::SessionsController < Devise::SessionsController respond_to :json + def create + # taken from super except the respond_with + self.resource = warden.authenticate!(auth_options) + set_flash_message!(:notice, :signed_in) + sign_in(resource_name, resource) + yield resource if block_given? + render json: {employee_id: current_employee.id, auth_token: current_employee.authentication_token} + end + def destroy session[:supplier_id] = nil super diff --git a/app/controllers/suppliers/suppliers_controller.rb b/app/controllers/suppliers/suppliers_controller.rb index 7aedf569..6014e714 100644 --- a/app/controllers/suppliers/suppliers_controller.rb +++ b/app/controllers/suppliers/suppliers_controller.rb @@ -32,7 +32,7 @@ module Suppliers def switch_to @switch_supplier = Supplier.find(params[:id]) session[:supplier_id] = params[:id] if @switch_supplier.employee_ids.include? current_employee.id - redirect_to supplier_root_path(anchor: '/my-account') + redirect_to supplier_root_path(other: '/my-account') end def mark_as_open diff --git a/app/models/employee_shift.rb b/app/models/employee_shift.rb index 4f7589a3..fa70c0f6 100644 --- a/app/models/employee_shift.rb +++ b/app/models/employee_shift.rb @@ -5,6 +5,7 @@ class EmployeeShift property :start_from property :end_on property :description + property :full_day, type: :boolean, default: false belongs_to :supplier belongs_to :employee @@ -12,6 +13,7 @@ class EmployeeShift validates :start_from, presence: true validates :end_on, presence: true + view :by_supplier_id_and_id, key: [:supplier_id, :_id] # supplier safe find view :for_supplier_view, type: :custom, map_function: %|function(doc){ if(doc.ruby_class == 'EmployeeShift' && doc.start_from && doc.end_on){ emit([doc.supplier_id, doc.end_on], 1) diff --git a/app/serializers/employees/employee_shift_serializer.rb b/app/serializers/employees/employee_shift_serializer.rb index 80383112..4c752f48 100644 --- a/app/serializers/employees/employee_shift_serializer.rb +++ b/app/serializers/employees/employee_shift_serializer.rb @@ -1,6 +1,6 @@ class Employees::EmployeeShiftSerializer include Qwaiter::EmployeeBaseSerializer - attributes :start_from, :end_on, :description + attributes :start_from, :end_on, :description, :full_day has_one :supplier, serializer: Employees::SupplierSerializer has_one :employee, serializer: Employees::EmployeeSerializer end diff --git a/app/serializers/suppliers/employee_serializer.rb b/app/serializers/suppliers/employee_serializer.rb index 8394758d..ab21b16c 100644 --- a/app/serializers/suppliers/employee_serializer.rb +++ b/app/serializers/suppliers/employee_serializer.rb @@ -1,4 +1,7 @@ class Suppliers::EmployeeSerializer include Qwaiter::SupplierBaseSerializer attributes :name, :email, :manager, :active, :color + has_many :suppliers, serializer: Suppliers::SupplierSerializer + + related_link_for :suppliers end diff --git a/app/serializers/suppliers/employee_shift_serializer.rb b/app/serializers/suppliers/employee_shift_serializer.rb index c1892b9b..31a61d26 100644 --- a/app/serializers/suppliers/employee_shift_serializer.rb +++ b/app/serializers/suppliers/employee_shift_serializer.rb @@ -1,6 +1,6 @@ class Suppliers::EmployeeShiftSerializer include Qwaiter::SupplierBaseSerializer - attributes :start_from, :end_on + attributes :start_from, :end_on, :description, :full_day has_one :supplier, serializer: Suppliers::SupplierSerializer has_one :employee, serializer: Suppliers::EmployeeSerializer end diff --git a/config/routes.rb b/config/routes.rb index 5620cc46..bb5767e7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -116,7 +116,6 @@ Qwaiter::Application.routes.draw do get :tests, to: 'test_squad#tests' unless Rails.env.production? # SUPPLIER - get '/supplier' => 'supplier#home', as: :supplier_root if Rails.env.test? or Rails.env.development? namespace :tests do resources :suppliers @@ -124,7 +123,7 @@ Qwaiter::Application.routes.draw do end end - namespace :suppliers, path: '/supplier' do + namespace :suppliers, path: '/supplier/api/v1' do get 'employee_and_supplier', controller: 'application' resources :suppliers do member do @@ -152,7 +151,9 @@ Qwaiter::Application.routes.draw do end end - resources :employees + resources :employees do + get :suppliers, on: :member + end resources :employee_shifts resources :products do collection do @@ -208,6 +209,7 @@ Qwaiter::Application.routes.draw do #devise_scope :supplier do #get '/:locale/suppliers/sign_up' => 'registrations#new', constraints: {locale: ALLOWED_LOCALES} #end + get '/supplier*other' => 'supplier#home', as: :supplier_root scope '(/:locale)', constraints: {locale: ALLOWED_LOCALES}, defaults: { locale: Rails.application.config.i18n.default_locale.to_s } do root to: 'pages#home' resources :contact_forms, only: [:create] diff --git a/drb_counter/rebuild-docker.sh b/drb_counter/rebuild-docker.sh index 0646aea3..ec243310 100755 --- a/drb_counter/rebuild-docker.sh +++ b/drb_counter/rebuild-docker.sh @@ -24,3 +24,6 @@ docker build -f drb_counter/Dockerfile -t mozo_drb_counter . # 5. Spin up the counter container from the generated image docker run --network=host --env DRB_ENV=production --detach --name=mozo_drb_counter mozo_drb_counter + +# To just start the container created through al these steps without rebuilding them: +# docker container start $(docker ps -a -q --filter ancestor=mozo_drb_counter) diff --git a/faye/rebuild-docker.sh b/faye/rebuild-docker.sh index b05cac2d..9471950c 100755 --- a/faye/rebuild-docker.sh +++ b/faye/rebuild-docker.sh @@ -24,3 +24,6 @@ docker build -f faye/Dockerfile -t mozo_faye . # 5. Spin up the counter container from the generated image docker run --network=host --env DRB_ENV=production --detach --name=mozo_faye mozo_faye + +# To just start the container created through al these steps without rebuilding them: +# docker container start $(docker ps -a -q --filter ancestor=mozo_faye) diff --git a/lib/qwaiter/supplier_base_serializer.rb b/lib/qwaiter/supplier_base_serializer.rb index 5baf08b5..b51a4422 100644 --- a/lib/qwaiter/supplier_base_serializer.rb +++ b/lib/qwaiter/supplier_base_serializer.rb @@ -8,7 +8,7 @@ module Qwaiter::SupplierBaseSerializer end def base_url - "/supplier/api/v1" + "/supplier/api/v1" # no api here, should be, but result from refacoring in steps, future update end #def format_name(attribute_name)