86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
/**
|
|
* Test: Verify profile fetching logic
|
|
*/
|
|
|
|
require('dotenv').config();
|
|
const { sequelize, TeacherDetail, UserProfile } = require('../models');
|
|
|
|
async function testProfileFetch() {
|
|
console.log('🧪 Testing Profile Fetch Logic\n');
|
|
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('✅ Database connected\n');
|
|
|
|
// Get first teacher
|
|
const teacher = await TeacherDetail.findOne({
|
|
order: [['created_at', 'ASC']],
|
|
});
|
|
|
|
if (!teacher) {
|
|
console.log('❌ No teachers found');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('📋 Teacher Found:');
|
|
console.log(` ID: ${teacher.id}`);
|
|
console.log(` Code: ${teacher.teacher_code}`);
|
|
console.log(` User ID: ${teacher.user_id}`);
|
|
console.log('');
|
|
|
|
// Fetch profile manually
|
|
console.log('🔍 Fetching profile manually...');
|
|
const profile = await UserProfile.findOne({
|
|
where: { user_id: teacher.user_id },
|
|
});
|
|
|
|
if (profile) {
|
|
console.log('✅ Profile found!');
|
|
console.log(` Profile ID: ${profile.id}`);
|
|
console.log(` Full Name: ${profile.full_name}`);
|
|
console.log(` Phone: ${profile.phone || 'N/A'}`);
|
|
console.log(` School ID: ${profile.school_id}`);
|
|
console.log('');
|
|
|
|
// Create combined object like in controller
|
|
const teacherData = {
|
|
...teacher.toJSON(),
|
|
profile: profile.toJSON(),
|
|
};
|
|
|
|
console.log('✅ Combined data structure works!');
|
|
console.log('');
|
|
console.log('Sample response:');
|
|
console.log(JSON.stringify({
|
|
id: teacherData.id,
|
|
teacher_code: teacherData.teacher_code,
|
|
teacher_type: teacherData.teacher_type,
|
|
status: teacherData.status,
|
|
profile: {
|
|
id: teacherData.profile.id,
|
|
full_name: teacherData.profile.full_name,
|
|
phone: teacherData.profile.phone,
|
|
school_id: teacherData.profile.school_id,
|
|
}
|
|
}, null, 2));
|
|
|
|
} else {
|
|
console.log('❌ Profile NOT found!');
|
|
console.log(' This is the issue - profile should exist');
|
|
}
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('✅ Test completed!');
|
|
console.log('='.repeat(60));
|
|
|
|
process.exit(0);
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error:', error.message);
|
|
console.error(error.stack);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
testProfileFetch();
|