sendAnalysisShareEmail() reuses the existing nodemailer SMTP transport
(same path as magic-link/invitation mail). Share route persists the
audit row first, then attempts send; on failure returns
{share, emailSent:false, emailError} at HTTP 200 so the audit log
stays intact. Modal surfaces send failures as a warning toast.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
130 lines
3.6 KiB
TypeScript
130 lines
3.6 KiB
TypeScript
/**
|
|
* POST /api/analyzer/analyses/:id/share
|
|
*
|
|
* Body: { recipientEmail: string, note?: string }
|
|
*
|
|
* Records a share row in analyzer_shares (audit log) and emails the recipient
|
|
* a link to the analysis. The audit row is persisted FIRST; if the email
|
|
* send fails the response carries `emailSent: false` + the error message,
|
|
* but the share row stays. This matches the spec's "share is an audit log"
|
|
* framing — we'd rather record an attempted share than lose it.
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { requireAuth } from '@/lib/auth-utils';
|
|
import { ShareAnalysisRequest } from '@/lib/types/analyzer';
|
|
import {
|
|
createShare,
|
|
getAnalysisById,
|
|
} from '@/lib/services/analyzer/persistence';
|
|
import { sendAnalysisShareEmail } from '@/lib/services/email';
|
|
|
|
function getAllowedDomains(): string[] {
|
|
const raw = process.env.ALLOWED_SHARE_DOMAINS ?? '';
|
|
return raw
|
|
.split(',')
|
|
.map((d) => d.trim().toLowerCase())
|
|
.filter((d) => d.length > 0);
|
|
}
|
|
|
|
function getAppBaseUrl(): string {
|
|
return (
|
|
process.env.BETTER_AUTH_URL ||
|
|
process.env.NEXT_PUBLIC_BETTER_AUTH_URL ||
|
|
'http://localhost:3100'
|
|
);
|
|
}
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { session, error } = await requireAuth();
|
|
if (error) return error;
|
|
|
|
const { id } = await params;
|
|
|
|
const body = await request.json().catch(() => ({}));
|
|
const result = ShareAnalysisRequest.safeParse(body);
|
|
if (!result.success) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid request body', details: result.error.issues },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
const { recipientEmail, note } = result.data;
|
|
|
|
const allowedDomains = getAllowedDomains();
|
|
if (allowedDomains.length === 0) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Sharing is not configured',
|
|
message: 'ALLOWED_SHARE_DOMAINS env var is empty.',
|
|
},
|
|
{ status: 503 }
|
|
);
|
|
}
|
|
const domain = recipientEmail.split('@')[1]?.toLowerCase();
|
|
if (!domain || !allowedDomains.includes(domain)) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Recipient domain is not allowed',
|
|
message: `Allowed domains: ${allowedDomains.join(', ')}`,
|
|
},
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
const analysis = await getAnalysisById(id);
|
|
if (!analysis) {
|
|
return NextResponse.json({ error: 'Analysis not found' }, { status: 404 });
|
|
}
|
|
|
|
const sessionUser = session?.user as {
|
|
id: string;
|
|
email: string;
|
|
name?: string;
|
|
};
|
|
const share = await createShare({
|
|
analysis_id: id,
|
|
shared_by_user_id: sessionUser.id,
|
|
shared_with_email: recipientEmail,
|
|
note,
|
|
});
|
|
|
|
const analysisUrl = `${getAppBaseUrl().replace(/\/$/, '')}/analyzer/analysis/${id}`;
|
|
let emailSent = true;
|
|
let emailError: string | null = null;
|
|
try {
|
|
await sendAnalysisShareEmail({
|
|
recipientEmail,
|
|
senderName: sessionUser.name || sessionUser.email,
|
|
senderEmail: sessionUser.email,
|
|
ticketNumber: analysis.ticketNumber,
|
|
analysisVersion: analysis.analysisVersion,
|
|
summary: analysis.summary,
|
|
nextStep: analysis.nextStep,
|
|
analysisUrl,
|
|
note,
|
|
});
|
|
} catch (err) {
|
|
emailSent = false;
|
|
emailError = err instanceof Error ? err.message : 'Unknown email error';
|
|
console.error(
|
|
`[ANALYZER-SHARE] email send failed for share ${share.id}:`,
|
|
err
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
share: {
|
|
id: share.id,
|
|
analysisId: id,
|
|
sharedWithEmail: recipientEmail,
|
|
sharedAt: share.shared_at,
|
|
note: note ?? null,
|
|
},
|
|
emailSent,
|
|
emailError,
|
|
});
|
|
}
|