feat(analyzer): phase 7 — share-via-email
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>
This commit is contained in:
parent
8f8b5ab7be
commit
ed3b363d02
4 changed files with 231 additions and 9 deletions
|
|
@ -3,10 +3,11 @@
|
|||
*
|
||||
* Body: { recipientEmail: string, note?: string }
|
||||
*
|
||||
* Records a share row in analyzer_shares. Email send is deferred to phase 8
|
||||
* — this endpoint validates the recipient domain against ALLOWED_SHARE_DOMAINS
|
||||
* and persists the audit row. The share is "pending delivery" until phase 8
|
||||
* wires up nodemailer.
|
||||
* 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';
|
||||
|
|
@ -16,6 +17,7 @@ import {
|
|||
createShare,
|
||||
getAnalysisById,
|
||||
} from '@/lib/services/analyzer/persistence';
|
||||
import { sendAnalysisShareEmail } from '@/lib/services/email';
|
||||
|
||||
function getAllowedDomains(): string[] {
|
||||
const raw = process.env.ALLOWED_SHARE_DOMAINS ?? '';
|
||||
|
|
@ -25,6 +27,14 @@ function getAllowedDomains(): string[] {
|
|||
.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 }> }
|
||||
|
|
@ -65,20 +75,47 @@ export async function POST(
|
|||
);
|
||||
}
|
||||
|
||||
// Confirm the analysis exists.
|
||||
const analysis = await getAnalysisById(id);
|
||||
if (!analysis) {
|
||||
return NextResponse.json({ error: 'Analysis not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const userId = (session?.user as { id: string }).id;
|
||||
const sessionUser = session?.user as {
|
||||
id: string;
|
||||
email: string;
|
||||
name?: string;
|
||||
};
|
||||
const share = await createShare({
|
||||
analysis_id: id,
|
||||
shared_by_user_id: userId,
|
||||
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,
|
||||
|
|
@ -87,5 +124,7 @@ export async function POST(
|
|||
sharedAt: share.shared_at,
|
||||
note: note ?? null,
|
||||
},
|
||||
emailSent,
|
||||
emailError,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue