cats_core 1.3.11 → 1.3.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/cats/core/access_controller.rb +2 -1
  3. data/app/controllers/cats/core/dispatch_plan_items_controller.rb +2 -1
  4. data/app/controllers/cats/core/dispatch_plans_controller.rb +9 -6
  5. data/app/models/cats/core/dispatch_plan.rb +6 -0
  6. data/app/models/cats/core/dispatch_plan_item.rb +1 -0
  7. data/app/models/cats/core/monthly_plan.rb +18 -2
  8. data/app/models/cats/core/monthly_plan_item_detail.rb +1 -1
  9. data/app/models/cats/core/monthly_ration.rb +4 -0
  10. data/app/models/cats/core/plan.rb +3 -1
  11. data/app/models/cats/core/rhn_request.rb +7 -1
  12. data/app/serializers/cats/core/dispatch_plan_item_serializer.rb +1 -1
  13. data/app/serializers/cats/core/dispatch_plan_serializer.rb +2 -1
  14. data/app/services/cats/core/dispatch_plan_service.rb +25 -5
  15. data/db/migrate/20210718043328_create_cats_core_dispatch_plans.rb +4 -0
  16. data/db/migrate/20210718043401_create_cats_core_dispatch_plan_items.rb +1 -0
  17. data/db/migrate/20220107121752_create_cats_core_monthly_plans.rb +5 -3
  18. data/db/migrate/{20220107224630_create_cats_core_monthly_rations.rb → 20220107124630_create_cats_core_monthly_rations.rb} +0 -0
  19. data/db/migrate/20220107132433_create_cats_core_monthly_plan_item_details.rb +3 -3
  20. data/lib/cats/core/version.rb +1 -1
  21. data/spec/factories/cats/core/dispatch_plan_items.rb +1 -0
  22. data/spec/factories/cats/core/dispatch_plans.rb +1 -0
  23. data/spec/factories/cats/core/monthly_plan_item_details.rb +1 -1
  24. data/spec/factories/cats/core/monthly_plans.rb +3 -1
  25. metadata +3 -6
  26. data/app/models/cats/core/regional_request.rb +0 -36
  27. data/db/migrate/20220105084347_create_cats_core_regional_requests.rb +0 -20
  28. data/spec/factories/cats/core/regional_requests.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07bfd5b8cfedc80be5973e4c41ab660e71e1cc55dafea9dc3f0ad9669ce54382
4
- data.tar.gz: 44265ec189d02cbb4e8553c3f2a837f78275a03a3a82d5c33e1a43f5695592e8
3
+ metadata.gz: 2ad7f2aaa0c106926140445109463f49c20ef3c4d7336de19a4f99abe4152b48
4
+ data.tar.gz: ffb46327fe5ab23994d9ada4ab2d8cbdcfba90f7f44436a8f95b87bb8e9fc21d
5
5
  SHA512:
6
- metadata.gz: c06175c94d4b3a39e011355104a963553d271a296e12f6e72af2e541d94aa2cf53a7898b953416f014012d6dddfa5738ba07d52e268193d7aab4103a692746b3
7
- data.tar.gz: a21021e5054816d3ede7ac9ecadb7746bd2f9f62a43f977d90452350989003398fcb807aa59d15ff4a87975a7dd88debbdf7ae3ee1fc992ffbb6604aab77a025
6
+ metadata.gz: e7ec684441796002b858da2a27ae38eecc14dea715682ea44f361434af24324236ddcd1ecd178dca5b9f44e99442341b6a667e43a0de9afafe12f37b21619628
7
+ data.tar.gz: 8887e74996a6a483da128576e049b630d20c0deb097d6e309a96be5421e11794b4e2033b8a25ee26f08fcb8de40e0a9e592336f4b97b79a1e71461364c0215bd
@@ -15,7 +15,8 @@ module Cats
15
15
  end
16
16
 
17
17
  payload = {
18
- id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles
18
+ id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles,
19
+ details: user.details
19
20
  }
20
21
  jwt = Cats::Core::TokenAuthService.issue(payload)
21
22
  render json: { token: jwt, user: payload }
@@ -11,7 +11,8 @@ module Cats
11
11
  private
12
12
 
13
13
  def model_params
14
- params.require(:payload).permit(:dispatch_plan_id, :source_id, :destination_id, :quantity, :commodity_id)
14
+ params.require(:payload).permit(:dispatch_plan_id, :source_id, :destination_id, :quantity, :commodity_id,
15
+ :commodity_status)
15
16
  end
16
17
  end
17
18
  end
@@ -14,13 +14,16 @@ module Cats
14
14
  end
15
15
 
16
16
  def create
17
- obj = Cats::Core::DispatchPlan.new(model_params)
18
- obj.prepared_by = current_user
19
- if obj.save
20
- render json: { success: true, data: serialize(obj) }, status: :created
17
+ service = DispatchPlanService.new
18
+
19
+ data = service.create(model_params, current_user)
20
+ if data[0]
21
+ render json: { success: true, data: serialize(data[1]) }, status: :created
21
22
  else
22
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
23
+ render json: { success: false, error: data[1].errors.full_messages[0] }, status: :unprocessable_entity
23
24
  end
25
+ rescue StandardError => e
26
+ render json: { success: false, error: e.message }
24
27
  end
25
28
 
26
29
  def approve
@@ -34,7 +37,7 @@ module Cats
34
37
  private
35
38
 
36
39
  def model_params
37
- params.require(:payload).permit(:reference_no, :request_id)
40
+ params.require(:payload).permit(:reference_no, :request_id, :commodity_id)
38
41
  end
39
42
  end
40
43
  end
@@ -8,12 +8,18 @@ module Cats
8
8
  belongs_to :request, polymorphic: true, optional: true
9
9
  belongs_to :prepared_by, class_name: 'Cats::Core::User'
10
10
  belongs_to :approved_by, class_name: 'Cats::Core::User', optional: true
11
+ belongs_to :commodity
11
12
 
12
13
  has_many :dispatch_plan_items
13
14
 
14
15
  validates :reference_no, presence: true, uniqueness: true
15
16
  validates :status, inclusion: { in: STATUSES }
16
17
 
18
+ delegate(:batch_no, to: :commodity, prefix: true)
19
+ delegate(:name, to: :commodity, prefix: true)
20
+ delegate(:quantity, to: :commodity, prefix: true)
21
+ delegate(:reference_no, to: :request, prefix: true, allow_nil: true)
22
+
17
23
  def approve
18
24
  raise(StandardError, 'Dispatch plan already approved.') if status == APPROVED
19
25
 
@@ -7,6 +7,7 @@ module Cats
7
7
  belongs_to :dispatch_plan
8
8
  has_many :dispatches
9
9
 
10
+ validates :commodity_status, presence: true, inclusion: { in: Cats::Core::Commodity::COMMODITY_STATUSES }
10
11
  validates :quantity, presence: true, numericality: { greater_than: 0 }
11
12
 
12
13
  delegate(:reference_no, to: :dispatch_plan, prefix: :plan, allow_nil: true)
@@ -2,15 +2,31 @@ module Cats
2
2
  module Core
3
3
  class MonthlyPlan < ApplicationRecord
4
4
  belongs_to :plan
5
- belongs_to :regional_request
5
+ belongs_to :region, class_name: 'Cats::Core::Location'
6
6
 
7
7
  has_many :monthly_plan_items
8
+ has_many :monthly_plan_item_details, through: :monthly_plan_items
8
9
  has_many :monthly_rations
9
10
 
10
11
  validates :reference_no, presence: true, uniqueness: true
11
12
  validates :status, presence: true, inclusion: { in: Cats::Core::Plan::STATUSES }
13
+ validates :no_of_days, :month, presence: true, numericality: { greater_than: 0 }
14
+ validate :validate_month, :validate_region
12
15
 
13
- delegate(:reference_no, to: :regional_request, prefix: 'request')
16
+ delegate(:reference_no, to: :plan, prefix: true)
17
+ delegate(:name, to: :region, prefix: true)
18
+
19
+ def validate_month
20
+ return unless month
21
+
22
+ errors.add(:month, 'cannot be greater than 12.') if month > 12
23
+ end
24
+
25
+ def validate_region
26
+ return unless region
27
+
28
+ errors.add(:region, 'is not valid.') unless region.location_type == Cats::Core::Location::REGION
29
+ end
14
30
  end
15
31
  end
16
32
  end
@@ -2,7 +2,7 @@ module Cats
2
2
  module Core
3
3
  class MonthlyPlanItemDetail < ApplicationRecord
4
4
  belongs_to :monthly_plan_item
5
- belongs_to :ration_item
5
+ belongs_to :monthly_ration
6
6
 
7
7
  validates :amount, presence: true, numericality: { greater_than: 0 }
8
8
  end
@@ -6,6 +6,10 @@ module Cats
6
6
  belongs_to :monthly_plan
7
7
 
8
8
  validates :amount, :no_of_days, presence: true, numericality: { greater_than: 0 }
9
+
10
+ delegate(:name, to: :commodity_category, prefix: true)
11
+ delegate(:abbreviation, to: :unit_of_measure, prefix: true)
12
+ delegate(:reference_no, to: :monthly_plan, prefix: true)
9
13
  end
10
14
  end
11
15
  end
@@ -9,11 +9,13 @@ module Cats
9
9
 
10
10
  BELG = 'Belg'.freeze
11
11
  MEHER = 'Meher'.freeze
12
- SEASONS = [BELG, MEHER].freeze
12
+ ANNUAL = 'Annual'.freeze
13
+ SEASONS = [BELG, MEHER, ANNUAL].freeze
13
14
 
14
15
  belongs_to :program
15
16
  belongs_to :ration, optional: true
16
17
  has_many :plan_items
18
+ has_many :plan_item_details, through: :plan_items
17
19
 
18
20
  validates :reference_no, :year, :status, presence: true
19
21
  validates :season, presence: true, inclusion: { in: SEASONS }
@@ -3,7 +3,8 @@ module Cats
3
3
  class RhnRequest < ApplicationRecord
4
4
  DRAFT = 'Draft'.freeze
5
5
  APPROVED = 'Approved'.freeze
6
- STATUSES = [DRAFT, APPROVED].freeze
6
+ ALLOCATED = 'Allocated'.freeze
7
+ STATUSES = [DRAFT, APPROVED, ALLOCATED].freeze
7
8
 
8
9
  belongs_to :commodity
9
10
 
@@ -35,6 +36,11 @@ module Cats
35
36
  self.status = APPROVED
36
37
  save!
37
38
  end
39
+
40
+ def allocate
41
+ self.status = ALLOCATED
42
+ save!
43
+ end
38
44
  end
39
45
  end
40
46
  end
@@ -2,7 +2,7 @@ module Cats
2
2
  module Core
3
3
  class DispatchPlanItemSerializer < ActiveModel::Serializer
4
4
  attributes :id, :dispatch_plan_id, :plan_reference_no, :source_id, :source_name, :destination_id,
5
- :destination_name, :quantity, :source_location_type, :destination_location_type
5
+ :destination_name, :quantity, :source_location_type, :destination_location_type, :commodity_status
6
6
  end
7
7
  end
8
8
  end
@@ -1,7 +1,8 @@
1
1
  module Cats
2
2
  module Core
3
3
  class DispatchPlanSerializer < ActiveModel::Serializer
4
- attributes :id, :reference_no, :status, :request_id, :upstream
4
+ attributes :id, :reference_no, :status, :request_id, :commodity_id, :commodity_name, :commodity_batch_no,
5
+ :commodity_quantity, :request_reference_no, :upstream
5
6
  end
6
7
  end
7
8
  end
@@ -1,7 +1,27 @@
1
- class DispatchPlanService
2
- def approve(plan)
3
- plan.approve
4
- send_notification(plan)
5
- plan
1
+ module Cats
2
+ module Core
3
+ class DispatchPlanService
4
+ def approve(plan)
5
+ plan.approve
6
+ send_notification(plan)
7
+ plan
8
+ end
9
+
10
+ def create(params, user)
11
+ plan = Cats::Core::DispatchPlan.new(params)
12
+ raise 'Commodity is not assigned for plan.' unless plan.commodity_id || plan.request_id
13
+
14
+ unless plan.commodity_id
15
+ request = Cats::Core::RhnRequest.find(plan.request_id)
16
+ plan.commodity_id = request.commodity_id
17
+ plan.request_type = 'Cats::Core::RhnRequest'
18
+ end
19
+ plan.prepared_by = user
20
+ result = plan.save
21
+ plan.request.allocate if result && plan.request_id
22
+
23
+ [result, plan]
24
+ end
25
+ end
6
26
  end
7
27
  end
@@ -6,6 +6,10 @@ class CreateCatsCoreDispatchPlans < ActiveRecord::Migration[6.1]
6
6
  t.string :status, null: false, default: 'Draft'
7
7
  t.references :request, polymorphic: true
8
8
  t.boolean :upstream, null: false, default: false
9
+ t.references :commodity,
10
+ null: false,
11
+ index: { name: 'commodity_on_dp_indx' },
12
+ foreign_key: { to_table: :cats_core_commodities }
9
13
 
10
14
  t.references :prepared_by,
11
15
  null: false,
@@ -18,6 +18,7 @@ class CreateCatsCoreDispatchPlanItems < ActiveRecord::Migration[6.1]
18
18
  index: { name: 'dpi_on_destination_indx'},
19
19
  foreign_key: { to_table: :cats_core_locations }
20
20
  t.float :quantity, null: false
21
+ t.string :commodity_status, null: false, default: 'Good'
21
22
 
22
23
  t.timestamps
23
24
  end
@@ -2,15 +2,17 @@ class CreateCatsCoreMonthlyPlans < ActiveRecord::Migration[6.1]
2
2
  def change
3
3
  create_table :cats_core_monthly_plans do |t|
4
4
  t.string :reference_no, unique: true
5
+ t.integer :month, null: false
6
+ t.integer :no_of_days, null: false
5
7
  t.string :status, null: false, default: 'Draft'
6
8
  t.references :plan,
7
9
  null: false,
8
10
  index: { name: 'plan_on_mp_indx' },
9
11
  foreign_key: { to_table: :cats_core_plans }
10
- t.references :regional_request,
12
+ t.references :region,
11
13
  null: false,
12
- index: { name: 'rr_on_mp_indx' },
13
- foreign_key: { to_table: :cats_core_regional_requests }
14
+ index: { name: 'region_on_mp_indx' },
15
+ foreign_key: { to_table: :cats_core_locations }
14
16
 
15
17
  t.timestamps
16
18
  end
@@ -5,10 +5,10 @@ class CreateCatsCoreMonthlyPlanItemDetails < ActiveRecord::Migration[6.1]
5
5
  null: false,
6
6
  index: { name: 'mpi_on_mpid_indx' },
7
7
  foreign_key: { to_table: :cats_core_monthly_plan_items }
8
- t.references :ration_item,
8
+ t.references :monthly_ration,
9
9
  null: false,
10
- index: { name: 'ri_on_mpid_indx' },
11
- foreign_key: { to_table: :cats_core_ration_items }
10
+ index: { name: 'ri_on_mr_indx' },
11
+ foreign_key: { to_table: :cats_core_monthly_rations }
12
12
  t.float :amount, null: false
13
13
 
14
14
  t.timestamps
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.3.11'.freeze
3
+ VERSION = '1.3.16'.freeze
4
4
  end
5
5
  end
@@ -5,5 +5,6 @@ FactoryBot.define do
5
5
  destination factory: :location
6
6
  dispatch_plan
7
7
  quantity { 100 }
8
+ commodity_status { Cats::Core::Commodity::GOOD }
8
9
  end
9
10
  end
@@ -4,6 +4,7 @@ FactoryBot.define do
4
4
  status { Cats::Core::DispatchPlan::DRAFT }
5
5
  request { nil }
6
6
  upstream { false }
7
+ commodity
7
8
  prepared_by factory: :user
8
9
  approved_by { nil }
9
10
  end
@@ -1,7 +1,7 @@
1
1
  FactoryBot.define do
2
2
  factory :monthly_plan_item_detail, class: 'Cats::Core::MonthlyPlanItemDetail' do
3
3
  monthly_plan_item
4
- ration_item
4
+ monthly_ration
5
5
  amount { 150 }
6
6
  end
7
7
  end
@@ -3,6 +3,8 @@ FactoryBot.define do
3
3
  reference_no { FFaker::Name.name }
4
4
  status { Cats::Core::Plan::DRAFT }
5
5
  plan
6
- regional_request
6
+ region factory: :location
7
+ month { 1 }
8
+ no_of_days { 30 }
7
9
  end
8
10
  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.3.11
4
+ version: 1.3.16
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-01-08 00:00:00.000000000 Z
11
+ date: 2022-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -286,7 +286,6 @@ files:
286
286
  - app/models/cats/core/ration_item.rb
287
287
  - app/models/cats/core/receipt.rb
288
288
  - app/models/cats/core/receipt_transaction.rb
289
- - app/models/cats/core/regional_request.rb
290
289
  - app/models/cats/core/rhn_request.rb
291
290
  - app/models/cats/core/role.rb
292
291
  - app/models/cats/core/role_menu.rb
@@ -385,11 +384,10 @@ files:
385
384
  - db/migrate/20211229160128_create_cats_core_contract_items.rb
386
385
  - db/migrate/20211229160129_create_cats_core_commodity_substitutions.rb
387
386
  - db/migrate/20220103152802_create_cats_core_tenderers.rb
388
- - db/migrate/20220105084347_create_cats_core_regional_requests.rb
389
387
  - db/migrate/20220107121752_create_cats_core_monthly_plans.rb
388
+ - db/migrate/20220107124630_create_cats_core_monthly_rations.rb
390
389
  - db/migrate/20220107125025_create_cats_core_monthly_plan_items.rb
391
390
  - db/migrate/20220107132433_create_cats_core_monthly_plan_item_details.rb
392
- - db/migrate/20220107224630_create_cats_core_monthly_rations.rb
393
391
  - lib/cats/core.rb
394
392
  - lib/cats/core/engine.rb
395
393
  - lib/cats/core/version.rb
@@ -430,7 +428,6 @@ files:
430
428
  - spec/factories/cats/core/rations.rb
431
429
  - spec/factories/cats/core/receipt_transactions.rb
432
430
  - spec/factories/cats/core/receipts.rb
433
- - spec/factories/cats/core/regional_requests.rb
434
431
  - spec/factories/cats/core/rhn_requests.rb
435
432
  - spec/factories/cats/core/role_menus.rb
436
433
  - spec/factories/cats/core/roles.rb
@@ -1,36 +0,0 @@
1
- module Cats
2
- module Core
3
- class RegionalRequest < ApplicationRecord
4
- belongs_to :plan
5
- belongs_to :region, class_name: 'Cats::Core::Location'
6
-
7
- validates :reference_no, presence: true, uniqueness: true
8
- validates :beneficiaries, :no_of_days, :month, presence: true, numericality: { greater_than: 0 }
9
- validate :validate_month, :validate_beneficiaries, :validate_region
10
-
11
- delegate(:reference_no, to: :plan, prefix: true)
12
- delegate(:name, to: :region, prefix: true)
13
-
14
- def validate_month
15
- return unless month
16
-
17
- errors.add(:month, 'cannot be greater than 12.') if month > 12
18
- end
19
-
20
- def validate_beneficiaries
21
- return unless beneficiaries
22
-
23
- return unless plan
24
-
25
- total = plan.plan_items.sum(:beneficiaries)
26
- errors.add(:beneficiaries, "cannot be higher than #{total}.") if total < beneficiaries
27
- end
28
-
29
- def validate_region
30
- return unless region
31
-
32
- errors.add(:region, 'is not valid.') unless region.location_type == Cats::Core::Location::REGION
33
- end
34
- end
35
- end
36
- end
@@ -1,20 +0,0 @@
1
- class CreateCatsCoreRegionalRequests < ActiveRecord::Migration[6.1]
2
- def change
3
- create_table :cats_core_regional_requests do |t|
4
- t.string :reference_no, unique: true
5
- t.integer :beneficiaries, null: false
6
- t.integer :month, null: false
7
- t.integer :no_of_days, null: false
8
- t.references :plan,
9
- null: false,
10
- index: { name: 'plan_on_rr_indx' },
11
- foreign_key: { to_table: :cats_core_plans }
12
- t.references :region,
13
- null: false,
14
- index: { name: 'region_on_rr_indx' },
15
- foreign_key: { to_table: :cats_core_locations }
16
-
17
- t.timestamps
18
- end
19
- end
20
- end
@@ -1,14 +0,0 @@
1
- FactoryBot.define do
2
- factory :regional_request, class: 'Cats::Core::RegionalRequest' do
3
- plan do
4
- pl = create(:plan)
5
- 3.times { create(:plan_item, plan: pl, beneficiaries: 100) }
6
- pl
7
- end
8
- region factory: :location
9
- reference_no { FFaker::Name.name }
10
- beneficiaries { 100 }
11
- month { 1 }
12
- no_of_days { 30 }
13
- end
14
- end