@@ -10,6 +10,8 @@ class DraftVaccinationRecord
1010 include VaccinationRecordPerformedByConcern
1111
1212 attribute :batch_id , :integer
13+ attribute :batch_name , :string
14+ attribute :batch_expiry , :date
1315 attribute :delivery_method , :string
1416 attribute :delivery_site , :string
1517 attribute :disease_types , array : true
@@ -34,6 +36,7 @@ class DraftVaccinationRecord
3436 attribute :session_id , :integer
3537 attribute :source , :string
3638 attribute :supplied_by_user_id , :integer
39+ attribute :vaccine_id , :integer
3740
3841 def initialize ( current_user :, **attributes )
3942 @current_user = current_user
@@ -83,7 +86,11 @@ def wizard_steps
8386 end
8487
8588 on_wizard_step :batch , exact : true do
86- validates :batch_id , presence : true
89+ validates :batch_id , presence : true , unless : :bulk_upload_user_and_record?
90+
91+ validates :vaccine_id , presence : true , if : :bulk_upload_user_and_record?
92+ validates :batch_name , batch_name : true , if : :bulk_upload_user_and_record?
93+ validates :batch_expiry , presence : true , if : :bulk_upload_user_and_record?
8794 end
8895
8996 on_wizard_step :dose , exact : true do
@@ -155,6 +162,17 @@ def already_had?
155162 alias_method :administered , :administered?
156163
157164 def batch
165+ if batch_expiry && batch_name && vaccine_id && bulk_upload_user_and_record?
166+ return (
167+ Batch . create_with ( archived_at : Time . current ) . find_or_create_by! (
168+ expiry : batch_expiry ,
169+ name : batch_name ,
170+ team_id : nil ,
171+ vaccine_id : vaccine_id
172+ )
173+ )
174+ end
175+
158176 return nil if batch_id . nil?
159177 Batch . find ( batch_id )
160178 end
@@ -236,8 +254,6 @@ def delivery_method=(value)
236254
237255 delegate :vaccine , to : :batch , allow_nil : true
238256
239- delegate :id , to : :vaccine , prefix : true , allow_nil : true
240-
241257 def vaccine_id_changed? = batch_id_changed?
242258
243259 def location_is_school
@@ -331,6 +347,24 @@ def bulk_upload_user_and_record?
331347 sourced_from_bulk_upload?
332348 end
333349
350+ def read_from! ( vaccination_record )
351+ self . batch_name = vaccination_record . batch &.name
352+ self . batch_expiry = vaccination_record . batch &.expiry
353+ self . vaccine_id = vaccination_record . vaccine &.id
354+
355+ super ( vaccination_record )
356+ end
357+
358+ def write_to! ( vaccination_record )
359+ super ( vaccination_record )
360+
361+ if batch_expiry && batch_name && vaccine_id && bulk_upload_user_and_record?
362+ vaccination_record . batch_id = batch &.id
363+ end
364+
365+ vaccination_record . vaccine_id = batch &.vaccine_id
366+ end
367+
334368 private
335369
336370 def readable_attribute_names
0 commit comments