@@ -7,28 +7,6 @@ export { getSharedAuthToken }
77
88const logger = pino ( )
99
10- const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
11- const DIGITS = "0123456789" ;
12-
13- const randomChar = ( chars ) => chars [ Math . floor ( Math . random ( ) * chars . length ) ] ;
14-
15- /** Generate one two-letter, three-digit ODS code, e.g. "AB123" */
16- const generateOdsCode = ( ) =>
17- `${ randomChar ( LETTERS ) } ${ randomChar ( LETTERS ) } ${ randomChar ( DIGITS ) } ${ randomChar ( DIGITS ) } ${ randomChar ( DIGITS ) } ` ;
18-
19- function buildFullOdsCodes ( targetCount , seedCodes ) {
20- const codes = new Set ( seedCodes ) ;
21-
22- while ( codes . size < targetCount ) {
23- codes . add ( generateOdsCode ( ) ) ;
24- }
25-
26- return Array . from ( codes ) ;
27- }
28-
29- // The complete list of ODS codes
30- const fullOdsCodes = allowedOdsCodes . concat ( blockedOdsCodes )
31-
3210function computeCheckDigit ( nhsNumber ) {
3311 const factors = [ 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ]
3412 let total = 0
@@ -62,23 +40,42 @@ function sampleNormal(mean = 0, sd = 1) {
6240 return z * sd + mean ;
6341}
6442
65- export function initUser ( context , events , done ) {
66- // Generate data for a patient
67- context . vars . odsCode = fullOdsCodes [ Math . floor ( Math . random ( ) * fullOdsCodes . length ) ]
68- context . vars . nhsNumber = generateValidNhsNumber ( )
43+ function initUserContextVars ( context ) {
44+ context . vars . nhsNumber = generateValidNhsNumber ( )
6945
70- let prescriptionCount = Math . round ( sampleNormal ( 3 , 1 ) )
71- if ( prescriptionCount < 1 ) prescriptionCount = 1 // just truncate at 1.
72- context . vars . prescriptionCount = prescriptionCount
73- context . vars . loopcount = 0
74-
75- logger . info ( `Patient ${ context . vars . nhsNumber } , ODS ${ context . vars . odsCode } has ${ context . vars . prescriptionCount } prescriptions` )
76-
77- done ( )
46+ let prescriptionCount = Math . round ( sampleNormal ( 3 , 1 ) )
47+ if ( prescriptionCount < 1 ) prescriptionCount = 1 // just truncate at 1.
48+ context . vars . prescriptionCount = prescriptionCount
49+ context . vars . loopcount = 0
50+ }
51+
52+ export function initUserAllowed ( context , events , done ) {
53+ logger . info ( "Initializing user context variables for allowed ODS codes" )
54+ initUserContextVars ( context )
55+
56+ // Generate data for a patient with an allowed ODS code
57+ context . vars . odsCode = allowedOdsCodes [ Math . floor ( Math . random ( ) * allowedOdsCodes . length ) ]
58+
59+ logger . info ( `[ALLOWED] Patient ${ context . vars . nhsNumber } , ODS ${ context . vars . odsCode } has ${ context . vars . prescriptionCount } prescriptions` )
60+ done ( )
61+ }
62+
63+ export function initUserBlocked ( context , events , done ) {
64+ logger . info ( "Initializing user context variables for allowed ODS codes" )
65+ initUserContextVars ( context )
66+
67+ // Generate data for a patient with a blocked ODS code
68+ context . vars . odsCode = blockedOdsCodes [ Math . floor ( Math . random ( ) * blockedOdsCodes . length ) ]
69+
70+ logger . info ( `[BLOCKED] Patient ${ context . vars . nhsNumber } , ODS ${ context . vars . odsCode } has ${ context . vars . prescriptionCount } prescriptions` )
71+ done ( )
7872}
7973
8074export function generatePrescData ( requestParams , context , ee , next ) {
81- logger . debug ( `Generating a prescription for patient ${ context . vars . nhsNumber } ` )
75+ const isAllowed = allowedOdsCodes . includes ( context . vars . odsCode ) ;
76+ const logPrefix = isAllowed ? "[ALLOWED]" : "[BLOCKED]" ;
77+
78+ logger . debug ( `${ logPrefix } Generating a prescription for patient ${ context . vars . nhsNumber } ` )
8279 const body = getBody (
8380 true , /* isValid */
8481 "completed" , /* status */
@@ -87,11 +84,11 @@ export function generatePrescData(requestParams, context, ee, next) {
8784 "ready to collect" /* Item status */
8885 )
8986 // The body is fine - it works when I put it in postman
90-
87+
9188 requestParams . json = body
9289 context . vars . x_request_id = uuidv4 ( )
9390 context . vars . x_correlation_id = uuidv4 ( )
94-
91+
9592 context . vars . loopcount += 1
9693
9794 // Wait this long between requests
@@ -104,8 +101,6 @@ export function generatePrescData(requestParams, context, ee, next) {
104101 }
105102
106103 context . vars . nextDelay = delay
107- logger . debug ( `Patient ${ context . vars . nhsNumber } (on prescription update ${ context . vars . loopcount } /${ context . vars . prescriptionCount } ) will think for ${ context . vars . nextDelay } seconds` )
108-
104+ logger . debug ( `${ logPrefix } Patient ${ context . vars . nhsNumber } (on prescription update ${ context . vars . loopcount } /${ context . vars . prescriptionCount } ) will think for ${ context . vars . nextDelay } seconds` )
109105 next ( )
110106}
111-
0 commit comments