cats_core 1.4.24 → 1.4.25

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: 3dd00b147a4dfa4ab46e10d7961b4291fbdb39ed68e49a0766e69a4a4d22704a
4
- data.tar.gz: 1c37f11f41f76263cb197cdd881b820475a38e8addf1424fce593287ab1e30ad
3
+ metadata.gz: ad828e18f9f084f3c2e0343af564a1f1cc4832e935c9eec068503f4293442b87
4
+ data.tar.gz: 8e0520ff2a6df1e2b4dd3f726a60aef02ef45915158c72c73ccbc74a9ae2786d
5
5
  SHA512:
6
- metadata.gz: ce78191aa4dde60ba521f32f95a87088f7e9380fcba3e3a05f2cb570c4fd722b6b1962ddff54ab76ea0278552b1640f3a72b0e96571e0160b096bc49b00abd56
7
- data.tar.gz: f5e36206d35c62b26cd16bdcaeea9f11c711d90c2c68b6f5c5eeb4e174621c4ad6e8ef28f0082ad74cb505e82bbc44876cd67da9be92b045beebfd0f22c1b269
6
+ metadata.gz: cc1d8fc0861a5f8c46734cbbf029608625e8734d31361cf924834769166cc6d429f247b169b299616a5bedcf9dffd0cdc2ad4960e4caa502757a1a98cfd55865
7
+ data.tar.gz: '0850a3f89b88aea548765a78f46a8d4cac259642dc3f8c15837ab54c1d5c7911b311b8b55719972fe19312cf614d07042652376556a134ea2e20c8b1f7227fc1'
@@ -10,8 +10,8 @@ module Cats
10
10
  end
11
11
 
12
12
  def confirm
13
- service = AuthorizationService.new
14
- authorization = service.confirm(params[:id])
13
+ authorization = DispatchAuthorization.find(params[:id])
14
+ authorization = authorization.confirm
15
15
  render json: { success: true, data: serialize(authorization) }
16
16
  rescue StandardError => e
17
17
  render json: { success: false, error: e.message }
@@ -2,8 +2,8 @@ module Cats
2
2
  module Core
3
3
  class DispatchesController < ApplicationController
4
4
  include Common
5
-
6
- before_action :set_service, only: %i[create_receipt_authorization approve start search confirm]
5
+ skip_before_action :authenticate, only: %i[start_with_pin]
6
+ before_action :set_service, only: %i[create_receipt_authorization approve start start_with_pin search confirm]
7
7
 
8
8
  def index
9
9
  super do
@@ -35,6 +35,13 @@ module Cats
35
35
  render json: { success: false, error: e.message }
36
36
  end
37
37
 
38
+ def start_with_pin
39
+ dispatch = @service.start_with_pin(params[:id], start_with_pin_params[:pin])
40
+ render json: { success: true, data: serialize(dispatch) }
41
+ rescue StandardError => e
42
+ render json: { success: false, error: e.message }
43
+ end
44
+
38
45
  def confirm
39
46
  dispatch = set_object
40
47
  dispatch.confirm
@@ -68,6 +75,10 @@ module Cats
68
75
  params.require(:payload).permit(:reference_no, :dispatch_plan_item_id, :transporter_id, :plate_no, :unit_id,
69
76
  :driver_name, :driver_phone, :remark)
70
77
  end
78
+
79
+ def start_with_pin_params
80
+ params.require(:payload).permit(:pin)
81
+ end
71
82
  end
72
83
  end
73
84
  end
@@ -46,11 +46,19 @@ module Cats
46
46
 
47
47
  def confirm
48
48
  if instance_of?(DispatchAuthorization)
49
- raise(StandardError, 'Authorization does not have transactions.') if transactions.blank?
50
-
51
- transactions.each(&:commit)
52
- # count = transactions.where(status: Transaction::DRAFT).count
53
- # raise(StandardError, 'There are uncommitted transactions.') if count.positive?
49
+ ActiveRecord::Base.transaction do
50
+ raise(StandardError, 'Authorization does not have transactions.') if transactions.blank?
51
+
52
+ transactions.each(&:commit)
53
+ self.status = CONFIRMED
54
+ save!
55
+ if dispatch.all_authorizations_confirmed?
56
+ dispatch.generate_pin
57
+ dispatch.quantity = dispatch.dispatch_transactions.sum(:quantity)
58
+ dispatch.dispatch_status = Dispatch::READY_TO_START
59
+ dispatch.save!
60
+ end
61
+ end
54
62
  elsif instance_of?(ReceiptAuthorization)
55
63
  raise(StandardError, 'Authorization does not have receipts.') unless receipts.count.positive?
56
64
 
@@ -60,8 +68,16 @@ module Cats
60
68
  raise(StandardError, "A quantity of #{diff} is unaccounted for.") unless diff.zero?
61
69
 
62
70
  self.received_quantity = total_received
71
+ self.status = CONFIRMED
72
+ save!
73
+ generate_pin
63
74
  end
64
- self.status = CONFIRMED
75
+ self
76
+ end
77
+
78
+ def generate_pin
79
+ pin = SecureRandom.hex(10)
80
+ self.auth_details = { pin: pin, active: true, expires_at: DateTime.now + 1.hour }
65
81
  save!
66
82
  end
67
83
 
@@ -92,6 +92,12 @@ module Cats
92
92
  }
93
93
  end
94
94
  end
95
+
96
+ def generate_pin
97
+ pin = SecureRandom.hex(10)
98
+ self.auth_details = { pin: pin, active: true, expires_at: DateTime.now + 1.hour }
99
+ save!
100
+ end
95
101
  end
96
102
  end
97
103
  end
@@ -2,7 +2,7 @@ module Cats
2
2
  module Core
3
3
  class DispatchAuthorizationSerializer < ActiveModel::Serializer
4
4
  attributes :id, :dispatch_id, :dispatch_reference_no, :store_id, :store_name, :quantity, :authorized_by_id,
5
- :authorizer_full_name, :dispatch_status, :plate_no, :driver_name
5
+ :authorizer_full_name, :dispatch_status, :plate_no, :driver_name, :status
6
6
  end
7
7
  end
8
8
  end
@@ -3,7 +3,7 @@ module Cats
3
3
  class DispatchSerializer < ActiveModel::Serializer
4
4
  attributes :id, :reference_no, :dispatch_plan_item_id, :transporter_id, :transporter_name, :plate_no,
5
5
  :driver_name, :driver_phone, :quantity, :remark, :prepared_by_id, :prepared_by_email,
6
- :dispatch_status, :destination
6
+ :dispatch_status, :destination, :auth_details
7
7
 
8
8
  def destination
9
9
  object.dispatch_plan_item.destination.name
@@ -2,7 +2,7 @@ module Cats
2
2
  module Core
3
3
  class ReceiptAuthorizationSerializer < ActiveModel::Serializer
4
4
  attributes :id, :dispatch_id, :dispatch_reference_no, :store_id, :store_name, :quantity, :received_quantity,
5
- :remark, :status, :authorized_by_id, :authorizer_full_name
5
+ :remark, :status, :authorized_by_id, :authorizer_full_name, :auth_details
6
6
  end
7
7
  end
8
8
  end
@@ -1,23 +1,25 @@
1
1
  module Cats
2
2
  module Core
3
3
  class AuthorizationService
4
- # A method to confirm a dispatch authorization. This method
5
- # also checks if all authorizations under a dispatch are confirmed
6
- # and if so it sets the dispatch status to READY_TO_START.
7
- def confirm(id)
8
- authorization = DispatchAuthorization.find(id)
4
+ def driver_confirm(authorization_id, pin)
5
+ authorization = ReceiptAuthorization.find(authorization_id)
6
+ if authorization.status == Authorization::AUTHORIZED
7
+ raise(StandardError, 'Authorization not confirmed by storekeeper.')
8
+ end
9
+
10
+ raise(StandardError, 'No pin has been generated for receipt.') if authorization.auth_details.nil?
11
+
12
+ raise(StandardError, 'Incorrect pin.') unless pin == authorization.auth_details['pin']
9
13
 
10
- ActiveRecord::Base.transaction do
11
- authorization.confirm
14
+ raise(StandardError, 'Pin is not active.') unless authorization.auth_details['active']
12
15
 
13
- # Check if all authorizations under the dispatch are confirmed.
14
- dispatch = authorization.dispatch
15
- if dispatch.all_authorizations_confirmed?
16
- dispatch.quantity = dispatch.dispatch_transactions.sum(:quantity)
17
- dispatch.dispatch_status = Dispatch::READY_TO_START
18
- dispatch.save!
19
- end
16
+ if DateTime.now > authorization.auth_details['expires_at']
17
+ authorization.auth_details[:active] = false
18
+ authorization.save!
19
+ raise(StandardError, 'Pin has expired.')
20
20
  end
21
+ authorization.driver_confirmed = true
22
+ authorization.save!
21
23
  authorization
22
24
  end
23
25
  end
@@ -37,6 +37,28 @@ module Cats
37
37
  dispatch
38
38
  end
39
39
 
40
+ def start_with_pin(dispatch_id, pin)
41
+ dispatch = Dispatch.find(dispatch_id)
42
+ raise(StandardError, 'Dispatch is already started.') if dispatch.dispatch_status == Dispatch::STARTED
43
+
44
+ raise(StandardError, 'No pin has been generated for dispatch.') if dispatch.auth_details.nil?
45
+
46
+ raise(StandardError, 'Incorrect pin.') unless pin == dispatch.auth_details['pin']
47
+
48
+ raise(StandardError, 'Pin is not active.') unless dispatch.auth_details['active']
49
+
50
+ if DateTime.now > dispatch.auth_details['expires_at']
51
+ dispatch.auth_details[:active] = false
52
+ dispatch.save!
53
+ raise(StandardError, 'Pin has expired.')
54
+ end
55
+ dispatch.start
56
+ send_notification(dispatch)
57
+ dispatch.auth_details = nil
58
+ dispatch.save!
59
+ dispatch
60
+ end
61
+
40
62
  def send_notification(dispatch)
41
63
  notification_rule = Cats::Core::NotificationRule.find_by(code: 'dispatch')
42
64
  raise(StandardError, 'Notification rule not found for dispatch notification.') unless notification_rule
data/config/routes.rb CHANGED
@@ -107,6 +107,7 @@ Cats::Core::Engine.routes.draw do
107
107
  post 'approve'
108
108
  post 'start'
109
109
  post 'confirm'
110
+ post 'start_with_pin'
110
111
  end
111
112
  end
112
113
 
@@ -25,6 +25,7 @@ class CreateCatsCoreDispatches < ActiveRecord::Migration[6.1]
25
25
  index: { name: 'pb_on_dispatches_indx' },
26
26
  foreign_key: { to_table: :cats_core_users }
27
27
  t.string :dispatch_status, null: false
28
+ t.jsonb :auth_details
28
29
 
29
30
  t.timestamps
30
31
  end
@@ -13,6 +13,8 @@ class CreateCatsCoreReceiptAuthorizations < ActiveRecord::Migration[6.1]
13
13
  t.float :received_quantity, null: false, default: 0
14
14
  t.string :status, null: false, default: 'Authorized'
15
15
  t.string :remark
16
+ t.jsonb :auth_details
17
+ t.boolean :driver_confirmed, null: false, default: false
16
18
  t.references :authorized_by,
17
19
  null: false,
18
20
  index: { name: 'ab_on_receipt_authorizations_indx' },
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.4.24'.freeze
3
+ VERSION = '1.4.25'.freeze
4
4
  end
5
5
  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.24
4
+ version: 1.4.25
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-28 00:00:00.000000000 Z
11
+ date: 2022-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers