Files
sena_db_api_layer/update-grade-code-format.js
silverpro89 5957636b07
All checks were successful
Deploy to Production / deploy (push) Successful in 29s
update new API
2026-02-09 23:17:03 +07:00

145 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Update grade code format in all JSON files
* Old format: GUUL (4 digits) - e.g., 1011 = Grade 1, Unit 01, Lesson 1
* New format: GGUULL (6 digits) - e.g., 010101 = Grade 01, Unit 01, Lesson 01
*/
const fs = require('fs');
const path = require('path');
// Configuration
const DATA_DIR = path.join(__dirname, 'data');
const DRY_RUN = false; // Set to true to preview changes without updating files
function convertGradeCode(oldCode) {
const codeStr = oldCode.toString();
if (codeStr.length === 4) {
// Format: GUUL -> GGUULL
const grade = codeStr[0]; // G
const unit = codeStr.substring(1, 3); // UU
const lesson = codeStr[3]; // L
return `0${grade}${unit}0${lesson}`;
} else if (codeStr.length === 3) {
// Format: GUL -> GGUULL
const grade = codeStr[0]; // G
const unit = codeStr[1]; // U
const lesson = codeStr[2]; // L
return `0${grade}0${unit}0${lesson}`;
} else if (codeStr.length === 6) {
// Already in new format
return codeStr;
}
// Unknown format, return as is
console.log(` ⚠️ Unknown format: ${codeStr}`);
return codeStr;
}
function processJsonFile(filePath) {
try {
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
if (!Array.isArray(data)) {
return { updated: 0, changes: [] };
}
let updated = 0;
const changes = [];
// Update grade codes
data.forEach(entry => {
if (entry.grade) {
const oldGrade = entry.grade;
const newGrade = convertGradeCode(oldGrade);
if (oldGrade !== newGrade) {
changes.push({ old: oldGrade, new: newGrade });
entry.grade = newGrade;
updated++;
}
}
});
// Write updated data back to file
if (!DRY_RUN && updated > 0) {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
}
return { updated, changes };
} catch (error) {
console.error(` ❌ Error processing ${filePath}: ${error.message}`);
return { updated: 0, changes: [] };
}
}
function processDirectory(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
let totalUpdated = 0;
const allChanges = [];
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
const result = processDirectory(fullPath);
totalUpdated += result.updated;
allChanges.push(...result.changes);
} else if (entry.isFile() && entry.name.endsWith('.json')) {
console.log(` 📄 Processing ${entry.name}...`);
const { updated, changes } = processJsonFile(fullPath);
if (updated > 0) {
console.log(` ✅ Updated ${updated} grade codes`);
changes.forEach(c => {
console.log(` ${c.old}${c.new}`);
});
totalUpdated += updated;
allChanges.push(...changes);
} else {
console.log(` ⏭️ No changes needed`);
}
}
}
return { updated: totalUpdated, changes: allChanges };
}
async function main() {
try {
console.log('🔄 Starting grade code format update...\n');
if (DRY_RUN) {
console.log('⚠️ DRY RUN MODE - No files will be modified\n');
}
// Check if data directory exists
if (!fs.existsSync(DATA_DIR)) {
console.error('❌ Data directory not found:', DATA_DIR);
process.exit(1);
}
// Process all JSON files in data directory
console.log('📂 Processing JSON files...\n');
const { updated, changes } = processDirectory(DATA_DIR);
console.log('\n✅ Processing complete!');
console.log(`📊 Total grade codes updated: ${updated}`);
if (DRY_RUN) {
console.log('\n⚠ This was a DRY RUN. Set DRY_RUN = false to apply changes.');
}
process.exit(0);
} catch (error) {
console.error('❌ Error:', error.message);
console.error(error.stack);
process.exit(1);
}
}
// Run the script
main();