| 70 | |
| 71 | // Validation for secure endpoints |
| 72 | export async function validateReportAccess( |
| 73 | reportId: string, |
| 74 | shareId: string, |
| 75 | shareType: 'dashboard' | 'report', |
| 76 | ) { |
| 77 | if (shareType === 'dashboard') { |
| 78 | const share = await db.shareDashboard.findUnique({ |
| 79 | where: { id: shareId }, |
| 80 | include: { |
| 81 | dashboard: { |
| 82 | include: { |
| 83 | reports: { |
| 84 | where: { id: reportId }, |
| 85 | }, |
| 86 | }, |
| 87 | }, |
| 88 | }, |
| 89 | }); |
| 90 | |
| 91 | if (!share || !share.public) { |
| 92 | throw new Error('Share not found or not public'); |
| 93 | } |
| 94 | |
| 95 | if (!share.dashboard.reports.some((r) => r.id === reportId)) { |
| 96 | throw new Error('Report does not belong to this dashboard'); |
| 97 | } |
| 98 | |
| 99 | return share; |
| 100 | } |
| 101 | |
| 102 | const share = await db.shareReport.findUnique({ |
| 103 | where: { id: shareId }, |
| 104 | include: { |
| 105 | report: true, |
| 106 | }, |
| 107 | }); |
| 108 | |
| 109 | if (!share || !share.public) { |
| 110 | throw new Error('Share not found or not public'); |
| 111 | } |
| 112 | |
| 113 | if (share.reportId !== reportId) { |
| 114 | throw new Error('Report ID mismatch'); |
| 115 | } |
| 116 | |
| 117 | return share; |
| 118 | } |
| 119 | |
| 120 | // Unified validation for share access |
| 121 | export async function validateShareAccess( |