-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview.js
More file actions
140 lines (116 loc) · 4.87 KB
/
review.js
File metadata and controls
140 lines (116 loc) · 4.87 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fs = require('fs');
const { execSync } = require('child_process');
const fieldsWherePrevStaffNeedsToBeRemoved = ["To do","Acknowledged", "Mi nuh need dis"];
function getData(pageId) {
// Read the JSON file
const rawData = execSync(`notion-cli page ${pageId}`, { encoding: 'utf-8' });
const data = JSON.parse(rawData);
fs.writeFileSync('page.json', rawData);
// Function to extract all properties of type 'people'
function extractPeopleProperties(result) {
const extractedProperties = {};
for (const [key, value] of Object.entries(result.properties)) {
if (value.type === 'people') {
extractedProperties[key] = {
id: value.id,
type: value.type,
people: value.people.map(person => ({
id: person.id
}))
};
}
}
return Object.keys(extractedProperties).length > 0 ? { properties: extractedProperties } : null;
}
// Extract all 'people' properties from the first result (assuming there's only one)
const extractedData = extractPeopleProperties(data.result[0]);
// Output the result
if (extractedData) {
console.log(JSON.stringify(extractedData, null, 2));
// Optionally, write to a file
fs.writeFileSync('data/tmp/extracted_people.json', JSON.stringify(extractedData, null, 2));
return extractedData;
} else {
console.log("No properties of type 'people' found.");
return false;
}
}
function movePreviousUser(properties, from, to, currentIds, ignoreIds) {
if (properties[from] && properties[to]) {
const [remaining, moved] = properties[from].people.reduce((acc, p) => {
if (currentIds.includes(p.id) || ignoreIds.includes(p.id)) {
acc[0].push(p);
} else {
acc[1].push(p);
}
return acc;
}, [[], []]);
properties[from].people = remaining;
properties[to].people.push(...moved);
}
}
function updateData(data, currentIds, ignoreIds) {
if (!data || !data.properties) {
console.log("Invalid data structure");
return false;
}
const properties = data.properties;
const primaryField = fieldsWherePrevStaffNeedsToBeRemoved[0];
for (const field of fieldsWherePrevStaffNeedsToBeRemoved) {
// (1) Move people from current field to "[field] (previous staff)" if not in currentIds
movePreviousUser(properties, field, field + " (previous staff)", currentIds, ignoreIds);
}
// (2) Add people to primary field if in currentIds but not in other specified fields
if (properties[primaryField]) {
const primaryFieldIds = properties[primaryField].people.map(p => p.id);
const existingIds = new Set(primaryFieldIds);
// Collect IDs from all specified fields
for (const field of fieldsWherePrevStaffNeedsToBeRemoved) {
if (field !== primaryField && properties[field]) {
properties[field].people.forEach(p => existingIds.add(p.id));
}
}
// Add current users who are not in any of the specified fields to the primary field
currentIds.forEach(id => {
if (!existingIds.has(id)) {
properties[primaryField].people.push({ id });
}
});
}
// Output the result
console.log(JSON.stringify(data, null, 2));
// Optionally, write to a file
fs.writeFileSync('data/tmp/updated_people.json', JSON.stringify(data, null, 2));
return data;
}
function adjustUsers(pageId) {
// List of current user IDs
let currentEmailsObj;
let currentEmailsObj2;
try {
const rawData = fs.readFileSync('data/users.json', 'utf8');
currentEmailsObj = JSON.parse(rawData);
const rawData2 = fs.readFileSync('data/users-ignore.json', 'utf8');
currentEmailsObj2 = JSON.parse(rawData2);
} catch (error) {
console.error("Error reading users.json:", error);
process.exit(1);
}
const currentIds = Object.values(currentEmailsObj);
const ignoreIds = Object.values(currentEmailsObj2)
const extractedData = getData(pageId);
if (extractedData) {
const updatedData = updateData(extractedData, currentIds, ignoreIds);
// Save updatedData to file:
fs.writeFileSync('data/tmp/updated_data.json', JSON.stringify(updatedData.properties, null, 2));
console.log('Updated data saved to updated_data.json');
const newData = execSync(`notion-cli update --data data/tmp/updated_data.json ${pageId}`, { encoding: 'utf-8' });
}
}
// Read page IDs from command line arguments
const pageIds = process.argv.slice(2);
// Iterate through each page ID
pageIds.forEach(pageId => {
console.log(`Processing page ID: ${pageId}`);
adjustUsers(pageId);
});