1+ require ( 'dotenv' ) . config ( ) ;
2+ require ( 'process' ) ;
3+ const { MongoClient } = require ( 'mongodb' ) ;
4+
5+ /**
6+ * Method that runs convertor script
7+ */
8+ async function run ( ) {
9+ const fullUri = process . env . MONGO_HAWK_DB_URL ;
10+
11+ // Parse the Mongo URL manually
12+ const mongoUrl = new URL ( fullUri ) ;
13+ const hawkDatabaseName = 'hawk' ;
14+
15+ // Extract query parameters
16+ const queryParams = Object . fromEntries ( mongoUrl . searchParams . entries ( ) ) ;
17+
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+ } ;
29+
30+ // Remove query string from URI
31+ mongoUrl . search = '' ;
32+ const cleanUri = mongoUrl . toString ( ) ;
33+
34+ console . log ( 'Connecting to:' , cleanUri ) ;
35+ console . log ( 'With options:' , options ) ;
36+
37+ const client = new MongoClient ( cleanUri , options ) ;
38+
39+ await client . connect ( ) ;
40+ const hawkDb = client . db ( hawkDatabaseName ) ;
41+
42+ console . log ( `Connected to database: ${ hawkDatabaseName } ` ) ;
43+
44+ const collections = await hawkDb . listCollections ( { } , {
45+ authorizedCollections : true ,
46+ nameOnly : true ,
47+ } ) . toArray ( ) ;
48+
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 ) ;
50+
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+ }
76+ }
77+
78+ i ++ ;
79+ }
80+
81+ await client . close ( ) ;
82+ }
83+
84+ run ( ) . catch ( err => {
85+ console . error ( '❌ Script failed:' , err ) ;
86+ process . exit ( 1 ) ;
87+ } ) ;
0 commit comments