Skip to content

Commit

Permalink
Merge pull request #12 from xlpero/managing_group
Browse files Browse the repository at this point in the history
Add migration, model and controller för managing groups
  • Loading branch information
gnucifer authored Mar 6, 2019
2 parents 238cb34 + 1f1d504 commit 6d4ea8d
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 8 deletions.
54 changes: 54 additions & 0 deletions app/controllers/managing_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class ManagingGroupsController < ApplicationController
before_filter :validate_token
# Find all managing_group and show them.
def index
objs = ManagingGroup.all.order(:name)
if objs
render json: {managing_groups: objs}, status: 200
else
render json: {}, status: 500
end
rescue => error
render json: {}, status: 500
end

# Find one managing_group using id and show it.
def show
objid = params[:id]
obj = ManagingGroup.find_by_id(objid)
if obj
render json: {managing_group: obj}, status: 200
else
render json: {}, status: 404
end
rescue => error
render json: {}, status: 500
end

# Find one managing_group using label and show it.
def show_by_label
objid = params[:label]
obj = ManagingGroup.find_by_label(objid)
if obj
render json: {managing_group: obj}, status: 200
else
render json: {}, status: 404
end
rescue => error
render json: {}, status: 500
end


def create
render json: {}, status: 501
end

def update
render json: {}, status: 501
end

def delete
render json: {}, status: 501
end

end
28 changes: 22 additions & 6 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def index
sortfield = params[:sortfield] || 'created_at'
sortdir = params[:sortdir] || 'DESC'

user = params[:user]
user = params[:user]

is_archived_str = params[:is_archived] || ''
apply_archive_filter = false
Expand All @@ -51,8 +51,12 @@ def index
apply_to_be_invoiced_filter = true
end

current_managing_group = params[:current_managing_group] || ''
current_managing_group = '' if current_managing_group.downcase.eql?('null')
current_managing_group = '' if current_managing_group.eql?('0')
current_managing_group = '' if current_managing_group.downcase.eql?('all')

current_pickup_location = params[:currentPickupLocation] || ''
current_pickup_location = params[:current_pickup_location] || ''
current_pickup_location = '' if current_pickup_location.downcase.eql?('null')
current_pickup_location = '' if current_pickup_location.eql?('0')
current_pickup_location = '' if current_pickup_location.downcase.eql?('all')
Expand Down Expand Up @@ -96,9 +100,10 @@ def index
@orders = @orders.where(status_id: status_group_obj.statuses.map(&:id))
end

@orders = @orders.where(pickup_location_id: current_pickup_location) if current_pickup_location.present?
@orders = @orders.where(user_id: user) if user.present?
@orders = @orders.where(order_type_id: order_type) if order_type.present?
@orders = @orders.where(managing_group_id: current_managing_group) if current_managing_group.present?
@orders = @orders.where(pickup_location_id: current_pickup_location) if current_pickup_location.present?
@orders = @orders.where(user_id: user) if user.present?
@orders = @orders.where(order_type_id: order_type) if order_type.present?


if search_term.present?
Expand Down Expand Up @@ -216,6 +221,9 @@ def create
logger.info "OrdersController#create: Status id does not exist, set it to New."
obj.update_attribute(:status_id, Status.find_by_label('new')[:id])
end

# TBD: Set managing group

# Set pickup_location to the same library as the sigel
if obj[:pickup_location_id].blank?
logger.info "OrdersController#create: Pickup Location id does not exist, set it to the same as the sigel / label."
Expand Down Expand Up @@ -354,9 +362,16 @@ def handle_order_changes order_id, user_id, old_order, new_order
log_entries << "Beställningstyp ändrades från #{OrderType.find_by_id(old_order[:order_type_id])[:name_sv]} till #{OrderType.find_by_id(new_order[:order_type_id])[:name_sv]}."
end

# Check managing_group difference
if old_order[:managing_group_id].blank? && new_order[:managing_group_id].present?
log_entries << "Handläggningsgrupp ändrades från Ingen till #{ManagingGroup.find_by_id(new_order[:managing_group_id])[:name]}."
elsif old_order[:managing_group_id] != new_order[:managing_group_id]
log_entries << "Handläggningsgrupp ändrades från #{ManagingGroup.find_by_id(old_order[:managing_group_id])[:name]} till #{ManagingGroup.find_by_id(new_order[:managing_group_id])[:name]}."
end

# Check pickup_location difference
if old_order[:pickup_location_id] != new_order[:pickup_location_id]
log_entries << "Remittering ändrades från #{PickupLocation.find_by_id(old_order[:pickup_location_id])[:name_sv]} till #{PickupLocation.find_by_id(new_order[:pickup_location_id])[:name_sv]}."
log_entries << "Avhämtningsbibliotek ändrades från #{PickupLocation.find_by_id(old_order[:pickup_location_id])[:name_sv]} till #{PickupLocation.find_by_id(new_order[:pickup_location_id])[:name_sv]}."
end

# Check lending library difference
Expand Down Expand Up @@ -794,6 +809,7 @@ def metadata_attributes
[:id,
:order_number,
:order_type_id,
:managing_group_id,
:pickup_location_id,
:order_path,
:status_id,
Expand Down
16 changes: 15 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,25 @@ def create
end

def update
render json: {}, status: 501
user_id = params[:id]
user = User.find_by_id(user_id)
# TBD: check user
if user
user.update_attributes(permitted_params)
render json: {user: user}, status: 200
else
render json: {}, status: 404
end
rescue => error
render json: {}, status: 500
end

def delete
render json: {}, status: 501
end

private
def permitted_params
params.require(:user).permit([:managing_group_id, :pickup_location_id])
end
end
11 changes: 11 additions & 0 deletions app/models/managing_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ManagingGroup < ActiveRecord::Base
has_many :orders
has_many :users

validates_presence_of :label
validates_uniqueness_of :label

def as_json(options = {})
super(:except => [:created_at, :updated_at])
end
end
1 change: 1 addition & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Order < ActiveRecord::Base
belongs_to :user
has_many :users, :through => :notes
belongs_to :pickup_location
belongs_to :managing_group
belongs_to :status
belongs_to :order_type
belongs_to :delivery_source
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class User < ActiveRecord::Base
has_many :orders
has_many :notes
has_many :orders, :through => :notes
belongs_to :managing_group
belongs_to :pickup_location
has_many :access_tokens

Expand Down Expand Up @@ -47,6 +48,7 @@ def as_json(options = {})
{
id: id,
xkonto: xkonto,
managing_group_id: managing_group_id,
pickup_location_id: pickup_location_id,
name: name
}
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get 'users' => 'users#index'
get 'users/:id' => 'users#show', :constraints => { :id => /[0-9]+/ }
get 'users/:xkonto' => 'users#show_by_xkonto', :constraints => { :xkonto => /[A-Za-z]+/ }
put 'users/:id' => 'users#update', :constraints => { :id => /[0-9]+/ }

get 'statuses' => 'statuses#index'
get 'statuses/:id' => 'statuses#show', :constraints => { :id => /[0-9]+/ }
Expand All @@ -25,6 +26,10 @@
get 'pickup_locations/:id' => 'pickup_locations#show', :constraints => { :id => /[0-9]+/ }
get 'pickup_locations/:label' => 'pickup_locations#show_by_label', :constraints => { :label => /[A-Za-z\-]+/ }

get 'managing_groups' => 'managing_groups#index'
get 'managing_groups/:id' => 'managing_groups#show', :constraints => { :id => /[0-9]+/ }
get 'managing_groups/:label' => 'managing_groups#show_by_label', :constraints => { :label => /[A-Za-z\-]+/ }

get "form_orders/:id(.format)" => "orders#show", :constraints => { :id => /[0-9]+/ }, :defaults => { :format => 'json' }
get "form_orders/:order_number" => "orders#show_by_order_number", :constraints => { :order_number => /[0-9\-\.]+/ }
post "form_orders/" => "orders#create"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColumnIsActiveToPickupLocations < ActiveRecord::Migration
def change
add_column :pickup_locations, :is_active, :boolean
end
end
13 changes: 13 additions & 0 deletions db/migrate/20190301133326_add_table_managing_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddTableManagingGroups < ActiveRecord::Migration
def change
create_table :managing_groups do |t|
t.string :label
t.string :name
t.string :email
t.string :sublocation
t.boolean :is_active

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20190304101848_add_columns_managing_group_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddColumnsManagingGroupId < ActiveRecord::Migration
def change
add_column :orders, :managing_group_id, :integer
add_column :users, :managing_group_id, :integer
end
end
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20190301121623) do
ActiveRecord::Schema.define(version: 20190304101848) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -42,6 +42,16 @@
t.datetime "updated_at"
end

create_table "managing_groups", force: :cascade do |t|
t.string "label"
t.string "name"
t.string "email"
t.string "sublocation"
t.boolean "is_active"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "notes", force: :cascade do |t|
t.integer "order_id"
t.text "message"
Expand Down Expand Up @@ -122,6 +132,7 @@
t.text "period"
t.text "delivery_box"
t.text "delivery_comments"
t.integer "managing_group_id"
end

create_table "pickup_locations", force: :cascade do |t|
Expand All @@ -133,6 +144,7 @@
t.datetime "updated_at"
t.boolean "is_sigel"
t.string "sigel", limit: 255
t.boolean "is_active"
end

create_table "status_group_members", force: :cascade do |t|
Expand Down Expand Up @@ -167,6 +179,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "pickup_location_id"
t.integer "managing_group_id"
end

add_foreign_key "orders", "pickup_locations", name: "orders_location_id_fkey"
Expand Down

0 comments on commit 6d4ea8d

Please sign in to comment.