igcrmapi/scratch/test_prisma_followup.js

67 lines
2.1 KiB
JavaScript

const { PrismaClient } = require('@prisma/client');
require('dotenv').config();
const prisma = new PrismaClient();
async function main() {
console.log('Testing Prisma follow-up creation...');
try {
// 1. Fetch a user and client to get valid IDs
const user = await prisma.user.findFirst();
const client = await prisma.client.findFirst();
if (!user || !client) {
console.error('Could not find user or client in DB to test. Make sure db is seeded.');
return;
}
console.log(`Using User ID: ${user.id}, Client ID: ${client.id}`);
// 2. Mock a CreateFollowupDto payload
const createDto = {
userId: user.id,
clientId: client.id,
notes: 'Test notes',
date: new Date().toISOString(),
status: 'PENDING',
type: 'FOLLOWUP',
stage: 'LEAD',
};
console.log('Inserting followup via Prisma...');
const followup = await prisma.followup.create({
data: createDto,
include: { client: true }
});
console.log('Followup created successfully:', followup.id);
// 3. Mock notification raw query
console.log('Inserting notification via raw query...');
const notifId = require('crypto').randomUUID();
const metaStr = null;
const title = 'New Follow-up Assigned 📅';
const body = `You have been assigned a new follow-up task for client. Deadline: ${new Date(createDto.date).toLocaleString()}`;
const type = 'FOLLOWUP_ASSIGNED';
await prisma.$executeRaw`
INSERT INTO notification (id, userId, title, body, type, metadata)
VALUES (${notifId}, ${user.id}, ${title}, ${body}, ${type}, ${metaStr})
`;
console.log('Notification raw insert succeeded!');
// Clean up
console.log('Cleaning up test data...');
await prisma.followup.delete({ where: { id: followup.id } });
await prisma.$executeRaw`
DELETE FROM notification WHERE id = ${notifId}
`;
console.log('Clean up done.');
} catch (error) {
console.error('Error encountered:', error);
} finally {
await prisma.$disconnect();
}
}
main();