{"size": 2438, "ext": "js", "max_stars_repo_path": "routes/auth/index.js", "max_stars_repo_name": "Pinjasaur/whenisgucci", "max_stars_repo_stars_event_min_datetime": "2018-03-21T22:36:30.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-31T14:38:13.000Z", "max_issues_repo_path": "routes/auth/index.js", "max_issues_repo_name": "Pinjasaur/whenisgucci", "max_issues_repo_issues_event_min_datetime": "2018-03-04T21:44:30.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-03T01:34:25.000Z", "max_forks_repo_path": "routes/auth/index.js", "max_forks_repo_name": "Pinjasaur/whenisgucci", "max_forks_repo_forks_event_min_datetime": "2018-04-16T00:00:31.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-03T22:26:05.000Z"}
{"max_stars_count": 4, "max_issues_count": 59, "max_forks_count": 6, "avg_line_length": 26.7912087912, "max_line_length": 92, "alphanum_fraction": 0.6730926989}
const express = require("express");
const router = express.Router();
const Hashids = require("hashids");
const hashids = new Hashids(
  process.env.HASHIDS_EVENT_SALT,
  process.env.HASHIDS_EVENT_LENGTH,
  process.env.HASHIDS_EVENT_ALPHABET
);
const hashids2 = new Hashids(
  process.env.HASHIDS_CREATOR_SALT,
  process.env.HASHIDS_CREATOR_LENGTH
);

const Event = require("../../models/event");
const Creator = require("../../models/creator");

const asyncMiddleware = require("../../middlewares/async");

const { RequestError } = require("../../utils/errors");
const { isValidEvent } = require("../../utils/validators");
const { sendVerification,
        sendCreated,
        sendInvites }  = require("../../utils/mailer");

// Get (GET) an event + responses
router.get("/auth/:id", asyncMiddleware(async (req, res, next) => {

  const token = req.query.token;

  if (!token)
    throw new RequestError("Token required");

  const id = hashids2.decode(req.params.id)[0];

  // Verify ID was decoded
  if (id === undefined)
    throw new RequestError("Creator ID invalid");

  const resp = {
    status: true,
    messages: [],
    result: {}
  };

  const creator = await Creator.findOne({ _id: id }).exec();

  // Verify there was an Creator found
  if (!creator)
    throw new RequestError("No Creator found", 404);

  if (token !== creator.token)
    throw new RequestError("Token invalid");

  if (creator.authenticated)
    return res
      .redirect("/?utm_source=emailauth&auth=0");

  creator.authenticated = true;
  await creator.save();

  // NOTE! Cannot use forEach w/ async/await (to get expected results)
  // See: https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop
  for (let eventID of creator.events) {

    const event = await Event.findOne({ _id: eventID }).exec();

    // Verify there was an Event found
    if (!event)
      return res
        .redirect("/?utm_source=emailauth&auth=2");

    event.verified = true;
    await event.save();

    // Send confirmation email to creator
    sendCreated(creator, event);

    // Send invite emails to any invitees
    if (event.invitedTo.length > 0)
      sendInvites(event);
  }

  // Redirect to passed-in event or first event created if not present
  const event = req.query.event || hashids.encode(creator.events[0]);

  return res
    .redirect(`/event/${event}/results?utm_source=emailauth&auth=1`);
}));

module.exports = router;
