- Build complete Next.js CRM for commercial real estate - Add authentication with JWT sessions and role-based access - Add GoHighLevel API integration for contacts, conversations, opportunities - Add AI-powered Control Center with tool calling - Add Setup page with onboarding checklist (/setup) - Add sidebar navigation with Setup menu item - Fix type errors in onboarding API, GHL services, and control center tools - Add Prisma schema with SQLite for local development - Add UI components with clay morphism design system Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { getSession } from '@/lib/auth';
|
|
import { getGHLClientForUser } from '@/lib/ghl/helpers';
|
|
|
|
const sendSMSSchema = z.object({
|
|
type: z.literal('SMS'),
|
|
contactId: z.string(),
|
|
message: z.string().min(1),
|
|
});
|
|
|
|
const sendEmailSchema = z.object({
|
|
type: z.literal('EMAIL'),
|
|
contactId: z.string(),
|
|
subject: z.string().min(1),
|
|
htmlBody: z.string().min(1),
|
|
fromName: z.string().optional(),
|
|
replyTo: z.string().email().optional(),
|
|
});
|
|
|
|
const sendMessageSchema = z.discriminatedUnion('type', [sendSMSSchema, sendEmailSchema]);
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClientForUser(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json({ error: 'GHL not configured' }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const validated = sendMessageSchema.parse(body);
|
|
|
|
let message;
|
|
if (validated.type === 'SMS') {
|
|
message = await ghl.conversations.sendSMS({
|
|
contactId: validated.contactId,
|
|
message: validated.message,
|
|
});
|
|
} else {
|
|
message = await ghl.conversations.sendEmail({
|
|
contactId: validated.contactId,
|
|
subject: validated.subject,
|
|
htmlBody: validated.htmlBody,
|
|
fromName: validated.fromName,
|
|
replyTo: validated.replyTo,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json(message, { status: 201 });
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json({ error: 'Validation failed', details: error.issues }, { status: 400 });
|
|
}
|
|
console.error('Failed to send message:', error);
|
|
return NextResponse.json({ error: 'Failed to send message' }, { status: 500 });
|
|
}
|
|
}
|