File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -151,6 +151,7 @@ def save(draft_vaccination_record:)
151151 draft_vaccination_record . source = "service"
152152 draft_vaccination_record . supplied_by_user_id =
153153 supplied_by_user_id || psd_created_by_user_id
154+ draft_vaccination_record . uuid = generate_uuid
154155
155156 draft_vaccination_record . save # rubocop:disable Rails/SaveBang
156157 end
@@ -211,4 +212,12 @@ def pre_screening
211212 programme :
212213 )
213214 end
215+
216+ def generate_uuid
217+ 10
218+ . times
219+ . map { SecureRandom . uuid }
220+ . find { !VaccinationRecord . exists? ( uuid : it ) } or
221+ raise "Unable to generate unique UUID"
222+ end
214223end
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ class DraftVaccinationRecord
3737 attribute :session_id , :integer
3838 attribute :source , :string
3939 attribute :supplied_by_user_id , :integer
40+ attribute :uuid , :string
4041 attribute :vaccine_id , :integer
4142
4243 MMR_OR_MMRV_INTRODUCTION_DATE = Date . new ( 2020 , 1 , 1 ) . freeze
@@ -417,6 +418,7 @@ def writable_attribute_names
417418 session_id
418419 source
419420 supplied_by_user_id
421+ uuid
420422 vaccine_id
421423 ]
422424 end
Original file line number Diff line number Diff line change 9191 end
9292 end
9393 end
94+
95+ describe "#save" do
96+ subject ( :form ) do
97+ described_class . new (
98+ programme :,
99+ current_user :,
100+ session :,
101+ patient : create ( :patient ) ,
102+ identity_check_confirmed_by_patient : true ,
103+ pre_screening_notes : "" ,
104+ pre_screening_confirmed : true ,
105+ vaccine_method : "injection" ,
106+ delivery_site : "left_arm_upper_position"
107+ )
108+ end
109+
110+ describe "a uuid" do
111+ let ( :draft_vaccination_record ) do
112+ DraftVaccinationRecord . new ( request_session : { } , current_user :)
113+ end
114+
115+ it "is generated" do
116+ form . save ( draft_vaccination_record :) # rubocop:disable Rails/SaveBang
117+ expect ( draft_vaccination_record . uuid ) . to be_present
118+ end
119+
120+ it "tries 10 times to find a uuid that's not already in use" do
121+ allow ( VaccinationRecord ) . to receive ( :exists? ) . and_return ( true )
122+
123+ expect { form . save ( draft_vaccination_record :) } . to raise_error (
124+ RuntimeError ,
125+ "Unable to generate unique UUID"
126+ )
127+ expect ( VaccinationRecord ) . to have_received ( :exists? ) . exactly ( 10 ) . times
128+ end
129+ end
130+ end
94131end
You can’t perform that action at this time.
0 commit comments