-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (190 loc) · 7.18 KB
/
index.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import inquirer from 'inquirer';
import chalk from "chalk";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const dataFilePath = path.join(__dirname, 'consentPreferences.json');
// Define location-based consent options
const locationConsentOptions = {
FL: 'Florida',
GA: 'Georgia',
CA: 'California',
CO: 'Colorado',
UT: 'Utah',
};
// Define data privacy options
const dataPrivacyOptions = {
personalizedRecommendations: 'Allow data collection for personalized recommendations',
dataSharingWithThirdParties: 'Allow data sharing with third parties',
};
const specialDataPrivacyOptions = {
personalizedRecommendations: 'Allow data collection for personalized recommendations',
personalizedRecommendationsEXTRA: 'Allow data collection for personalized recommendations',
dataSharingWithThirdParties: 'Allow data sharing with third parties',
dataSharingWithThirdPartiesEXTRA: 'Allow data sharing with third parties',
};
// Define consent options based on location
const consentOptionsByLocation = {
FL: specialDataPrivacyOptions,
GA: specialDataPrivacyOptions,
CA: specialDataPrivacyOptions,
CO: specialDataPrivacyOptions,
UT: specialDataPrivacyOptions,
// Default options for other states
default: dataPrivacyOptions,
};
// Load existing consent preferences from the JSON file
function loadConsentPreferences() {
try {
const data = fs.readFileSync(dataFilePath, 'utf8');
return JSON.parse(data);
} catch (err) {
// If the file doesn't exist or has an error, return an empty object
return {};
}
}
// Save consent preferences to the JSON file
function saveConsentPreferences(consentPreferences) {
fs.writeFileSync(dataFilePath, JSON.stringify(consentPreferences, null, 2), 'utf8');
}
// Define a data structure to store user consent preferences
const userConsentData = {};
// Function to get or create a user based on their name
function getUserByName(name) {
const existingUser = Object.keys(userConsentData).find(userID => userConsentData[userID].name === name);
if (existingUser) {
return userConsentData[existingUser];
} else {
const userID = Math.random().toString(36).substr(2, 10);
userConsentData[userID] = {
name: name,
consentPreferences: {},
};
return userConsentData[userID];
}
}
// Function to view consent status for each option with color-coding
function viewConsentStatus(user) {
console.log(`Consent Status for ${user.name}:`);
for (const option in user.consentPreferences) {
const status = user.consentPreferences[option] ? 'Given' : 'Not Given';
const statusColor = user.consentPreferences[option] ? chalk.green : chalk.red;
console.log(`${option}: ${statusColor(status)}`);
}
}
// Function to manage consent for a user
async function manageConsent(user) {
const consentPrompt = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: `Manage consent preferences for ${user.name} in ${user.location}:`,
choices: ['View Consent Status', 'Give Consent', 'Revoke Consent', 'Exit'],
},
]);
switch (consentPrompt.action) {
case 'View Consent Status':
viewConsentStatus(user);
break;
case 'Give Consent':
const giveConsentPrompt = await inquirer.prompt([
{
type: 'checkbox',
name: 'consentOptions',
message: 'Select options to give consent:',
choices: Object.keys(dataPrivacyOptions).map(option => ({
name: dataPrivacyOptions[option],
value: option,
})),
validate: (input) => {
if (input.length === 0) {
return 'Please select at least one option to give consent.';
}
return true;
},
},
]);
giveConsentPrompt.consentOptions.forEach(option => {
user.consentPreferences[option] = true;
});
console.log('Consent given for selected options.');
break;
case 'Revoke Consent':
const revokeConsentPrompt = await inquirer.prompt([
{
type: 'checkbox',
name: 'consentOptions',
message: 'Select options to revoke consent:',
choices: Object.keys(dataPrivacyOptions).map(option => ({
name: dataPrivacyOptions[option],
value: option,
})),
validate: (input) => {
if (input.length === 0) {
return 'Please select at least one option to revoke consent.';
}
return true;
},
},
]);
const confirmRevoke = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmRevoke',
message: 'Are you sure you want to revoke consent for the selected options?',
},
]);
if (confirmRevoke.confirmRevoke) {
revokeConsentPrompt.consentOptions.forEach(option => {
user.consentPreferences[option] = false;
});
console.log('Consent revoked for selected options.');
} else {
console.log('Consent revocation canceled.');
}
break;
case 'Exit':
// Save the updated consent preferences to the JSON file before exiting
saveConsentPreferences(userConsentData);
return;
}
// Recursively call the function for further actions
manageConsent(user);
}
// Start the consent manager
(async () => {
// Load existing consent preferences from the JSON file
const loadedConsentPreferences = loadConsentPreferences();
Object.assign(userConsentData, loadedConsentPreferences);
const namePrompt = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Enter your name:',
validate: (input) => {
if (!input) {
return 'Please enter your name.';
}
return true;
},
},
]);
const locationPrompt = await inquirer.prompt([
{
type: 'input',
name: 'location',
message: 'Enter your location (e.g., FL, GA, CA, CO, UT):',
validate: (input) => {
if (!input || !/^[A-Z]{2}$/.test(input)) {
return 'Please enter a valid two-letter state code (e.g., FL, GA, CA, CO, UT).';
}
return true;
},
},
]);
const user = getUserByName(namePrompt.name);
console.log(`Welcome, ${user.name}!`);
manageConsent(user);
})();