cats_core 1.4.27 → 1.4.28

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: f2cbd9de4f75e95103bf09a853e5d0098e925f6d63c058567657dca04c8e915e
4
+ data.tar.gz: 39dc39668040493ad924ae291ea3f13b34b3644f113558274bbe8f15564d9f1b
5
5
  SHA512:
6
- metadata.gz: 45e67cd0b18c50abbd5d08698ad47312dfbc8b735327a3645906d16f60db5addb24edef7180a9adb2a1ce9c8ea69858e63dd600c3aaeb1767de3f022c3a2b598
7
- data.tar.gz: d46af8e447713c4981e3b5d8796f246fc13c0a294b15239adbbeccede51b76e68f5e7d1d24ab9ecfb0f93f586f8f5d015396a0bc6dc51bca58d06ace5081f1c3
6
+ metadata.gz: dca66c438930ac24dc216e3ef8f162bae9d0ef54442efaeb2139953379366e9fd93a3fd13ed08cc396f19b6383007f0cee5433ef69cdc863e39a96da92969f8c
7
+ data.tar.gz: 22e99525720f4e34f0bae5fec105ebfaa80f28c73ce0b60c7323536e167bf54ef735969cafcd602124cfa148983207c9364707f62a4d74835d0fad597f85c793
@@ -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,28 @@ 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, if: :psnp?
13
+ validates :rounds, :rounds_served, absence: true, unless: :psnp?
14
+ validate :validate_rounds
15
+
11
16
  delegate(:name, to: :beneficiary_category, prefix: true)
17
+
18
+ def psnp?
19
+ return false unless plan_item
20
+
21
+ plan_item.plan.program.code == 'PSNP'
22
+ end
23
+
24
+ def validate_rounds
25
+ return unless rounds && rounds_served
26
+
27
+ errors.add(:rounds, 'should not be set for non PSNP plans.') unless psnp?
28
+
29
+ errors.add(:rounds_served, 'should not be set for non PSNP plans.') unless psnp?
30
+
31
+ errors.add(:rounds, 'cannot be lower than rounds served.') if rounds < rounds_served
32
+ end
12
33
  end
13
34
  end
14
35
  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
@@ -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.28'.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,7 +1,7 @@
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.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.