Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/api/src/policies/policies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,8 +1435,12 @@ export class PoliciesService {
organizationId,
);

const fileName = `${organizationName} - All Policies.pdf`;
const downloadUrl =
await this.attachmentsService.getPresignedDownloadUrl(key);
await this.attachmentsService.getPresignedDownloadUrlWithFilename(
key,
fileName,
);

this.logger.log(
`Generated PDF bundle for organization ${organizationId} with ${policies.length} policies`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ vi.mock('@/components/sheets/create-policy-sheet', () => ({
CreatePolicySheet: () => <div data-testid="create-policy-sheet" />,
}));

// Mock pdf-generator
vi.mock('@/lib/pdf-generator', () => ({
downloadAllPolicies: vi.fn(),
}));

// Mock api client
vi.mock('@/lib/api-client', () => ({
api: { get: vi.fn() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
import { CreatePolicySheet } from '@/components/sheets/create-policy-sheet';
import { api } from '@/lib/api-client';
import { Add, Download } from '@carbon/icons-react';
import type { AuditLog, Member, Organization, Policy, User } from '@db';
import type { Policy } from '@db';
import { Button, HStack } from '@trycompai/design-system';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { useState } from 'react';
import { toast } from 'sonner';
import { usePermissions } from '@/hooks/use-permissions';

type AuditLogWithRelations = AuditLog & {
user: User | null;
member: Member | null;
organization: Organization;
};

interface PolicyPageActionsProps {
policies: Policy[];
}
Expand All @@ -29,21 +24,23 @@ export function PolicyPageActions({ policies }: PolicyPageActionsProps) {
const handleDownloadAll = async () => {
setIsDownloadingAll(true);
try {
const logsEntries = await Promise.all(
policies.map(async (policy) => {
const res = await api.get<{ data: AuditLogWithRelations[] }>(
`/v1/audit-logs?entityType=policy&entityId=${policy.id}`,
);
const allLogs = Array.isArray(res.data?.data) ? res.data.data : [];
const approvalLogs = allLogs.filter((log) =>
log.description?.toLowerCase().includes('published'),
);
return [policy.id, approvalLogs] as const;
}),
const res = await api.get<{ downloadUrl: string; name: string; policyCount: number }>(
'/v1/policies/download-all',
);
const policyLogs = Object.fromEntries(logsEntries);
const { downloadAllPolicies } = await import('@/lib/pdf-generator');
await downloadAllPolicies(policies, policyLogs);

if (res.error || !res.data?.downloadUrl) {
toast.error('Failed to generate PDF. Please try again.');
return;
}

const link = document.createElement('a');
link.href = res.data.downloadUrl;
link.download = `${res.data.name ?? 'all-policies'}.pdf`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch {
toast.error('Failed to download policies.');
} finally {
setIsDownloadingAll(false);
}
Expand Down
Loading