![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/scripts/ |
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function testCommentSystem() {
console.log('=== TESTING COMMENT SYSTEM ===\n');
try {
// 1. Find a public case
console.log('1. Finding a public case...');
const publicCase = await prisma.legalCase.findFirst({
where: { isPublic: true },
select: { id: true, title: true }
});
if (!publicCase) {
console.log('❌ No public cases found. Creating one...');
const newCase = await prisma.legalCase.create({
data: {
title: 'Test Case for Comments',
description: 'A test case to verify comment functionality',
isPublic: true,
status: 'ACTIVE',
urgency: 'NORMAL',
category: 'CIVIL',
jurisdiction: 'QUEBEC',
courtType: 'SUPERIOR',
caseNumber: 'TEST-2024-001',
filingDate: new Date(),
estimatedDuration: '3-6 months',
budget: 5000,
complexity: 'MEDIUM',
clientId: null,
leadLawyerId: null,
lawFirmId: null,
createdById: null,
updatedById: null
}
});
console.log('✅ Created test case:', newCase.id);
} else {
console.log('✅ Found public case:', publicCase.id, '-', publicCase.title);
}
const caseId = publicCase?.id || (await prisma.legalCase.findFirst({ where: { isPublic: true } })).id;
// 2. Find a test user
console.log('\n2. Finding a test user...');
const testUser = await prisma.user.findFirst({
where: { role: { in: ['CLIENT', 'LAWYER', 'ADMIN'] } },
select: { id: true, name: true, email: true, role: true }
});
if (!testUser) {
console.log('❌ No test users found');
return;
}
console.log('✅ Found test user:', testUser.name, '(', testUser.role, ')');
// 3. Check existing comments
console.log('\n3. Checking existing comments...');
const existingComments = await prisma.caseComment.findMany({
where: { caseId: caseId, isDeleted: false },
include: {
user: { select: { name: true, role: true } },
reactions: { include: { user: { select: { name: true } } } },
replies: {
where: { isDeleted: false },
include: { user: { select: { name: true, role: true } } }
}
},
orderBy: { createdAt: 'desc' }
});
console.log(`✅ Found ${existingComments.length} existing comments`);
if (existingComments.length > 0) {
console.log('Sample comment:', {
id: existingComments[0].id,
content: existingComments[0].content.substring(0, 50) + '...',
user: existingComments[0].user.name,
reactions: existingComments[0].reactions.length,
replies: existingComments[0].replies.length
});
}
// 4. Test comment creation (if no comments exist)
if (existingComments.length === 0) {
console.log('\n4. Creating a test comment...');
const testComment = await prisma.caseComment.create({
data: {
caseId: caseId,
userId: testUser.id,
content: 'This is a test comment to verify the comment system is working properly.',
isDeleted: false,
isEdited: false
},
include: {
user: { select: { name: true, role: true } }
}
});
console.log('✅ Created test comment:', {
id: testComment.id,
content: testComment.content,
user: testComment.user.name
});
}
// 5. Check comment reactions
console.log('\n5. Checking comment reactions...');
const commentsWithReactions = await prisma.caseComment.findMany({
where: { caseId: caseId, isDeleted: false },
include: {
reactions: {
include: {
user: { select: { name: true } }
}
}
}
});
const totalReactions = commentsWithReactions.reduce((sum, comment) => sum + comment.reactions.length, 0);
console.log(`✅ Found ${totalReactions} total reactions across ${commentsWithReactions.length} comments`);
// 6. Test reaction creation (if no reactions exist)
if (totalReactions === 0 && commentsWithReactions.length > 0) {
console.log('\n6. Creating a test reaction...');
const testReaction = await prisma.commentReaction.create({
data: {
commentId: commentsWithReactions[0].id,
userId: testUser.id,
reactionType: 'like'
},
include: {
user: { select: { name: true } },
comment: { select: { content: true } }
}
});
console.log('✅ Created test reaction:', {
id: testReaction.id,
type: testReaction.reactionType,
user: testReaction.user.name,
comment: testReaction.comment.content.substring(0, 30) + '...'
});
}
// 7. Check database schema
console.log('\n7. Verifying database schema...');
const commentCount = await prisma.caseComment.count();
const reactionCount = await prisma.commentReaction.count();
const attachmentCount = await prisma.commentAttachment.count();
console.log('✅ Database counts:', {
comments: commentCount,
reactions: reactionCount,
attachments: attachmentCount
});
// 8. Test permissions
console.log('\n8. Testing comment permissions...');
const testComment = await prisma.caseComment.findFirst({
where: { caseId: caseId, isDeleted: false },
include: { user: true }
});
if (testComment) {
const canDelete = testComment.userId === testUser.id ||
testUser.role === 'ADMIN' ||
testUser.role === 'SUPERADMIN';
console.log('✅ Permission check:', {
commentOwner: testComment.user.name,
currentUser: testUser.name,
currentUserRole: testUser.role,
canDelete: canDelete
});
}
console.log('\n=== COMMENT SYSTEM TEST COMPLETE ===');
console.log('✅ All tests passed! The comment system appears to be working correctly.');
} catch (error) {
console.error('❌ Error testing comment system:', error);
} finally {
await prisma.$disconnect();
}
}
// Run the test
testCommentSystem();