All files / modules/contact index.js

100% Statements 18/18
100% Branches 3/3
100% Functions 2/2
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59      1x 1x 1x                       1x 3x 3x 3x 3x 3x 3x   1x 1x     1x 1x     1x                     1x 3x                   1x      
/**
 * @module contact
 */
const mail = require("../mail");
const admin = require("../admin");
const octokit = require("../octokit");
 
/**
 * Process contact request submitted by a user
 * on the /contact route: Send the contact request
 * email to admin, log the request via the admin module
 * and create an anonymised issue on Github if the
 * reason of request is either a bugreport or a
 * feature request.
 * @param {String} ip
 * @param {Object} data form data
 */
const processContactRequest = async (ip, data) => {
    const contactRequest = {...data};
    contactRequest.timestamp = (new Date()).toUTCString();
    contactRequest.ip = ip;
    await sendContactRequest(contactRequest);
    admin.logContactRequest(contactRequest);
    switch (contactRequest.reason) {
        case "bugreport":
            octokit.createBugreportIssue(contactRequest);
            break;
 
        case "feature_request":
            octokit.createFeatureRequestIssue(contactRequest);
            break;
 
        default:
            break;
    }
};
 
/**
 * Send a contact request email to the admin email address
 * specified in .env. This sets the sender of the email
 * to be the server email address, but the user's email
 * is specified in the body of the email.
 * @param {Object} contactRequest
 */
const sendContactRequest = async (contactRequest) => {
    await mail.sendEmail(
        process.env.CONTACT_EMAIL_ADDRESS,
        process.env.CONTACT_EMAIL_ADDRESS,
        `[rdfmapped.com] Contact form: ${contactRequest.subject}`,
        `Contact request [${contactRequest.reason}] from ${contactRequest.email} - ${contactRequest.name}:\n${contactRequest.message}`,
        `<h2>Contact request</h2><p>reason: ${contactRequest.reason}</p><p>subject: ${contactRequest.subject}</p>` +
            `<p>email: ${contactRequest.email}</p><p>name: ${contactRequest.email}</p><p>message: ${contactRequest.message}</p>`,
    );
};
 
module.exports = {
    processContactRequest,
};