@@ -43,6 +43,36 @@ const cache = new Map();
4343
4444const readMutex = new Mutex ( ) ;
4545
46+ async function getHistoryVCs ( name , grouplist , retrieveFromHistory = true ) {
47+ // Retrieve VC list from the user's job history
48+ let vcsFromJob = [ ] ;
49+ if ( retrieveFromHistory ) {
50+ logger . info ( `Retrieving VC list from job history for user: ${ name } ` ) ;
51+ vcsFromJob = await job . listVCsFromJob ( name ) ;
52+ }
53+
54+ // Retrieve VC list from each group the user belongs to
55+ const vcSet = new Set ( ) ;
56+ const vcPromises = grouplist . map ( async group => {
57+ try {
58+ return await groupModel . getGroupVCs ( group ) ;
59+ } catch ( error ) {
60+ console . error ( `Failed to fetch VCs for group ${ group } :` , error ) ;
61+ return [ ] ; // Return an empty array on failure
62+ }
63+ } ) ;
64+ const vcResults = await Promise . all ( vcPromises ) ;
65+ vcResults . forEach ( vcs => vcs . forEach ( vc => vcSet . add ( vc ) ) ) ;
66+
67+ // Merge VC lists and remove duplicates
68+ const mergedVCList = new Set ( [
69+ ...vcsFromJob ,
70+ ...Array . from ( vcSet ) ,
71+ ] ) ;
72+
73+ return Array . from ( mergedVCList ) ;
74+ }
75+
4676async function read ( key ) {
4777 if ( cache . has ( key ) ) {
4878 logger . info ( `Read user info from cache: ${ key } ` ) ;
@@ -207,28 +237,7 @@ async function create(key, value) {
207237 await User . encryptUserPassword ( userInstance ) ;
208238
209239 // retrieve VC list from the history job list belonging to the user if exists
210- const vcsFromJob = await job . listVCsFromJob ( userInstance . username ) ;
211- userInstance . history_vclist = vcsFromJob ;
212-
213- // retrieve VC from group list
214- const vcSet = new Set ( ) ;
215- const vcPromises = value . grouplist . map ( async group => {
216- try {
217- return await groupModel . getGroupVCs ( group ) ;
218- } catch ( error ) {
219- console . error ( `Failed to fetch VCs for group ${ group } :` , error ) ;
220- return [ ] ; // Return an empty array on failure
221- }
222- } ) ;
223- const vcResults = await Promise . all ( vcPromises ) ;
224- vcResults . forEach ( vcs => vcs . forEach ( vc => vcSet . add ( vc ) ) ) ;
225-
226- // Merge vcResults into userInstance.history_vclist and remove duplicates
227- const mergedVCList = new Set ( [
228- ...userInstance . history_vclist ,
229- ...Array . from ( vcSet ) ,
230- ] ) ;
231- userInstance . history_vclist = Array . from ( mergedVCList ) ;
240+ userInstance . history_vclist = await getHistoryVCs ( userInstance . username , userInstance . grouplist ) ;
232241
233242 const userData = {
234243 metadata : { name : hexKey } ,
@@ -287,39 +296,25 @@ async function update(key, value, updatePassword = false) {
287296 grouplist : value . grouplist ,
288297 email : value . email ,
289298 extension : value . extension ,
290- //history_vclist: value.history_vclist || [],
291- history_vclist : [ ] ,
299+ history_vclist : value . history_vclist || [ ] ,
292300 } ) ;
293301 if ( updatePassword ) {
294302 await User . encryptUserPassword ( userInstance ) ;
295303 }
296304
297305 // if userInstance.history_vclist is empty, set it to the retrieved VC list
298306 // retrieve VC list from the job list belonging to the user
299- if ( ! userInstance . history_vclist || userInstance . history_vclist . length === 0 ) {
300- const vcsFromJob = await job . listVCsFromJob ( userInstance . username ) ;
301- userInstance . history_vclist = vcsFromJob ;
302- }
307+ const vclist = await getHistoryVCs (
308+ userInstance . username ,
309+ userInstance . grouplist ,
310+ userInstance . history_vclist . length === 0
311+ ) ;
303312
304- // retrieve VC from group list
305- const vcSet = new Set ( ) ;
306- const vcPromises = value . grouplist . map ( async group => {
307- try {
308- return await groupModel . getGroupVCs ( group ) ;
309- } catch ( error ) {
310- console . error ( `Failed to fetch VCs for group ${ group } :` , error ) ;
311- return [ ] ; // Return an empty array on failure
312- }
313- } ) ;
314- const vcResults = await Promise . all ( vcPromises ) ;
315- vcResults . forEach ( vcs => vcs . forEach ( vc => vcSet . add ( vc ) ) ) ;
316-
317- // Merge vcResults into userInstance.history_vclist and remove duplicates
318- const mergedVCList = new Set ( [
319- ...userInstance . history_vclist ,
320- ...Array . from ( vcSet ) ,
321- ] ) ;
322- userInstance . history_vclist = Array . from ( mergedVCList ) ;
313+ // Merge userInstance.history_vclist with vclist and remove duplicates
314+ userInstance . history_vclist = Array . from ( new Set ( [
315+ ...( userInstance . history_vclist || [ ] ) ,
316+ ...( vclist || [ ] ) ,
317+ ] ) ) ;
323318
324319 const userData = {
325320 metadata : { name : hexKey } ,
0 commit comments