cats_core 1.4.26 → 1.4.29

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: 67116f414334d42cf370f7a2ec48cf137bd1ac0c8d1345e0ce5c99f8fc87c57c
4
- data.tar.gz: 0fc96065462743887b84a6bc750f97d52bd1f047a32c71fa1b70bb3eda9148cb
3
+ metadata.gz: cf68527d50707b488472a3aaa759722ce59d59ed171f75cb018a3bfe19421179
4
+ data.tar.gz: 2183b2b605d8b73ee8c29c4a8cf6ca6438a72ad2569342fb8da615da9ebd05e7
5
5
  SHA512:
6
- metadata.gz: 148225081058c17d1435f7888da4076ecfacb458a37154e64fd5eabe1fb3470cc3ff4fcafa696db893eb3d3a70ad90920cb25b408723fd4140b3a6e1f70d02ea
7
- data.tar.gz: a7ee6bc61afe79bfbb22d65a02852ea00a0872971c023fd0783f30de6f33b59eb47a4807887b239aa8e54996eca58924fdae8321c73c1af2db078e193f39067d
6
+ metadata.gz: b15751ad2c5557da8aaed254e538ccf0d7bab769b3af085ce3a46c5e0bdbc975c09e8217f1694275976207d5b99d3455d8587bb419a49b85d7153d8718e4924b
7
+ data.tar.gz: c4b5d706eb93c2c12e3d5517daec41360c4557e47edc1c6d25cac94bc090dbdba8e24082d5806db026c17f0e41da2f44696be45d61c21049bbf37762875e35af
@@ -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
@@ -29,8 +29,8 @@ module Cats
29
29
 
30
30
  def receipt_stacks
31
31
  service = StackService.new
32
- receipt = Receipt.find(params[:id])
33
- stacks = service.receipt_stacks(receipt)
32
+ authorization = ReceiptAuthorization.find(params[:id])
33
+ stacks = service.receipt_stacks(authorization.id)
34
34
  render json: { success: true, data: serialize(stacks) }
35
35
  end
36
36
 
@@ -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
@@ -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
@@ -135,7 +135,9 @@ Cats::Core::Engine.routes.draw do
135
135
  get 'transactions', controller: :receipt_transactions, action: :index
136
136
  get 'lost', controller: :lost_commodities, action: :index
137
137
  get 'receipts', controller: :receipts, action: :index
138
- post 'confirm', controller: :receipt_authorizations, action: :confirm
138
+ get 'stacks', controller: :stacks, action: :receipt_stacks
139
+ post 'confirm'
140
+ post 'driver_confirm'
139
141
  post 'stack'
140
142
  end
141
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.26'.freeze
3
+ VERSION = '1.4.29'.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.26
4
+ version: 1.4.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.