-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_update_config.js
More file actions
106 lines (90 loc) · 3.11 KB
/
db_update_config.js
File metadata and controls
106 lines (90 loc) · 3.11 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
#!/usr/bin/env node
const readline = require('readline');
const { getSupabaseClient } = require('./db_utils');
const { showConfiguration } = require('./db_show_config');
// CLI interface
let rl;
function createInterface() {
if (!rl) {
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
return rl;
}
// Update configuration
async function updateConfiguration() {
try {
const supabase = getSupabaseClient();
const rl = createInterface();
// First get the current configuration
const currentConfig = await showConfiguration().catch(error => {
console.error('Error retrieving current configuration:', error.message);
throw error;
});
return new Promise((resolve, reject) => {
rl.question('\nEnter the new configuration as JSON (press Enter to keep current): ', async (input) => {
if (!input.trim()) {
console.log('Configuration unchanged.');
resolve();
return;
}
try {
const newConfig = JSON.parse(input);
// Validate the configuration
if (!Array.isArray(newConfig)) {
throw new Error('Configuration must be an array of question sets');
}
// Ensure each set has required properties
for (const set of newConfig) {
if (!set.name || typeof set.percentage !== 'number') {
throw new Error('Each question set must have a name and percentage');
}
}
// Ensure percentages sum to 100
const totalPercentage = newConfig.reduce((sum, set) => sum + set.percentage, 0);
if (Math.abs(totalPercentage - 100) > 0.01) {
throw new Error(`Percentages must sum to 100, got ${totalPercentage}`);
}
// Update the configuration
try {
const { error: updateError } = await supabase
.from('configuration')
.update({ question_sets: newConfig, updated_at: new Date() })
.eq('id', 1);
if (updateError) throw updateError;
console.log('Configuration updated successfully!');
// Show the new configuration
await showConfiguration();
resolve();
} catch (dbError) {
console.error('Database error:', dbError.message);
reject(dbError);
}
} catch (error) {
console.error('Error updating configuration:', error.message);
reject(error);
}
});
});
} catch (error) {
console.error('Error preparing to update configuration:', error.message);
throw error;
}
}
// If this script is run directly
if (require.main === module) {
updateConfiguration()
.then(() => {
if (rl) rl.close();
console.log('Update process complete.');
process.exit(0);
})
.catch(error => {
if (rl) rl.close();
console.error('Update process failed:', error);
process.exit(1);
});
}
module.exports = { updateConfiguration };