Skip to content

Commit fcd2598

Browse files
committed
added bug report backend
1 parent 04d2679 commit fcd2598

File tree

4 files changed

+381
-35
lines changed

4 files changed

+381
-35
lines changed

server/api/email/config/routes.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"routes": [
3-
{
4-
"method": "POST",
5-
"path": "/emails",
6-
"handler": "Email.send",
7-
"config": {}
8-
}
9-
]
10-
}
2+
"routes": [
3+
{
4+
"method": "POST",
5+
"path": "/bug-report",
6+
"handler": "Email.send",
7+
"config": {}
8+
}
9+
]
10+
}

server/api/email/controllers/Email.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// File /api/email/controllers/Email.js
2-
'use strict'
2+
'use strict';
33

44
/**
55
* Read the documentation () to implement custom controller functions
@@ -10,22 +10,48 @@ module.exports = {
1010
* Sends an email to the recipient in the body of the request
1111
*/
1212
send: async (ctx) => {
13-
const body = ctx.request.body
14-
const sendTo = body.email
15-
strapi.log.debug(`Trying to send an email to ${sendTo}`)
13+
// ensure request was not sent as formdata
14+
if (ctx.is('multipart'))
15+
return ctx.badRequest('Multipart requests are not accepted!', {
16+
error: 'ValidationError',
17+
});
18+
19+
// ensure the request has the right number of params
20+
const params = Object.keys(ctx.request.body).length;
21+
if (params !== 5)
22+
return ctx.badRequest('Invalid number of params!', {
23+
error: 'ValidationError',
24+
});
25+
26+
// validate the request
27+
const { description, steps, name, email, systemInfo } = ctx.request.body;
28+
if (!description || !steps || !name || !email || !systemInfo)
29+
return ctx.badRequest(
30+
'A description, steps, name, email and systemInfo must be provided!',
31+
{ error: 'ValidationError' }
32+
);
1633

1734
try {
1835
const emailOptions = {
19-
to: sendTo,
20-
subject: 'This is a test',
21-
html: `<h1>Welcome!</h1><p>This is a test HTML email.</p>`,
22-
}
23-
await strapi.plugins['email'].services.email.send(emailOptions)
24-
strapi.log.debug(`Email sent to ${sendTo}`)
25-
ctx.send({ message: 'Email sent' })
36+
37+
subject: 'Bug Report',
38+
html: `
39+
<h3>Description of the bug: </h3>
40+
<p>${description}<p>
41+
<h3>Steps to reproduce: </h3>
42+
<p>${steps}<p>
43+
<h3>Reported by: </h3>
44+
<p>${name} <p> <a href='mailto: ${email}'>(${email})</a>
45+
<h3>Captured in: </h3>
46+
<p>${systemInfo}</p>
47+
`,
48+
};
49+
await strapi.plugins['email'].services.email.send(emailOptions);
50+
strapi.log.debug(`Email sent to [email protected]`);
51+
ctx.send({ message: 'Email sent' });
2652
} catch (err) {
27-
strapi.log.error(`Error sending email to ${sendTo}`, err)
28-
ctx.send({ error: 'Error sending email' })
53+
strapi.log.error(`Error sending email to [email protected], `, err);
54+
ctx.send({ error: 'Error sending email' });
2955
}
3056
},
31-
}
57+
};

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
"strapi": "3.6.7",
1919
"strapi-admin": "3.6.7",
2020
"strapi-connector-bookshelf": "3.6.7",
21-
"strapi-plugin-upload": "3.6.7",
2221
"strapi-plugin-content-manager": "3.6.7",
2322
"strapi-plugin-content-type-builder": "3.6.7",
2423
"strapi-plugin-email": "3.6.7",
2524
"strapi-plugin-sentry": "^3.6.8",
25+
"strapi-plugin-upload": "3.6.7",
2626
"strapi-plugin-users-permissions": "3.6.7",
2727
"strapi-provider-email-nodemailer": "^3.6.8",
2828
"strapi-utils": "3.6.7"

0 commit comments

Comments
 (0)