cats_core 1.4.7 → 1.4.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/cats/core/access_controller.rb +2 -2
  3. data/app/controllers/cats/core/application_controller.rb +2 -2
  4. data/app/controllers/cats/core/commodities_controller.rb +2 -15
  5. data/app/controllers/cats/core/commodity_categories_controller.rb +8 -39
  6. data/app/controllers/cats/core/dispatch_plan_items_controller.rb +4 -3
  7. data/app/controllers/cats/core/dispatch_plans_controller.rb +3 -3
  8. data/app/controllers/cats/core/dispatch_transactions_controller.rb +3 -2
  9. data/app/controllers/cats/core/dispatches_controller.rb +16 -20
  10. data/app/controllers/cats/core/locations_controller.rb +5 -4
  11. data/app/controllers/cats/core/lost_commodities_controller.rb +3 -2
  12. data/app/controllers/cats/core/monthly_plans_controller.rb +5 -4
  13. data/app/controllers/cats/core/notifications_controller.rb +1 -0
  14. data/app/controllers/cats/core/receipt_transactions_controller.rb +16 -18
  15. data/app/controllers/cats/core/receipts_controller.rb +9 -13
  16. data/app/controllers/cats/core/roles_controller.rb +7 -7
  17. data/app/controllers/cats/core/routes_controller.rb +1 -1
  18. data/app/controllers/cats/core/stacks_controller.rb +3 -2
  19. data/app/controllers/cats/core/transporters_controller.rb +2 -2
  20. data/app/controllers/cats/core/unit_of_measures_controller.rb +1 -37
  21. data/app/controllers/cats/core/users_controller.rb +33 -44
  22. data/app/controllers/concerns/cats/core/common.rb +23 -6
  23. data/app/models/cats/core/commodity.rb +9 -0
  24. data/app/models/cats/core/transport_order.rb +24 -0
  25. data/app/models/cats/core/transport_order_item.rb +25 -0
  26. data/app/models/cats/core/transport_requisition.rb +28 -0
  27. data/db/migrate/20220506082329_create_cats_core_transport_orders.rb +21 -0
  28. data/db/migrate/20220506083042_create_cats_core_transport_order_items.rb +33 -0
  29. data/lib/cats/core/version.rb +1 -1
  30. data/spec/factories/cats/core/transport_order_items.rb +10 -0
  31. data/spec/factories/cats/core/transport_orders.rb +13 -0
  32. metadata +8 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd88ba053bde7688e286bd43301fbd8b7d8c09eb3d76a22c5da0f70c80f8f733
4
- data.tar.gz: 3377e493badbe09bd8d152bb93aad0843b427e72fad83273eeb5177257fc7dd0
3
+ metadata.gz: 5c497f5ed7c7fd9a82dd7a8f2450db08ab9e6ea307cfebf448819c15affe9baa
4
+ data.tar.gz: 0c7514559659448cf6b1b83381b3eda706b37497b7fc2d4e2556430c50cfe21a
5
5
  SHA512:
6
- metadata.gz: 011ad9d0b3d1906c2cd7a3f978d5a2116c37af34bb199f1c52618682cc49064abcf533543cb088026f10d06b658cfbb40b21eccfb4d045605f4d93a55b729f47
7
- data.tar.gz: 605b33ec788fff1f0514e7093f4d03698de791f84bd49744856664f045349570bf767ed767e31eded4ef0206902f3e7d07f3686796d31e219baf4d0c21c2ecab
6
+ metadata.gz: cda6953720eb9492fd45083008ab61c38dc28ed1d7f1995f882335725d9f2beeb07b4e1bf1b012eddb6639bce628c6c3e98c939fe6ce59d18ad450d91bd17ffc
7
+ data.tar.gz: 8785eba470967b8c6b765363c9c88d27270e482a1196decc995182030a6448160590eabe0bb696fabcdceed73e59f17cfffd2c6edb88d9870b4c45fe496652e7
@@ -4,7 +4,7 @@ module Cats
4
4
  skip_before_action :authenticate, only: [:login]
5
5
 
6
6
  def login
7
- user = Cats::Core::User.find_by(email: auth_params[:email])
7
+ user = User.find_by(email: auth_params[:email])
8
8
  if user
9
9
  if user.authenticate(auth_params[:password])
10
10
  roles = user.roles.map(&:name)
@@ -18,7 +18,7 @@ module Cats
18
18
  id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles,
19
19
  details: user.details
20
20
  }
21
- jwt = Cats::Core::TokenAuthService.issue(payload)
21
+ jwt = TokenAuthService.issue(payload)
22
22
  render json: { token: jwt, user: payload }
23
23
  else
24
24
  render json: { error: 'Invalid password.' }, status: 400
@@ -6,7 +6,7 @@ module Cats
6
6
  def current_user
7
7
  return if token.nil?
8
8
 
9
- user = Cats::Core::User.find(auth['id'])
9
+ user = User.find(auth['id'])
10
10
  @current_user ||= user
11
11
  end
12
12
 
@@ -27,7 +27,7 @@ module Cats
27
27
  end
28
28
 
29
29
  def auth
30
- Cats::Core::TokenAuthService.decode(token)
30
+ TokenAuthService.decode(token)
31
31
  end
32
32
  end
33
33
  end
@@ -4,28 +4,15 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def approve
7
- commodity = Cats::Core::Commodity.find(params[:id])
7
+ commodity = Commodity.find(params[:id])
8
8
  result = commodity.approve
9
9
  render json: { success: result, data: serialize(commodity) }
10
10
  rescue StandardError => e
11
11
  render json: { success: false, error: e.message }
12
12
  end
13
13
 
14
- def update
15
- if @obj.status == Cats::Core::Commodity::APPROVED
16
- render json: { success: false, error: 'An approved record cannot be edited.' }
17
- return
18
- end
19
-
20
- if @obj.update(model_params)
21
- render json: { success: true, data: serialize(@obj) }
22
- else
23
- render json: { success: false, error: @obj.errors.full_messages[0] }, status: :unprocessable_entity
24
- end
25
- end
26
-
27
14
  def filter
28
- query = Cats::Core::Commodity.ransack(params[:q])
15
+ query = Commodity.ransack(params[:q])
29
16
  render json: { success: true, data: serialize(query.result) }
30
17
  end
31
18
 
@@ -1,62 +1,31 @@
1
1
  module Cats
2
2
  module Core
3
3
  class CommodityCategoriesController < ApplicationController
4
- before_action :set_commodity_category, only: %i[show update]
4
+ include Common
5
5
 
6
6
  def index
7
- commodity_categories = Cats::Core::CommodityCategory.all.reject(&:has_children?)
8
- data = ActiveModelSerializers::SerializableResource.new(commodity_categories)
9
- render json: { success: true, data: data }
7
+ super do
8
+ CommodityCategory.all.reject(&:has_children?)
9
+ end
10
10
  end
11
11
 
12
12
  def all
13
- data = Cats::Core::CommodityCategory.all
13
+ data = CommodityCategory.all
14
14
  render json: { success: true, data: data }
15
15
  end
16
16
 
17
17
  def root
18
- data = Cats::Core::CommodityCategory.roots
18
+ data = CommodityCategory.roots
19
19
  render json: { success: true, data: serialize(data) }
20
20
  end
21
21
 
22
- def show
23
- data = ActiveModelSerializers::SerializableResource.new(@commodity_category)
24
- render json: { success: true, data: data }
25
- end
26
-
27
22
  def children
28
- parent = Cats::Core::CommodityCategory.find(params[:id])
29
- data = ActiveModelSerializers::SerializableResource.new(parent.children)
30
- render json: { success: true, data: data }
31
- end
32
-
33
- def create
34
- obj = Cats::Core::CommodityCategory.new(model_params)
35
- if obj.save
36
- data = ActiveModelSerializers::SerializableResource.new(obj)
37
- render json: { success: true, data: data }, status: :created
38
- else
39
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
40
- end
41
- end
42
-
43
- def update
44
- if @commodity_category.update(model_params)
45
- data = ActiveModelSerializers::SerializableResource.new(@commodity_category)
46
- render json: { success: true, data: data }
47
- else
48
- render json: {
49
- success: false, error: @commodity_category.errors.full_messages[0]
50
- }, status: :unprocessable_entity
51
- end
23
+ parent = CommodityCategory.find(params[:id])
24
+ render json: { success: true, data: serialize(parent.children) }
52
25
  end
53
26
 
54
27
  private
55
28
 
56
- def set_commodity_category
57
- @commodity_category = Cats::Core::CommodityCategory.find(params[:id])
58
- end
59
-
60
29
  def model_params
61
30
  params.require('payload').permit(:code, :name, :description, :parent_id)
62
31
  end
@@ -4,12 +4,13 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- items = Cats::Core::DispatchPlanItem.where(dispatch_plan_id: params[:id])
8
- render json: { success: true, data: serialize(items) }
7
+ super do
8
+ DispatchPlanItem.where(dispatch_plan_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def filter
12
- query = Cats::Core::DispatchPlanItem.ransack(params[:q])
13
+ query = DispatchPlanItem.ransack(params[:q])
13
14
  render json: { success: true, data: serialize(query.result) }
14
15
  end
15
16
 
@@ -4,12 +4,12 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def plans_for_user
7
- plans = Cats::Core::DispatchPlan.where(prepared_by: current_user)
7
+ plans = DispatchPlan.where(prepared_by: current_user)
8
8
  render json: { success: true, data: serialize(plans) }
9
9
  end
10
10
 
11
11
  def filter
12
- query = Cats::Core::DispatchPlan.ransack(params[:q])
12
+ query = DispatchPlan.ransack(params[:q])
13
13
  render json: { success: true, data: serialize(query.result) }
14
14
  end
15
15
 
@@ -23,7 +23,7 @@ module Cats
23
23
 
24
24
  def approve
25
25
  service = DispatchPlanService.new
26
- plan = Cats::Core::DispatchPlan.find(params[:id])
26
+ plan = DispatchPlan.find(params[:id])
27
27
  plan = service.approve(plan)
28
28
  render json: { success: true, data: serialize(plan) }
29
29
  rescue StandardError => e
@@ -4,8 +4,9 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- transactions = Cats::Core::DispatchTransaction.where(dispatch_id: params[:id])
8
- render json: { success: true, data: serialize(transactions) }
7
+ super do
8
+ DispatchTransaction.where(dispatch_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  # This end-point is used to create upstream dispatch transactions.
@@ -4,41 +4,41 @@ module Cats
4
4
  include Common
5
5
 
6
6
  before_action :set_service, only: %i[create_receipt_authorization approve start search confirm]
7
- before_action :set_dispatch, only: %i[approve start confirm commodity]
8
7
 
9
8
  def index
10
- dispatches = Cats::Core::Dispatch.where(dispatch_plan_item_id: params[:id])
11
- render json: { success: true, data: serialize(dispatches) }
9
+ super do
10
+ Dispatch.where(dispatch_plan_item_id: params[:id])
11
+ end
12
12
  end
13
13
 
14
14
  def create
15
- dispatch = Cats::Core::Dispatch.new(model_params)
16
- dispatch.dispatch_status = Cats::Core::Dispatch::DRAFT
17
- dispatch.prepared_by = current_user
18
- if dispatch.save
19
- render json: { success: true, data: serialize(dispatch) }, status: :created
20
- else
21
- render json: { success: false, error: dispatch.errors.full_messages[0] }, status: :unprocessable_entity
15
+ super do
16
+ dispatch = Dispatch.new(model_params)
17
+ dispatch.dispatch_status = Dispatch::DRAFT
18
+ dispatch.prepared_by = current_user
19
+ dispatch
22
20
  end
23
21
  end
24
22
 
25
23
  def approve
26
- @dispatch.approve
27
- render json: { success: true, data: serialize(@dispatch) }
24
+ dispatch = set_object
25
+ dispatch.approve
26
+ render json: { success: true, data: serialize(dispatch) }
28
27
  rescue StandardError => e
29
28
  render json: { success: false, error: e.message }
30
29
  end
31
30
 
32
31
  def start
33
- dispatch = @service.start(@dispatch)
32
+ dispatch = @service.start(set_object)
34
33
  render json: { success: true, data: serialize(dispatch) }
35
34
  rescue StandardError => e
36
35
  render json: { success: false, error: e.message }
37
36
  end
38
37
 
39
38
  def confirm
40
- @dispatch.confirm
41
- render json: { success: true, data: serialize(@dispatch) }
39
+ dispatch = set_object
40
+ dispatch.confirm
41
+ render json: { success: true, data: serialize(dispatch) }
42
42
  rescue StandardError => e
43
43
  render json: { success: false, error: e.message }
44
44
  end
@@ -49,7 +49,7 @@ module Cats
49
49
  end
50
50
 
51
51
  def commodity
52
- data = @dispatch.dispatch_plan_item.commodity
52
+ data = set_object.dispatch_plan_item.commodity
53
53
  render json: { success: true, data: serialize(data) }
54
54
  end
55
55
 
@@ -59,10 +59,6 @@ module Cats
59
59
  @service = DispatchService.new
60
60
  end
61
61
 
62
- def set_dispatch
63
- @dispatch = Cats::Core::Dispatch.find(params[:id])
64
- end
65
-
66
62
  def model_params
67
63
  params.require(:payload).permit(:reference_no, :dispatch_plan_item_id, :transporter_id, :plate_no,
68
64
  :driver_name, :driver_phone, :quantity, :remark)
@@ -4,17 +4,18 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- locations = Cats::Core::Location.where(location_type: params[:location_type])
8
- render json: { success: true, data: serialize(locations) }
7
+ super do
8
+ Location.where(location_type: params[:location_type])
9
+ end
9
10
  end
10
11
 
11
12
  def children
12
- parent = Cats::Core::Location.find(params[:id])
13
+ parent = Location.find(params[:id])
13
14
  render json: { success: true, data: serialize(parent.children) }
14
15
  end
15
16
 
16
17
  def filter
17
- query = Cats::Core::Location.ransack(params[:q])
18
+ query = Location.ransack(params[:q])
18
19
  render json: { success: true, data: serialize(query.result) }
19
20
  end
20
21
 
@@ -4,8 +4,9 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- data = LostCommodity.where(dispatch_id: params[:id])
8
- render json: { success: true, data: serialize(data) }
7
+ super do
8
+ LostCommodity.where(dispatch_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def model_params
@@ -6,17 +6,18 @@ module Cats
6
6
  before_action :set_service, only: %i[generate remove_items generate_monthly_needs]
7
7
 
8
8
  def index
9
- plans = Cats::Core::MonthlyPlan.where(plan: params[:id])
10
- render json: { success: true, data: serialize(plans) }
9
+ super do
10
+ MonthlyPlan.where(plan: params[:id])
11
+ end
11
12
  end
12
13
 
13
14
  def filter
14
- query = Cats::Core::MonthlyPlan.ransack(params[:q])
15
+ query = MonthlyPlan.ransack(params[:q])
15
16
  render json: { success: true, data: serialize(query.result) }
16
17
  end
17
18
 
18
19
  def approve
19
- plan = Cats::Core::MonthlyPlan.find(params[:id])
20
+ plan = MonthlyPlan.find(params[:id])
20
21
  plan.approve
21
22
  render json: { success: true, data: serialize(plan) }
22
23
  rescue StandardError => e
@@ -2,6 +2,7 @@ module Cats
2
2
  module Core
3
3
  class NotificationsController < ApplicationController
4
4
  before_action :set_notification, only: %i[mark_as_read mark_as_unread]
5
+
5
6
  def index
6
7
  data = Notification.messages(current_user.notifications.newest_first)
7
8
  render json: { success: true, data: data }
@@ -4,28 +4,26 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- transactions = Cats::Core::ReceiptTransaction.where(receipt_id: params[:id])
8
- render json: { success: true, data: serialize(transactions) }
7
+ super do
8
+ ReceiptTransaction.where(receipt_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def create
12
- p = model_params
13
-
14
- # Look for a transaction with the same destination as incoming
15
- transaction = Cats::Core::ReceiptTransaction.find_by(
16
- receipt_id: p[:receipt_id],
17
- destination_id: p[:destination_id]
18
- )
19
- if transaction
20
- transaction.quantity += p[:quantity]
21
- else
22
- transaction = Cats::Core::ReceiptTransaction.new(p)
23
- end
13
+ super do
14
+ p = model_params
24
15
 
25
- if transaction.save
26
- render json: { success: true, data: serialize(transaction) }, status: :created
27
- else
28
- render json: { success: false, error: transaction.errors.full_messages[0] }, status: :unprocessable_entity
16
+ # Look for a transaction with the same destination as incoming
17
+ transaction = ReceiptTransaction.find_by(
18
+ receipt_id: p[:receipt_id],
19
+ destination_id: p[:destination_id]
20
+ )
21
+ if transaction
22
+ transaction.quantity += p[:quantity]
23
+ else
24
+ transaction = ReceiptTransaction.new(p)
25
+ end
26
+ transaction
29
27
  end
30
28
  end
31
29
 
@@ -4,25 +4,21 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- receipts = Cats::Core::Receipt.where(dispatch_id: params[:id])
8
- render json: { success: true, data: serialize(receipts) }
7
+ super do
8
+ Receipt.where(dispatch_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def create
12
- service = ReceiptService.new
13
- receipt = service.init(model_params)
14
-
15
- if receipt.save
16
- render json: { success: true, data: serialize(receipt) }
17
- else
18
- render json: { success: false, error: receipt.errors.full_messages[0] }, status: :unprocessable_entity
13
+ super do
14
+ service = ReceiptService.new
15
+ receipt = service.init(model_params)
16
+ receipt
19
17
  end
20
- rescue StandardError => e
21
- render json: { success: false, error: e.message }
22
18
  end
23
19
 
24
20
  def start_stacking
25
- receipt = Cats::Core::Receipt.find(params[:id])
21
+ receipt = Receipt.find(params[:id])
26
22
  service = ReceiptService.new
27
23
  result = service.start_stacking(receipt)
28
24
  render json: { success: true, data: serialize(result) }
@@ -31,7 +27,7 @@ module Cats
31
27
  end
32
28
 
33
29
  def finish_stacking
34
- receipt = Cats::Core::Receipt.find(params[:id])
30
+ receipt = Receipt.find(params[:id])
35
31
  service = ReceiptService.new
36
32
  result = service.finish_stacking(receipt)
37
33
  render json: { success: true, data: serialize(result) }
@@ -6,21 +6,21 @@ module Cats
6
6
  before_action :set_role, only: [:users]
7
7
 
8
8
  def index
9
- roles = Cats::Core::Role.joins(:application_module)
10
- .where(cats_core_application_modules: { prefix: params[:prefix] })
11
- render json: { success: true, data: serialize(roles) }
9
+ super do
10
+ Role.joins(:application_module)
11
+ .where(cats_core_application_modules: { prefix: params[:prefix] })
12
+ end
12
13
  end
13
14
 
14
15
  def unassigned_roles
15
- user = Cats::Core::User.find(params[:id])
16
- roles = Cats::Core::Role.where(application_module_id: user.application_module_id)
16
+ user = User.find(params[:id])
17
+ roles = Role.where(application_module_id: user.application_module_id)
17
18
  unassigned = roles - user.roles
18
19
  render json: { success: true, data: serialize(unassigned) }
19
20
  end
20
21
 
21
22
  def users
22
- data = ActiveModelSerializers::SerializableResource.new(@role.users)
23
- render json: { success: true, data: data }
23
+ render json: { success: true, data: serialize(@role.users) }
24
24
  end
25
25
 
26
26
  private
@@ -4,7 +4,7 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def filter
7
- query = Cats::Core::Route.ransack(params[:q])
7
+ query = Route.ransack(params[:q])
8
8
  render json: { success: true, data: serialize(query.result) }
9
9
  end
10
10
 
@@ -4,8 +4,9 @@ module Cats
4
4
  include Common
5
5
 
6
6
  def index
7
- stacks = Stack.where(store_id: params[:id])
8
- render json: { success: true, data: serialize(stacks) }
7
+ super do
8
+ Stack.where(store_id: params[:id])
9
+ end
9
10
  end
10
11
 
11
12
  def filter
@@ -1,10 +1,10 @@
1
1
  module Cats
2
2
  module Core
3
3
  class TransportersController < ApplicationController
4
- include Cats::Core::Common
4
+ include Common
5
5
 
6
6
  def filter
7
- query = Cats::Core::Transporter.ransack(params[:q])
7
+ query = Transporter.ransack(params[:q])
8
8
  render json: { success: true, data: serialize(query.result) }
9
9
  end
10
10
 
@@ -1,46 +1,10 @@
1
1
  module Cats
2
2
  module Core
3
3
  class UnitOfMeasuresController < ApplicationController
4
- before_action :set_unit_of_measure, only: %i[show update]
5
-
6
- def index
7
- data = ActiveModelSerializers::SerializableResource.new(Cats::Core::UnitOfMeasure.all)
8
- render json: { success: true, data: data }
9
- end
10
-
11
- def show
12
- data = ActiveModelSerializers::SerializableResource.new(@unit_of_measure)
13
- render json: { success: true, data: data }
14
- end
15
-
16
- def create
17
- obj = Cats::Core::UnitOfMeasure.new(model_params)
18
- if obj.save
19
- data = ActiveModelSerializers::SerializableResource.new(obj)
20
- render json: { success: true, data: data }, status: :created
21
- else
22
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
23
- end
24
- end
25
-
26
- def update
27
- if @unit_of_measure.update(model_params)
28
- data = ActiveModelSerializers::SerializableResource.new(@unit_of_measure)
29
- render json: { success: true, data: data }
30
- else
31
- render json: {
32
- success: false,
33
- error: @unit_of_measure.errors.full_messages[0]
34
- }, status: :unprocessable_entity
35
- end
36
- end
4
+ include Common
37
5
 
38
6
  private
39
7
 
40
- def set_unit_of_measure
41
- @unit_of_measure = Cats::Core::UnitOfMeasure.find(params[:id])
42
- end
43
-
44
8
  def model_params
45
9
  params.require(:payload).permit(:abbreviation, :name, :unit_type)
46
10
  end
@@ -3,95 +3,84 @@ module Cats
3
3
  class UsersController < ApplicationController
4
4
  include Common
5
5
 
6
- before_action :set_user, only: %i[update roles assign_role revoke_role stores warehouse hub]
7
-
8
6
  def index
9
- users = Cats::Core::User.joins(:application_module)
10
- .where(cats_core_application_modules: { prefix: params[:prefix] })
11
- render json: { success: true, data: serialize(users) }
7
+ super do
8
+ User.joins(:application_module).where(cats_core_application_modules: { prefix: params[:prefix] })
9
+ end
12
10
  end
13
11
 
14
12
  def create
15
- prefix = model_params[:application_prefix]
16
- application_module = Cats::Core::ApplicationModule.find_by(prefix: prefix)
13
+ super do
14
+ prefix = model_params[:application_prefix]
15
+ application_module = ApplicationModule.find_by(prefix: prefix)
17
16
 
18
- obj = Cats::Core::User.new(model_params.except(:application_prefix))
19
- obj.application_module = application_module
20
- if obj.save
21
- render json: { success: true, data: serialize(obj) }, status: :created
22
- else
23
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
17
+ obj = User.new(model_params.except(:application_prefix))
18
+ obj.application_module = application_module
19
+ obj
24
20
  end
25
21
  end
26
22
 
27
23
  def update
24
+ user = set_object
28
25
  if model_params[:application_prefix]
29
26
  prefix = model_params[:application_prefix]
30
- application_module = Cats::Core::ApplicationModule.find_by(prefix: prefix)
31
- @user.assign_attributes(model_params.except(:application_prefix))
32
- @user.application_module = application_module
27
+ application_module = ApplicationModule.find_by(prefix: prefix)
28
+ user.assign_attributes(model_params.except(:application_prefix))
29
+ user.application_module = application_module
33
30
  else
34
- @user.assign_attributes(model_params)
31
+ user.assign_attributes(model_params)
35
32
  end
36
33
 
37
- if @user.save
38
- render json: { success: true, data: serialize(@user) }
34
+ if user.save
35
+ render json: { success: true, data: serialize(user) }
39
36
  else
40
- render json: { success: false, error: @user.errors.full_messages[0] }, status: :unprocessable_entity
37
+ render json: { success: false, error: user.errors.full_messages[0] }, status: :unprocessable_entity
41
38
  end
42
39
  end
43
40
 
44
41
  def roles
45
- data = ActiveModelSerializers::SerializableResource.new(@user.roles)
46
- render json: { success: true, data: data }
42
+ render json: { success: true, data: serialize(set_object.roles) }
47
43
  end
48
44
 
49
45
  def assign_role
50
- role = Cats::Core::Role.find(params[:role_id])
51
- @user.roles << role
46
+ role = Role.find(params[:role_id])
47
+ user = set_object
48
+ user.roles << role
52
49
 
53
50
  case role.name
54
51
  when 'warehouse_manager'
55
- @user.add_detail(:warehouse, params[:warehouse_id])
52
+ user.add_detail(:warehouse, params[:warehouse_id])
56
53
  when 'hub_manager'
57
- @user.add_detail(:hub, params[:hub_id])
54
+ user.add_detail(:hub, params[:hub_id])
58
55
  when 'regional_manager'
59
- @user.add_detail(:region, params[:region_id])
56
+ user.add_detail(:region, params[:region_id])
60
57
  end
61
58
 
62
- data = ActiveModelSerializers::SerializableResource.new(role)
63
- render json: { success: true, data: data }
59
+ render json: { success: true, data: serialize(role) }
64
60
  end
65
61
 
66
62
  def revoke_role
67
- role = Cats::Core::Role.find(params[:role_id])
68
- @user.roles.delete(role)
69
- @user.remove_detail(role.name) if %w[warehouse_manager hub_manager regional_manager].include?(role.name)
70
- data = ActiveModelSerializers::SerializableResource.new(role)
71
- render json: { success: true, data: data }
63
+ user = set_object
64
+ role = Role.find(params[:role_id])
65
+ user.roles.delete(role)
66
+ user.remove_detail(role.name) if %w[warehouse_manager hub_manager regional_manager].include?(role.name)
67
+ render json: { success: true, data: serialize(role) }
72
68
  end
73
69
 
74
70
  def stores
75
- data = ActiveModelSerializers::SerializableResource.new(@user.stores)
76
- render json: { success: true, data: data }
71
+ render json: { success: true, data: serialize(set_object.stores) }
77
72
  end
78
73
 
79
74
  def warehouse
80
- data = ActiveModelSerializers::SerializableResource.new(@user.warehouse)
81
- render json: { success: true, data: data }
75
+ render json: { success: true, data: serialize(set_object.warehouse) }
82
76
  end
83
77
 
84
78
  def hub
85
- data = ActiveModelSerializers::SerializableResource.new(@user.hub)
86
- render json: { success: true, data: data }
79
+ render json: { success: true, data: serialize(set_object.hub) }
87
80
  end
88
81
 
89
82
  private
90
83
 
91
- def set_user
92
- @user = Cats::Core::User.find(params[:id])
93
- end
94
-
95
84
  def model_params
96
85
  params.require(:payload).permit(
97
86
  :first_name, :last_name, :email, :phone_number, :active, :password, :password_confirmation, :details,
@@ -9,8 +9,12 @@ module Cats
9
9
  end
10
10
 
11
11
  def index
12
- data = serialize(@clazz.all)
13
- render json: { success: true, data: data }
12
+ data = if block_given?
13
+ yield
14
+ else
15
+ @clazz.all
16
+ end
17
+ render json: { success: true, data: serialize(data) }
14
18
  end
15
19
 
16
20
  def show
@@ -18,20 +22,33 @@ module Cats
18
22
  end
19
23
 
20
24
  def create
21
- obj = @clazz.new(model_params)
25
+ obj = if block_given?
26
+ yield
27
+ else
28
+ @clazz.new(model_params)
29
+ end
22
30
  if obj.save
23
31
  render json: { success: true, data: serialize(obj) }, status: :created
24
32
  else
25
33
  render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
26
34
  end
35
+ rescue StandardError => e
36
+ render json: { success: false, error: e.message }
27
37
  end
28
38
 
29
39
  def update
30
- if @obj.update(model_params)
31
- render json: { success: true, data: serialize(@obj) }
40
+ obj = if block_given?
41
+ yield
42
+ else
43
+ obj = @obj
44
+ end
45
+ if obj.update(model_params)
46
+ render json: { success: true, data: serialize(obj) }
32
47
  else
33
- render json: { success: false, error: @obj.errors.full_messages[0] }, status: :unprocessable_entity
48
+ render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
34
49
  end
50
+ rescue StandardError => e
51
+ render json: { success: false, error: e.message }
35
52
  end
36
53
 
37
54
  private
@@ -37,6 +37,7 @@ module Cats
37
37
  validates :volume_per_metric_ton, numericality: { greater_than: 0, allow_nil: true }
38
38
  validates :arrival_status, presence: true, inclusion: { in: ARRIVAL_STATUSES }
39
39
  validates :status, presence: true, inclusion: { in: STATUSES }
40
+ validate :validate_updateability, on: :update
40
41
 
41
42
  delegate(:abbreviation, to: :unit_of_measure, prefix: 'unit')
42
43
  delegate(:reference_no, to: :source, prefix: true)
@@ -45,6 +46,14 @@ module Cats
45
46
  source.commodity_category.name
46
47
  end
47
48
 
49
+ def validate_updateability
50
+ return unless status
51
+
52
+ return if status_changed?
53
+
54
+ errors.add(:base, 'Commodity cannot be updated because it is in approved state.') if status == APPROVED
55
+ end
56
+
48
57
  def approve
49
58
  raise(StandardError, 'Commodity already approved.') if status == APPROVED
50
59
 
@@ -0,0 +1,24 @@
1
+ module Cats
2
+ module Core
3
+ class TransportOrder < ApplicationRecord
4
+ DRAFT = 'Draft'.freeze
5
+ APPROVED = 'Approved'.freeze
6
+ STATUSES = [DRAFT, APPROVED].freeze
7
+
8
+ belongs_to :transport_requisition
9
+ belongs_to :prepared_by, class_name: 'Cats::Core::User'
10
+ belongs_to :approved_by, class_name: 'Cats::Core::User'
11
+
12
+ validates :status, presence: true, inclusion: { in: STATUSES }
13
+ validate :validate_against_requisition
14
+
15
+ def validate_against_requisition
16
+ return unless transport_requisition
17
+
18
+ return if transport_requisition.approved?
19
+
20
+ errors.add(:transport_requisition, 'is not approved.')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Cats
2
+ module Core
3
+ class TransportOrderItem < ApplicationRecord
4
+ belongs_to :transport_order
5
+ belongs_to :transporter
6
+ belongs_to :transport_requisition_item
7
+ belongs_to :transport_contract, optional: true
8
+
9
+ validates :order_date, :valid_for, presence: true
10
+ validates :valid_for, numericality: { greater_than: 0 }
11
+ validates :transport_requisition_item_id, uniqueness: true
12
+ validate :validate_requisition
13
+
14
+ def validate_requisition
15
+ return unless transport_requisition_item
16
+
17
+ return unless transport_order
18
+
19
+ return if transport_order.transport_requisition_id == transport_requisition_item.transport_requisition_id
20
+
21
+ errors.add(:transport_requisition_item, 'does not belong to the requisition of the order.')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -14,6 +14,7 @@ module Cats
14
14
 
15
15
  validates :reference_no, presence: true, uniqueness: true
16
16
  validates :status, presence: true, inclusion: { in: STATUSES }
17
+ validate :validate_status
17
18
 
18
19
  delegate(:full_name, to: :requested_by, prefix: 'requestor')
19
20
  delegate(:full_name, to: :approved_by, prefix: 'approver', allow_nil: true)
@@ -24,6 +25,33 @@ module Cats
24
25
  quantities = transport_requisition_items.map { |item| UnitConversion.convert(item.unit, unit, item.quantity) }
25
26
  quantities.sum
26
27
  end
28
+
29
+ def approve
30
+ raise(StandardError, 'Transport requisition is already approved.') if status == APPROVED
31
+
32
+ begin
33
+ self.status = APPROVED
34
+ save!
35
+ self
36
+ rescue ActiveRecord::RecordInvalid => e
37
+ error = e.record.errors.full_messages_for(:status)[0]
38
+ raise(StandardError, error)
39
+ end
40
+ end
41
+
42
+ def approved?
43
+ status == APPROVED
44
+ end
45
+
46
+ def validate_status
47
+ return unless status
48
+
49
+ return if status == DRAFT
50
+
51
+ return if transport_requisition_items.count.positive?
52
+
53
+ errors.add(:status, 'cannot be set to "APPROVED" because the requisition has no items.') if status == APPROVED
54
+ end
27
55
  end
28
56
  end
29
57
  end
@@ -0,0 +1,21 @@
1
+ class CreateCatsCoreTransportOrders < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :cats_core_transport_orders do |t|
4
+ t.references :transport_requisition,
5
+ null: false,
6
+ index: { name: 'tr_on_to_indx' },
7
+ foreign_key: { to_table: :cats_core_transport_requisitions }
8
+ t.references :prepared_by,
9
+ null: false,
10
+ index: { name: 'pb_on_to_indx' },
11
+ foreign_key: { to_table: :cats_core_users }
12
+ t.references :approved_by,
13
+ null: false,
14
+ index: { name: 'ab_on_to_indx' },
15
+ foreign_key: { to_table: :cats_core_users }
16
+ t.string :status, null: false, default: 'Draft'
17
+
18
+ t.timestamps
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ class CreateCatsCoreTransportOrderItems < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :cats_core_transport_order_items do |t|
4
+ t.references :transport_order,
5
+ null: false,
6
+ index: { name: 'to_on_toi_indx' },
7
+ foreign_key: { to_table: :cats_core_transport_orders }
8
+ t.references :transporter,
9
+ null: false,
10
+ index: { name: 'transporter_on_toi_indx' },
11
+ foreign_key: { to_table: :cats_core_transporters }
12
+ t.references :transport_contract,
13
+ null: true,
14
+ index: { name: 'tc_on_toi_indx' },
15
+ foreign_key: { to_table: :cats_core_transport_contracts }
16
+ t.references :transport_requisition_item,
17
+ null: false,
18
+ index: { name: 'tri_on_toi_indx' },
19
+ foreign_key: { to_table: :cats_core_transport_requisition_items }
20
+ t.date :order_date, null: false, default: Date.today
21
+ t.integer :valid_for, null: false, default: 10
22
+
23
+ t.timestamps
24
+ end
25
+
26
+ add_index(
27
+ :cats_core_transport_order_items,
28
+ :transport_requisition_item_id,
29
+ unique: true,
30
+ name: 'tri_on_toi_uniq_indx'
31
+ )
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.4.7'.freeze
3
+ VERSION = '1.4.10'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,10 @@
1
+ FactoryBot.define do
2
+ factory :transport_order_item, class: 'Cats::Core::TransportOrderItem' do
3
+ transport_order
4
+ transporter
5
+ transport_contract
6
+ transport_requisition_item { transport_order.transport_requisition.transport_requisition_items.first }
7
+ order_date { Date.today }
8
+ valid_for { 10 }
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+ factory :transport_order, class: 'Cats::Core::TransportOrder' do
3
+ transport_requisition do
4
+ requisition = create(:transport_requisition)
5
+ create(:transport_requisition_item, transport_requisition: requisition)
6
+ requisition.approve
7
+ requisition
8
+ end
9
+ prepared_by factory: :user
10
+ approved_by factory: :user
11
+ status { Cats::Core::TransportOrder::DRAFT }
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.7
4
+ version: 1.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-18 00:00:00.000000000 Z
11
+ date: 2022-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -307,6 +307,8 @@ files:
307
307
  - app/models/cats/core/transport_bid_item.rb
308
308
  - app/models/cats/core/transport_contract.rb
309
309
  - app/models/cats/core/transport_offer.rb
310
+ - app/models/cats/core/transport_order.rb
311
+ - app/models/cats/core/transport_order_item.rb
310
312
  - app/models/cats/core/transport_plan.rb
311
313
  - app/models/cats/core/transport_plan_item.rb
312
314
  - app/models/cats/core/transport_requisition.rb
@@ -410,6 +412,8 @@ files:
410
412
  - db/migrate/20220417105839_create_cats_core_transport_requisitions.rb
411
413
  - db/migrate/20220417123835_create_cats_core_transport_requisition_items.rb
412
414
  - db/migrate/20220417151821_create_cats_core_transport_requisition_details.rb
415
+ - db/migrate/20220506082329_create_cats_core_transport_orders.rb
416
+ - db/migrate/20220506083042_create_cats_core_transport_order_items.rb
413
417
  - lib/cats/core.rb
414
418
  - lib/cats/core/engine.rb
415
419
  - lib/cats/core/version.rb
@@ -464,6 +468,8 @@ files:
464
468
  - spec/factories/cats/core/transport_bids.rb
465
469
  - spec/factories/cats/core/transport_contracts.rb
466
470
  - spec/factories/cats/core/transport_offers.rb
471
+ - spec/factories/cats/core/transport_order_items.rb
472
+ - spec/factories/cats/core/transport_orders.rb
467
473
  - spec/factories/cats/core/transport_plan_items.rb
468
474
  - spec/factories/cats/core/transport_plans.rb
469
475
  - spec/factories/cats/core/transport_requisition_details.rb