task-i
This commit is contained in:
36
src/app/api/crm/document-artifacts/[id]/download/route.ts
Normal file
36
src/app/api/crm/document-artifacts/[id]/download/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { downloadArtifact } from '@/features/foundation/document-artifact/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentArtifactDownload
|
||||
});
|
||||
const { object } = await downloadArtifact({
|
||||
artifactId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
auditDownload: true
|
||||
});
|
||||
|
||||
return new NextResponse(object.body, {
|
||||
headers: {
|
||||
'Content-Type': object.contentType,
|
||||
'Content-Disposition': `inline; filename="${object.fileName || 'artifact'}"`
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: 'Unable to download document artifact' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -15,10 +15,13 @@ type Params = {
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization } = await requireOrganizationAccess({
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmQuotationPdfDownload
|
||||
});
|
||||
const pdf = await getStoredApprovedQuotationPdf(id, organization.id);
|
||||
const pdf = await getStoredApprovedQuotationPdf(id, organization.id, {
|
||||
userId: session.user.id,
|
||||
auditDownload: true
|
||||
});
|
||||
|
||||
return new NextResponse(pdf.buffer, {
|
||||
headers: {
|
||||
@@ -52,6 +55,7 @@ export async function POST(_request: NextRequest, { params }: Params) {
|
||||
action: 'generate_approved',
|
||||
beforeData: before,
|
||||
afterData: {
|
||||
approvedArtifactId: after.approvedArtifactId,
|
||||
approvedPdfUrl: after.approvedPdfUrl,
|
||||
approvedTemplateVersionId: after.approvedTemplateVersionId
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user