@@ -15,8 +15,6 @@ import Crypto from 'crypto';
15
15
import { HapiRequest , HapiServer } from "../types" ;
16
16
import { AdapterFormModel } from "../plugins/engine/models" ;
17
17
import Boom from "boom" ;
18
- import { FormConfiguration } from "@xgovformbuilder/model" ;
19
- import { AdapterSchema } from "@communitiesuk/model" ;
20
18
21
19
const partition = "cache" ;
22
20
const LOGGER_DATA = {
@@ -59,22 +57,26 @@ const createRedisClient = (): Redis | null => {
59
57
} ;
60
58
61
59
export class AdapterCacheService extends CacheService {
60
+ private formStorage : Redis | any ;
62
61
63
62
constructor ( server : HapiServer ) {
64
63
//@ts -ignore
65
64
super ( server ) ;
66
- // @ts -ignore
67
- server . app . redis = this . getRedisClient ( )
68
- // @ts -ignore
69
- if ( ! server . app . redis ) {
70
- // starting up the in memory cache
65
+ const redisClient = this . getRedisClient ( ) ;
66
+ if ( redisClient ) {
67
+ this . formStorage = redisClient ;
68
+ } else {
69
+ // Starting up the in memory cache
71
70
this . cache . client . start ( ) ;
72
- //@ts -ignore
73
- server . app . inMemoryFormKeys = [ ]
71
+ this . formStorage = {
72
+ get : ( key ) => this . cache . get ( key ) ,
73
+ set : ( key , value ) => this . cache . set ( key , value , { expiresIn : 0 } ) ,
74
+ setex : ( key , ttl , value ) => this . cache . set ( key , value , { expiresIn : ttl } ) ,
75
+ }
74
76
}
75
77
}
76
78
77
- async activateSession ( jwt , request ) {
79
+ async activateSession ( jwt , request ) : Promise < { redirectPath : string } > {
78
80
request . logger . info ( `[ACTIVATE-SESSION] jwt ${ jwt } ` ) ;
79
81
const initialisedSession = await this . cache . get ( this . JWTKey ( jwt ) ) ;
80
82
request . logger . info ( `[ACTIVATE-SESSION] session details ${ initialisedSession } ` ) ;
@@ -83,14 +85,12 @@ export class AdapterCacheService extends CacheService {
83
85
const userSessionKey = { segment : partition , id : `${ request . yar . id } :${ payload . group } ` } ;
84
86
request . logger . info ( `[ACTIVATE-SESSION] session metadata ${ userSessionKey } ` ) ;
85
87
const { redirectPath} = await super . activateSession ( jwt , request ) ;
86
-
87
88
let redirectPathNew = redirectPath
88
89
const form_session_identifier = initialisedSession . metadata ?. form_session_identifier ;
89
90
if ( form_session_identifier ) {
90
91
userSessionKey . id = `${ userSessionKey . id } :${ form_session_identifier } ` ;
91
92
redirectPathNew = `${ redirectPathNew } ?form_session_identifier=${ form_session_identifier } ` ;
92
93
}
93
-
94
94
if ( config . overwriteInitialisedSession ) {
95
95
request . logger . info ( "[ACTIVATE-SESSION] Replacing user session with initialisedSession" ) ;
96
96
this . cache . set ( userSessionKey , initialisedSession , sessionTimeout ) ;
@@ -120,7 +120,7 @@ export class AdapterCacheService extends CacheService {
120
120
* @param additionalIdentifier - appended to the id
121
121
*/
122
122
//@ts -ignore
123
- Key ( request : HapiRequest , additionalIdentifier ?: ADDITIONAL_IDENTIFIER ) {
123
+ Key ( request : HapiRequest , additionalIdentifier ?: ADDITIONAL_IDENTIFIER ) : { segment : string ; id : string } {
124
124
let id = `${ request . yar . id } :${ request . params . id } ` ;
125
125
126
126
if ( request . query . form_session_identifier ) {
@@ -141,117 +141,44 @@ export class AdapterCacheService extends CacheService {
141
141
* @param configuration form definition configurations
142
142
* @param server server object
143
143
*/
144
- async setFormConfiguration ( formId : string , configuration : any , server : HapiServer ) {
145
- if ( formId && configuration ) {
146
- //@ts -ignore
147
- if ( server . app . redis ) {
148
- await this . addConfigurationsToRedisCache ( server , configuration , formId ) ;
149
- } else {
150
- await this . addConfigurationIntoInMemoryCache ( configuration , formId , server ) ;
151
- }
152
- }
153
- }
154
-
155
- private async addConfigurationIntoInMemoryCache ( configuration : any , formId : string , server : HapiServer ) {
156
- const hashValue = Crypto . createHash ( 'sha256' ) . update ( JSON . stringify ( configuration ) ) . digest ( 'hex' )
144
+ async setFormConfiguration ( formId : string , configuration : any ) : Promise < void > {
145
+ if ( ! formId || ! configuration ) return ;
146
+ const hashValue = Crypto . createHash ( 'sha256' )
147
+ . update ( JSON . stringify ( configuration ) )
148
+ . digest ( 'hex' ) ;
149
+ const key = `${ FORMS_KEY_PREFIX } ${ formId } ` ;
157
150
try {
158
- const jsonDataString = await this . cache . get ( ` ${ FORMS_KEY_PREFIX } ${ formId } ` ) ;
159
- if ( jsonDataString === null ) {
160
- // Adding new config into redis cache service with the hash value
151
+ const existingConfigString = await this . formStorage . get ( key ) ;
152
+ if ( existingConfigString === null ) {
153
+ // Adding new config with the hash value
161
154
const stringConfig = JSON . stringify ( {
162
155
...configuration ,
163
156
id : configuration . id ,
164
157
hash : hashValue
165
158
} ) ;
166
- //@ts -ignore
167
- server . app . inMemoryFormKeys . push ( `${ FORMS_KEY_PREFIX } ${ formId } ` )
168
- // Adding data into redis cache
169
- await this . cache . set ( `${ FORMS_KEY_PREFIX } ${ formId } ` , stringConfig , { expiresIn : 0 } ) ;
159
+ await this . formStorage . set ( key , stringConfig ) ;
170
160
} else {
171
- // Redis has the data and gets current data set to check hash
172
- const configObj = JSON . parse ( jsonDataString ) ;
173
- if ( configObj && configObj . hash && hashValue !== configObj . hash ) {
174
- // if hash function is change then updating the configuration
161
+ // Check if hash has changed
162
+ const existingConfig = JSON . parse ( existingConfigString ) ;
163
+ if ( existingConfig ? .hash !== hashValue ) {
164
+ // Hash has changed, update the configuration
175
165
const stringConfig = JSON . stringify ( {
176
166
...configuration ,
177
167
id : configuration . id ,
178
168
hash : hashValue
179
169
} ) ;
180
- await this . cache . set ( ` ${ FORMS_KEY_PREFIX } ${ formId } ` , stringConfig , { expiresIn : 0 } ) ;
170
+ await this . formStorage . set ( key , stringConfig ) ;
181
171
}
182
172
}
183
173
} catch ( error ) {
184
174
console . log ( error ) ;
185
175
}
186
176
}
187
177
188
- private async addConfigurationsToRedisCache ( server : HapiServer , configuration : any , formId : string ) {
189
- //@ts -ignore
190
- const redisClient : Redis = server . app . redis
191
- const hashValue = Crypto . createHash ( 'sha256' ) . update ( JSON . stringify ( configuration ) ) . digest ( 'hex' )
192
- if ( redisClient ) {
193
- const jsonDataString = await redisClient . get ( `${ FORMS_KEY_PREFIX } ${ formId } ` ) ;
194
- if ( jsonDataString === null ) {
195
- // Adding new config into redis cache service with the hash value
196
- const stringConfig = JSON . stringify ( {
197
- ...configuration ,
198
- id : configuration . id ,
199
- hash : hashValue
200
- } ) ;
201
- // Adding data into redis cache
202
- await redisClient . set ( `${ FORMS_KEY_PREFIX } ${ formId } ` , stringConfig ) ;
203
- } else {
204
- // Redis has the data and gets current data set to check hash
205
- const configObj = JSON . parse ( jsonDataString ) ;
206
- if ( configObj && configObj . hash && hashValue !== configObj . hash ) {
207
- // if hash function is change then updating the configuration
208
- const stringConfig = JSON . stringify ( {
209
- ...configuration ,
210
- id : configuration . id ,
211
- hash : hashValue
212
- } ) ;
213
- await redisClient . set ( `${ FORMS_KEY_PREFIX } ${ formId } ` , stringConfig ) ;
214
- }
215
- }
216
- }
217
- }
218
-
219
- async getFormAdapterModel ( formId : string , request : HapiRequest ) {
220
- //@ts -ignore
221
- if ( request . server . app . redis ) {
222
- return await this . getConfigurationFromRedisCache ( request , formId ) ;
223
- } else {
224
- return await this . getConfigurationFromInMemoryCache ( request , formId ) ;
225
- }
226
- }
227
-
228
- private async getConfigurationFromInMemoryCache ( request : HapiRequest , formId : string ) {
229
- const { translationLoaderService} = request . services ( [ ] ) ;
230
- const translations = translationLoaderService . getTranslations ( ) ;
231
- const jsonDataString = await this . cache . get ( `${ FORMS_KEY_PREFIX } ${ formId } ` ) ;
232
- if ( jsonDataString !== null ) {
233
- const configObj = JSON . parse ( jsonDataString ) ;
234
- return new AdapterFormModel ( configObj . configuration , {
235
- basePath : configObj . id ? configObj . id : formId ,
236
- hash : configObj . hash ,
237
- previewMode : true ,
238
- translationEn : translations . en ,
239
- translationCy : translations . cy
240
- } )
241
- }
242
- request . logger . error ( {
243
- ...LOGGER_DATA ,
244
- message : `[FORM-CACHE] Cannot find the form ${ formId } `
245
- } ) ;
246
- throw Boom . notFound ( "Cannot find the given form" ) ;
247
- }
248
-
249
- private async getConfigurationFromRedisCache ( request : HapiRequest , formId : string ) {
250
- //@ts -ignore
251
- const redisClient : Redis = request . server . app . redis
178
+ async getFormAdapterModel ( formId : string , request : HapiRequest ) : Promise < AdapterFormModel > {
252
179
const { translationLoaderService} = request . services ( [ ] ) ;
253
180
const translations = translationLoaderService . getTranslations ( ) ;
254
- const jsonDataString = await redisClient . get ( `${ FORMS_KEY_PREFIX } ${ formId } ` ) ;
181
+ const jsonDataString = await this . formStorage . get ( `${ FORMS_KEY_PREFIX } ${ formId } ` ) ;
255
182
if ( jsonDataString !== null ) {
256
183
const configObj = JSON . parse ( jsonDataString ) ;
257
184
return new AdapterFormModel ( configObj . configuration , {
@@ -260,7 +187,7 @@ export class AdapterCacheService extends CacheService {
260
187
previewMode : true ,
261
188
translationEn : translations . en ,
262
189
translationCy : translations . cy
263
- } )
190
+ } ) ;
264
191
}
265
192
request . logger . error ( {
266
193
...LOGGER_DATA ,
0 commit comments