-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathset-user-workspaces-membership.js
More file actions
49 lines (35 loc) · 1.52 KB
/
set-user-workspaces-membership.js
File metadata and controls
49 lines (35 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require('dotenv').config();
require('process');
const { setup } = require('./setup');
/**
* Method that runs convertor script
*/
async function run() {
const { client, hawkDb } = await setup();
const collections = await hawkDb.listCollections({}, {
authorizedCollections: true,
nameOnly: true,
}).toArray();
let usersMembershipCollectionsToCheck = collections.filter(col => /^membership:/.test(col.name)).map(col => col.name);
console.log(`Found ${usersMembershipCollectionsToCheck.length} users membership collections.`);
const usersDocuments = await hawkDb.collection('users').find({}).toArray();
// Convert events
let i = 1;
for (const collectionName of usersMembershipCollectionsToCheck) {
console.log(`[${i}/${usersMembershipCollectionsToCheck.length}] Processing ${collectionName}`);
const userId = collectionName.split(':')[1];
const userDocument = usersDocuments.find(u => u._id.toString() === userId);
const memberships = await hawkDb.collection(collectionName).find({}).toArray();
for (const membership of memberships) {
const workspaceId = membership.workspaceId.toString();
const isPending = membership.isPending || false;
await hawkDb.collection('users').updateOne({ _id: userDocument._id }, { $set: { [`workspaces.${workspaceId}`]: { isPending } } });
}
i++;
}
await client.close();
}
run().catch(err => {
console.error('❌ Script failed:', err);
process.exit(1);
});