cats_core 1.4.27 → 1.4.30

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fddf0547d798ce7e1b0807f6a41c63d7b9d4cd65aa8647fcdfb67ca47326b41
4
- data.tar.gz: 1784fbfd4ab5dd5af00f07c07dd2aef0f561d49381ab2bf8dbf4726dc9b3d85d
3
+ metadata.gz: a4956ad89bea4861d95aaffc40534eab2b319009d2a10f2762e9b1ee6b68b60e
4
+ data.tar.gz: 6d5aaf08e18bb95e8fd09c0957bbfeb982df76c1061f1b001028f1e91a1ac8ff
5
5
  SHA512:
6
- metadata.gz: 45e67cd0b18c50abbd5d08698ad47312dfbc8b735327a3645906d16f60db5addb24edef7180a9adb2a1ce9c8ea69858e63dd600c3aaeb1767de3f022c3a2b598
7
- data.tar.gz: d46af8e447713c4981e3b5d8796f246fc13c0a294b15239adbbeccede51b76e68f5e7d1d24ab9ecfb0f93f586f8f5d015396a0bc6dc51bca58d06ace5081f1c3
6
+ metadata.gz: 88a42101e62d3a13b23a905bcc06852115797cd3a0e160ae7204510777cb494b03f108500f7e0c5201439695ac084d737f903c619a4e1fc9d0e926ceec7aa396
7
+ data.tar.gz: 7de9d35e696f2b2421cefa54da0e706b0826b3c63389eff498d4e0964f354cf61f7ae3eb4baba80f16b214b405615a2adcd3c2c19f1e97649a6fe4313c7b598f
@@ -3,6 +3,8 @@ module Cats
3
3
  class ReceiptAuthorizationsController < ApplicationController
4
4
  include Common
5
5
 
6
+ skip_before_action :authenticate, only: %i[driver_confirm]
7
+
6
8
  def index
7
9
  super do
8
10
  ReceiptAuthorization.where(dispatch_id: params[:id])
@@ -17,6 +19,14 @@ module Cats
17
19
  render json: { success: false, error: e.message }
18
20
  end
19
21
 
22
+ def driver_confirm
23
+ service = AuthorizationService.new
24
+ authorization = service.driver_confirm(params[:id], driver_confirm_params[:pin])
25
+ render json: { success: true, data: serialize(authorization) }
26
+ rescue StandardError => e
27
+ render json: { success: false, error: e.message }
28
+ end
29
+
20
30
  def stack
21
31
  service = AuthorizationService.new
22
32
  authorization = ReceiptAuthorization.find(params[:id])
@@ -39,6 +49,10 @@ module Cats
39
49
  def model_params
40
50
  params.require(:payload).permit(:dispatch_id, :store_id, :quantity, :remark, :status, :authorized_by_id)
41
51
  end
52
+
53
+ def driver_confirm_params
54
+ params.require(:payload).permit(:pin)
55
+ end
42
56
  end
43
57
  end
44
58
  end
@@ -3,7 +3,7 @@ module Cats
3
3
  class RoundPlansController < ApplicationController
4
4
  include Common
5
5
 
6
- before_action :set_service, only: %i[generate remove_items generate_round_needs]
6
+ before_action :set_service, only: %i[approve generate remove_items generate_round_needs]
7
7
 
8
8
  def index
9
9
  super do
@@ -17,8 +17,7 @@ module Cats
17
17
  end
18
18
 
19
19
  def approve
20
- plan = RoundPlan.find(params[:id])
21
- plan.approve
20
+ plan = @service.approve(params[:id])
22
21
  render json: { success: true, data: serialize(plan) }
23
22
  rescue StandardError => e
24
23
  render json: { success: false, error: e.message }, status: :unprocessable_entity
@@ -8,7 +8,29 @@ module Cats
8
8
  validates :beneficiaries, presence: true, numericality: { greater_than: 0 }
9
9
  validates :plan_item_id, uniqueness: { scope: :beneficiary_category_id }
10
10
 
11
+ validates :rounds, presence: true, numericality: { greater_than: 0 }, if: :psnp?
12
+ validates :rounds_served, presence: true, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true,
13
+ if: :psnp?
14
+ validates :rounds, :rounds_served, absence: true, unless: :psnp?
15
+ validate :validate_rounds
16
+
11
17
  delegate(:name, to: :beneficiary_category, prefix: true)
18
+
19
+ def psnp?
20
+ return false unless plan_item
21
+
22
+ plan_item.plan.program.code == 'PSNP'
23
+ end
24
+
25
+ def validate_rounds
26
+ return unless rounds && rounds_served
27
+
28
+ errors.add(:rounds, 'should not be set for non PSNP plans.') unless psnp?
29
+
30
+ errors.add(:rounds_served, 'should not be set for non PSNP plans.') unless psnp?
31
+
32
+ errors.add(:rounds, 'cannot be lower than rounds served.') if rounds < rounds_served
33
+ end
12
34
  end
13
35
  end
14
36
  end
@@ -7,6 +7,7 @@ module Cats
7
7
 
8
8
  validates :beneficiaries, presence: true, numericality: { greater_than_or_equal_to: 0 }
9
9
  validates :round_plan_item_id, uniqueness: { scope: :beneficiary_category_id }
10
+ validates :beneficiary_plan_item_id, presence: true
10
11
 
11
12
  delegate(:name, to: :beneficiary_category, prefix: true)
12
13
  end
@@ -80,6 +80,11 @@ module Cats
80
80
  end
81
81
 
82
82
  def stack
83
+ # check if all receipt transactions have been committed and change status
84
+ # of dispatch to stacked.
85
+ statuses = receipt_transactions.map(&:status).uniq
86
+ return unless statuses.length == 1 && statuses[0] == Transaction::COMMITTED
87
+
83
88
  self.dispatch_status = STACKED
84
89
  save!
85
90
  end
@@ -39,6 +39,7 @@ module Cats
39
39
  end
40
40
 
41
41
  authorization.transactions.each(&:commit)
42
+
42
43
  authorization.dispatch.stack
43
44
  authorization
44
45
  end
@@ -79,7 +79,8 @@ module Cats
79
79
  round_plan_item_id: item[0],
80
80
  beneficiary_category_id: ben_plan_item.beneficiary_category_id,
81
81
  planned_beneficiaries: ben_plan_item.beneficiaries,
82
- beneficiaries: ben_plan_item.beneficiaries
82
+ beneficiaries: ben_plan_item.beneficiaries,
83
+ beneficiary_plan_item_id: ben_plan_item.id
83
84
  }
84
85
  end
85
86
  Cats::Core::BeneficiaryRoundPlanItem.insert_all!(ben_round_plan_items, record_timestamps: true)
@@ -103,6 +104,41 @@ module Cats
103
104
  Cats::Core::RoundPlanItem.delete_by(id: ids)
104
105
  plan
105
106
  end
107
+
108
+ def approve(plan_id)
109
+ plan = RoundPlan.find(plan_id)
110
+ if plan.plan.program.code == 'PSNP'
111
+ rounds = plan.rounds.count
112
+ item_ids = plan.beneficiary_round_plan_items.pluck('beneficiary_plan_item_id').flatten
113
+ plan_items = BeneficiaryPlanItem.where(id: item_ids)
114
+ invalid = plan_items.select { |pi| pi.rounds - (pi.rounds_served || 0) < rounds }
115
+ error = 'There are plan items whose served rounds may exceed the allowed number of rounds.'
116
+ raise(StandardError, error) if invalid.count.positive?
117
+
118
+ items = plan_items.each_with_object([]) do |plan_item, res|
119
+ rounds_served = plan_item.rounds_served || 0
120
+ res << "(#{plan_item.id}, #{rounds + rounds_served})"
121
+ end
122
+ values = items.join(',')
123
+ sql = <<-SQL
124
+ UPDATE cats_core_beneficiary_plan_items AS bpi
125
+ SET
126
+ rounds_served = l.rounds_served
127
+ FROM (
128
+ VALUES
129
+ #{values}
130
+ ) AS l(id, rounds_served)
131
+ WHERE l.id = bpi.id
132
+ SQL
133
+ ActiveRecord::Base.transaction do
134
+ ActiveRecord::Base.connection.execute(sql)
135
+ plan.approve
136
+ end
137
+ else
138
+ plan.approve
139
+ end
140
+ plan
141
+ end
106
142
  end
107
143
  end
108
144
  end
data/config/routes.rb CHANGED
@@ -136,7 +136,8 @@ Cats::Core::Engine.routes.draw do
136
136
  get 'lost', controller: :lost_commodities, action: :index
137
137
  get 'receipts', controller: :receipts, action: :index
138
138
  get 'stacks', controller: :stacks, action: :receipt_stacks
139
- post 'confirm', controller: :receipt_authorizations, action: :confirm
139
+ post 'confirm'
140
+ post 'driver_confirm'
140
141
  post 'stack'
141
142
  end
142
143
  end
@@ -11,6 +11,9 @@ class CreateCatsCoreBeneficiaryPlanItems < ActiveRecord::Migration[7.0]
11
11
  foreign_key: { to_table: :cats_core_beneficiary_categories }
12
12
  t.integer :beneficiaries, null: false
13
13
 
14
+ t.integer :rounds
15
+ t.integer :rounds_served
16
+
14
17
  t.timestamps
15
18
  end
16
19
  add_index(
@@ -11,6 +11,7 @@ class CreateCatsCoreBeneficiaryRoundPlanItems < ActiveRecord::Migration[7.0]
11
11
  foreign_key: { to_table: :cats_core_beneficiary_categories }
12
12
  t.integer :planned_beneficiaries, null: false
13
13
  t.integer :beneficiaries, null: false
14
+ t.integer :beneficiary_plan_item_id, null: false
14
15
 
15
16
  t.timestamps
16
17
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.4.27'.freeze
3
+ VERSION = '1.4.30'.freeze
4
4
  end
5
5
  end
@@ -3,5 +3,7 @@ FactoryBot.define do
3
3
  plan_item
4
4
  beneficiary_category
5
5
  beneficiaries { 100 }
6
+ rounds { nil }
7
+ rounds_served { nil }
6
8
  end
7
9
  end
@@ -4,5 +4,6 @@ FactoryBot.define do
4
4
  beneficiary_category
5
5
  planned_beneficiaries { 100 }
6
6
  beneficiaries { 50 }
7
+ beneficiary_plan_item_id { 1 }
7
8
  end
8
9
  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.27
4
+ version: 1.4.30
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-05-30 00:00:00.000000000 Z
11
+ date: 2022-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers