Skip to content

Commit 94af054

Browse files
authored
220 services for bug reporting and feedback (#221)
* file dump init * services for feedback * /feedback/feedback is not a good route * Update cd_dev.yaml * stop if things are missing * adding verify on main * test results * added docs and ES6 conversion * what the Golden AI hell is adding imports from my docs?
1 parent c2922a4 commit 94af054

6 files changed

Lines changed: 156 additions & 1 deletion

File tree

app.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import lineRouter from './line/index.mjs'
2727
import userProfileRouter from './userProfile/index.mjs'
2828
import privateProfileRouter from './userProfile/privateProfile.mjs'
2929
import proxyRouter from './utilities/proxy.js'
30-
30+
31+
// Beta Feedback routes
32+
import feedbackRouter from './feedback/feedbackRoutes.js'
33+
3134
let app = express()
3235

3336
//Middleware to use
@@ -65,6 +68,9 @@ app.use('/user', userProfileRouter)
6568
app.use('/my', privateProfileRouter)
6669
app.use('/proxy', proxyRouter)
6770

71+
// Beta Feedback routes
72+
app.use('/beta', feedbackRouter)
73+
6874
//catch 404 because of an invalid site path
6975
app.use('*', function(req, res, next) {
7076
let message = res.statusMessage ?? "This page does not exist"

feedback/feedbackController.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

feedback/feedbackRoutes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import express from 'express'
2+
import auth0Middleware from "../auth/index.mjs"
3+
import { submitFeedback, submitBug } from './feedbackController.js'
4+
5+
const feedbackRouter = express.Router()
6+
7+
feedbackRouter.route('/feedback').post(auth0Middleware(), submitFeedback)
8+
feedbackRouter.route('/bug').post(auth0Middleware(), submitBug)
9+
10+
export default feedbackRouter

feedback/githubService.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export async function createGitHubIssue(label, title, body) {
2+
const url = `https://api.github.com/repos/${process.env.REPO_OWNER}/${process.env.REPO_NAME}/issues`
3+
const headers = {
4+
Authorization: `token ${process.env.GITHUB_TOKEN}`,
5+
Accept: 'application/vnd.github.v3+json',
6+
'Content-Type': 'application/json'
7+
}
8+
9+
const response = await fetch(url, {
10+
method: 'POST',
11+
headers,
12+
body: JSON.stringify({
13+
title,
14+
body,
15+
labels: [label]
16+
})
17+
})
18+
19+
if (!response.ok) {
20+
const error = await response.json()
21+
throw new Error(`GitHub API error: ${error.message}`)
22+
}
23+
}

feedback/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import express from 'express'
2+
import bodyParser from 'body-parser'
3+
import { createGitHubIssue } from './githubService.js'
4+
5+
const feedbackRouter = express.Router()
6+
feedbackRouter.use(bodyParser.json())
7+
8+
// Endpoint for feedback form submissions
9+
feedbackRouter.post('/submit-feedback', async (req, res) => {
10+
const { user, page, feedback } = req.body
11+
12+
if (!feedback) {
13+
return res.status(400).json({ error: 'Feedback is required' })
14+
}
15+
16+
try {
17+
await createGitHubIssue('Feedback', `Feedback from ${user}`, `Page: ${page}\n\nFeedback: ${feedback}`)
18+
res.status(200).json({ message: 'Feedback submitted successfully' })
19+
} catch (error) {
20+
res.status(500).json({ error: 'Failed to submit feedback' })
21+
}
22+
})
23+
24+
// Endpoint for bug report submissions
25+
feedbackRouter.post('/submit-bug', async (req, res) => {
26+
const { user, page, bugDescription } = req.body
27+
28+
if (!bugDescription) {
29+
return res.status(400).json({ error: 'Bug description is required' })
30+
}
31+
32+
try {
33+
await createGitHubIssue('Bug Report', `Bug reported by ${user}`, `Page: ${page}\n\nBug: ${bugDescription}`)
34+
res.status(200).json({ message: 'Bug report submitted successfully' })
35+
} catch (error) {
36+
res.status(500).json({ error: 'Failed to submit bug report' })
37+
}
38+
})
39+
40+
export default feedbackRouter

utilities/mailer/index.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,37 @@ export const sendMail = async (email, subject, message) => {
3636
htmlTemplate = htmlTemplate.replace("{{subject}}", subject)
3737
htmlTemplate = htmlTemplate.replace("{{messageBody}}", message)
3838

39+
// Stop execution if any of the required environment variables are not set
40+
const requiredEnvVars = [
41+
"SMTP_HOST",
42+
"SMTP_PORT",
43+
"TPEN_SUPPORT_EMAIL",
44+
"TPEN_EMAIL_CC"
45+
]
46+
47+
requiredEnvVars.forEach(varName => {
48+
if (!process.env[varName]) {
49+
return {
50+
status: 500,
51+
message: `${varName} environment variable is not set.`
52+
}
53+
}
54+
})
55+
3956
try {
4057
const transporter = nodemailer.createTransport({
4158
host: process.env.SMTP_HOST,
4259
port: process.env.SMTP_PORT,
4360
})
4461

62+
transporter.verify((error, success) => {
63+
if (error) {
64+
console.log("Error in SMTP configuration: ", error)
65+
return {status: 500, message: error.toString()}
66+
}
67+
console.log("Server is ready to take our messages")
68+
})
69+
4570
const mailOptions = {
4671
from: process.env.TPEN_SUPPORT_EMAIL,
4772
to: email,

0 commit comments

Comments
 (0)