(
reports: { reportId: string; reason?: string }[],
accept: boolean,
inboxConfig: Configuration["users"]["inbox"],
)
| 77 | } |
| 78 | |
| 79 | export async function handleReports( |
| 80 | reports: { reportId: string; reason?: string }[], |
| 81 | accept: boolean, |
| 82 | inboxConfig: Configuration["users"]["inbox"], |
| 83 | ): Promise<void> { |
| 84 | const reportIds = reports.map(({ reportId }) => reportId); |
| 85 | |
| 86 | const reportsFromDb = await ReportDAL.getReports(reportIds); |
| 87 | const reportById = new Map(reportsFromDb.map((it) => [it.id, it])); |
| 88 | |
| 89 | const existingReportIds = new Set(reportsFromDb.map((report) => report.id)); |
| 90 | const missingReportIds = reportIds.filter( |
| 91 | (reportId) => !existingReportIds.has(reportId), |
| 92 | ); |
| 93 | |
| 94 | if (missingReportIds.length > 0) { |
| 95 | throw new MonkeyError( |
| 96 | 404, |
| 97 | `Reports not found for some IDs ${missingReportIds.join(",")}`, |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | await ReportDAL.deleteReports(reportIds); |
| 102 | |
| 103 | for (const { reportId, reason } of reports) { |
| 104 | try { |
| 105 | const report = reportById.get(reportId); |
| 106 | if (!report) { |
| 107 | throw new MonkeyError(404, `Report not found for ID: ${reportId}`); |
| 108 | } |
| 109 | |
| 110 | let mailBody = ""; |
| 111 | if (accept) { |
| 112 | mailBody = `Your report regarding ${report.type} ${ |
| 113 | report.contentId |
| 114 | } (${report.reason.toLowerCase()}) has been approved. Thank you.`; |
| 115 | } else { |
| 116 | mailBody = `Sorry, but your report regarding ${report.type} ${ |
| 117 | report.contentId |
| 118 | } (${report.reason.toLowerCase()}) has been denied. ${ |
| 119 | reason !== undefined ? `\nReason: ${reason}` : "" |
| 120 | }`; |
| 121 | } |
| 122 | |
| 123 | const mailSubject = accept ? "Report approved" : "Report denied"; |
| 124 | const mail = buildMonkeyMail({ |
| 125 | subject: mailSubject, |
| 126 | body: mailBody, |
| 127 | }); |
| 128 | await UserDAL.addToInbox(report.uid, [mail], inboxConfig); |
| 129 | } catch (e) { |
| 130 | if (e instanceof MonkeyError) { |
| 131 | throw new MonkeyError(e.status, e.message); |
| 132 | } else { |
| 133 | throw new MonkeyError( |
| 134 | 500, |
| 135 | `Error handling reports: ${getErrorMessage(e)}`, |
| 136 | ); |
no test coverage detected