11require ( 'dotenv' ) . config ( ) ;
22require ( 'process' ) ;
3- const { MongoClient } = require ( 'mongodb ' ) ;
3+ const { setup } = require ( './setup ' ) ;
44
55/**
66 * Method that runs convertor script
77 */
88async function run ( ) {
9- const fullUri = process . env . MONGO_HAWK_DB_URL ;
9+ const { client , hawkDb } = await setup ( ) ;
1010
11- // Parse the Mongo URL manually
12- const mongoUrl = new URL ( fullUri ) ;
13- const hawkDatabaseName = 'hawk' ;
11+ const collections = await hawkDb . listCollections ( { } , {
12+ authorizedCollections : true ,
13+ nameOnly : true ,
14+ } ) . toArray ( ) ;
1415
15- // Extract query parameters
16- const queryParams = Object . fromEntries ( mongoUrl . searchParams . entries ( ) ) ;
16+ let usersInProjectCollectionsToCheck = collections . filter ( col => / ^ u s e r s - i n - p r o j e c t : / . test ( col . name ) ) . map ( col => col . name ) ;
1717
18- // Compose connection options manually
19- const options = {
20- useNewUrlParser : true ,
21- useUnifiedTopology : true ,
22- authSource : queryParams . authSource || 'admin' ,
23- replicaSet : queryParams . replicaSet || undefined ,
24- tls : queryParams . tls === 'true' ,
25- tlsInsecure : queryParams . tlsInsecure === 'true' ,
26- // connectTimeoutMS: 3600000,
27- // socketTimeoutMS: 3600000,
28- } ;
18+ console . log ( `Found ${ usersInProjectCollectionsToCheck . length } users in project collections.` ) ;
2919
30- // Remove query string from URI
31- mongoUrl . search = '' ;
32- const cleanUri = mongoUrl . toString ( ) ;
20+ const usersDocuments = await hawkDb . collection ( 'users' ) . find ( { } ) . toArray ( ) ;
3321
34- console . log ( 'Connecting to:' , cleanUri ) ;
35- console . log ( 'With options:' , options ) ;
22+ // Convert events
23+ let i = 1 ;
3624
37- const client = new MongoClient ( cleanUri , options ) ;
25+ for ( const collectionName of usersInProjectCollectionsToCheck ) {
26+ console . log ( `[${ i } /${ usersInProjectCollectionsToCheck . length } ] Processing ${ collectionName } ` ) ;
3827
39- await client . connect ( ) ;
40- const hawkDb = client . db ( hawkDatabaseName ) ;
28+ const usersInProject = await hawkDb . collection ( collectionName ) . find ( { } ) . toArray ( ) ;
4129
42- console . log ( `Connected to database: ${ hawkDatabaseName } ` ) ;
30+ console . log ( `Found ${ usersInProject . length } users in project ${ collectionName } . ` ) ;
4331
44- const collections = await hawkDb . listCollections ( { } , {
45- authorizedCollections : true ,
46- nameOnly : true ,
47- } ) . toArray ( ) ;
32+ let usersUpdatedCount = 0 ;
4833
49- let usersInProjectCollectionsToCheck = collections . filter ( col => / ^ u s e r s - i n - p r o j e c t : / . test ( col . name ) ) . map ( col => col . name ) ;
34+ for ( const userInProject of usersInProject ) {
35+ const userDocument = usersDocuments . find ( u => u . _id . toString ( ) === userInProject . userId . toString ( ) ) ;
36+ if ( userDocument ) {
37+ const projectId = collectionName . split ( ':' ) [ 1 ] ;
38+ await hawkDb . collection ( 'users' ) . updateOne ( { _id : userDocument . _id } , { $set : { projectsLastVisit : { [ projectId ] : userInProject . timestamp } } } ) ;
39+ usersUpdatedCount ++ ;
40+ console . log ( `Updated ${ usersUpdatedCount } /${ usersInProject . length } users in project ${ collectionName } .` ) ;
41+ }
42+ }
5043
51- console . log ( `Found ${ usersInProjectCollectionsToCheck . length } users in project collections.` ) ;
52-
53- const usersDocuments = await hawkDb . collection ( 'users' ) . find ( { } ) . toArray ( ) ;
54-
55- // Convert events
56- let i = 1 ;
57- let documentsUpdatedCount = 1 ;
58-
59- for ( const collectionName of usersInProjectCollectionsToCheck ) {
60- console . log ( `[${ i } /${ usersInProjectCollectionsToCheck . length } ] Processing ${ collectionName } ` ) ;
61-
62- const usersInProject = await hawkDb . collection ( collectionName ) . find ( { } ) . toArray ( ) ;
63-
64- console . log ( `Found ${ usersInProject . length } users in project ${ collectionName } .` ) ;
65-
66- let usersUpdatedCount = 0 ;
67-
68- for ( const userInProject of usersInProject ) {
69- const userDocument = usersDocuments . find ( u => u . _id . toString ( ) === userInProject . userId . toString ( ) ) ;
70- if ( userDocument ) {
71- const projectId = collectionName . split ( ':' ) [ 1 ] ;
72- await hawkDb . collection ( 'users' ) . updateOne ( { _id : userDocument . _id } , { $set : { projectsLastVisit : { [ projectId ] : userInProject . timestamp } } } ) ;
73- usersUpdatedCount ++ ;
74- console . log ( `Updated ${ usersUpdatedCount } /${ usersInProject . length } users in project ${ collectionName } .` ) ;
75- }
44+ i ++ ;
7645 }
7746
78- i ++ ;
79- }
80-
81- await client . close ( ) ;
47+ await client . close ( ) ;
8248}
8349
8450run ( ) . catch ( err => {
85- console . error ( '❌ Script failed:' , err ) ;
86- process . exit ( 1 ) ;
51+ console . error ( '❌ Script failed:' , err ) ;
52+ process . exit ( 1 ) ;
8753} ) ;
0 commit comments