update
All checks were successful
Deploy to Production / deploy (push) Successful in 20s

This commit is contained in:
vuongps38770
2026-02-28 20:00:38 +07:00
parent f96833a7e4
commit 72283443ab
15 changed files with 972 additions and 318 deletions

View File

@@ -0,0 +1,108 @@
const { Context, Vocab, Sentences } = require('../models');
const { sequelize } = require('../config/database');
// --- helper: xác định slot image từ tên file/URL ---
function resolveImageSlot(imageUrl) {
if (!imageUrl) return null;
const filename = imageUrl.split('/').pop().split('?')[0];
const parts = filename.split('_');
for (const part of parts) {
const key = part.toLowerCase();
if (key === 'square') return 'image_square';
if (key === 'small') return 'image_small';
if (key === 'normal') return 'image_normal';
}
// Nếu không tìm thấy trong underscore, thử tìm trong toàn bộ URL
if (imageUrl.toLowerCase().includes('square')) return 'image_square';
if (imageUrl.toLowerCase().includes('small')) return 'image_small';
if (imageUrl.toLowerCase().includes('normal')) return 'image_normal';
return null;
}
async function syncImages() {
try {
await sequelize.authenticate();
console.log('✅ Kết nối database thành công.');
const contexts = await Context.findAll({
where: { status: 5 }
});
console.log(`📦 Tìm thấy ${contexts.length} bản ghi Context ở status 5.`);
let vocabUpdated = 0;
let sentencesUpdated = 0;
let skipped = 0;
for (const ctx of contexts) {
const imageUrl = ctx.image;
const slot = resolveImageSlot(imageUrl);
if (!imageUrl || !slot) {
console.warn(`⚠️ ID ${ctx.uuid}: Không xác định được ảnh hoặc slot (URL: ${imageUrl}). Bỏ qua.`);
skipped++;
continue;
}
let updated = false;
// Ưu tiên 1: Dùng reference_id
if (ctx.reference_id) {
// Thử tìm trong Vocab
const vocab = await Vocab.findByPk(ctx.reference_id);
if (vocab) {
await vocab.update({ [slot]: [imageUrl] });
vocabUpdated++;
updated = true;
} else {
// Thử tìm trong Sentences
const sentence = await Sentences.findByPk(ctx.reference_id);
if (sentence) {
await sentence.update({ [slot]: [imageUrl] });
sentencesUpdated++;
updated = true;
}
}
}
// Ưu tiên 2: Nếu chưa update được (hoặc ko có ref_id), thử match theo text
if (!updated) {
// Thử khớp Vocab.text = Context.title
const vocabByText = await Vocab.findOne({ where: { text: ctx.title } });
if (vocabByText) {
await vocabByText.update({ [slot]: [imageUrl] });
vocabUpdated++;
updated = true;
} else if (ctx.context) {
// Thử khớp Sentences.text = Context.context
const sentenceByText = await Sentences.findOne({ where: { text: ctx.context } });
if (sentenceByText) {
await sentenceByText.update({ [slot]: [imageUrl] });
sentencesUpdated++;
updated = true;
}
}
}
if (updated) {
await ctx.update({ status: 6 });
} else {
console.warn(`❌ ID ${ctx.uuid}: Không tìm thấy entity đích để cập nhật ảnh (Title: ${ctx.title}).`);
skipped++;
}
}
console.log('\n--- KẾT QUẢ ĐỒNG BỘ ẢNH ---');
console.log(`✅ Cập nhật Vocab: ${vocabUpdated} bản ghi.`);
console.log(`✅ Cập nhật Sentences: ${sentencesUpdated} bản ghi.`);
console.log(`⚠️ Bỏ qua hoặc lỗi: ${skipped} bản ghi.`);
console.log(`📊 Tổng xử lý: ${contexts.length}`);
process.exit(0);
} catch (error) {
console.error('❌ Lỗi khi đồng bộ:', error);
process.exit(1);
}
}
syncImages();