/** * 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();