|
| 1 | +import { createGitHubIssue } from './githubService.js' |
| 2 | +import { respondWithError } from "../utilities/shared.mjs" |
| 3 | + |
| 4 | +/** |
| 5 | + * |
| 6 | + * @param {user, body: page, feedback } req auth offers user and API is used with body: page:URL, feedback:String |
| 7 | + * @param {Response} res Express response object |
| 8 | + * @description This function handles the submission of feedback from the user. It creates a GitHub issue with the feedback details. |
| 9 | + * @returns 200 if feedback is submitted successfully, 204 if no feedback is provided, or an error response if the submission fails. |
| 10 | + */ |
| 11 | +export async function submitFeedback(req, res) { |
| 12 | + const user = req.user ? `${req.user.profile.displayName} (${req.user._id})` : 'Anonymous' |
| 13 | + const { page, feedback } = req.body |
| 14 | + |
| 15 | + if (!feedback) { |
| 16 | + return res.status(204).send() |
| 17 | + } |
| 18 | + try { |
| 19 | + await createGitHubIssue('Feedback', `Feedback from ${user}`, `Page: ${page}\n\nFeedback: ${sanitizeUserInput(feedback)}`) |
| 20 | + res.status(200).json({ message: 'Feedback submitted successfully' }) |
| 21 | + } catch (error) { |
| 22 | + respondWithError(res, error.status ?? 500, error.message ?? 'Failed to submit feedback') |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * |
| 28 | + * @param {user, body: page, bugDescription } req auth offers user and API is used with body: page:URL, bugDescription:String |
| 29 | + * @param {Response} res Express response object |
| 30 | + * @description This function handles the submission of bug reports from the user. It creates a GitHub issue with the bug details. |
| 31 | + * @returns 200 if the bug report is submitted successfully, 204 if no bug description is provided, or an error response if the submission fails. |
| 32 | + */ |
| 33 | +export async function submitBug(req, res) { |
| 34 | + const user = req.user ? `${req.user.profile.displayName} (${req.user._id})` : 'Anonymous' |
| 35 | + const { page, bugDescription } = req.body |
| 36 | + |
| 37 | + if (!bugDescription) { |
| 38 | + return res.status(204).send() |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + await createGitHubIssue('Bug Report', `Bug reported by ${user}`, `Page: ${page}\n\nBug: ${sanitizeUserInput(bugDescription)}`) |
| 43 | + res.status(200).json({ message: 'Bug report submitted successfully' }) |
| 44 | + } catch (error) { |
| 45 | + respondWithError(res, error.status ?? 500, error.message ??'Failed to submit bug report') |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function sanitizeUserInput(input) { |
| 50 | + return input.replace(/[^\w\s.,!?'"()-]/g, '') |
| 51 | +} |
0 commit comments