Skip to content
Merged
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
17 changes: 14 additions & 3 deletions apps/api/src/policies/policies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,16 +1187,16 @@ export class PoliciesService {
throw new NotFoundException('Organization not found');
}

// Get all published policies with currentVersion
// Get all non-archived policies, prioritizing published > needs_review > draft
const policies = await db.policy.findMany({
where: {
organizationId,
status: 'published',
isArchived: false,
},
select: {
id: true,
name: true,
status: true,
content: true,
pdfUrl: true,
currentVersion: {
Expand All @@ -1210,9 +1210,19 @@ export class PoliciesService {
});

if (policies.length === 0) {
throw new NotFoundException('No published policies available');
throw new NotFoundException('No policies available');
}

// Sort by status priority: published first, then needs_review, then draft
const statusPriority: Record<string, number> = {
published: 0,
needs_review: 1,
draft: 2,
};
policies.sort(
(a, b) => (statusPriority[a.status] ?? 3) - (statusPriority[b.status] ?? 3),
);

const mergedPdf = await PDFDocument.create();
const organizationName = organization.name || 'Organization';
const accentColor = this.getAccentColor(organization.primaryColor);
Expand All @@ -1231,6 +1241,7 @@ export class PoliciesService {
};

// Helper to get effective content and pdfUrl (version first, fallback to policy)
// Matches single policy download logic
const getEffectiveData = (policy: (typeof policies)[0]) => {
const content = policy.currentVersion?.content ?? policy.content;
const pdfUrl = policy.currentVersion?.pdfUrl ?? policy.pdfUrl;
Expand Down
Loading